]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/requests/Node.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / requests / Node.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.requests;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 import java.util.Comparator;\r
18 import java.util.List;\r
19 \r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.common.ResourceArray;\r
22 import org.simantics.utils.page.PageDesc;\r
23 import org.simantics.utils.strings.AlphanumComparator;\r
24 \r
25 /**\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public class Node implements Comparable<Node> {\r
29 \r
30     public static final Comparator<Node> CASE_INSENSITIVE_COMPARATOR = new Comparator<Node>() {\r
31         @Override\r
32         public int compare(Node o1, Node o2) {\r
33             return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());\r
34         }\r
35     };\r
36 \r
37     private final Node       parent;\r
38     private final List<Node> children = new ArrayList<Node>();\r
39 \r
40     /**\r
41      * May be <code>null</code> if there is no diagram for this node.\r
42      */\r
43     private final Resource         diagram;\r
44     private final ResourceArray    definingResource; // i.e. Composite\r
45     private final String           name;\r
46 \r
47 //    private String[]         partOfGroups = {};\r
48 \r
49     private PageDesc         pageDesc;\r
50     private String           RVI;\r
51 \r
52     /**\r
53      * @param parent\r
54      * @param name\r
55      * @param diagram may be <code>null</code> if there is no diagram for this node\r
56      * @param definingResources\r
57      */\r
58     public Node(Node parent, String name, Resource diagram, Resource... definingResources) {\r
59         if (definingResources.length == 0)\r
60             throw new IllegalArgumentException("must provide at least one defining resource");\r
61         this.parent = parent;\r
62         this.name = name;\r
63         this.diagram = diagram;\r
64         this.definingResource = new ResourceArray(definingResources);\r
65 \r
66         if (parent != null)\r
67             parent.addChild(this);\r
68     }\r
69 \r
70     public Node getParent() {\r
71         return parent;\r
72     }\r
73 \r
74     /**\r
75      * @return <code>null</code> if there is no diagram for this node\r
76      */\r
77     public Resource getDiagramResource() {\r
78         return diagram;\r
79     }\r
80 \r
81     public ResourceArray getDefiningResources() {\r
82         return definingResource;\r
83     }\r
84 \r
85     public String getName() {\r
86         return name;\r
87     }\r
88 \r
89     @Override\r
90     public String toString() {\r
91         return (parent != null ? parent : "") + "/" + name + definingResource;\r
92     }\r
93 \r
94     public void addChild(Node child) {\r
95         children.add(child);\r
96     }\r
97 \r
98     public void removeChild(Node child) {\r
99         children.remove(child);\r
100     }\r
101 \r
102     public Collection<Node> getChildren() {\r
103         return Collections.unmodifiableCollection(children);\r
104     }\r
105 \r
106 //    public void setPartOfGroups(Collection<String> groups) {\r
107 //        this.partOfGroups = groups.toArray(new String[groups.size()]);\r
108 //    }\r
109 //\r
110 //    public String[] getPartOfGroups() {\r
111 //        return partOfGroups;\r
112 //    }\r
113 \r
114     public void setPageDesc(PageDesc pageDesc) {\r
115         this.pageDesc = pageDesc;\r
116     }\r
117     \r
118     public void setRVI(String RVI) {\r
119         this.RVI = RVI;\r
120     }\r
121     \r
122     public String getRVI() {\r
123         return RVI;\r
124     }\r
125 \r
126     public PageDesc getPageDesc() {\r
127         return pageDesc;\r
128     }\r
129 \r
130     @Override\r
131     public int compareTo(Node o) {\r
132         int ret = name.compareTo(o.name);\r
133         return ret;\r
134     }\r
135 \r
136     @Override\r
137     public int hashCode() {\r
138         final int prime = 31;\r
139         int result = 1;\r
140         result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());\r
141         result = prime * result + ((parent == null) ? 0 : parent.hashCode());\r
142         result = prime * result + ((definingResource == null) ? 0 : definingResource.hashCode());\r
143         return result;\r
144     }\r
145 \r
146     @Override\r
147     public boolean equals(Object obj) {\r
148         if (this == obj)\r
149             return true;\r
150         if (obj == null)\r
151             return false;\r
152         if (getClass() != obj.getClass())\r
153             return false;\r
154         Node other = (Node) obj;\r
155         if (diagram == null) {\r
156             if (other.diagram != null)\r
157                 return false;\r
158         } else if (!diagram.equals(other.diagram))\r
159             return false;\r
160         if (parent == null) {\r
161             if (other.parent != null)\r
162                 return false;\r
163         } else if (!parent.equals(other.parent))\r
164             return false;\r
165         if (definingResource == null) {\r
166             if (other.definingResource != null)\r
167                 return false;\r
168         } else if (!definingResource.equals(other.definingResource))\r
169             return false;\r
170         return true;\r
171     }\r
172 \r
173 }