]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java
Handle SelectionOutline interface implementation in SCLScenegraph
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLScenegraph.java
index d3fd1cca9b9314cf4933c78d331fe22f349d85be..82115ef0e0825e2c9b8b2c69793f6bffd015344b 100644 (file)
@@ -51,6 +51,7 @@ import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.diagram.DiagramHints;
 import org.simantics.g2d.diagram.IDiagram;
 import org.simantics.g2d.diagram.handler.DataElementMap;
+import org.simantics.g2d.diagram.participant.ElementPainter.SelectionShapeNode;
 import org.simantics.g2d.diagram.participant.Selection;
 import org.simantics.g2d.element.IElement;
 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
@@ -68,6 +69,7 @@ import org.simantics.scenegraph.g2d.nodes.BackgroundNode;
 import org.simantics.scenegraph.g2d.nodes.BoundsNode;
 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
 import org.simantics.scenegraph.g2d.nodes.DataNode;
+import org.simantics.scenegraph.g2d.nodes.LinkNode;
 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
 import org.simantics.scenegraph.g2d.nodes.SVGNode;
 import org.simantics.scenegraph.g2d.nodes.SelectionNode;
@@ -84,6 +86,7 @@ import org.slf4j.LoggerFactory;
 import org.w3c.dom.DOMImplementation;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 public class SCLScenegraph {
@@ -673,22 +676,28 @@ public class SCLScenegraph {
                        // get the bounds of the content
                        Rectangle2D content = rtreeBounds;
                        
-                       // To account for dynamic padding of selection rectangles (5 units + stroke width)
-                       int offset = 6;
                        
-                       double scale = width < 0 || height < 0 ? 1.0 : Math.min((width - 2*offset) / content.getWidth(), (height - 2*offset) / content.getHeight());
-               
-                       AffineTransform tr = new AffineTransform();
-                       tr.translate(offset, offset);
-                       tr.scale(scale, scale);
-                       tr.translate(-content.getX(), -content.getY());
-                       tr.getMatrix(matrix);
+                       if (content != null) {
+                        // To account for dynamic padding of selection rectangles (5 units + stroke width)
+                   int offset = 6;
+                   
+                   double scale = width < 0 || height < 0 ? 1.0 : Math.min((width - 2*offset) / content.getWidth(), (height - 2*offset) / content.getHeight());
+               
+                   AffineTransform tr = new AffineTransform();
+                   tr.translate(offset, offset);
+                   tr.scale(scale, scale);
+                           tr.translate(-content.getX(), -content.getY());
+                           tr.getMatrix(matrix);
+                           svgGenerator.setSVGCanvasSize(new Dimension((int)Math.ceil(scale * content.getWidth()) + 2*offset, (int)Math.ceil(scale * content.getHeight()) + 2*offset));
+                       } else {
+                           svgGenerator.setSVGCanvasSize(new Dimension(100, 100));
+                       }
                        //svgGenerator.translate(offset, offset);
                        //svgGenerator.scale(scale, scale);
                        // translate svgGenerator to the x and y coordinates of current content
                        //svgGenerator.translate(-content.getX(), -content.getY());
 
-                       svgGenerator.setSVGCanvasSize(new Dimension((int)Math.ceil(scale * content.getWidth()) + 2*offset, (int)Math.ceil(scale * content.getHeight()) + 2*offset));
+                       
                        //svgGenerator.setClip(content);
                        
                        result.append(MAIN_SECTION, "<g class=\"symbols\">");
@@ -906,6 +915,84 @@ public class SCLScenegraph {
                         parentBuilder.append(ALL_SECTIONS, "\n<g transform=\"" + m + "\">");
                     }
                 }
+            } else if (node instanceof TextNode) {
+                TextNode text = (TextNode)node;
+                               
+                 SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class);
+                 if(parentSEN != null) {
+                        
+                        text.setShowSelection(false);
+                        Element doc = renderSVGNode(svgGenerator, (IG2DNode)node);
+                     String svg = printSVGDocument(doc);
+                     parentBuilder.append(MAIN_SECTION, svg);
+                     
+                        RenderSVGContext parentBuilder2 = getParentBuilder(parentSEN);
+                     
+                     String key = getKey(parentSEN);
+                     text.setShowSelection(true);
+                     doc = renderSVGNode(svgGenerator, (IG2DNode)node);
+                     svg = printSVGDocument(doc);
+                     
+                     parentBuilder2.append(SELECTION_SECTION, "\n<g style=\"visibility:hidden\" class=\"selection\" id=\"" + key + "\">");
+                     parentBuilder2.append(SELECTION_SECTION, svg);
+                     parentBuilder2.append(SELECTION_SECTION, "\n</g>");
+                     parentBuilder2.append(SELECTION_MASK_SECTION, "\n<g class=\"selectionMask\" id=\"" + key /*+ "\" transform=\"" + matrixString + "\"*/+ "\">");
+                     Rectangle2D rect = text.getBounds();
+                     // NaN
+                     if(rect.getHeight() == rect.getHeight() && rect.getWidth() == rect.getWidth()) {
+                           parentBuilder2.append(SELECTION_MASK_SECTION,"<rect style=\"fill:#fff\" opacity=\"" + OPACITY + "\"");
+                           parentBuilder2.append(SELECTION_MASK_SECTION," x=\"" + rect.getX() + "\" y=\"" + rect.getY() + "\"");
+                           parentBuilder2.append(SELECTION_MASK_SECTION," width=\"" + rect.getWidth() + "\" height=\"" + rect.getHeight() + "\"");
+                           parentBuilder2.append(SELECTION_MASK_SECTION,"></rect>");
+                     }
+                     parentBuilder2.append(SELECTION_MASK_SECTION,"\n</g>");
+                    
+                 }
+            } else if (!(node instanceof RouteGraphNode) && !(node instanceof LinkNode)){
+               try {
+                       Element doc = renderSVGNode(svgGenerator, (IG2DNode)node);
+                       NodeList gList = doc.getElementsByTagName("g");
+                       if (gList.getLength() == 0)
+                               return;
+                       boolean hasContent = false;
+                       for (int i = 0; i < gList.getLength(); i++) {
+                           Node gNode = gList.item(i);
+                           if (gNode.hasChildNodes()) {
+                               hasContent = true;
+                               break;
+                           }
+                       }
+                       if (!hasContent)
+                           return;
+                       String svg = printSVGDocument(doc);
+                       if (node instanceof SelectionShapeNode) {
+                               SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class);
+                        if(parentSEN != null) {
+                                String key = getKey(parentSEN);
+                                RenderSVGContext parentBuilder2 = getParentBuilder(parentSEN);
+                                parentBuilder2.append(SELECTION_SECTION, "\n<g style=\"visibility:hidden\" class=\"selection\" id=\"" + key + "\">");
+                             parentBuilder2.append(SELECTION_SECTION, svg);
+                             parentBuilder2.append(SELECTION_SECTION, "\n</g>");
+                             
+                             parentBuilder2.append(SELECTION_MASK_SECTION, "\n<g class=\"selectionMask\" id=\"" + key /*+ "\" transform=\"" + matrixString + "\"*/+ "\">");
+                             Rectangle2D rect = node.getBounds();
+                             // NaN
+                             if(rect.getHeight() == rect.getHeight() && rect.getWidth() == rect.getWidth()) {
+                                   parentBuilder2.append(SELECTION_MASK_SECTION,"<rect style=\"fill:#fff\" opacity=\"" + OPACITY + "\"");
+                                   parentBuilder2.append(SELECTION_MASK_SECTION," x=\"" + rect.getX() + "\" y=\"" + rect.getY() + "\"");
+                                   parentBuilder2.append(SELECTION_MASK_SECTION," width=\"" + rect.getWidth() + "\" height=\"" + rect.getHeight() + "\"");
+                                   parentBuilder2.append(SELECTION_MASK_SECTION,"></rect>");
+                             }
+                             parentBuilder2.append(SELECTION_MASK_SECTION,"\n</g>");
+                        }
+                       } else {
+                               parentBuilder.append(MAIN_SECTION, "<g class=\"" +node.getSimpleClassName() +"\">");
+                               parentBuilder.append(MAIN_SECTION, svg);
+                               parentBuilder.append(MAIN_SECTION, "\n</g>");
+                       }
+               } catch (Exception e) {
+                       // TODO: There are nodes that do not behave well when rendered to SVG. For backwards compatibility, we don't handle the exceptions.
+               }
             }
 
             //enters.put(node, b.length());