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