From 64e7d67f61e5cd5916760727eecab48c314d657c Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Tue, 7 Nov 2017 20:51:17 +0200 Subject: [PATCH] Deprecating old API:s and inventing new ones for ICanvasContext in SCL refs #7608 Change-Id: I89aa437ca8c98df7fd2644afffc037030fff2fd7 --- .../diagram/elements/DiagramNodeUtil.java | 4 +- .../scl/Simantics/Scenegraph.scl | 8 +++- .../org/simantics/modeling/SCLScenegraph.java | 40 ++++++++++++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/DiagramNodeUtil.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/DiagramNodeUtil.java index f2cccf774..467e4e349 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/DiagramNodeUtil.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/DiagramNodeUtil.java @@ -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; diff --git a/bundles/org.simantics.modeling/scl/Simantics/Scenegraph.scl b/bundles/org.simantics.modeling/scl/Simantics/Scenegraph.scl index 978eff21a..5bf540e35 100644 --- a/bundles/org.simantics.modeling/scl/Simantics/Scenegraph.scl +++ b/bundles/org.simantics.modeling/scl/Simantics/Scenegraph.scl @@ -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 -> ICanvasSceneGraphProvider + @Deprecated disposeSceneGraphProvider :: ICanvasSceneGraphProvider -> () + doWithICanvasSceneGraphProvider :: Resource -> (ICanvasSceneGraphProvider -> a) -> a + + doWithCanvasContext :: Resource -> (ICanvasContext -> a) -> a + editNodeText :: ICanvasContext -> String -> String -> String -> String copyPaste :: ICanvasContext -> ICanvasContext -> [Resource] -> Boolean diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java index b3a0ff251..c7f3fe4ec 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java @@ -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 doWithICanvasSceneGraphProvider(Resource diagram, Function1 func) throws DatabaseException { + return doWithICanvasSceneGraphProvider(diagram, (Function) provider -> func.apply(provider)); + } + + public static T doWithICanvasSceneGraphProvider(Resource diagram, Function func) throws DatabaseException { + Tuple2 result = Simantics.getSession().syncRequest(new ResourceRead(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 doWithCanvasContext(Resource diagram, Function1 func) throws DatabaseException { + return doWithCanvasContext(diagram, (Function) canvasContext -> func.apply(canvasContext)); + } + + public static T doWithCanvasContext(Resource diagram, Function func) throws DatabaseException { + return doWithICanvasSceneGraphProvider(diagram, (Function) provider -> func.apply(provider.getCanvasContext())); + } + public static String getNodeTransform(ICanvasContext ctx, String name) { Set texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class); -- 2.47.1