]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLScenegraph.java
1 package org.simantics.modeling;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.diagram.elements.DiagramNodeUtil;
11 import org.simantics.diagram.elements.TextGridNode;
12 import org.simantics.diagram.elements.TextNode;
13 import org.simantics.g2d.canvas.ICanvasContext;
14 import org.simantics.g2d.diagram.DiagramHints;
15 import org.simantics.g2d.diagram.IDiagram;
16 import org.simantics.g2d.diagram.handler.DataElementMap;
17 import org.simantics.g2d.diagram.participant.Selection;
18 import org.simantics.g2d.element.IElement;
19 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
20 import org.simantics.g2d.utils.CanvasUtils;
21 import org.simantics.scenegraph.ParentNode;
22 import org.simantics.scenegraph.g2d.G2DParentNode;
23 import org.simantics.scenegraph.g2d.G2DSceneGraph;
24 import org.simantics.scenegraph.g2d.events.command.Commands;
25 import org.simantics.scenegraph.g2d.nodes.BackgroundNode;
26 import org.simantics.scenegraph.g2d.nodes.BoundsNode;
27 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
28 import org.simantics.scenegraph.g2d.nodes.DataNode;
29 import org.simantics.scenegraph.g2d.nodes.DecorationSVGNode;
30 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
31 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
32 import org.simantics.scenegraph.utils.NodeUtil;
33 import org.simantics.trend.impl.ItemNode;
34 import org.simantics.utils.threads.ThreadUtils;
35
36 public class SCLScenegraph {
37         
38         public static ICanvasSceneGraphProvider getICanvasSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException, InterruptedException {
39                 ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(model, diagram, diagramRVI);
40                 return provider;
41         }
42         
43         public static void disposeSceneGraphProvider(ICanvasSceneGraphProvider provider) {
44                 provider.dispose();
45         }
46         
47         //public static Resource getDiagramRuntime(Resource ) 
48         
49         
50 //      public static String getNodeTransform(ICanvasContext ctx, String name) throws DatabaseException, InterruptedException {
51 //              return getNodeTransform(ctx, name);
52 //      }
53         
54         public static String getNodeTransform(ICanvasContext ctx, String name) {
55                 
56                 Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
57                 for (TextNode text : texts) {
58                         String nodeName = NodeUtil.getNodeName(text);
59                         if (nodeName.equals(name)) {
60                                 String transform = text.getTransform().toString();
61                                 return transform;
62                         }
63                 }
64                 return "No node found";
65         }
66         
67         public static String getNodeText(ICanvasContext ctx, String name) {
68                 
69                 Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
70                 for (TextNode text : texts) {
71                         String nodeName = NodeUtil.getNodeName(text);
72                         if (nodeName.equals(name)) {
73                                 String texti = text.getText();
74                                 return texti;
75                         }
76                 }
77                 return "No node found";
78         }
79         
80         public static String getNodeCount(ICanvasContext ctx) {
81                 G2DSceneGraph g2 = ctx.getSceneGraph();
82                 int amount = NodeUtil.countTreeNodes(g2);
83                 return "Node count: " + amount;
84         }
85         
86     public static String getAllNodes (ICanvasContext ctx) {
87         
88         Set<G2DSceneGraph> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DSceneGraph.class);
89         int amount = g2.size() +1;
90         return "All nodes: " + amount;
91     }
92     
93     public static String getBoundsNodes (ICanvasContext ctx) {
94         
95         Set<BoundsNode> bn = NodeUtil.collectNodes(ctx.getSceneGraph(), BoundsNode.class);
96         int amount = bn.size();
97         return "BoundsNodes: " + amount;
98     }
99     
100     public static String getBackgroundNodes (ICanvasContext ctx) {
101         
102         Set<BackgroundNode> bg = NodeUtil.collectNodes(ctx.getSceneGraph(), BackgroundNode.class);
103         int amount = bg.size();
104         return "BackgroundNodes: " + amount;
105     }
106     
107     public static String getDataNodes (ICanvasContext ctx) {
108         
109         Set<DataNode> dn = NodeUtil.collectNodes(ctx.getSceneGraph(), DataNode.class);
110         int amount = dn.size();
111         return "DataNodes: " + amount;
112     }
113     
114     public static String getNavigationNodes (ICanvasContext ctx) {
115         
116         Set<NavigationNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), NavigationNode.class);
117         int amount = g2.size();
118         return "NavigationNodes: " + amount;
119     }
120     
121     public static String getParentNodes (ICanvasContext ctx) {
122         
123         Set<G2DParentNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DParentNode.class);
124         int amount = g2.size();
125         return "ParentNodes: " + amount;
126     }
127     
128     public static String getDecorationNodes (ICanvasContext ctx) {
129         
130         Set<DecorationSVGNode> deco = NodeUtil.collectNodes(ctx.getSceneGraph(), DecorationSVGNode.class);
131         int amount = deco.size();
132         return "DecorationNodes: " + amount;
133     }
134     
135     public static String getSingleElementNodes (ICanvasContext ctx) {
136         
137         Set<SingleElementNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), SingleElementNode.class);
138         int amount = g2.size();
139         return "SingleElementNodes: " + amount;
140     }
141     
142     public static String getConnectionNodes (ICanvasContext ctx) {
143         
144         Set<ConnectionNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), ConnectionNode.class);
145         int amount = g2.size();
146         return "ConnectionNodes: " + amount;
147     }
148     
149     public static String getTextNodes (ICanvasContext ctx) {
150         
151         Set<TextNode> tn = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
152         Set<TextGridNode> tgn = NodeUtil.collectNodes(ctx.getSceneGraph(), TextGridNode.class);
153         int amount = tn.size() + tgn.size();
154         return "TextNodes: " + amount;
155     }
156     
157     public static String getItemNodes (ICanvasContext ctx) {
158         
159         Set<ItemNode> item = NodeUtil.collectNodes(ctx.getSceneGraph(), ItemNode.class);
160         int amount = item.size();
161         return "ItemNodes: " + amount;
162     }
163   
164     public static String editNodeText (ICanvasContext ctx, String module, String previous_value, String new_value) {
165                 
166         Set<TextNode> textGridNodes = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
167         for (TextNode modulenode : textGridNodes) {
168                 if (module.equals(modulenode.getText())) {
169                         //System.out.println("Module what we were looking for: " + module);
170                         //System.out.println("Modulenode: " + modulenode.getText());
171                         
172                         ParentNode<?> parentnode = modulenode.getParent();
173                         //System.out.println("Parentnode: " + parentnode);
174                         
175                         Collection<TextNode> textnodes = (Collection<TextNode>) parentnode.getNodes();
176                         for (TextNode valuenode : textnodes) {
177                                 if (previous_value.equals(valuenode.getText())) {
178                                         //System.out.println("Value what we were looking for: " + previous_value);
179                                         //System.out.println("Valuenode: " + valuenode.getText());
180                                         
181                                         //valuenode.setEditMode(true);
182                                         valuenode.activateEdit(0, null, ctx);
183                                         valuenode.setText(new_value);
184                                         valuenode.fireTextEditingEnded();
185                                         
186                                         //System.out.println("valuenode modified: " + valuenode);
187                                         return "Modified module " + module + " with value " + new_value;
188                                 }
189                         }
190                         return "Not found module : " + module;
191                 }
192         }
193         return "No nodes in scenegraph!";
194     }
195
196     public static String sceneGraphTest (ICanvasContext ctx, String module, String value) {
197         
198         boolean module_founded = false;
199         boolean value_founded = false;
200         
201         Set<G2DSceneGraph> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DSceneGraph.class);
202         System.out.println("Total amount of nodes: " + g2.size() + 1);
203         
204         Set<TextGridNode> grid = NodeUtil.collectNodes(ctx.getSceneGraph(), TextGridNode.class);
205         Integer textGridNodeAmount = grid.size();
206         System.out.println("Amount of TextGridNodes " + textGridNodeAmount);
207         
208         Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
209         Integer textNodeAmount = grid.size();
210         System.out.println("Amount of TextNodes " + textNodeAmount);
211
212         for (TextNode node : texts) {
213             if (module.equals(node.getText())) {
214                 module_founded = true;
215                 System.out.println("Correct module " + module + " founded.");
216             }
217             if (value.equals(node.getText())) {
218                 value_founded = true;
219                 System.out.println("Correct value " + value + " founded.");
220             }
221         }
222         
223         if (value_founded == true && module_founded == true) {
224                 return "Found both correct module " + module + " and value " + value;
225         }
226         if (value_founded == false && module_founded == true) {
227                 return "Found only correct module " + module + " but not value " + value;
228         }
229         if (value_founded == true && module_founded == false) {
230                 return "Found only correct value " + value + " but not module " + module;
231         }
232         else {
233                 return "Didn't found either module " + module + " or value " + value;
234         }
235     }
236     
237      public static boolean copyPaste (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {
238         
239         IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
240
241                 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
242                 if (dem != null) {
243                         final Collection<IElement> newSelection = new ArrayList<IElement>();
244                         for (Resource module : modules) {
245                                 IElement element = dem.getElement(idiagram, module);
246                                 if (element != null) {
247                                         newSelection.add(element);
248                                 } else {
249                                         throw new DatabaseException("Could not find IElement for " + element);
250                                 }
251                         }
252                         
253                         ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
254                     @Override
255                     public void run() {
256                         if (source_ctx.isDisposed())
257                             return;
258                         Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);
259                         if (selection != null) {
260                             // This prevents workbench selection from being left over.
261                             // Also prevents scene graph crap from being left on the screen.
262                             selection.setSelection(0, newSelection);
263                         }
264                                 CanvasUtils.sendCommand(source_ctx, Commands.COPY);
265                                 CanvasUtils.sendCommand(target_ctx, Commands.PASTE);
266                     }
267                 });
268                         
269                 //}
270                 
271                 while(source_ctx.getEventQueue().size() > 0) {
272                         try {
273                                 Thread.sleep(10);
274                         } catch (InterruptedException e) {
275                                 throw new DatabaseException(e);
276                         }
277                 }
278
279                 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
280             @Override
281             public void run() {
282             }
283         });             
284                                 
285                 }
286                 return true;
287     }
288
289
290 }