Deprecating old API:s and inventing new ones for ICanvasContext in SCL 08/1208/3
authorjsimomaa <jani.simomaa@gmail.com>
Tue, 7 Nov 2017 18:51:17 +0000 (20:51 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Tue, 7 Nov 2017 18:57:20 +0000 (20:57 +0200)
refs #7608

Change-Id: I89aa437ca8c98df7fd2644afffc037030fff2fd7

bundles/org.simantics.diagram/src/org/simantics/diagram/elements/DiagramNodeUtil.java
bundles/org.simantics.modeling/scl/Simantics/Scenegraph.scl
bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java

index f2cccf7749527df9c5d7a851a17040fac3f19ef5..467e4e349d5f494bf428ae2f1c1e3601c714e83f 100644 (file)
@@ -117,7 +117,7 @@ public class DiagramNodeUtil {
         };
     }
 
-    public static ICanvasSceneGraphProvider loadSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException, InterruptedException {
+    public static ICanvasSceneGraphProvider loadSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException {
         Resource composite = Simantics.getSession().syncRequest( diagramToComposite(diagram) );
         return loadSceneGraphProviderForComposite(model, composite, diagramRVI);
     }
@@ -152,7 +152,7 @@ public class DiagramNodeUtil {
         return cp.getCanvasContext();
     }
 
-    public static ICanvasSceneGraphProvider loadSceneGraphProviderForComposite(Resource model, Resource composite, String diagramRVI) throws DatabaseException, InterruptedException {
+    public static ICanvasSceneGraphProvider loadSceneGraphProviderForComposite(Resource model, Resource composite, String diagramRVI) throws DatabaseException {
         String modelURI = Simantics.getSession().syncRequest(Queries.uri(model));
         ISceneGraphProvider provider = Simantics.getSession().syncRequest( Queries.adapt( composite, ISceneGraphProvider.class ) );
         ICanvasSceneGraphProvider cp = (ICanvasSceneGraphProvider) provider;
index 978eff21a8f7c0f68a57e38769129c7509b337b5..5bf540e3563fdfd9649086af82a55e79393e59d2 100644 (file)
@@ -44,10 +44,16 @@ importJava "org.simantics.modeling.SCLScenegraph" where
     @JavaName getNodeTransform
     getTransform :: ICanvasContext -> String -> String
     
-    getICanvasSceneGraphProvider :: Model -> Resource -> String -> ICanvasSceneGraphProvider
+    @Deprecated
+    getICanvasSceneGraphProvider :: Model -> Resource -> String -> <Proc> ICanvasSceneGraphProvider
     
+    @Deprecated
     disposeSceneGraphProvider :: ICanvasSceneGraphProvider -> <Proc> ()
     
+    doWithICanvasSceneGraphProvider :: Resource -> (ICanvasSceneGraphProvider -> <e> a) -> <Proc, Exception> a
+    
+    doWithCanvasContext :: Resource -> (ICanvasContext -> <e> a) -> <Proc, Exception> a
+    
     editNodeText :: ICanvasContext -> String -> String -> String -> <Proc> String
     
     copyPaste :: ICanvasContext -> ICanvasContext -> [Resource] -> <Proc> Boolean
index b3a0ff251502a41cc772352710553cfcc64957eb..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,12 @@ 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;
@@ -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);