]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGNode.java
Supply SVG text editor with element measurement context
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / SVGNode.java
index bccba4aa769ad96ebcc47080187846e4e3f2db22..4d654791925080230f51a9b525cc777a1f7760d7 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2017 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -28,13 +28,19 @@ import org.simantics.scenegraph.g2d.events.command.CommandEvent;
 import org.simantics.scenegraph.g2d.nodes.SVGNodeAssignment;
 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
 import org.simantics.scenegraph.utils.NodeUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.kitfox.svg.SVGDiagram;
 import com.kitfox.svg.SVGException;
+import com.kitfox.svg.Text;
+import com.kitfox.svg.Tspan;
 
 @RasterOutputWidget
 public class SVGNode extends org.simantics.scenegraph.g2d.nodes.SVGNode {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(SVGNode.class);
+
     private static final long serialVersionUID = 4735066193941274186L;
 
     transient EditorStateManager esm  = null;
@@ -150,4 +156,22 @@ public class SVGNode extends org.simantics.scenegraph.g2d.nodes.SVGNode {
         return EventTypes.KeyPressedMask | EventTypes.MouseClickMask | EventTypes.CommandMask;
     }
 
+    public void modifyTextElement(String id, String newText) {
+        Text text = (Text) diagramCache.getElement(id);
+
+        EditorState es = new EditorState();
+        es.base = new EditorStateStatic();
+        es.base.textElementId = text.getId();
+
+        Tspan span = (Tspan)text.getContent().get(0);
+
+        try {
+            span.setText(newText);
+            text.rebuild();
+            diagramCache.updateTime(0);
+        } catch (SVGException e) {
+            LOGGER.error("Failed to update text span element " + id + " with text " + newText, e);
+        }
+    }
+
 }