]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/scenegraph/structural/ComponentNode.java
Copyrights
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / scenegraph / structural / ComponentNode.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * 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.g3d.scenegraph.structural;\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.List;\r
18 \r
19 import org.simantics.g3d.ontology.G3D;\r
20 import org.simantics.g3d.property.annotations.GetPropertyValue;\r
21 import org.simantics.g3d.property.annotations.SetPropertyValue;\r
22 import org.simantics.g3d.scenegraph.base.Node;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.objmap.graph.annotations.RelatedGetValue;\r
25 import org.simantics.objmap.graph.annotations.RelatedSetValue;\r
26 import org.simantics.objmap.structural.IStructuralObject;\r
27 import org.simantics.utils.datastructures.MapList;\r
28 \r
29 public abstract class ComponentNode<T extends Connection, T2 extends IComponentNode> extends Node implements IComponentNode<T, T2>{\r
30 \r
31         \r
32         private String name;\r
33         \r
34 \r
35         @RelatedGetValue(Layer0.URIs.HasName)\r
36         @GetPropertyValue(value = Layer0.URIs.HasName, tabId = "Default", name = "Name")\r
37         public String getName() {\r
38                 return name;\r
39         }\r
40         \r
41         @RelatedSetValue(Layer0.URIs.HasName)\r
42         @SetPropertyValue(Layer0.URIs.HasName)\r
43         public void setName(String name) {\r
44                 if (name == null)\r
45                         return;\r
46                 this.name = name;\r
47                 firePropertyChanged(Layer0.URIs.HasName);\r
48         }\r
49         \r
50         @Override\r
51         public String toString() {\r
52                 return getName();\r
53         }\r
54         \r
55         public boolean isPartOfInstantiatedModel() {\r
56                 return ctx.size() > 0;\r
57         }\r
58         \r
59         public boolean isExposed() {\r
60                 if (ctx.size() == 0)\r
61                         return true;\r
62                 return getPublishedBy().contains(ctx.get(0));\r
63         }\r
64         \r
65         @Override\r
66         public boolean isInstantiatedModelRoot() {\r
67                 return ctx.size() == 1 && this.equals(ctx.get(0));\r
68         }\r
69         \r
70         @Override\r
71         public boolean isPublishable() {\r
72                 return !isPartOfInstantiatedModel();\r
73         }\r
74         \r
75         private List<IStructuralObject> ctx = new ArrayList<IStructuralObject>(1);\r
76         @Override\r
77         public List<IStructuralObject> getContext() {\r
78                 return ctx;\r
79         }\r
80         \r
81         @Override\r
82         public void setContext(List<IStructuralObject> object) {\r
83                 ctx = object;\r
84         }\r
85         \r
86         private List<IStructuralNode> publisher = new ArrayList<IStructuralNode>(1);\r
87         \r
88         \r
89         \r
90         protected abstract boolean isValidConnectionId(String id);\r
91         \r
92         @Override\r
93         public void connectionChanged(T c, T2 node, boolean add) {\r
94                 \r
95         }\r
96         \r
97         @Override\r
98         public void addPublishedBy(IStructuralNode node) {\r
99                 publisher.add(node);\r
100                 firePropertyChanged(G3D.URIs.publishes);\r
101         }\r
102         \r
103         @Override\r
104         public Collection<IStructuralNode> getPublishedBy() {\r
105                 return publisher;\r
106         }\r
107         \r
108         @Override\r
109         public void removePublishedBy(IStructuralNode node) {\r
110                 if (publisher.remove(node))\r
111                         firePropertyChanged(G3D.URIs.publishes);\r
112         }\r
113         \r
114         \r
115         @Override\r
116         public List<T> getConnections(String id) {\r
117                 List<T> list = new ArrayList<T>();\r
118                 list.addAll(connections.getValues(id));\r
119                 list.addAll(connections.getValues(id+"/str"));\r
120                 return list;\r
121         }       \r
122         \r
123         public List<T2> getAllConnectedNodes() {\r
124                 List<T2> list = new ArrayList<T2>();\r
125                 for (T c : getAllConnections()) {\r
126                         T2 node = (T2)c.getOther(this);\r
127                         if (node != null)\r
128                                 list.add(node);\r
129                 }\r
130                 return list;\r
131         }\r
132         \r
133     \r
134     @Override\r
135     public void addConnection(String id, T c) {\r
136         assert(isValidConnectionId(id));\r
137         if (!isPartOfInstantiatedModel())\r
138                 _addConnection(id, c);\r
139         else\r
140                 _addConnection(id+"/str", c);\r
141     }\r
142     \r
143     @Override\r
144     public void removeConnection(String id, T c) {\r
145         assert(isValidConnectionId(id));\r
146         if (!isPartOfInstantiatedModel())\r
147                 _removeConnection(id, c);\r
148         else\r
149                 _removeConnection(id+"/str", c);\r
150     }\r
151     \r
152     @Override\r
153     public void removeConnection(String id) {\r
154         assert(isValidConnectionId(id));\r
155         if (!isPartOfInstantiatedModel())\r
156                 _removeConnection(id);\r
157         else\r
158                 _removeConnection(id+"/str");\r
159     }\r
160         \r
161         private MapList<String, T> connections = new MapList<String, T>();\r
162         \r
163         \r
164         protected void _addConnection(String id, T c) {\r
165                 connections.add(id, c);\r
166                 c.addConnect(this);\r
167                 firePropertyChanged(id);\r
168         }\r
169         \r
170         protected void _addStrConnection(String id, T c) {\r
171                 _addConnection(id+"/str", c);\r
172         }\r
173         \r
174         protected  MapList<String, T> _getConnections() {\r
175                 return connections;\r
176         }\r
177         \r
178         protected  List<T> _getConnections(String id) {\r
179                 return connections.getValues(id);\r
180         }\r
181         \r
182         protected  List<T> _getStrConnections(String id) {\r
183                 return _getConnections(id+"/str");\r
184         }\r
185         \r
186         protected void _removeConnection(String id, T c) {\r
187                 if (connections.remove(id, c)) {\r
188                         c.remove();\r
189                         firePropertyChanged(id);\r
190                 }\r
191         }\r
192         \r
193         protected void _removeConnection(String id) {\r
194                 List<T> conns = new ArrayList<T>();\r
195                 conns.addAll(_getConnections(id));\r
196                 for (T c : conns) {\r
197                         _removeConnection(id, c);\r
198                 }\r
199         }\r
200         \r
201         protected void _removeStrConnection(String id, T c) {\r
202                 _removeConnection(id+"/str", c);\r
203         }\r
204         \r
205         public List<T> getAllConnections() {\r
206                 List<T> list = new ArrayList<T>();\r
207                 List<String> keys = new ArrayList<String>();\r
208                 keys.addAll(connections.getKeys());\r
209                 Collections.sort(keys);\r
210                 for (String s : keys)\r
211                         list.addAll(connections.getValues(s));\r
212                 return list;\r
213         }\r
214         \r
215         public void removeConnection(T c) {\r
216                 String keys[] = connections.getKeys(new String[connections.getKeySize()]);\r
217                 for (String s : keys) {\r
218                         if (connections.contains(s, c))\r
219                                 _removeConnection(s, c);\r
220                 }\r
221         }\r
222         \r
223         public List<T2> getConnectedNodes(String id) {\r
224                 List<T2> list = new ArrayList<T2>();\r
225                 for (T c : getConnections(id)) {\r
226                         T2 node = (T2)c.getOther(this);\r
227                         if (node != null)\r
228                                 list.add(node);\r
229                 }\r
230                 return list;\r
231         }\r
232         \r
233         @Override\r
234         public String getConnectionId(T c) {\r
235                 for (String s : connections.getKeys()) {\r
236                         if (connections.contains(s, c)) {\r
237                                 if (!s.endsWith("/str"))\r
238                                         return s;\r
239                                 return s.substring(0, s.length()-4);\r
240                         }\r
241                 }\r
242                 return null;\r
243         }\r
244         \r
245         @Override\r
246         public Collection<String> getConnectionIds(T2 node) {\r
247                 Collection<String> ids = new ArrayList<String>();\r
248                 for (String s : connections.getKeys()) {\r
249                         for (T c : connections.getValues(s)) {\r
250                                 if (node.equals(c.getOther(this))) {\r
251                                         if (!s.endsWith("/str"))\r
252                                                 ids.add(s);\r
253                                         else\r
254                                                 ids.add(s.substring(0, s.length()-4));\r
255                                 }\r
256                         }\r
257                 }\r
258                 return ids;\r
259         }\r
260         \r
261         @Override\r
262         public void remove() {\r
263                 List<T> connections = getAllConnections();\r
264                 for (T c : connections) {\r
265                         removeConnection(c);\r
266                 }\r
267                 super.remove();\r
268         \r
269         }\r
270 }\r