]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/Node.java
Merge "Debug logging through SLF4J Logger for Expressions"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / requests / Node.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.requests;
13
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;
20 import java.util.Map;
21
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;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class Node implements Comparable<Node> {
31
32     public static final Comparator<Node> CASE_INSENSITIVE_COMPARATOR =
33             (o1, o2) -> AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());
34
35     private final Node       parent;
36     private final List<Node> children = new ArrayList<Node>();
37     private final List<Node> unmodifiableChildren = Collections.unmodifiableList(children);
38
39     /**
40      * May be <code>null</code> if there is no diagram for this node.
41      */
42     private final Resource         diagram;
43     private final ResourceArray    definingResource; // i.e. Composite
44     private final String           name;
45
46     private PageDesc         pageDesc;
47     private String           rvi;
48
49     private Map<String, Object> properties;
50
51     /**
52      * @param parent
53      * @param name
54      * @param diagram may be <code>null</code> if there is no diagram for this node
55      * @param definingResources
56      */
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");
60         this.parent = parent;
61         this.name = name;
62         this.diagram = diagram;
63         this.definingResource = new ResourceArray(definingResources);
64
65         if (parent != null)
66             parent.addChild(this);
67     }
68
69     public Node cloneWithoutChildren(Node parent) {
70         Node clone = new Node(parent, name, diagram, definingResource.resources);
71         clone.setRVI(rvi);
72         clone.setPageDesc(pageDesc);
73         return clone;
74     }
75
76     public Node getParent() {
77         return parent;
78     }
79
80     /**
81      * @return <code>null</code> if there is no diagram for this node
82      */
83     public Resource getDiagramResource() {
84         return diagram;
85     }
86
87     public ResourceArray getDefiningResources() {
88         return definingResource;
89     }
90
91     public String getName() {
92         return name;
93     }
94
95     @Override
96     public String toString() {
97         return (parent != null ? parent : "") + "/" + name + definingResource;
98     }
99
100     public void addChild(Node child) {
101         children.add(child);
102     }
103
104     public void removeChild(Node child) {
105         children.remove(child);
106     }
107
108     public Collection<Node> getChildren() {
109         return unmodifiableChildren;
110     }
111
112     public void setPageDesc(PageDesc pageDesc) {
113         this.pageDesc = pageDesc;
114     }
115     
116     public void setRVI(String rvi) {
117         this.rvi = rvi;
118     }
119     
120     public String getRVI() {
121         return rvi;
122     }
123
124     public PageDesc getPageDesc() {
125         return pageDesc;
126     }
127
128     /**
129      * @param key
130      * @param value
131      * @return this node for chained initialization
132      */
133     public Node setProperty(String key, Object value) {
134         if (properties == null)
135             properties = new HashMap<>();
136         properties.put(key, value);
137         return this;
138     }
139
140     @SuppressWarnings("unchecked")
141     public <T> T getProperty(String key) {
142         if (properties == null)
143             return null;
144         return (T) properties.get(key);
145     }
146
147     @Override
148     public int compareTo(Node o) {
149         int ret = name.compareTo(o.name);
150         return ret;
151     }
152
153     @Override
154     public int hashCode() {
155         final int prime = 31;
156         int result = 1;
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());
160         return result;
161     }
162
163     @Override
164     public boolean equals(Object obj) {
165         if (this == obj)
166             return true;
167         if (obj == null)
168             return false;
169         if (getClass() != obj.getClass())
170             return false;
171         Node other = (Node) obj;
172         if (diagram == null) {
173             if (other.diagram != null)
174                 return false;
175         } else if (!diagram.equals(other.diagram))
176             return false;
177         if (parent == null) {
178             if (other.parent != null)
179                 return false;
180         } else if (!parent.equals(other.parent))
181             return false;
182         if (definingResource == null) {
183             if (other.definingResource != null)
184                 return false;
185         } else if (!definingResource.equals(other.definingResource))
186             return false;
187         return true;
188     }
189
190 }