]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Ääkköset and spaces not allowed in variable names
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 14 Sep 2010 12:27:40 +0000 (12:27 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 14 Sep 2010 12:27:40 +0000 (12:27 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17905 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java [new file with mode: 0644]

index e5b502d764ec14c27815cd97ddca3a28bd85fef8..22ea233fc815397a78de5e0b5b0622059753591a 100644 (file)
@@ -25,6 +25,7 @@ import org.simantics.g2d.element.ElementUtils;
 import org.simantics.g2d.element.IElement;\r
 import org.simantics.g2d.utils.Alignment;\r
 import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.sysdyn.ui.properties.VariableNameValidator;\r
 import org.simantics.utils.datastructures.Callback;\r
 import org.simantics.utils.datastructures.hints.IHintListener;\r
 import org.simantics.utils.datastructures.hints.IHintObservable;\r
@@ -38,102 +39,123 @@ import org.simantics.utils.datastructures.hints.IHintContext.Key;
  */\r
 public class HoverTextElementNoBounds extends TextElementNoBounds {\r
 \r
-    private static final long serialVersionUID = -148784588840819612L;\r
-\r
-    public static final HoverTextElementNoBounds INSTANCE         = new HoverTextElementNoBounds();\r
-\r
-    private IHintListener hoverHintListener;\r
-\r
-    public HoverTextElementNoBounds() {\r
-        super(0, 0, Alignment.LEADING, 0);\r
-    }\r
-\r
-    public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) {\r
-        super(originX, originY, horizontalAlignment, 0);\r
-    }\r
-\r
-    public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {\r
-        super(originX, originY, horizontalAlignment, borderWidth);\r
-    }\r
-\r
-    public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable) {\r
-        super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable);\r
-    }\r
-\r
-    @Override\r
-    public void init(final IElement e, G2DParentNode parent) {\r
-        HoverTextNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", HoverTextNode.class, new Callback<HoverTextNode>() {\r
-            @Override\r
-            public void run(HoverTextNode node) {\r
-                node.setTextListener(new ITextListener() {\r
-                    @Override\r
-                    public void textChanged() {}\r
-\r
-                    @Override\r
-                    public void textEditingStarted() {}\r
-\r
-                    @Override\r
-                    public void textEditingCancelled() {\r
-                        TextNode node = (TextNode) e.getHint(SG_NODE);\r
-                        if (node != null)\r
-                            endEdit(node);\r
-                    }\r
-\r
-                    @Override\r
-                    public void textEditingEnded() {\r
-                        TextNode node = (TextNode) e.getHint(SG_NODE);\r
-                        if (node == null)\r
-                            return;\r
-                        //System.out.println("Node text changed: " + node.getText());\r
-                        ElementUtils.setText(e, node.getText());\r
-                        IDiagram diagram = ElementUtils.getDiagram(e);\r
-                        DiagramUtils.synchronizeHintsToBackend(diagram, e);\r
-                        endEdit(node);\r
-                    }\r
-                });\r
-            }\r
-        });\r
-\r
-        //Font font = new Font("Tahoma", 0, 12);\r
-        Font font = ElementUtils.getTextFont(e);\r
-        Color color = ElementUtils.getTextColor(e);\r
-        Color fillColor = ElementUtils.getFillColor(e);\r
-        Color borderColor = ElementUtils.getBorderColor(e, Color.BLACK);\r
-        String text = ElementUtils.getText(e);\r
-        AffineTransform at = ElementUtils.getTransform(e);\r
-        node.init(text, font, color, originX, originY, 0.235);\r
-        node.setBackgroundColor(fillColor);\r
-        node.setBorderColor(borderColor);\r
-        node.setHorizontalAlignment((byte) horizontalAlignment.ordinal());\r
-        node.setPadding(paddingX, paddingY);\r
-        node.setBorderWidth((float) borderWidth);\r
-        node.setEditable(editable);\r
-        if(at != null)\r
-            node.setTransform(at);\r
-            \r
-        this.hoverHintListener = new IHintListener() {\r
-            @Override\r
-            public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {\r
-\r
-            }\r
-\r
-            @Override\r
-            public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
-                if(key == ElementHints.KEY_HOVER) {\r
-                    IElement e = (IElement)sender;\r
-                    TextNode name = (TextNode) e.getHint(SG_NODE);\r
-                    if(name != null)\r
-                        name.setHover((Boolean)e.getHint(ElementHints.KEY_HOVER));\r
-                }\r
-            }\r
-        };\r
-        e.addHintListener(hoverHintListener);\r
-    }\r
-\r
-    @Override\r
-    public void cleanup(IElement e) {\r
-        if(hoverHintListener != null)\r
-            e.removeHintListener(hoverHintListener);\r
-        ElementUtils.removePossibleNode(e, SG_NODE);\r
-    }\r
+       private static final long serialVersionUID = -148784588840819612L;\r
+\r
+       public static final HoverTextElementNoBounds INSTANCE         = new HoverTextElementNoBounds();\r
+\r
+       private IHintListener hoverHintListener;\r
+\r
+       public HoverTextElementNoBounds() {\r
+               super(0, 0, Alignment.LEADING, 0);\r
+       }\r
+\r
+       public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) {\r
+               super(originX, originY, horizontalAlignment, 0);\r
+       }\r
+\r
+       public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {\r
+               super(originX, originY, horizontalAlignment, borderWidth);\r
+       }\r
+\r
+       public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable) {\r
+               super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable);\r
+       }\r
+\r
+       @Override\r
+       public void init(final IElement e, G2DParentNode parent) {\r
+               HoverTextNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", HoverTextNode.class, new Callback<HoverTextNode>() {\r
+                       @Override\r
+                       public void run(HoverTextNode node) {\r
+                               node.setTextListener(new ITextListener() {\r
+\r
+                                       String textBeforeEdit;\r
+\r
+                                       @Override\r
+                                       public void textChanged() {\r
+                                               TextNode node = (TextNode) e.getHint(SG_NODE);\r
+                                               if(!VariableNameValidator.isValid(node.getText())) {\r
+                                                       node.setColor(Color.RED);\r
+                                               } else {\r
+                                                       node.setColor(Color.BLACK);\r
+                                               }\r
+\r
+\r
+                                       }\r
+\r
+                                       @Override\r
+                                       public void textEditingStarted() {\r
+                                               TextNode node = (TextNode) e.getHint(SG_NODE);\r
+                                               textBeforeEdit = node.getText();\r
+                                       }\r
+\r
+                                       @Override\r
+                                       public void textEditingCancelled() {\r
+                                               TextNode node = (TextNode) e.getHint(SG_NODE);\r
+                                               if (node != null)\r
+                                                       endEdit(node);\r
+                                       }\r
+\r
+                                       @Override\r
+                                       public void textEditingEnded() {\r
+                                               TextNode node = (TextNode) e.getHint(SG_NODE);\r
+                                               if (node == null)\r
+                                                       return;\r
+                                               String text = node.getText();\r
+                                               if(!VariableNameValidator.isValid(text)) {\r
+                                                       text = textBeforeEdit;\r
+                                                       node.setText(text);\r
+                                                       if(VariableNameValidator.isValid(text))\r
+                                                               node.setColor(Color.BLACK);\r
+                                               }\r
+                                               ElementUtils.setText(e, text);\r
+                                               IDiagram diagram = ElementUtils.getDiagram(e);\r
+                                               DiagramUtils.synchronizeHintsToBackend(diagram, e);\r
+                                               endEdit(node);\r
+                                       }\r
+                               });\r
+                       }\r
+               });\r
+\r
+               //Font font = new Font("Tahoma", 0, 12);\r
+               Font font = ElementUtils.getTextFont(e);\r
+               Color color = ElementUtils.getTextColor(e);\r
+               Color fillColor = ElementUtils.getFillColor(e);\r
+               Color borderColor = ElementUtils.getBorderColor(e, Color.BLACK);\r
+               String text = ElementUtils.getText(e);\r
+               AffineTransform at = ElementUtils.getTransform(e);\r
+               node.init(text, font, color, originX, originY, 0.235);\r
+               node.setBackgroundColor(fillColor);\r
+               node.setBorderColor(borderColor);\r
+               node.setHorizontalAlignment((byte) horizontalAlignment.ordinal());\r
+               node.setPadding(paddingX, paddingY);\r
+               node.setBorderWidth((float) borderWidth);\r
+               node.setEditable(editable);\r
+               if(at != null)\r
+                       node.setTransform(at);\r
+\r
+               this.hoverHintListener = new IHintListener() {\r
+                       @Override\r
+                       public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {\r
+\r
+                       }\r
+\r
+                       @Override\r
+                       public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
+                               if(key == ElementHints.KEY_HOVER) {\r
+                                       IElement e = (IElement)sender;\r
+                                       TextNode name = (TextNode) e.getHint(SG_NODE);\r
+                                       if(name != null)\r
+                                               name.setHover((Boolean)e.getHint(ElementHints.KEY_HOVER));\r
+                               }\r
+                       }\r
+               };\r
+               e.addHintListener(hoverHintListener);\r
+       }\r
+\r
+       @Override\r
+       public void cleanup(IElement e) {\r
+               if(hoverHintListener != null)\r
+                       e.removeHintListener(hoverHintListener);\r
+               ElementUtils.removePossibleNode(e, SG_NODE);\r
+       }\r
 }
