1 package org.simantics.modeling;
3 import java.util.ArrayList;
4 import java.util.Collection;
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;
36 public class SCLScenegraph {
38 public static ICanvasSceneGraphProvider getICanvasSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException, InterruptedException {
39 ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(model, diagram, diagramRVI);
43 public static void disposeSceneGraphProvider(ICanvasSceneGraphProvider provider) {
47 //public static Resource getDiagramRuntime(Resource )
50 // public static String getNodeTransform(ICanvasContext ctx, String name) throws DatabaseException, InterruptedException {
51 // return getNodeTransform(ctx, name);
54 public static String getNodeTransform(ICanvasContext ctx, String name) {
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();
64 return "No node found";
67 public static String getNodeText(ICanvasContext ctx, String name) {
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();
77 return "No node found";
80 public static String getNodeCount(ICanvasContext ctx) {
81 G2DSceneGraph g2 = ctx.getSceneGraph();
82 int amount = NodeUtil.countTreeNodes(g2);
83 return "Node count: " + amount;
86 public static String getAllNodes (ICanvasContext ctx) {
88 Set<G2DSceneGraph> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DSceneGraph.class);
89 int amount = g2.size() +1;
90 return "All nodes: " + amount;
93 public static String getBoundsNodes (ICanvasContext ctx) {
95 Set<BoundsNode> bn = NodeUtil.collectNodes(ctx.getSceneGraph(), BoundsNode.class);
96 int amount = bn.size();
97 return "BoundsNodes: " + amount;
100 public static String getBackgroundNodes (ICanvasContext ctx) {
102 Set<BackgroundNode> bg = NodeUtil.collectNodes(ctx.getSceneGraph(), BackgroundNode.class);
103 int amount = bg.size();
104 return "BackgroundNodes: " + amount;
107 public static String getDataNodes (ICanvasContext ctx) {
109 Set<DataNode> dn = NodeUtil.collectNodes(ctx.getSceneGraph(), DataNode.class);
110 int amount = dn.size();
111 return "DataNodes: " + amount;
114 public static String getNavigationNodes (ICanvasContext ctx) {
116 Set<NavigationNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), NavigationNode.class);
117 int amount = g2.size();
118 return "NavigationNodes: " + amount;
121 public static String getParentNodes (ICanvasContext ctx) {
123 Set<G2DParentNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DParentNode.class);
124 int amount = g2.size();
125 return "ParentNodes: " + amount;
128 public static String getDecorationNodes (ICanvasContext ctx) {
130 Set<DecorationSVGNode> deco = NodeUtil.collectNodes(ctx.getSceneGraph(), DecorationSVGNode.class);
131 int amount = deco.size();
132 return "DecorationNodes: " + amount;
135 public static String getSingleElementNodes (ICanvasContext ctx) {
137 Set<SingleElementNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), SingleElementNode.class);
138 int amount = g2.size();
139 return "SingleElementNodes: " + amount;
142 public static String getConnectionNodes (ICanvasContext ctx) {
144 Set<ConnectionNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), ConnectionNode.class);
145 int amount = g2.size();
146 return "ConnectionNodes: " + amount;
149 public static String getTextNodes (ICanvasContext ctx) {
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;
157 public static String getItemNodes (ICanvasContext ctx) {
159 Set<ItemNode> item = NodeUtil.collectNodes(ctx.getSceneGraph(), ItemNode.class);
160 int amount = item.size();
161 return "ItemNodes: " + amount;
164 public static String editNodeText (ICanvasContext ctx, String module, String previous_value, String new_value) {
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());
172 ParentNode<?> parentnode = modulenode.getParent();
173 //System.out.println("Parentnode: " + parentnode);
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());
181 //valuenode.setEditMode(true);
182 valuenode.activateEdit(0, null, ctx);
183 valuenode.setText(new_value);
184 valuenode.fireTextEditingEnded();
186 //System.out.println("valuenode modified: " + valuenode);
187 return "Modified module " + module + " with value " + new_value;
190 return "Not found module : " + module;
193 return "No nodes in scenegraph!";
196 public static String sceneGraphTest (ICanvasContext ctx, String module, String value) {
198 boolean module_founded = false;
199 boolean value_founded = false;
201 Set<G2DSceneGraph> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DSceneGraph.class);
202 System.out.println("Total amount of nodes: " + g2.size() + 1);
204 Set<TextGridNode> grid = NodeUtil.collectNodes(ctx.getSceneGraph(), TextGridNode.class);
205 Integer textGridNodeAmount = grid.size();
206 System.out.println("Amount of TextGridNodes " + textGridNodeAmount);
208 Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
209 Integer textNodeAmount = grid.size();
210 System.out.println("Amount of TextNodes " + textNodeAmount);
212 for (TextNode node : texts) {
213 if (module.equals(node.getText())) {
214 module_founded = true;
215 System.out.println("Correct module " + module + " founded.");
217 if (value.equals(node.getText())) {
218 value_founded = true;
219 System.out.println("Correct value " + value + " founded.");
223 if (value_founded == true && module_founded == true) {
224 return "Found both correct module " + module + " and value " + value;
226 if (value_founded == false && module_founded == true) {
227 return "Found only correct module " + module + " but not value " + value;
229 if (value_founded == true && module_founded == false) {
230 return "Found only correct value " + value + " but not module " + module;
233 return "Didn't found either module " + module + " or value " + value;
237 public static boolean copyPaste (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {
239 IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
241 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
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);
249 throw new DatabaseException("Could not find IElement for " + element);
253 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
256 if (source_ctx.isDisposed())
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);
264 CanvasUtils.sendCommand(source_ctx, Commands.COPY);
265 CanvasUtils.sendCommand(target_ctx, Commands.PASTE);
271 while(source_ctx.getEventQueue().size() > 0) {
274 } catch (InterruptedException e) {
275 throw new DatabaseException(e);
279 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {