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
*/\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