]> gerrit.simantics Code Review - simantics/3d.git/blob
05587e5df3ceed25eacce21520be8c211d20fe35
[simantics/3d.git] /
1 package org.simantics.processeditor.adapters;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.HashMap;\r
6 import java.util.List;\r
7 import java.util.Map;\r
8 \r
9 import org.simantics.db.Graph;\r
10 import org.simantics.db.Resource;\r
11 import org.simantics.db.Session;\r
12 import org.simantics.layer0.utils.EntityFactory;\r
13 import org.simantics.layer0.utils.IEntity;\r
14 import org.simantics.layer0.utils.Property;\r
15 import org.simantics.processeditor.ProcessResource;\r
16 import org.simantics.processeditor.common.ControlPointTools;\r
17 import org.simantics.processeditor.common.PipingRules;\r
18 import org.simantics.processeditor.scenegraph.NonVisibleNode;\r
19 import org.simantics.processeditor.scenegraph.PipeComponentNode;\r
20 import org.simantics.processeditor.scenegraph.PipeRunNode;\r
21 import org.simantics.processeditor.scenegraph.PipelineComponentNode;\r
22 import org.simantics.processeditor.stubs.PipeControlPoint;\r
23 import org.simantics.processeditor.stubs.PipeRun;\r
24 import org.simantics.processeditor.stubs.Plant3DResource;\r
25 import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
26 import org.simantics.proconf.g3d.base.ScenegraphAdapterImpl;\r
27 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
28 import org.simantics.proconf.g3d.scenegraph.IGeometryNode;\r
29 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;\r
30 import org.simantics.proconf.g3d.scenegraph.ISelectableNode;\r
31 import org.simantics.proconf.g3d.scenegraph.ParameterizedModelNode;\r
32 import org.simantics.proconf.g3d.stubs.G3DNode;\r
33 import org.simantics.utils.ui.ErrorLogger;\r
34 \r
35 public class ProcessEditorAdapter extends ScenegraphAdapterImpl {\r
36         private ThreeDimensionalEditorBase editor;\r
37         \r
38          public ProcessEditorAdapter(ThreeDimensionalEditorBase editor, Session session, JmeRenderingComponent component) {\r
39                 super(session, component);\r
40                 this.editor = editor;\r
41          }\r
42          \r
43          private class NormalScenegraphQuery extends ScenegraphQuery {\r
44                 \r
45                 public NormalScenegraphQuery(Resource node) {\r
46                         super(node);\r
47                 } \r
48                  \r
49                  \r
50                  @Override\r
51                 public void shapeAdded(Graph graph, IGraphicsNode node) {\r
52                          // FIXME : this won't work like in previous ProConf\r
53                 }\r
54          }\r
55          \r
56          private Map<Resource,PipeRunControlPointQuery> pipeRunQueries = new HashMap<Resource, PipeRunControlPointQuery>();\r
57 \r
58         protected ScenegraphQuery newSubnodeListener(G3DNode node) {\r
59                 if (node.isInstanceOf(ProcessResource.plant3Dresource.PipeRun)) {\r
60                     PipeRunControlPointQuery query = new PipeRunControlPointQuery(node.getResource());\r
61                     pipeRunQueries.put(node.getResource(), query);\r
62                     node.getGraph().performQuery(query);\r
63                 }\r
64                 return new NormalScenegraphQuery(node.getResource());\r
65                  \r
66             }\r
67         \r
68         @Override\r
69         protected NodePropertyQuery newRootPropertyListener(G3DNode root) {\r
70                 // currently Plant does not have any properties.\r
71                 return null;\r
72         }\r
73         \r
74         private class TransformationQuery extends NodeTransformationQuery {\r
75                 \r
76                 public TransformationQuery(Resource res) {\r
77                         super(res);\r
78                 }\r
79                 \r
80                 @Override\r
81                 public void shapeUpdated(Graph graph, IGraphicsNode shape) {\r
82                         //if (shape instanceof IGeometryNode) {\r
83            //    updateGeometry((IGeometryNode)shape);\r
84            //} else {\r
85                 shape.updateTransform(graph);\r
86            //}\r
87                 }\r
88         }\r
89         \r
90         @Override\r
91         protected NodeTransformationQuery newTransformationListener(G3DNode root) {\r
92                 return new TransformationQuery(root.getResource());\r
93         }\r
94         \r
95         private class NormalNodePropertyQuery extends org.simantics.proconf.g3d.base.ScenegraphAdapterImpl.NodePropertyQuery {\r
96                 \r
97                 public NormalNodePropertyQuery(Resource resource) {\r
98                         super(resource);\r
99                 }\r
100 \r
101                 @Override\r
102                 public void shapeUpdated(Graph graph,IGraphicsNode shape) {\r
103                         if (shape instanceof IGeometryNode) {\r
104                updateGeometry((IGeometryNode)shape);\r
105            } else {\r
106                 shape.updateTransform(graph);\r
107            }\r
108                 }\r
109         }\r
110          @Override\r
111         protected NodePropertyQuery newPropertyListener(G3DNode node) {\r
112                 return new NormalNodePropertyQuery(node.getResource());\r
113         }\r
114 \r
115          @Override\r
116         protected IGraphicsNode instantiateNode(IGraphicsNode parent,\r
117                         G3DNode node) {\r
118                 Plant3DResource p3r = ProcessResource.plant3Dresource;\r
119                 IGraphicsNode newNode = null;\r
120                 try {\r
121                         if (node.isInstanceOf(p3r.Equipment)) {\r
122                                 newNode = new ParameterizedModelNode(\r
123                                                 editor, parent, node.getGraph(),\r
124                                                 node.getResource(), p3r.HasGraphics);\r
125                         } else if (node.isInstanceOf(p3r.PipeRun)) {\r
126                                 newNode = new PipeRunNode(parent, node.getGraph(), node.getResource());\r
127                         } else if (node.isInstanceOf(p3r.Nozzle)) {\r
128                                 newNode = new ParameterizedModelNode(\r
129                                                 editor, parent, node.getGraph(),\r
130                                                 node.getResource(), p3r.HasGraphics);\r
131                                 // CodedComponent must be handled first since it uses\r
132                                 // hard-coded geometries\r
133                                 // TODO : is this really necessary, or could we unify\r
134                                 // PipeComponentNode, InlineComponentNode,...\r
135                         } else if (node.isInstanceOf(p3r.CodedComponent)) {\r
136                                 newNode = new PipeComponentNode(editor,\r
137                                                 parent, node.getGraph(), node.getResource());\r
138                         } else if (node.isInstanceOf(p3r.NonVisibleComponent)) {\r
139                                 newNode = new NonVisibleNode(parent, node.getGraph(), node.getResource());\r
140                         } else if (node.isInstanceOf(p3r.PipelineComponent)) {\r
141                                 newNode = new PipelineComponentNode(editor,\r
142                                                 parent, node.getGraph(), node.getResource());\r
143                         } \r
144 \r
145                         // } else if (node instanceof Shape) // Markers (ar/mobile)\r
146                         // needed this\r
147                         // newNode = new ShapeNode(TestProcessEditor.this,parent,node);\r
148                         if (newNode != null) {\r
149                                 if (newNode instanceof ISelectableNode)\r
150                                         ((ISelectableNode) newNode).setVisible(true);\r
151                                 if (newNode instanceof IGeometryNode) {\r
152                                         updateGeometry((IGeometryNode) newNode);\r
153                                 }\r
154                                 return newNode;\r
155                         }\r
156                 } catch (Exception e) {\r
157                         ErrorLogger.defaultLogError("Cannot handle node " + node.getResource(), e);\r
158                         return null;\r
159                 }\r
160                 ErrorLogger.defaultLogError("Cannot handle node " + node.getResource(), null);\r
161                 return null;\r
162 \r
163         }\r
164 \r
165         /**\r
166          * This is used to create elbows and straight pipes to pipeline TODO :\r
167          * this should be done with rule-engine!\r
168          * \r
169          * \r
170          * @author Marko Luukkainen\r
171          * \r
172          */\r
173         protected class PipeRunControlPointQuery extends NodeQuery {\r
174                 private List<Resource> removed = new ArrayList<Resource>();\r
175                 private List<Resource> added = new ArrayList<Resource>();\r
176 \r
177                 public PipeRunControlPointQuery(Resource r) {\r
178                         super(r);\r
179                         if (DEBUG) System.out.println("Created PipeRunControlPointQuery for " + r);\r
180 \r
181                 }\r
182 \r
183                 @Override\r
184                 protected Object compute2(Graph graph) {\r
185                         PipeRun run = new PipeRun(graph, nodeResource);\r
186                         Collection<IEntity> cps = run\r
187                                         .getRelatedObjects(ProcessResource.plant3Dresource.HasControlPoints);\r
188                         List<Resource> res = new ArrayList<Resource>();\r
189                         for (IEntity t : cps)\r
190                                 res.add(t.getResource());\r
191                         return res;\r
192                 }\r
193 \r
194                 @Override\r
195                 public boolean updated(Graph graph, Object oldResult,\r
196                                 Object newResult) {\r
197                         \r
198                         removed.clear();\r
199                         added.clear();\r
200 \r
201                         List<Resource> oldCps = (List<Resource>) oldResult;\r
202                         List<Resource> newCps = (List<Resource>) newResult;\r
203                         if (oldCps == null)\r
204                                 oldCps = new ArrayList<Resource>();\r
205 \r
206                         for (Resource r : oldCps) {\r
207                                 if (!newCps.contains(r))\r
208                                         removed.add(r);\r
209                         }\r
210 \r
211                         for (Resource r : newCps) {\r
212                                 if (!oldCps.contains(r))\r
213                                         added.add(r);\r
214                         }\r
215                         for (Resource r : removed)\r
216                                 removeControlPoint(graph, r);\r
217                         for (Resource r : added) {\r
218                                 addControlPoint(graph, r);\r
219                                 // ControlPointTools.addControlPoint(new\r
220                                 // PipeRun(graph,pipeRun), new PipeControlPoint(graph, r));\r
221                         }\r
222 \r
223                         return (added.size() > 0 || removed.size() > 0);\r
224                 }\r
225                 \r
226                 @Override\r
227                 public void dispose() {\r
228                         super.dispose();\r
229                         for (ControlPointPropertyQuery q : controlPointPropertyQueries.values())\r
230                                 q.dispose();\r
231                         controlPointPropertyQueries.clear();\r
232                 }\r
233                 \r
234                 private Map<Resource,ControlPointPropertyQuery> controlPointPropertyQueries = new HashMap<Resource, ControlPointPropertyQuery>();\r
235                 \r
236                 private void addControlPoint(Graph graph, Resource resource) {\r
237                         ControlPointPropertyQuery query = new ControlPointPropertyQuery(resource);\r
238                         graph.performQuery(query);\r
239                         controlPointPropertyQueries.put(resource,query);\r
240                 }\r
241                 \r
242                 private void removeControlPoint(Graph graph, Resource resource) {\r
243                         ControlPointPropertyQuery query = controlPointPropertyQueries.remove(resource);\r
244                         query.dispose();\r
245                         ControlPointTools.removeControlPoint(new PipeControlPoint(\r
246                                         graph, resource));\r
247                 }\r
248 \r
249         }\r
250 \r
251         protected class ControlPointPropertyQuery extends NodeQuery {\r
252                 boolean initialized = false;\r
253                 \r
254                 public ControlPointPropertyQuery(Resource r) {\r
255                         super(r);\r
256                         if (DEBUG) System.out.println("Created ControlPointPropertyQuery for " + r);\r
257                 }\r
258                 \r
259                 @Override\r
260         public List<Object> compute2(Graph g) {\r
261                 IEntity t = EntityFactory.create(g,nodeResource);\r
262                 \r
263                 Collection<Property> properties = t.getRelatedProperties(ProcessResource.builtins.HasProperty);\r
264                 List<Object> propertyValues = new ArrayList<Object>();\r
265                 p(properties,propertyValues);\r
266 \r
267                 return propertyValues;\r
268         }\r
269         \r
270         private void p(Collection<Property> properties, List<Object> propertyValues) {\r
271                 for (Property p : properties) {\r
272                         Collection<Property> subProperties = p.getRelatedProperties(p.getGraph().getBuiltins().HasProperty);\r
273                         if (subProperties.size() != 0) {\r
274                                 p(subProperties,propertyValues);\r
275                         } \r
276                         if (p.hasValue()){\r
277                                 propertyValues.add(p.getValue());\r
278                         }\r
279                 }\r
280         }\r
281                 \r
282                 @Override\r
283                 public boolean updated(Graph graph, Object oldResult, Object newResult) {\r
284                         PipingRules.pipeControlPointPositionUpdate(graph, this.nodeResource);\r
285                         if (initialized) {\r
286                                 //PipingRules.pipeControlPointPositionUpdate(graph, this.nodeResource);\r
287                         } else {\r
288                                 initialized = true;\r
289                         }\r
290                         return true;\r
291                 }\r
292         }\r
293         \r
294         @Override\r
295         protected void removeNode(Resource parent, Resource r) {\r
296                 super.removeNode(parent, r);\r
297                 PipeRunControlPointQuery q = pipeRunQueries.get(r);\r
298                 if (q != null)\r
299                         q.dispose();\r
300         }\r
301         \r
302         @Override\r
303         public void dispose() {\r
304                 super.dispose();\r
305         }\r
306 \r
307 }\r