1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.requests;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.HashMap;
19 import java.util.List;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.ResourceArray;
24 import org.simantics.utils.page.PageDesc;
25 import org.simantics.utils.strings.AlphanumComparator;
28 * @author Tuukka Lehtonen
30 public class Node implements Comparable<Node> {
32 public static final Comparator<Node> CASE_INSENSITIVE_COMPARATOR =
33 (o1, o2) -> AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());
35 private final Node parent;
36 private final List<Node> children = new ArrayList<Node>();
37 private final List<Node> unmodifiableChildren = Collections.unmodifiableList(children);
40 * May be <code>null</code> if there is no diagram for this node.
42 private final Resource diagram;
43 private final ResourceArray definingResource; // i.e. Composite
44 private final String name;
46 private PageDesc pageDesc;
49 private Map<String, Object> properties;
54 * @param diagram may be <code>null</code> if there is no diagram for this node
55 * @param definingResources
57 public Node(Node parent, String name, Resource diagram, Resource... definingResources) {
58 if (definingResources.length == 0)
59 throw new IllegalArgumentException("must provide at least one defining resource");
62 this.diagram = diagram;
63 this.definingResource = new ResourceArray(definingResources);
66 parent.addChild(this);
69 public Node cloneWithoutChildren(Node parent) {
70 Node clone = new Node(parent, name, diagram, definingResource.resources);
72 clone.setPageDesc(pageDesc);
76 public Node getParent() {
81 * @return <code>null</code> if there is no diagram for this node
83 public Resource getDiagramResource() {
87 public ResourceArray getDefiningResources() {
88 return definingResource;
91 public String getName() {
96 public String toString() {
97 return (parent != null ? parent : "") + "/" + name + definingResource;
100 public void addChild(Node child) {
104 public void removeChild(Node child) {
105 children.remove(child);
108 public Collection<Node> getChildren() {
109 return unmodifiableChildren;
112 public void setPageDesc(PageDesc pageDesc) {
113 this.pageDesc = pageDesc;
116 public void setRVI(String rvi) {
120 public String getRVI() {
124 public PageDesc getPageDesc() {
131 * @return this node for chained initialization
133 public Node setProperty(String key, Object value) {
134 if (properties == null)
135 properties = new HashMap<>();
136 properties.put(key, value);
140 @SuppressWarnings("unchecked")
141 public <T> T getProperty(String key) {
142 if (properties == null)
144 return (T) properties.get(key);
148 public int compareTo(Node o) {
149 int ret = name.compareTo(o.name);
154 public int hashCode() {
155 final int prime = 31;
157 result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
158 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
159 result = prime * result + ((definingResource == null) ? 0 : definingResource.hashCode());
164 public boolean equals(Object obj) {
169 if (getClass() != obj.getClass())
171 Node other = (Node) obj;
172 if (diagram == null) {
173 if (other.diagram != null)
175 } else if (!diagram.equals(other.diagram))
177 if (parent == null) {
178 if (other.parent != null)
180 } else if (!parent.equals(other.parent))
182 if (definingResource == null) {
183 if (other.definingResource != null)
185 } else if (!definingResource.equals(other.definingResource))