From: lempinen Date: Tue, 14 Sep 2010 12:27:40 +0000 (+0000) Subject: Ääkköset and spaces not allowed in variable names X-Git-Tag: simantics-1.2.0~47 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a251a784f610db8a76aacd2a8532b0c6e8cb8d1a;p=simantics%2Fsysdyn.git Ääkköset and spaces not allowed in variable names git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17905 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java index e5b502d7..22ea233f 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java @@ -25,6 +25,7 @@ import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.utils.Alignment; import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.sysdyn.ui.properties.VariableNameValidator; import org.simantics.utils.datastructures.Callback; import org.simantics.utils.datastructures.hints.IHintListener; import org.simantics.utils.datastructures.hints.IHintObservable; @@ -38,102 +39,123 @@ import org.simantics.utils.datastructures.hints.IHintContext.Key; */ public class HoverTextElementNoBounds extends TextElementNoBounds { - private static final long serialVersionUID = -148784588840819612L; - - public static final HoverTextElementNoBounds INSTANCE = new HoverTextElementNoBounds(); - - private IHintListener hoverHintListener; - - public HoverTextElementNoBounds() { - super(0, 0, Alignment.LEADING, 0); - } - - public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) { - super(originX, originY, horizontalAlignment, 0); - } - - public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) { - super(originX, originY, horizontalAlignment, borderWidth); - } - - public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable) { - super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable); - } - - @Override - public void init(final IElement e, G2DParentNode parent) { - HoverTextNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", HoverTextNode.class, new Callback() { - @Override - public void run(HoverTextNode node) { - node.setTextListener(new ITextListener() { - @Override - public void textChanged() {} - - @Override - public void textEditingStarted() {} - - @Override - public void textEditingCancelled() { - TextNode node = (TextNode) e.getHint(SG_NODE); - if (node != null) - endEdit(node); - } - - @Override - public void textEditingEnded() { - TextNode node = (TextNode) e.getHint(SG_NODE); - if (node == null) - return; - //System.out.println("Node text changed: " + node.getText()); - ElementUtils.setText(e, node.getText()); - IDiagram diagram = ElementUtils.getDiagram(e); - DiagramUtils.synchronizeHintsToBackend(diagram, e); - endEdit(node); - } - }); - } - }); - - //Font font = new Font("Tahoma", 0, 12); - Font font = ElementUtils.getTextFont(e); - Color color = ElementUtils.getTextColor(e); - Color fillColor = ElementUtils.getFillColor(e); - Color borderColor = ElementUtils.getBorderColor(e, Color.BLACK); - String text = ElementUtils.getText(e); - AffineTransform at = ElementUtils.getTransform(e); - node.init(text, font, color, originX, originY, 0.235); - node.setBackgroundColor(fillColor); - node.setBorderColor(borderColor); - node.setHorizontalAlignment((byte) horizontalAlignment.ordinal()); - node.setPadding(paddingX, paddingY); - node.setBorderWidth((float) borderWidth); - node.setEditable(editable); - if(at != null) - node.setTransform(at); - - this.hoverHintListener = new IHintListener() { - @Override - public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { - - } - - @Override - public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { - if(key == ElementHints.KEY_HOVER) { - IElement e = (IElement)sender; - TextNode name = (TextNode) e.getHint(SG_NODE); - if(name != null) - name.setHover((Boolean)e.getHint(ElementHints.KEY_HOVER)); - } - } - }; - e.addHintListener(hoverHintListener); - } - - @Override - public void cleanup(IElement e) { - if(hoverHintListener != null) - e.removeHintListener(hoverHintListener); - ElementUtils.removePossibleNode(e, SG_NODE); - } + private static final long serialVersionUID = -148784588840819612L; + + public static final HoverTextElementNoBounds INSTANCE = new HoverTextElementNoBounds(); + + private IHintListener hoverHintListener; + + public HoverTextElementNoBounds() { + super(0, 0, Alignment.LEADING, 0); + } + + public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) { + super(originX, originY, horizontalAlignment, 0); + } + + public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) { + super(originX, originY, horizontalAlignment, borderWidth); + } + + public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable) { + super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable); + } + + @Override + public void init(final IElement e, G2DParentNode parent) { + HoverTextNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", HoverTextNode.class, new Callback() { + @Override + public void run(HoverTextNode node) { + node.setTextListener(new ITextListener() { + + String textBeforeEdit; + + @Override + public void textChanged() { + TextNode node = (TextNode) e.getHint(SG_NODE); + if(!VariableNameValidator.isValid(node.getText())) { + node.setColor(Color.RED); + } else { + node.setColor(Color.BLACK); + } + + + } + + @Override + public void textEditingStarted() { + TextNode node = (TextNode) e.getHint(SG_NODE); + textBeforeEdit = node.getText(); + } + + @Override + public void textEditingCancelled() { + TextNode node = (TextNode) e.getHint(SG_NODE); + if (node != null) + endEdit(node); + } + + @Override + public void textEditingEnded() { + TextNode node = (TextNode) e.getHint(SG_NODE); + if (node == null) + return; + String text = node.getText(); + if(!VariableNameValidator.isValid(text)) { + text = textBeforeEdit; + node.setText(text); + if(VariableNameValidator.isValid(text)) + node.setColor(Color.BLACK); + } + ElementUtils.setText(e, text); + IDiagram diagram = ElementUtils.getDiagram(e); + DiagramUtils.synchronizeHintsToBackend(diagram, e); + endEdit(node); + } + }); + } + }); + + //Font font = new Font("Tahoma", 0, 12); + Font font = ElementUtils.getTextFont(e); + Color color = ElementUtils.getTextColor(e); + Color fillColor = ElementUtils.getFillColor(e); + Color borderColor = ElementUtils.getBorderColor(e, Color.BLACK); + String text = ElementUtils.getText(e); + AffineTransform at = ElementUtils.getTransform(e); + node.init(text, font, color, originX, originY, 0.235); + node.setBackgroundColor(fillColor); + node.setBorderColor(borderColor); + node.setHorizontalAlignment((byte) horizontalAlignment.ordinal()); + node.setPadding(paddingX, paddingY); + node.setBorderWidth((float) borderWidth); + node.setEditable(editable); + if(at != null) + node.setTransform(at); + + this.hoverHintListener = new IHintListener() { + @Override + public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { + + } + + @Override + public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { + if(key == ElementHints.KEY_HOVER) { + IElement e = (IElement)sender; + TextNode name = (TextNode) e.getHint(SG_NODE); + if(name != null) + name.setHover((Boolean)e.getHint(ElementHints.KEY_HOVER)); + } + } + }; + e.addHintListener(hoverHintListener); + } + + @Override + public void cleanup(IElement e) { + if(hoverHintListener != null) + e.removeHintListener(hoverHintListener); + ElementUtils.removePossibleNode(e, SG_NODE); + } } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java index 49d09cec..85fec766 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java @@ -48,8 +48,8 @@ public class ConfigurationTab extends PropertyTabContributorImpl { @Override public String isValid(String newText) { - if (newText.contains(" ")) - return "Sorry but spaces are not allowed for names right now"; + if (!VariableNameValidator.isValid(newText)) + return "Sorry but spaces and special characters are not allowed for names right now"; return null; } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index 85994cc6..f197d89c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -4,6 +4,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; @@ -55,6 +56,15 @@ public class EquationTab extends PropertyTabContributorImpl { TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName)); nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName)); + nameText.setInputValidator(new IInputValidator() { + + @Override + public String isValid(String newText) { + if (!VariableNameValidator.isValid(newText)) + return "Sorry but spaces and special characters are not allowed for names right now"; + return null; + } + }); GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); 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 index 00000000..56ae17ab --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameValidator.java @@ -0,0 +1,14 @@ +package org.simantics.sysdyn.ui.properties; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class VariableNameValidator { + + public static boolean isValid(String name) { + String lowerCase = name.toLowerCase(); + Pattern p = Pattern.compile("[a-zA-Z0-9]++"); + Matcher m = p.matcher(lowerCase); + return m.matches(); + } +}