]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
SCLScenegraph: Use diagram resource as a prefix for generated IDs. 10/4610/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 10 Nov 2020 15:58:24 +0000 (17:58 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 10 Nov 2020 15:58:24 +0000 (17:58 +0200)
gitlab #641

Change-Id: I07c0aa70f3023a34fdbacb6a1ca6e70bd37d23c0

bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java

index 559469d4a21ce908aff65f9574355363a989d2f3..c8fd242a2f7a1d61adf99ae4629de350e1159c48 100644 (file)
@@ -31,6 +31,7 @@ import javax.xml.transform.stream.StreamResult;
 import org.apache.batik.dom.GenericDOMImplementation;
 import org.apache.batik.svggen.SVGGeneratorContext;
 import org.apache.batik.svggen.SVGGraphics2D;
+import org.apache.batik.svggen.SVGIDGenerator;
 import org.simantics.Simantics;
 import org.simantics.datatypes.literal.GUID;
 import org.simantics.db.ReadGraph;
@@ -46,6 +47,7 @@ import org.simantics.diagram.elements.DiagramNodeUtil;
 import org.simantics.diagram.elements.TextGridNode;
 import org.simantics.diagram.elements.TextNode;
 import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.ui.DiagramModelHints;
 import org.simantics.g2d.canvas.Hints;
 import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.diagram.DiagramHints;
@@ -399,6 +401,22 @@ public class SCLScenegraph {
                return true;
     }
 
+    
+    static class UniqueIDGenerator extends SVGIDGenerator{
+       
+       String overallId;
+       public UniqueIDGenerator(String overallId) {
+               super();
+               this.overallId = overallId;
+       }
+       
+       @Override
+       public String generateID(String prefix) {
+               return super.generateID(overallId+prefix);
+       }
+
+    }
+
        static class Generator extends SVGGraphics2D {
 
                int elemLevel = 0;
@@ -412,9 +430,14 @@ public class SCLScenegraph {
                }
 
                public Generator(Document document) {
+                       this(document,null);
+               }
+               public Generator(Document document, String id) {
                        super(document);
                        // prevent batik comment in each g-element
                        getGeneratorContext().setComment(null);
+                       if (id != null)
+                               getGeneratorContext().setIDGenerator(new UniqueIDGenerator(id));
                }
 
                @Override
@@ -640,6 +663,8 @@ public class SCLScenegraph {
        
     }
     
+    
+    
        private static String renderSVG0(double width, double height, ICanvasContext ctx, Function1<Set<?>, Map<?, ?>> mappingFunction) {
 
                // Get a DOMImplementation.
@@ -650,7 +675,13 @@ public class SCLScenegraph {
                Document document = domImpl.createDocument(svgNS, "svg", null);
 
                // Create an instance of the SVG Generator.
-               SVGGraphics2D svgGenerator = new Generator(document);
+               IDiagram d = ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
+               Resource r = d.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
+               SVGGraphics2D svgGenerator;
+               if (r != null)
+                       svgGenerator = new Generator(document,r.toString());
+               else
+                       svgGenerator = new Generator(document);
 
                RenderSVGContext result = new RenderSVGContext();
 
@@ -661,8 +692,7 @@ public class SCLScenegraph {
             if (selection != null) {
                 // This prevents workbench selection from being left over.
                 // Also prevents scene graph crap from being left on the screen.
-               IDiagram d = ctx.getDefaultHintContext().getHint(DiagramHints.KEY_DIAGRAM);
-                selection.setSelection(0, d.getElements());
+               selection.setSelection(0, d.getElements());
             }
 
                        G2DSceneGraph sg = ctx.getSceneGraph();