]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Robustness fixes for inline SVG text editing on diagrams 11/1011/4
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 22 Sep 2017 06:31:55 +0000 (09:31 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 22 Sep 2017 12:32:45 +0000 (15:32 +0300)
This fixes the code to not assume during scene graph rendering that the
canvas context is hosted by an AWTChassis.

refs #7458

Change-Id: Ibb9955424784a1d3c2069d6a668559d95d88b181

bundles/org.simantics.diagram/src/org/simantics/diagram/elements/DiagramNodeUtil.java
bundles/org.simantics.diagram/src/org/simantics/diagram/elements/EditorStateManager.java
bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGNode.java

index 859154d0b6be2e3003e1637e7c643526d2c6fe81..893573d0f057841e769e73f7216b22e48b2ba923 100644 (file)
@@ -43,7 +43,7 @@ import org.simantics.simulation.ontology.SimulationResource;
 public class DiagramNodeUtil {
 
     public static ICanvasContext getCanvasContext(IG2DNode node) {
-        G2DSceneGraph sg = NodeUtil.getPossibleRootNode(node);
+        G2DSceneGraph sg = node.getRootNode2D();
         if (sg == null)
             return null;
         Component rootPane = sg.getRootPane();
@@ -55,7 +55,7 @@ public class DiagramNodeUtil {
     }
 
     public static ICanvasContext getPossibleCanvasContext(IG2DNode node) {
-        G2DSceneGraph sg = NodeUtil.getPossibleRootNode(node);
+        G2DSceneGraph sg = node.getRootNode2D();
         if (sg == null)
             return null;
         Component rootPane = sg.getRootPane();
@@ -236,7 +236,7 @@ public class DiagramNodeUtil {
     }
     
     public static final Set<String> getTexts(Set<TextNode> nodes) {
-        Set<String> result = new HashSet<String>();
+        Set<String> result = new HashSet<String>(nodes.size());
         for(TextNode n : nodes) result.add(n.getText());
         return result;
     }
index 8ddb16ac76d53eec771f7c165a64f3b6a00fc7e9..5c59177cccf4f1c3a8088ff08690adf79c5c7ac7 100644 (file)
@@ -67,6 +67,9 @@ class EditorStateManager {
                String currentText = span.getText();
 
                SingleElementNode sne = node.getSingleElementNode();
+               if (sne == null)
+                       return;
+
                Function1<String,String> fullTextFunction = sne.getParameter("textEditorFullText"); 
                if(fullTextFunction != null)
                        es.currentText = fullTextFunction.apply(es.base.textElementId);
@@ -114,10 +117,12 @@ class EditorStateManager {
 
        void applyEdit() {
                SingleElementNode sne = node.getSingleElementNode();
-               EditorState es = currentState();
-               Function2<String,String,Object> editor = sne.getParameter("textEditor");
-               if(editor != null) {
-                       editor.apply(es.base.textElementId, es.currentText);
+               if (sne != null) {
+                       EditorState es = currentState();
+                       Function2<String,String,Object> editor = sne.getParameter("textEditor");
+                       if(editor != null) {
+                               editor.apply(es.base.textElementId, es.currentText);
+                       }
                }
        }
 
index 8084c2a41fcef9e6e2c8a235719cf648e381f24b..bccba4aa769ad96ebcc47080187846e4e3f2db22 100644 (file)
@@ -51,12 +51,14 @@ public class SVGNode extends org.simantics.scenegraph.g2d.nodes.SVGNode {
     public void render(Graphics2D g2d) {
         if (!parametersProcessed) {
             SingleElementNode sne = getSingleElementNode();
-            Boolean enableEditing = sne.getParameter("enableEditing");
-            if (enableEditing != null && enableEditing) {
-                esm = new EditorStateManager(this);
-                addEventHandler(this);
+            if (sne != null) {
+                Boolean enableEditing = sne.getParameter("enableEditing");
+                if (enableEditing != null && enableEditing) {
+                    esm = new EditorStateManager(this);
+                    addEventHandler(this);
+                }
+                parametersProcessed = true;
             }
-            parametersProcessed = true;
         }
 
         super.render(g2d);
@@ -76,7 +78,9 @@ public class SVGNode extends org.simantics.scenegraph.g2d.nodes.SVGNode {
     }
 
     private boolean isSelected(IG2DNode node) {
-        ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(this);
+        ICanvasContext ctx = DiagramNodeUtil.getPossibleCanvasContext(this);
+        if (ctx == null)
+            return false;
         IElement ie = DiagramNodeUtil.getElement(ctx, node);
         if (ie == null)
             return false;
@@ -119,10 +123,6 @@ public class SVGNode extends org.simantics.scenegraph.g2d.nodes.SVGNode {
     }
 
     SingleElementNode getSingleElementNode() {
-        ICanvasContext ctx = DiagramNodeUtil.getCanvasContext(this);
-        // FIXME: needed only because eventdelegator registrations are done before adding node to scene graph.
-        if (ctx == null)
-            return null;
         return (SingleElementNode)NodeUtil.getNearestParentOfType(this, SingleElementNode.class);
     }