\ No newline at end of file
index 49d09cec41786da2d142c86845aaf373a568fa88..85fec7668c86ffd4254c07f5f6ccf8a7a9233a8d 100644 (file)
@@ -48,8 +48,8 @@ public class ConfigurationTab extends PropertyTabContributorImpl {
             \r
             @Override\r
             public String isValid(String newText) {\r
-                if (newText.contains(" "))\r
-                    return "Sorry but spaces are not allowed for names right now";\r
+                if (!VariableNameValidator.isValid(newText))\r
+                    return "Sorry but spaces and special characters are not allowed for names right now";\r
                 return null;\r
             }\r
         });\r
index 85994cc6914188e128cdbf15bffd22f5536147c3..f197d89c2c020666b019d05b1eda054a7fd05abb 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Collection;
 import java.util.HashMap;\r
 import java.util.Map;\r
 \r
+import org.eclipse.jface.dialogs.IInputValidator;\r
 import org.eclipse.jface.layout.GridDataFactory;\r
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.swt.SWT;\r
@@ -55,6 +56,15 @@ public class EquationTab extends PropertyTabContributorImpl {
         TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
         nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
         nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+        nameText.setInputValidator(new IInputValidator() {\r
+            \r
+            @Override\r
+            public String isValid(String newText) {\r
+                if (!VariableNameValidator.isValid(newText))\r
+                    return "Sorry but spaces and special characters are not allowed for names right now";\r
+                return null;\r
+            }\r
+        });\r
         GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
 \r
 \r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java
new file mode 100644 (file)
index 0000000..56ae17a
--- /dev/null
@@ -0,0 +1,14 @@
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+public class VariableNameValidator {\r
+\r
+       public static boolean isValid(String name) {\r
+               String lowerCase = name.toLowerCase();\r
+               Pattern p = Pattern.compile("[a-zA-Z0-9]++");\r
+               Matcher m = p.matcher(lowerCase);\r
+               return m.matches();\r
+       }\r
+}\r