1 package org.simantics.modeling;
3 import java.awt.BasicStroke;
4 import java.awt.Dimension;
5 import java.awt.RenderingHints;
6 import java.awt.RenderingHints.Key;
7 import java.awt.geom.AffineTransform;
8 import java.awt.geom.Rectangle2D;
9 import java.io.ByteArrayOutputStream;
10 import java.io.OutputStreamWriter;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
18 import java.util.Random;
20 import java.util.UUID;
21 import java.util.stream.Collectors;
23 import javax.xml.transform.OutputKeys;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.stream.StreamResult;
29 import org.apache.batik.dom.GenericDOMImplementation;
30 import org.apache.batik.svggen.SVGGeneratorContext;
31 import org.apache.batik.svggen.SVGGraphics2D;
32 import org.simantics.Simantics;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.common.request.UnaryRead;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.db.exception.RuntimeDatabaseException;
38 import org.simantics.diagram.elements.DiagramNodeUtil;
39 import org.simantics.diagram.elements.TextGridNode;
40 import org.simantics.diagram.elements.TextNode;
41 import org.simantics.diagram.stubs.DiagramResource;
42 import org.simantics.g2d.canvas.ICanvasContext;
43 import org.simantics.g2d.diagram.DiagramHints;
44 import org.simantics.g2d.diagram.IDiagram;
45 import org.simantics.g2d.diagram.handler.DataElementMap;
46 import org.simantics.g2d.diagram.participant.Selection;
47 import org.simantics.g2d.element.IElement;
48 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
49 import org.simantics.g2d.utils.CanvasUtils;
50 import org.simantics.layer0.Layer0;
51 import org.simantics.scenegraph.INode;
52 import org.simantics.scenegraph.ParentNode;
53 import org.simantics.scenegraph.g2d.G2DParentNode;
54 import org.simantics.scenegraph.g2d.G2DRenderingHints;
55 import org.simantics.scenegraph.g2d.G2DSceneGraph;
56 import org.simantics.scenegraph.g2d.IG2DNode;
57 import org.simantics.scenegraph.g2d.IG2DNodeVisitor;
58 import org.simantics.scenegraph.g2d.events.command.Commands;
59 import org.simantics.scenegraph.g2d.nodes.BackgroundNode;
60 import org.simantics.scenegraph.g2d.nodes.BoundsNode;
61 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
62 import org.simantics.scenegraph.g2d.nodes.DataNode;
63 import org.simantics.scenegraph.g2d.nodes.DecorationSVGNode;
64 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
65 import org.simantics.scenegraph.g2d.nodes.SVGNode;
66 import org.simantics.scenegraph.g2d.nodes.SelectionNode;
67 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
68 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
69 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
70 import org.simantics.scenegraph.utils.NodeUtil;
71 import org.simantics.scl.runtime.function.Function1;
72 import org.simantics.trend.impl.ItemNode;
73 import org.simantics.utils.threads.ThreadUtils;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76 import org.w3c.dom.DOMImplementation;
77 import org.w3c.dom.Document;
78 import org.w3c.dom.Element;
79 import org.w3c.dom.NodeList;
81 public class SCLScenegraph {
83 private static final Logger LOGGER = LoggerFactory.getLogger(SCLScenegraph.class);
85 public static ICanvasSceneGraphProvider getICanvasSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException, InterruptedException {
86 ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(model, diagram, diagramRVI);
90 public static void disposeSceneGraphProvider(ICanvasSceneGraphProvider provider) {
94 public static String getNodeTransform(ICanvasContext ctx, String name) {
96 Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
97 for (TextNode text : texts) {
98 String nodeName = NodeUtil.getNodeName(text);
99 if (nodeName.equals(name)) {
100 String transform = text.getTransform().toString();
104 return "No node found";
107 public static String getNodeText(ICanvasContext ctx, String name) {
109 Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
110 for (TextNode text : texts) {
111 String nodeName = NodeUtil.getNodeName(text);
112 if (nodeName.equals(name)) {
113 String texti = text.getText();
117 return "No node found";
120 public static String getNodeCount(ICanvasContext ctx) {
121 G2DSceneGraph g2 = ctx.getSceneGraph();
122 int amount = NodeUtil.countTreeNodes(g2);
123 return "Node count: " + amount;
126 public static String getAllNodes (ICanvasContext ctx) {
128 Set<G2DSceneGraph> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DSceneGraph.class);
129 int amount = g2.size() +1;
130 return "All nodes: " + amount;
133 public static String getBoundsNodes (ICanvasContext ctx) {
135 Set<BoundsNode> bn = NodeUtil.collectNodes(ctx.getSceneGraph(), BoundsNode.class);
136 int amount = bn.size();
137 return "BoundsNodes: " + amount;
140 public static String getBackgroundNodes (ICanvasContext ctx) {
142 Set<BackgroundNode> bg = NodeUtil.collectNodes(ctx.getSceneGraph(), BackgroundNode.class);
143 int amount = bg.size();
144 return "BackgroundNodes: " + amount;
147 public static String getDataNodes (ICanvasContext ctx) {
149 Set<DataNode> dn = NodeUtil.collectNodes(ctx.getSceneGraph(), DataNode.class);
150 int amount = dn.size();
151 return "DataNodes: " + amount;
154 public static String getNavigationNodes (ICanvasContext ctx) {
156 Set<NavigationNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), NavigationNode.class);
157 int amount = g2.size();
158 return "NavigationNodes: " + amount;
161 public static String getParentNodes (ICanvasContext ctx) {
163 Set<G2DParentNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DParentNode.class);
164 int amount = g2.size();
165 return "ParentNodes: " + amount;
168 public static String getDecorationNodes (ICanvasContext ctx) {
170 Set<DecorationSVGNode> deco = NodeUtil.collectNodes(ctx.getSceneGraph(), DecorationSVGNode.class);
171 int amount = deco.size();
172 return "DecorationNodes: " + amount;
175 public static String getSingleElementNodes (ICanvasContext ctx) {
177 Set<SingleElementNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), SingleElementNode.class);
178 int amount = g2.size();
179 return "SingleElementNodes: " + amount;
182 public static String getConnectionNodes (ICanvasContext ctx) {
184 Set<ConnectionNode> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), ConnectionNode.class);
185 int amount = g2.size();
186 return "ConnectionNodes: " + amount;
189 public static String getTextNodes (ICanvasContext ctx) {
191 Set<TextNode> tn = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
192 Set<TextGridNode> tgn = NodeUtil.collectNodes(ctx.getSceneGraph(), TextGridNode.class);
193 int amount = tn.size() + tgn.size();
194 return "TextNodes: " + amount;
197 public static String getItemNodes (ICanvasContext ctx) {
199 Set<ItemNode> item = NodeUtil.collectNodes(ctx.getSceneGraph(), ItemNode.class);
200 int amount = item.size();
201 return "ItemNodes: " + amount;
204 public static String editNodeText (ICanvasContext ctx, String module, String previous_value, String new_value) {
206 Set<TextNode> textGridNodes = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
207 for (TextNode modulenode : textGridNodes) {
208 if (module.equals(modulenode.getText())) {
209 //System.out.println("Module what we were looking for: " + module);
210 //System.out.println("Modulenode: " + modulenode.getText());
212 ParentNode<?> parentnode = modulenode.getParent();
213 //System.out.println("Parentnode: " + parentnode);
215 Collection<TextNode> textnodes = (Collection<TextNode>) parentnode.getNodes();
216 for (TextNode valuenode : textnodes) {
217 if (previous_value.equals(valuenode.getText())) {
218 //System.out.println("Value what we were looking for: " + previous_value);
219 //System.out.println("Valuenode: " + valuenode.getText());
221 //valuenode.setEditMode(true);
222 valuenode.activateEdit(0, null, ctx);
223 valuenode.setText(new_value);
224 valuenode.fireTextEditingEnded();
226 //System.out.println("valuenode modified: " + valuenode);
227 return "Modified module " + module + " with value " + new_value;
230 return "Not found module : " + module;
233 return "No nodes in scenegraph!";
236 public static String sceneGraphTest (ICanvasContext ctx, String module, String value) {
238 boolean module_founded = false;
239 boolean value_founded = false;
241 Set<G2DSceneGraph> g2 = NodeUtil.collectNodes(ctx.getSceneGraph(), G2DSceneGraph.class);
242 System.out.println("Total amount of nodes: " + g2.size() + 1);
244 Set<TextGridNode> grid = NodeUtil.collectNodes(ctx.getSceneGraph(), TextGridNode.class);
245 Integer textGridNodeAmount = grid.size();
246 System.out.println("Amount of TextGridNodes " + textGridNodeAmount);
248 Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
249 Integer textNodeAmount = grid.size();
250 System.out.println("Amount of TextNodes " + textNodeAmount);
252 for (TextNode node : texts) {
253 if (module.equals(node.getText())) {
254 module_founded = true;
255 System.out.println("Correct module " + module + " founded.");
257 if (value.equals(node.getText())) {
258 value_founded = true;
259 System.out.println("Correct value " + value + " founded.");
263 if (value_founded == true && module_founded == true) {
264 return "Found both correct module " + module + " and value " + value;
266 if (value_founded == false && module_founded == true) {
267 return "Found only correct module " + module + " but not value " + value;
269 if (value_founded == true && module_founded == false) {
270 return "Found only correct value " + value + " but not module " + module;
273 return "Didn't found either module " + module + " or value " + value;
277 public static boolean copyPaste (final ICanvasContext source_ctx, final ICanvasContext target_ctx, List<Resource> modules) throws DatabaseException {
279 IDiagram idiagram = source_ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
281 DataElementMap dem = idiagram.getDiagramClass().getAtMostOneItemOfClass(DataElementMap.class);
283 final Collection<IElement> newSelection = new ArrayList<IElement>();
284 for (Resource module : modules) {
285 IElement element = dem.getElement(idiagram, module);
286 if (element != null) {
287 newSelection.add(element);
289 throw new DatabaseException("Could not find IElement for " + element);
293 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
296 if (source_ctx.isDisposed())
298 Selection selection = source_ctx.getAtMostOneItemOfClass(Selection.class);
299 if (selection != null) {
300 // This prevents workbench selection from being left over.
301 // Also prevents scene graph crap from being left on the screen.
302 selection.setSelection(0, newSelection);
304 CanvasUtils.sendCommand(source_ctx, Commands.COPY);
305 CanvasUtils.sendCommand(target_ctx, Commands.PASTE);
311 while(source_ctx.getEventQueue().size() > 0) {
314 } catch (InterruptedException e) {
315 throw new DatabaseException(e);
319 ThreadUtils.syncExec(source_ctx.getThreadAccess(), new Runnable() {
329 static class Generator extends SVGGraphics2D {
332 String newElementId = null;
333 ArrayList<Element> elements = new ArrayList<Element>();
335 public static final String svgNS = "http://www.w3.org/2000/svg";
337 public Generator(SVGGeneratorContext ctx, boolean joku) {
341 public Generator(Document document) {
346 public Element getRoot() {
347 Element root = super.getRoot();
348 for(Element e : elements) {
355 public void setRenderingHint(Key arg0, Object arg1) {
356 if(G2DRenderingHints.KEY_BEGIN_ELEMENT == arg0) {
359 if(G2DRenderingHints.KEY_ELEMENT_ID == arg0) {
361 newElementId = arg1.toString();
363 newElementId = UUID.randomUUID().toString();
365 if(G2DRenderingHints.KEY_END_ELEMENT == arg0) {
368 Element group = getDOMFactory().createElement(SVG_G_TAG);
369 //Element group = getDOMFactory().createElementNS(SVG_NAMESPACE_URI, SVG_G_TAG);
370 group.setAttributeNS(null, "id", newElementId);
371 group.setAttributeNS(null, "class", arg1.toString());
376 super.setRenderingHint(arg0, arg1);
381 public static Element renderSVGNode(IG2DNode node) {
383 // Get a DOMImplementation.
384 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
386 // Create an instance of org.w3c.dom.Document.
387 String svgNS = "http://www.w3.org/2000/svg";
388 Document document = domImpl.createDocument(svgNS, "svg", null);
390 SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
391 ctx.setComment(null);
393 // Create an instance of the SVG Generator.
394 SVGGraphics2D svgGenerator = new Generator(ctx, false);
398 svgGenerator.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
399 svgGenerator.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
400 svgGenerator.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
402 node.render(svgGenerator);
404 } catch (Throwable t) {
405 LOGGER.error("Problems rendering scene graph to SVG", t);
408 return svgGenerator.getRoot();
411 public static String printSVGDocument(Element doce) {
413 StringBuilder result = new StringBuilder();
415 NodeList nl = doce.getChildNodes();
417 for(int i=0;i<nl.getLength();i++) {
419 ByteArrayOutputStream os = new ByteArrayOutputStream();
423 TransformerFactory tf = TransformerFactory.newInstance();
424 Transformer transformer = tf.newTransformer();
425 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
426 transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
427 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
428 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
429 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
430 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
432 transformer.transform(new DOMSource(nl.item(i)),
433 new StreamResult(new OutputStreamWriter(os, "UTF-8")));
438 } catch (Throwable t) {
439 LOGGER.error("Problems formatting SVGDocument to text.", t);
442 result.append(new String(os.toByteArray()));
446 return result.toString();
450 public static String renderSVG3(ICanvasContext ctx) {
451 return renderSVG0(ctx, p0 -> p0.stream().collect(Collectors.toMap(p1 -> p1, p2 -> p2)));
457 * @throws DatabaseException
459 private static Object[] createURIBasedL0Identifier(ReadGraph graph, Resource component) throws DatabaseException {
460 String uri = graph.getPossibleURI(component);
461 int hashCode = uri.hashCode();
462 Random random = new Random(hashCode);
463 long l1 = random.nextLong();
464 long l2 = random.nextLong();
465 return new Object[] { l1, l2 };
469 * Default no-op mapper
471 private static final Function1<Set<?>, Map<?, ?>> mapper = p0 -> p0.stream().collect(Collectors.toMap(p1 -> p1, p2 -> p2));
473 public static String renderSVG(ICanvasContext ctx) {
474 return renderSVG0(ctx, mapper);
478 * Renders ICanvasContext into SVG by mapping the SVG id's into URI based
484 public static String renderSVGMapIdentifiers(ICanvasContext ctx) {
485 return renderSVG0(ctx, new Function1<Set<?>, Map<?, ?>>() {
488 public Map<?, ?> apply(Set<?> p0) {
490 return Simantics.getSession().syncRequest(new UnaryRead<Set<?>, Map<?, ?>>(p0) {
493 public Map<?, ?> perform(ReadGraph graph) throws DatabaseException {
494 ModelingResources MOD = ModelingResources.getInstance(graph);
495 DiagramResource DIA = DiagramResource.getInstance(graph);
496 Layer0 L0 = Layer0.getInstance(graph);
497 return parameter.stream().collect(Collectors.toMap(p -> p, p -> {
499 if (p instanceof Resource) {
500 Resource element = (Resource) p;
501 if (graph.isInstanceOf(element, DIA.Connection) || graph.isInstanceOf(element, DIA.Terminal)) {
502 // Ok, lets create a hashcode for connections and terminals as they do not have identifiers
503 return graph.getURI(element).hashCode();
504 } else if (graph.isInstanceOf(element, DIA.Element)) {
505 Resource component = graph.getPossibleObject(element, MOD.ElementToComponent);
506 if (component != null) {
507 Object relatedValue = graph.getPossibleRelatedValue(component, L0.identifier);
508 if (relatedValue != null) {
509 if (relatedValue instanceof Object[]) {
510 // OK, found guid, lets generate new based on URI
511 return Arrays.toString(createURIBasedL0Identifier(graph, component));
513 System.err.println("Component " + component + " identifeir is not normal: " + relatedValue);
516 System.err.println("Compoennt " + component + " dooes not have identifier");
519 System.err.println("Element " + element + " does not have component");
523 System.err.println("p is not resource but is " + p);
526 } catch (DatabaseException e) {
527 throw new RuntimeDatabaseException(e);
532 } catch (DatabaseException e) {
533 LOGGER.error("Could not apply mappings", e);
534 throw new RuntimeDatabaseException(e);
540 private static String renderSVG0(ICanvasContext ctx, Function1<Set<?>, Map<?, ?>> mappingFunction) {
542 // Get a DOMImplementation.
543 DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
545 // Create an instance of org.w3c.dom.Document.
546 String svgNS = "http://www.w3.org/2000/svg";
547 Document document = domImpl.createDocument(svgNS, "svg", null);
549 // Create an instance of the SVG Generator.
550 SVGGraphics2D svgGenerator = new Generator(document);
552 StringBuilder result = new StringBuilder();
556 Selection selection = ctx.getAtMostOneItemOfClass(Selection.class);
557 if (selection != null) {
558 // This prevents workbench selection from being left over.
559 // Also prevents scene graph crap from being left on the screen.
560 IDiagram d = ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
561 selection.setSelection(0, d.getElements());
564 G2DSceneGraph sg = ctx.getSceneGraph();
565 G2DParentNode root = (G2DParentNode) sg.getRootNode();
567 // rtree is the actual content of the diagram
568 RTreeNode rtree = NodeUtil.getNearestChildByClass(root, RTreeNode.class);
569 Rectangle2D rtreeBounds = NodeUtil.getLocalBounds(rtree);
571 // nav is a node that has zooming functionalities
572 NavigationNode nav = NodeUtil.getNearestChildByClass(root, NavigationNode.class);
573 nav.setZoomEnabled(true);
575 // fit view with the contents of rtreeBounds
576 nav.zoomTo(rtreeBounds);
578 // get the bounds of the content
579 Rectangle2D content = NodeUtil.getLocalBounds(nav);
581 svgGenerator.scale(3,3);
583 // translate svgGenerator to the x and y coordinates of current content
584 svgGenerator.translate(-1 * content.getX(), (-1 * content.getY()));
586 Rectangle2D destination = new Rectangle2D.Double(0,0,1000,1000);
587 double sx = destination.getWidth() / content.getWidth();
588 double sy = destination.getHeight() / content.getHeight();
589 double scale = sx < sy ? sx : sy;
591 // Set svgCanvasSize to the given size parameters
592 svgGenerator.setSVGCanvasSize(new Dimension((int)(scale * content.getWidth()), (int)(scale * content.getHeight())));
593 svgGenerator.setClip(content);
595 double trX = -1 * content.getX();
596 double trY = -1 * content.getY();
598 result.append("<svg width=\"100%\" height=\"100%\" stroke=\"black\"><g transform=\"translate(").append(trX).append(' ').append(trY).append(")\">");
600 KeyVisitor keyVisitor = new KeyVisitor();
601 sg.accept(keyVisitor);
603 Set<Object> keys = keyVisitor.getKeys();
605 Map<?, ?> mappings = mappingFunction.apply(keys);
607 IG2DNodeVisitor visitor = new PrintingVisitor(result, mappings);
610 } catch (Throwable t) {
611 LOGGER.error("Problems rendering canvas context to SVG", t);
614 result.append("</g></svg>");
615 //System.err.println(" == FINAL RESULT == ");
616 //System.err.println(b);
617 return result.toString();
622 private static class KeyVisitor implements IG2DNodeVisitor {
624 private Set<Object> keys = new HashSet<>();
627 public void enter(IG2DNode node) {
628 if (node instanceof SingleElementNode) {
629 Object key = ((SingleElementNode) node).getKey();
637 public void leave(IG2DNode node) {
641 public Set<Object> getKeys() {
646 private static class PrintingVisitor implements IG2DNodeVisitor {
650 HashMap<SingleElementNode,StringBuilder> senBuilders = new HashMap<>();
652 private StringBuilder result;
654 private Map<?, ?> mappings;
656 public PrintingVisitor(StringBuilder result, Map<?, ?> mappings) {
657 this.result = result;
658 this.mappings = mappings;
661 private String getKey(SingleElementNode node) {
663 if (mappings.containsKey(node.getKey()))
664 key = mappings.get(node.getKey()).toString();
666 key = node.getKey().toString();
671 public void enter(IG2DNode node) {
673 StringBuilder parentBuilder = getParentBuilder(node);
676 if(node instanceof ConnectionNode) {
678 for(RouteGraphNode n : NodeUtil.collectNodes(node, RouteGraphNode.class)) {
679 n.setIgnoreSelection(true);
682 String key = getKey((ConnectionNode) node);
683 parentBuilder.append("\n<g class=\"connection\" id=\"" + key + "\">");
684 Element doc = renderSVGNode((IG2DNode)node);
685 String svg = printSVGDocument(doc);
686 parentBuilder.append(svg);
688 for(RouteGraphNode n : NodeUtil.collectNodes(node, RouteGraphNode.class)) {
689 n.setIgnoreSelection(false);
692 parentBuilder.append("\n<g style=\"visibility:hidden\" class=\"selection\" id=\"" + key + "\">");
693 doc = renderSVGNode((IG2DNode)node);
694 svg = printSVGDocument(doc);
695 parentBuilder.append(svg);
696 parentBuilder.append("\n</g>");
698 BasicStroke bs = new BasicStroke(5f);
700 for(RouteGraphNode n : NodeUtil.collectNodes(node, RouteGraphNode.class)) {
701 n.setDynamicStroke(bs);
704 parentBuilder.append("\n<g class=\"selectionMask\" opacity=\"0.001\" id=\"" + key + "\">");
705 doc = renderSVGNode((IG2DNode)node);
706 svg = printSVGDocument(doc);
707 parentBuilder.append(svg);
708 parentBuilder.append("\n</g>");
710 parentBuilder.append("\n</g>");
712 } else if (node instanceof SelectionNode) {
714 SelectionNode n = (SelectionNode)node;
715 SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class);
716 if(parentSEN != null && parentSEN.getKey() != null) {
718 StringBuilder parentBuilder2 = getParentBuilder(parentSEN);
720 String key = getKey(parentSEN);
721 Element doc = renderSVGNode((IG2DNode)node);
722 String svg = printSVGDocument(doc);
723 parentBuilder2.append("\n<g style=\"visibility:hidden\" class=\"selection\" id=\"" + key + "\">");
724 parentBuilder2.append(svg);
725 parentBuilder2.append("\n</g>");
726 parentBuilder2.append("\n<g class=\"selectionMask\" id=\"" + key + "\">");
727 Rectangle2D rect = n.getRect();
728 parentBuilder2.append("<rect style=\"fill:#fff\" opacity=\"0.001\"");
729 parentBuilder2.append(" x=\"" + rect.getX() + "\" y=\"" + rect.getY() + "\"");
730 parentBuilder2.append(" width=\"" + rect.getWidth() + "\" height=\"" + rect.getHeight() + "\"");
731 parentBuilder2.append("></rect>");
732 parentBuilder2.append("\n</g>");
734 } else if (node instanceof SVGNode) {
735 SVGNode svg = (SVGNode)node;
736 parentBuilder.append(svg.getSVGText());
737 } else if (node instanceof G2DParentNode) {
738 AffineTransform at = node.getTransform();
739 if(node instanceof SingleElementNode) {
740 SingleElementNode sen = (SingleElementNode)node;
741 if(sen.getKey() != null) {
742 String key = getKey(sen);
743 parentBuilder.append("\n<g class=\"definedElement\" id=\"" + key + "\">");
745 senBuilders.put(sen, new StringBuilder());
747 if(!at.isIdentity()) {
748 if(at.getScaleX() == 1.0 && at.getScaleY() == 1.0 && at.getShearX() == 0.0 && at.getShearY() == 0.0) {
749 String m = "translate(" + at.getTranslateX() + " " + at.getTranslateY() + ")";
750 parentBuilder.append("\n<g transform=\"" + m + "\">");
752 double[] ds = new double[6];
754 String m = "matrix(" + ds[0] + " " + ds[1] + " " + ds[2] + " " + ds[3] + " " + ds[4] + " " + ds[5] + ")";
755 parentBuilder.append("\n<g transform=\"" + m + "\">");
760 //enters.put(node, b.length());
764 private StringBuilder getParentBuilder(IG2DNode node) {
766 INode parentSEN = NodeUtil.getNearestParentOfType(node, SingleElementNode.class);
767 if(parentSEN instanceof G2DSceneGraph) return result;
769 StringBuilder parentBuilder = senBuilders.get(parentSEN);
770 if(parentBuilder == null) return result;
772 return parentBuilder;
777 public void leave(IG2DNode node) {
779 if(node instanceof ConnectionNode || node instanceof SVGNode) {
781 } else if (node instanceof G2DParentNode) {
783 StringBuilder parentBuilder = getParentBuilder(node);
785 if(node instanceof SingleElementNode) {
786 SingleElementNode sen = (SingleElementNode)node;
787 // if(sen.getKey() != null) {
788 StringBuilder b = senBuilders.get(sen);
789 String content = b.toString();
790 if(content.isEmpty()) {
791 if(sen.getKey() != null) {
793 for(SelectionNode n : NodeUtil.collectNodes(node, SelectionNode.class)) {
797 Element doc = renderSVGNode((IG2DNode)node);
798 String svg = printSVGDocument(doc);
799 parentBuilder.append(svg);
802 parentBuilder.append(content);
808 AffineTransform at = node.getTransform();
809 if(!at.isIdentity()) {
810 parentBuilder.append("</g>");
812 if(node instanceof SingleElementNode) {
813 SingleElementNode sen = (SingleElementNode)node;
814 if(sen.getKey() != null) {
815 parentBuilder.append("</g>");