From: jsimomaa Date: Wed, 1 Feb 2017 09:59:07 +0000 (+0200) Subject: Add createSVGElementR which returns the created resource X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=ac7f1d61e38c3645585b994cf67eac4cbbbc9582;p=simantics%2Fplatform.git Add createSVGElementR which returns the created resource * add created SVG to diagram layers during import refs #6958 Change-Id: Ia7df8d1ee6c0cb6bb7a83d9a8708e3779d974b3b --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/ImportSVGPNG.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/ImportSVGPNG.java index 931bfdf3d..e3cbb5a7c 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/ImportSVGPNG.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/ImportSVGPNG.java @@ -36,7 +36,12 @@ import org.simantics.db.common.request.IndexRoot; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; +import org.simantics.diagram.synchronization.IModifiableSynchronizationContext; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.participant.MouseUtil; import org.simantics.g2d.participant.MouseUtil.MouseInfo; import org.simantics.modeling.ModelingResources; @@ -112,10 +117,12 @@ public class ImportSVGPNG { IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput(); Resource composite = input.getResource(); - addSVG(mpos.getX(), mpos.getY(), composite); + IDiagram idiagram = viewer.getAdapter(IDiagram.class); + + addSVG(mpos.getX(), mpos.getY(), composite, idiagram); } - public static void addSVG(final double mposX, final double mposY, final Resource composite) { + public static void addSVG(final double mposX, final double mposY, final Resource composite, IDiagram idiagram) { Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); @@ -135,9 +142,20 @@ public class ImportSVGPNG { @Override public void perform(WriteGraph g) throws DatabaseException { - Commands.get(g, "Simantics/Diagram/createSVGElement") + Object svg = Commands.get(g, "Simantics/Diagram/createSVGElementR") .execute(g, g.syncRequest(new IndexRoot(composite)), composite, suffix(filename), data, mposX, mposY); + + if (svg != null && svg instanceof Resource) { + Resource resource = (Resource) svg; + // 7. Put the element on all the currently active layers if possible. + IModifiableSynchronizationContext context = idiagram.getHint(SynchronizationHints.CONTEXT); + GraphLayerManager glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); + if (glm != null) { + glm.removeFromAllLayers(g, resource); + glm.putElementOnVisibleLayers(idiagram, g, resource); + } + } } }); diff --git a/bundles/org.simantics.modeling/scl/Simantics/Diagram.scl b/bundles/org.simantics.modeling/scl/Simantics/Diagram.scl index 6e04a6f6d..3536b3e28 100644 --- a/bundles/org.simantics.modeling/scl/Simantics/Diagram.scl +++ b/bundles/org.simantics.modeling/scl/Simantics/Diagram.scl @@ -794,8 +794,10 @@ setTransform element transform = claimRelatedValueWithType element DIA.HasTransf importJava "org.simantics.modeling.svg.CreateSVGElement" where createSVGElement :: Resource -> String -> ByteArray -> Double -> Double -> () + createSVGElementR :: Resource -> String -> ByteArray -> Double -> Double -> Resource importSVGElement :: Resource -> File -> Double -> Double -> () + importSVGElementR :: Resource -> File -> Double -> Double -> Resource importJava "org.simantics.diagram.synchronization.graph.RemoveElement" where removeElement :: Resource -> Resource -> () diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/svg/CreateSVGElement.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/svg/CreateSVGElement.java index 994d417c9..c0d208148 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/svg/CreateSVGElement.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/svg/CreateSVGElement.java @@ -19,6 +19,10 @@ import org.simantics.utils.FileUtils; public class CreateSVGElement { public static void createSVGElement(WriteGraph g, Resource diagram, String suffix, byte[] data, double mposX, double mposY) throws DatabaseException { + createSVGElement(g, diagram, suffix, data, mposX, mposY); + } + + public static Resource createSVGElementR(WriteGraph g, Resource diagram, String suffix, byte[] data, double mposX, double mposY) throws DatabaseException { Layer0 L0 = Layer0.getInstance(g); DiagramResource DIA = DiagramResource.getInstance(g); @@ -50,13 +54,17 @@ public class CreateSVGElement { throw new DatabaseException("Unknown image format " + suffix); OrderedSetUtils.addFirst(g, diagram, element); g.claim(diagram, L0.ConsistsOf, element); - + return element; } public static void importSVGElement(WriteGraph graph, Resource diagram, File file, double posX, double posY) throws DatabaseException, IOException { + importSVGElementR(graph, diagram, file, posX, posY); + } + + public static Resource importSVGElementR(WriteGraph graph, Resource diagram, File file, double posX, double posY) throws DatabaseException, IOException { final byte[] data = FileUtils.readFile(file); - createSVGElement(graph, diagram, suffix(file.getName()), data, posX, posY); + return createSVGElementR(graph, diagram, suffix(file.getName()), data, posX, posY); } private static String suffix(String fileName) {