1 package org.simantics.processeditor.adapters;
\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
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
35 public class ProcessEditorAdapter extends ScenegraphAdapterImpl {
\r
36 private ThreeDimensionalEditorBase editor;
\r
38 public ProcessEditorAdapter(ThreeDimensionalEditorBase editor, Session session, JmeRenderingComponent component) {
\r
39 super(session, component);
\r
40 this.editor = editor;
\r
43 private class NormalScenegraphQuery extends ScenegraphQuery {
\r
45 public NormalScenegraphQuery(Resource node) {
\r
51 public void shapeAdded(Graph graph, IGraphicsNode node) {
\r
52 // FIXME : this won't work like in previous ProConf
\r
56 private Map<Resource,PipeRunControlPointQuery> pipeRunQueries = new HashMap<Resource, PipeRunControlPointQuery>();
\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
64 return new NormalScenegraphQuery(node.getResource());
\r
69 protected NodePropertyQuery newRootPropertyListener(G3DNode root) {
\r
70 // currently Plant does not have any properties.
\r
74 private class TransformationQuery extends NodeTransformationQuery {
\r
76 public TransformationQuery(Resource res) {
\r
81 public void shapeUpdated(Graph graph, IGraphicsNode shape) {
\r
82 //if (shape instanceof IGeometryNode) {
\r
83 // updateGeometry((IGeometryNode)shape);
\r
85 shape.updateTransform(graph);
\r
91 protected NodeTransformationQuery newTransformationListener(G3DNode root) {
\r
92 return new TransformationQuery(root.getResource());
\r
95 private class NormalNodePropertyQuery extends org.simantics.proconf.g3d.base.ScenegraphAdapterImpl.NodePropertyQuery {
\r
97 public NormalNodePropertyQuery(Resource resource) {
\r
102 public void shapeUpdated(Graph graph,IGraphicsNode shape) {
\r
103 if (shape instanceof IGeometryNode) {
\r
104 updateGeometry((IGeometryNode)shape);
\r
106 shape.updateTransform(graph);
\r
111 protected NodePropertyQuery newPropertyListener(G3DNode node) {
\r
112 return new NormalNodePropertyQuery(node.getResource());
\r
116 protected IGraphicsNode instantiateNode(IGraphicsNode parent,
\r
118 Plant3DResource p3r = ProcessResource.plant3Dresource;
\r
119 IGraphicsNode newNode = null;
\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
145 // } else if (node instanceof Shape) // Markers (ar/mobile)
\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
156 } catch (Exception e) {
\r
157 ErrorLogger.defaultLogError("Cannot handle node " + node.getResource(), e);
\r
160 ErrorLogger.defaultLogError("Cannot handle node " + node.getResource(), null);
\r
166 * This is used to create elbows and straight pipes to pipeline TODO :
\r
167 * this should be done with rule-engine!
\r
170 * @author Marko Luukkainen
\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
177 public PipeRunControlPointQuery(Resource r) {
\r
179 if (DEBUG) System.out.println("Created PipeRunControlPointQuery for " + r);
\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
195 public boolean updated(Graph graph, Object oldResult,
\r
196 Object newResult) {
\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
206 for (Resource r : oldCps) {
\r
207 if (!newCps.contains(r))
\r
211 for (Resource r : newCps) {
\r
212 if (!oldCps.contains(r))
\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
223 return (added.size() > 0 || removed.size() > 0);
\r
227 public void dispose() {
\r
229 for (ControlPointPropertyQuery q : controlPointPropertyQueries.values())
\r
231 controlPointPropertyQueries.clear();
\r
234 private Map<Resource,ControlPointPropertyQuery> controlPointPropertyQueries = new HashMap<Resource, ControlPointPropertyQuery>();
\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
242 private void removeControlPoint(Graph graph, Resource resource) {
\r
243 ControlPointPropertyQuery query = controlPointPropertyQueries.remove(resource);
\r
245 ControlPointTools.removeControlPoint(new PipeControlPoint(
\r
251 protected class ControlPointPropertyQuery extends NodeQuery {
\r
252 boolean initialized = false;
\r
254 public ControlPointPropertyQuery(Resource r) {
\r
256 if (DEBUG) System.out.println("Created ControlPointPropertyQuery for " + r);
\r
260 public List<Object> compute2(Graph g) {
\r
261 IEntity t = EntityFactory.create(g,nodeResource);
\r
263 Collection<Property> properties = t.getRelatedProperties(ProcessResource.builtins.HasProperty);
\r
264 List<Object> propertyValues = new ArrayList<Object>();
\r
265 p(properties,propertyValues);
\r
267 return propertyValues;
\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
277 propertyValues.add(p.getValue());
\r
283 public boolean updated(Graph graph, Object oldResult, Object newResult) {
\r
284 PipingRules.pipeControlPointPositionUpdate(graph, this.nodeResource);
\r
286 //PipingRules.pipeControlPointPositionUpdate(graph, this.nodeResource);
\r
288 initialized = true;
\r
295 protected void removeNode(Resource parent, Resource r) {
\r
296 super.removeNode(parent, r);
\r
297 PipeRunControlPointQuery q = pipeRunQueries.get(r);
\r
303 public void dispose() {
\r