]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/Node.java
Merge "(refs #7307) Added features field to SCL module header"
[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.List;
19
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.ResourceArray;
22 import org.simantics.utils.page.PageDesc;
23 import org.simantics.utils.strings.AlphanumComparator;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class Node implements Comparable<Node> {
29
30     public static final Comparator<Node> CASE_INSENSITIVE_COMPARATOR =
31             (o1, o2) -> AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());
32
33     private final Node       parent;
34     private final List<Node> children = new ArrayList<Node>();
35     private final List<Node> unmodifiableChildren = Collections.unmodifiableList(children);
36
37     /**
38      * May be <code>null</code> if there is no diagram for this node.
39      */
40     private final Resource         diagram;
41     private final ResourceArray    definingResource; // i.e. Composite
42     private final String           name;
43
44     private PageDesc         pageDesc;
45     private String           rvi;
46
47     /**
48      * @param parent
49      * @param name
50      * @param diagram may be <code>null</code> if there is no diagram for this node
51      * @param definingResources
52      */
53     public Node(Node parent, String name, Resource diagram, Resource... definingResources) {
54         if (definingResources.length == 0)
55             throw new IllegalArgumentException("must provide at least one defining resource");
56         this.parent = parent;
57         this.name = name;
58         this.diagram = diagram;
59         this.definingResource = new ResourceArray(definingResources);
60
61         if (parent != null)
62             parent.addChild(this);
63     }
64
65     public Node cloneWithoutChildren(Node parent) {
66         Node clone = new Node(parent, name, diagram, definingResource.resources);
67         clone.setRVI(rvi);
68         clone.setPageDesc(pageDesc);
69         return clone;
70     }
71
72     public Node getParent() {
73         return parent;
74     }
75
76     /**
77      * @return <code>null</code> if there is no diagram for this node
78      */
79     public Resource getDiagramResource() {
80         return diagram;
81     }
82
83     public ResourceArray getDefiningResources() {
84         return definingResource;
85     }
86
87     public String getName() {
88         return name;
89     }
90
91     @Override
92     public String toString() {
93         return (parent != null ? parent : "") + "/" + name + definingResource;
94     }
95
96     public void addChild(Node child) {
97         children.add(child);
98     }
99
100     public void removeChild(Node child) {
101         children.remove(child);
102     }
103
104     public Collection<Node> getChildren() {
105         return unmodifiableChildren;
106     }
107
108     public void setPageDesc(PageDesc pageDesc) {
109         this.pageDesc = pageDesc;
110     }
111     
112     public void setRVI(String rvi) {
113         this.rvi = rvi;
114     }
115     
116     public String getRVI() {
117         return rvi;
118     }
119
120     public PageDesc getPageDesc() {
121         return pageDesc;
122     }
123
124     @Override
125     public int compareTo(Node o) {
126         int ret = name.compareTo(o.name);
127         return ret;
128     }
129
130     @Override
131     public int hashCode() {
132         final int prime = 31;
133         int result = 1;
134         result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
135         result = prime * result + ((parent == null) ? 0 : parent.hashCode());
136         result = prime * result + ((definingResource == null) ? 0 : definingResource.hashCode());
137         return result;
138     }
139
140     @Override
141     public boolean equals(Object obj) {
142         if (this == obj)
143             return true;
144         if (obj == null)
145             return false;
146         if (getClass() != obj.getClass())
147             return false;
148         Node other = (Node) obj;
149         if (diagram == null) {
150             if (other.diagram != null)
151                 return false;
152         } else if (!diagram.equals(other.diagram))
153             return false;
154         if (parent == null) {
155             if (other.parent != null)
156                 return false;
157         } else if (!parent.equals(other.parent))
158             return false;
159         if (definingResource == null) {
160             if (other.definingResource != null)
161                 return false;
162         } else if (!definingResource.equals(other.definingResource))
163             return false;
164         return true;
165     }
166
167 }