]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scl/P3DScriptNodeMap.java
Remove dependencies on log4j
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scl / P3DScriptNodeMap.java
1 package org.simantics.plant3d.scl;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.RequestProcessor;
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.g3d.ontology.G3D;
11 import org.simantics.g3d.scenegraph.base.INode;
12 import org.simantics.g3d.scenegraph.base.ParentNode;
13 import org.simantics.g3d.scl.ScriptNodeMap;
14 import org.simantics.objmap.graph.IMapping;
15 import org.simantics.plant3d.ontology.Plant3D;
16 import org.simantics.plant3d.scenegraph.IP3DNode;
17 import org.simantics.plant3d.scenegraph.IP3DVisualNode;
18 import org.simantics.plant3d.scenegraph.P3DParentNode;
19 import org.simantics.plant3d.scenegraph.P3DRootNode;
20 import org.simantics.plant3d.scenegraph.ParameterizedNode;
21 import org.simantics.plant3d.scenegraph.PipeRun;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
23 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
24
25 public class P3DScriptNodeMap extends ScriptNodeMap<Resource,INode> {
26         
27         private static final boolean DEBUG = false;
28         
29         public P3DScriptNodeMap(RequestProcessor session, IMapping mapping, P3DRootNode rootNode) {
30                 super(session,mapping,rootNode);
31                 rootNode.setMapping(mapping);
32         }
33         
34         @Override
35         protected void updateActor(INode n, Set<String> ids) {
36                 if (DEBUG) System.out.println("P3DNodeMap update " + n);
37                 if (!(n instanceof IP3DVisualNode)) {
38                         if (n instanceof PipeControlPoint) {
39                                 n = ((PipeControlPoint)n).getPipelineComponent();
40                                 if (n == null)
41                                         return;
42                         } else {
43                                 return;
44                         }
45                 }
46                 
47                 IP3DVisualNode node = (IP3DVisualNode)n;
48                 
49                 if (DEBUG) {
50                         System.out.print("P3DNodeMap update " + node);
51                         for (String s : ids)
52                                 System.out.print(" " + s);
53                         System.out.println();
54                 }
55                 
56                 if (ids.contains(Plant3D.URIs.hasGeometry)) {
57                         //node.visualize(view);
58                         //updateRenderObjectsFor(node);
59                         updateTransform(node);
60                 } 
61                 if (n instanceof ParameterizedNode) {
62                         ParameterizedNode geom = (ParameterizedNode)n;
63                         for (String id : geom.getParameterMap().keySet()) {
64                                 if (ids.contains(id)) {
65 //                    node.visualize(view);
66 //                    updateRenderObjectsFor(node);
67                                         updateTransform(node);
68                                         break;
69                                 }
70                         }
71                 } else if (n instanceof PipeRun) {
72                         // FIXME: may require rule based update!
73                         PipeRun run = (PipeRun)n;
74                         Set<String> ids2 = new HashSet<String>();
75                         ids2.add(Plant3D.URIs.hasGeometry);
76                         for (PipeControlPoint pcp : run.getControlPoints()) {
77                                 updateActor(pcp, ids2);
78                         }
79                 }
80                 
81                 if (ids.contains(G3D.URIs.hasPosition) || 
82                         ids.contains(G3D.URIs.hasOrientation) ||
83                         ids.contains(G3D.URIs.hasWorldPosition) ||
84                         ids.contains(G3D.URIs.hasWorldOrientation)) {
85                         updateTransform(node);
86                 }
87         }
88         
89         private void updateTransform(IP3DNode node) {
90                 if (DEBUG) System.out.println("P3DNodeMap update Transform " + node);
91
92                 
93                 if (node instanceof ParentNode<?>) {
94                         @SuppressWarnings("unchecked")
95                         ParentNode<IP3DNode> p = (ParentNode<IP3DNode>)node;
96                         for (IP3DNode n : p.getNodes())
97                                 updateTransform(n);
98                 }
99         }
100         
101         @Override
102         protected void removeActor(INode n) {
103                 if (DEBUG) System.out.println("P3DNodeMap.removeActor " + n);
104                 if (!(n instanceof IP3DVisualNode))
105                         return;
106                 IP3DVisualNode node = (IP3DVisualNode)n;
107 //        remActor(node);
108                 
109                 if (node instanceof P3DParentNode<?>) {
110                         for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
111                                 if (n2 instanceof IP3DVisualNode)
112                                         removeActor((IP3DVisualNode)n2);
113                 }
114         }
115         
116         @Override
117         protected void addActor(INode n) {
118                 if (DEBUG) System.out.println("P3DNodeMap.addActor " + n);
119                 if (!(n instanceof IP3DVisualNode))
120                         return;
121                 IP3DVisualNode node = (IP3DVisualNode)n;
122                 
123 //        if (hasActor(node))
124 //            return;
125                 
126                 if (node instanceof P3DParentNode<?>) {
127                         for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
128                                 if (n2 instanceof IP3DVisualNode)
129                                         addActor((IP3DVisualNode)n2);
130                 }
131                 
132                 updateTransform(node);
133         }
134         
135         @Override
136         protected void update(ReadGraph graph) throws DatabaseException {
137                 validate();
138 //      System.out.println("Graph updates");
139                 super.update(graph);
140                 validate();
141         }
142         
143         @Override
144         public void commit(String commitMessage) {
145                 validate();
146 //      System.out.println("Graph commit");
147                 super.commit(commitMessage);
148                 
149         }
150         @Override
151         protected void doCommit() throws DatabaseException{
152 //      System.out.println("Do commit");
153                 validate();
154                 super.doCommit();
155         }
156         
157         private void validate() {
158                 for (INode node : rootNode.getNodes()) {
159                         if (node instanceof PipeRun)
160                                 PipingRules.validate((PipeRun)node);
161                 }
162         }
163         
164         @Override
165         public void update() throws DatabaseException {
166                 try {
167                         boolean b = true;
168                         while (b) {
169                                 updateCycle();
170                                 b = PipingRules.update();
171                         }
172                 } catch (Exception e) {
173                         e.printStackTrace();
174                 }
175                 super.update();
176         }
177
178 }