]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Add createSVGElementR which returns the created resource 13/313/1
authorjsimomaa <jani.simomaa@gmail.com>
Wed, 1 Feb 2017 09:59:07 +0000 (11:59 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Wed, 1 Feb 2017 10:03:27 +0000 (12:03 +0200)
* add created SVG to diagram layers during import

refs #6958

Change-Id: Ia7df8d1ee6c0cb6bb7a83d9a8708e3779d974b3b

bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/ImportSVGPNG.java
bundles/org.simantics.modeling/scl/Simantics/Diagram.scl
bundles/org.simantics.modeling/src/org/simantics/modeling/svg/CreateSVGElement.java

index 931bfdf3d60d073c0b63c271dbda2c163f060c41..e3cbb5a7cba7e4b9bb8693b38b758986617e5755 100644 (file)
@@ -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);
+                        }
+                    }
                 }
 
             });
index 6e04a6f6d6f4c6e21cbc9e94d3fae840786838ae..3536b3e28a79c1c6114679b13afb009a3c21dc1f 100644 (file)
@@ -794,8 +794,10 @@ setTransform element transform = claimRelatedValueWithType element DIA.HasTransf
     
 importJava "org.simantics.modeling.svg.CreateSVGElement" where
     createSVGElement :: Resource -> String -> ByteArray -> Double -> Double -> <WriteGraph> ()
+    createSVGElementR :: Resource -> String -> ByteArray -> Double -> Double -> <WriteGraph> Resource
     
     importSVGElement :: Resource -> File -> Double -> Double -> <WriteGraph> ()
+    importSVGElementR :: Resource -> File -> Double -> Double -> <WriteGraph> Resource
     
 importJava "org.simantics.diagram.synchronization.graph.RemoveElement" where
     removeElement :: Resource -> Resource -> <WriteGraph> ()
index 994d417c99b815ba4c3deae54511777a84e8fc05..c0d2081480c51cd7941a1d1d15f0c6e43132fe80 100644 (file)
@@ -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) {