]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java
Deprecating old API:s and inventing new ones for ICanvasContext in SCL
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / SCLScenegraph.java
index 5f375095fb239db428f53a07040bc58e7b10b21d..c7f3fe4ec97431874529627aee3a09404748b4bc 100644 (file)
@@ -18,6 +18,7 @@ import java.util.Map;
 import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 import javax.xml.transform.OutputKeys;
@@ -33,9 +34,13 @@ import org.simantics.Simantics;
 import org.simantics.datatypes.literal.GUID;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.diagram.elements.DecorationSVGNode;
 import org.simantics.diagram.elements.DiagramNodeUtil;
 import org.simantics.diagram.elements.TextGridNode;
 import org.simantics.diagram.elements.TextNode;
@@ -61,7 +66,6 @@ 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.DecorationSVGNode;
 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
 import org.simantics.scenegraph.g2d.nodes.SVGNode;
 import org.simantics.scenegraph.g2d.nodes.SelectionNode;
@@ -70,6 +74,7 @@ import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
 import org.simantics.scenegraph.utils.NodeUtil;
 import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.tuple.Tuple2;
 import org.simantics.trend.impl.ItemNode;
 import org.simantics.utils.threads.ThreadUtils;
 import org.slf4j.Logger;
@@ -89,16 +94,47 @@ public class SCLScenegraph {
        
        private static final String[] ALL_SECTIONS = { MAIN_SECTION, SELECTION_SECTION, SELECTION_MASK_SECTION };
 
-
+       @Deprecated
        public static ICanvasSceneGraphProvider getICanvasSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException, InterruptedException {
                ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(model, diagram, diagramRVI);
                return provider;
        }
        
+       @Deprecated
        public static void disposeSceneGraphProvider(ICanvasSceneGraphProvider provider) {
                provider.dispose();
        }
-       
+
+    public static <T> T doWithICanvasSceneGraphProvider(Resource diagram, Function1<ICanvasSceneGraphProvider, T> func) throws DatabaseException {
+        return doWithICanvasSceneGraphProvider(diagram, (Function<ICanvasSceneGraphProvider, T>) provider -> func.apply(provider));
+    }
+
+    public static <T> T doWithICanvasSceneGraphProvider(Resource diagram, Function<ICanvasSceneGraphProvider, T> func) throws DatabaseException {
+        Tuple2 result = Simantics.getSession().syncRequest(new ResourceRead<Tuple2>(diagram) {
+
+            @Override
+            public Tuple2 perform(ReadGraph graph) throws DatabaseException {
+                Resource indexRoot = graph.syncRequest(new IndexRoot(resource));
+                String diagramRVI = Variables.getRVI(graph, resource);
+                return new Tuple2(indexRoot, diagramRVI);
+            }
+        });
+        ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider((Resource) result.c0, diagram, (String) result.c1);
+        try {
+            return func.apply(provider);
+        } finally {
+            provider.dispose();
+        }
+    }
+
+    public static <T> T doWithCanvasContext(Resource diagram, Function1<ICanvasContext, T> func) throws DatabaseException {
+        return doWithCanvasContext(diagram, (Function<ICanvasContext, T>) canvasContext -> func.apply(canvasContext));
+    }
+
+    public static <T> T doWithCanvasContext(Resource diagram, Function<ICanvasContext, T> func) throws DatabaseException {
+        return doWithICanvasSceneGraphProvider(diagram, (Function<ICanvasSceneGraphProvider, T>) provider -> func.apply(provider.getCanvasContext()));
+    }
+
        public static String getNodeTransform(ICanvasContext ctx, String name) {
                
                Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
@@ -599,6 +635,7 @@ public class SCLScenegraph {
             }
 
                        G2DSceneGraph sg = ctx.getSceneGraph();
+                       sg.performCleanup();
                        G2DParentNode root = (G2DParentNode) sg.getRootNode();
 
                        // rtree is the actual content of the diagram
@@ -632,15 +669,20 @@ public class SCLScenegraph {
                        double trX = -1 * content.getX();
                        double trY = -1 * content.getY();
                        
-                       result.append(MAIN_SECTION, "<g transform=\"translate(");
-                       result.append(MAIN_SECTION, "" + trX);
-                       result.append(MAIN_SECTION, " ");
-                       result.append(MAIN_SECTION, "" + trY);
-                       result.append(MAIN_SECTION, "\">");
-
-                       result.append(SELECTION_SECTION, "<g class=\"selections\">");
+                       // NaNs
+                       if(!Double.isFinite(trX)) trX = 0;
+                       if(!Double.isFinite(trY)) trY = 0;
                        
+                       result.append(MAIN_SECTION, "<g class=\"symbols\">");
+                       result.append(SELECTION_SECTION, "<g class=\"selections\">");
                        result.append(SELECTION_MASK_SECTION, "<g class=\"selectionMasks\">");
+
+                       result.append(ALL_SECTIONS, "<g transform=\"translate(");
+                       result.append(ALL_SECTIONS, "" + trX);
+                       result.append(ALL_SECTIONS, ", ");
+                       result.append(ALL_SECTIONS, "" + trY);
+                       result.append(ALL_SECTIONS, ")\">");
+
                        
                        KeyVisitor keyVisitor = new KeyVisitor();
                        sg.accept(keyVisitor);
@@ -658,16 +700,14 @@ public class SCLScenegraph {
                }
 
 
-               result.append(SELECTION_SECTION, "</g>");
-               result.append(SELECTION_MASK_SECTION, "</g>");
-               result.append(MAIN_SECTION, "</g>");
+               result.append(ALL_SECTIONS, "</g></g>");
 
                StringBuilder res = new StringBuilder();
                res.append("<svg width=\"100%\" height=\"100%\" stroke=\"black\">");
                res.append(result.get(MAIN_SECTION));
                res.append(result.get(SELECTION_SECTION));
                res.append(result.get(SELECTION_MASK_SECTION));
-               res.append(result.get("</svg>"));
+               res.append("</svg>");
 
 //             System.err.println(" == FINAL RESULT == ");
 //             System.err.println(res.toString());
@@ -778,18 +818,24 @@ public class SCLScenegraph {
                        RenderSVGContext parentBuilder2 = getParentBuilder(parentSEN);
                     
                     String key = getKey(parentSEN);
+                    n.setIgnore(false);
                     Element doc = renderSVGNode((IG2DNode)node);
+                    n.setIgnore(true);
                     String 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 + "\">");
                     Rectangle2D rect = n.getRect();
-                    parentBuilder2.append(SELECTION_MASK_SECTION,"<rect style=\"fill:#fff\" opacity=\"0.001\"");
-                    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>");
+                    // NaN
+                    if(rect.getHeight() == rect.getHeight() && rect.getWidth() == rect.getWidth()) {
+                           parentBuilder2.append(SELECTION_MASK_SECTION,"<rect style=\"fill:#fff\" opacity=\"0.001\"");
+                           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 SVGNode) {
                 SVGNode svg = (SVGNode)node;