]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Configurable prefix and suffix text for monitors
authorJussi Koskela <jussi.koskela@semantum.fi>
Mon, 29 Jun 2020 13:35:59 +0000 (16:35 +0300)
committerJussi Koskela <jussi.koskela@semantum.fi>
Mon, 29 Jun 2020 13:35:59 +0000 (16:35 +0300)
These can be used to show the attribute name and unit while keeping the
value editable. To be decided if this should be merged to Platform or if
the implementation should be duplicated to product.

Change-Id: I0078542238877a1decdbf989289440e9df85bafe

bundles/org.simantics.diagram/src/org/simantics/diagram/elements/MonitorClass.java

index c62422d4a5fe0f52e875825b72ef2c663b96d76e..080f6b9c4ee8b5b0924aadf7640fe3265e1f2f09 100644 (file)
@@ -107,6 +107,9 @@ public class MonitorClass {
     public static final Key  KEY_INPUT_VALIDATOR             = new KeyOf(Object.class, "INPUT_VALIDATOR");
     public static final Key  KEY_RVI             = new KeyOf(RVI.class, "RVI");
 
+    public static final Key KEY_PREFIX_TEXT = new KeyOf(String.class, "PREFIX_TEXT");
+    public static final Key KEY_SUFFIX_TEXT = new KeyOf(String.class, "SUFFIX_TEXT");
+
     /**
      * If this hint is defined, the monitor will force its x-axis to match this
      * angle. If this hint doesn't exist, the monitor will not force x-axis
@@ -118,6 +121,8 @@ public class MonitorClass {
 
     public static final Key  KEY_SG_NODE               = new SceneGraphNodeKey(TextNode.class, "MONITOR_SG_NODE");
     public static final Key  KEY_SG_NODE2              = new SceneGraphNodeKey(ShapeNode.class, "MONITOR_SG_NODE2");
+    public static final Key KEY_SG_PREFIX_NODE         = new SceneGraphNodeKey(ShapeNode.class, "MONITOR_PREFIX_NODE");
+    public static final Key KEY_SG_SUFFIX_NODE         = new SceneGraphNodeKey(ShapeNode.class, "MONITOR_SUFFIX_NODE");
 
     final static BasicStroke STROKE                    = new BasicStroke(1.0f);
 
@@ -354,18 +359,25 @@ public class MonitorClass {
                 node.setTextListener(new ITextListener() {
 
                     @Override
-                    public void textChanged() {}
+                    public void textChanged() {
+                        updatePrefixAndSuffixPositions(e);
+                    }
 
                     @Override
-                    public void textEditingStarted() {}
+                    public void textEditingStarted() {
+                        updatePrefixAndSuffixPositions(e);
+                    }
 
                     @Override
-                    public void textEditingCancelled() {}
+                    public void textEditingCancelled() {
+                        updatePrefixAndSuffixPositions(e);
+                    }
 
                     boolean isEndingEdit = false;
 
                     @Override
                     public void textEditingEnded() {
+                        updatePrefixAndSuffixPositions(e);
                         TextNode node = e.getHint(KEY_SG_NODE);
                         if (node == null)
                             return;
@@ -446,6 +458,46 @@ public class MonitorClass {
                     shape.remove();
             }
 
+            String prefixText = e.getHint(KEY_PREFIX_TEXT);
+            if (prefixText != null) {
+                TextNode prefixNode = e.getHint(KEY_SG_PREFIX_NODE);
+                if (prefixNode == null || prefixNode.getParent() != parent) {
+                    nodeId = ElementUtils.generateNodeId(e) + "-prefix";
+                    node = parent.addNode(nodeId, TextNode.class);
+                    e.setHint(KEY_SG_PREFIX_NODE, node);
+                    Font font = ElementUtils.getTextFont(e);
+                    Color color = ElementUtils.getTextColor(e);
+                    node.setEditable(false);
+                    node.setShowSelection(false);
+                    node.setHorizontalAlignment((byte) 1);
+                    node.init(prefixText, font, color, 0, 0, 1.0);
+                }
+            } else {
+                TextNode prefixNode = e.getHint(KEY_SG_PREFIX_NODE);
+                if (prefixNode != null)
+                    prefixNode.remove();
+            }
+
+            String suffixText = e.getHint(KEY_SUFFIX_TEXT);
+            if (suffixText != null) {
+                TextNode suffixNode = e.getHint(KEY_SG_SUFFIX_NODE);
+                if (suffixNode == null || suffixNode.getParent() != parent) {
+                    nodeId = ElementUtils.generateNodeId(e) + "-suffix";
+                    node = parent.addNode(nodeId, TextNode.class);
+                    e.setHint(KEY_SG_SUFFIX_NODE, node);
+                    Font font = ElementUtils.getTextFont(e);
+                    Color color = ElementUtils.getTextColor(e);
+                    node.setEditable(false);
+                    node.setShowSelection(false);
+                    node.setHorizontalAlignment((byte) 0);
+                    node.init(suffixText, font, color, 0, 0, 1.0);
+                }
+            } else {
+                TextNode suffixNode = e.getHint(KEY_SG_SUFFIX_NODE);
+                if (suffixNode != null)
+                    suffixNode.remove();
+            }
+
             update(e);
         }
 
@@ -498,6 +550,35 @@ public class MonitorClass {
                     at2.translate(bounds.getMinX(), bounds.getCenterY());
                     shape.setTransform(at2);
                 }
+
+                updatePrefixAndSuffixPositions(e);
+            }
+        }
+
+        public void updatePrefixAndSuffixPositions(IElement e) {
+            TextNode node = e.getHint(KEY_SG_NODE);
+            if (node != null) {
+                TextNode prefix = e.getHint(KEY_SG_PREFIX_NODE);
+                TextNode suffix = e.getHint(KEY_SG_SUFFIX_NODE);
+                if (prefix != null || suffix != null) {
+                    AffineTransform at = ElementUtils.getTransform(e);
+                    Alignment vAlign = getVerticalAlignment(e);
+                    if (prefix != null) {
+                        AffineTransform at2 = new AffineTransform(at);
+                        Rectangle2D bounds = node.getBoundsInLocal();
+                        at2.translate(bounds.getMinX(), 0);
+                        prefix.setTransform(at2);
+                        prefix.setVerticalAlignment((byte) vAlign.ordinal());
+                    }
+
+                    if (suffix != null) {
+                        AffineTransform at2 = new AffineTransform(at);
+                        Rectangle2D bounds = node.getBoundsInLocal();
+                        at2.translate(bounds.getMaxX(), 0);
+                        suffix.setTransform(at2);
+                        suffix.setVerticalAlignment((byte) vAlign.ordinal());
+                    }
+                }
             }
         }
 
@@ -519,19 +600,31 @@ public class MonitorClass {
                     size.setRect(bounds);
                 }
             }
+            TextNode prefixNode = (TextNode) e.getHint(KEY_SG_PREFIX_NODE);
+            if (prefixNode != null) {
+                Rectangle2D bounds = prefixNode.getBoundsInLocal();
+                if (size == null)
+                    size = new Rectangle2D.Double(0, 0, 0, 0);
+                size.add(bounds);
+            }
+            TextNode suffixNode = (TextNode) e.getHint(KEY_SG_SUFFIX_NODE);
+            if (suffixNode != null) {
+                Rectangle2D bounds = suffixNode.getBoundsInLocal();
+                if (size == null)
+                    size = new Rectangle2D.Double(0, 0, 0, 0);
+                size.add(bounds);
+            }
             return size;
         }
 
         @Override
         public Shape getElementShape(IElement e) {
-            Shape shape = new Rectangle2D.Double(0, 0, 0, 0);
-
-            TextNode node = (TextNode)e.getHint(KEY_SG_NODE);
-            if(node != null && node.getBoundsInLocal() != null) {
-                shape = node.getBoundsInLocal();
+            Rectangle2D bounds = getBounds(e, null);
+            if (bounds != null) {
+                return bounds;
+            } else {
+                return new Rectangle2D.Double(0, 0, 0, 0);
             }
-
-            return shape;
         }
 
     }