import java.awt.font.FontRenderContext;\r
import java.awt.geom.AffineTransform;\r
import java.awt.geom.Rectangle2D;\r
+import java.util.List;\r
\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.h2d.action.IAction;\r
import org.simantics.h2d.editor.IDiagramEditor;\r
+import org.simantics.h2d.element.IElement;\r
import org.simantics.h2d.element.handler.Connectable;\r
import org.simantics.h2d.element.handler.Movable;\r
import org.simantics.h2d.event.ClickEvent;\r
\r
@GraphType("http://www.simantics.org/Sysdyn#Auxiliary")\r
public class TextElement extends RectangularElement implements Movable, Connectable, IEventHandler {\r
- \r
- static final Font FONT = new Font("sans-serif", Font.PLAIN, 12);\r
- static final double FONT_SCALE = 0.3;\r
- static final AffineTransform FONT_TRANSFORM = new AffineTransform(FONT_SCALE, 0.0, 0.0, FONT_SCALE, 0.0, 0.0);\r
- \r
+\r
+ static final Font FONT = new Font("sans-serif", Font.PLAIN, 12);\r
+ static final double FONT_SCALE = 0.3;\r
+ static final AffineTransform FONT_TRANSFORM = new AffineTransform(FONT_SCALE, 0.0, 0.0, FONT_SCALE, 0.0, 0.0);\r
+\r
static final double XPADDING = 4.0;\r
static final double YPADDING = 2.0;\r
static final FontRenderContext FRC = new FontRenderContext(FONT_TRANSFORM, true, true);\r
// Properties\r
@RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName")\r
public String label = "Unnamed";\r
- \r
+\r
@RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType")\r
public String type = "Real";\r
- \r
- // Auxiliary fields\r
- double textX;\r
- double textY;\r
- \r
- TextNode textNode;\r
- \r
- public TextElement() { \r
- }\r
- \r
- public double getTextDeltaX() {\r
- return 0.0;\r
- }\r
- \r
- public double getTextDeltaY() {\r
- return 0.0;\r
- }\r
- \r
- public TextElement(String label, double x, double y) {\r
- this.label = label;\r
- this.posX = x;\r
- this.posY = y;\r
- }\r
-\r
- @Override\r
- public void remove() {\r
- textNode.remove();\r
- super.remove();\r
- }\r
-\r
- @Override\r
- public void init(G2DParentNode parent) {\r
- textNode = parent.addNode(TextNode.class);\r
- update();\r
- }\r
- \r
- protected void updateText() {\r
- Rectangle2D textBounds = FONT.getStringBounds(label, FRC);\r
+\r
+ // Auxiliary fields\r
+ double textX;\r
+ double textY;\r
+\r
+ TextNode textNode;\r
+\r
+ public TextElement() { \r
+ }\r
+\r
+ public double getTextDeltaX() {\r
+ return 0.0;\r
+ }\r
+\r
+ public double getTextDeltaY() {\r
+ return 0.0;\r
+ }\r
+\r
+ public TextElement(String label, double x, double y) {\r
+ this.label = label;\r
+ this.posX = x;\r
+ this.posY = y;\r
+ }\r
+\r
+ @Override\r
+ public void remove() {\r
+ textNode.remove();\r
+ super.remove();\r
+ }\r
+\r
+ @Override\r
+ public void init(G2DParentNode parent) {\r
+ textNode = parent.addNode(TextNode.class);\r
+ update();\r
+ }\r
+\r
+ protected void updateText() {\r
+ Rectangle2D textBounds = FONT.getStringBounds(label, FRC);\r
textBounds.setFrame(\r
textBounds.getX()*FONT_SCALE,\r
textBounds.getY()*FONT_SCALE,\r
textBounds.getWidth()*FONT_SCALE,\r
textBounds.getHeight()*FONT_SCALE\r
- );\r
- \r
+ );\r
+\r
textX = posX+getTextDeltaX()-textBounds.getCenterX();\r
textY = posY+getTextDeltaY()-textBounds.getCenterY();\r
bounds.setFrame(\r
posY+getTextDeltaY()-textBounds.getHeight()*0.5-YPADDING,\r
textBounds.getWidth()+XPADDING*2,\r
textBounds.getHeight()+YPADDING*2\r
- ); \r
- \r
+ ); \r
+\r
if(textNode != null) {\r
textNode.init(label, FONT, hasEquation ? Color.BLACK : Color.RED, \r
textX, textY, FONT_SCALE);\r
oldHasEquation = hasEquation;\r
}\r
- }\r
- \r
- protected void update() {\r
- updateText(); \r
- fireElementUpdated();\r
- }\r
-\r
- class TextEditingAction implements IAction, ITextListener {\r
- \r
- String oldText;\r
- IDiagramEditor editor;\r
+ }\r
+\r
+ protected void update() {\r
+ updateText(); \r
+ fireElementUpdated();\r
+ }\r
+\r
+ class TextEditingAction implements IAction, ITextListener {\r
+\r
+ String oldText;\r
+ IDiagramEditor editor;\r
\r
public TextEditingAction(IDiagramEditor editor) {\r
- super();\r
- this.editor = editor;\r
- }\r
+ super();\r
+ this.editor = editor;\r
+ }\r
\r
- @Override\r
+ @Override\r
public void init(G2DParentNode parent) {\r
- oldText = textNode.getText();\r
- textNode.setEditMode(true); \r
- textNode.setTextListener(this);\r
+ oldText = textNode.getText();\r
+ textNode.setEditMode(true); \r
+ textNode.setTextListener(this);\r
/*textFieldNode = parent.addNode(TextFieldNode.class);\r
textFieldNode.init(10.0, 10.0);*/\r
}\r
\r
@Override\r
public void remove() {\r
- textNode.setEditMode(false);\r
- textNode.setTextListener(null);\r
+ textNode.setEditMode(false);\r
+ textNode.setTextListener(null);\r
//textFieldNode.remove(); \r
}\r
\r
@Override\r
public boolean handle(IDiagramEditor editor, IEvent _event) {\r
- if(_event instanceof KeyboardEvent) {\r
+ if(_event instanceof KeyboardEvent) {\r
KeyboardEvent event = (KeyboardEvent)_event;\r
if(event.key.equals("Escape")) {\r
- label = oldText;\r
- update();\r
- editor.removeAction(this); \r
+ label = oldText;\r
+ update();\r
+ editor.removeAction(this); \r
editor.requestRepaint(); \r
}\r
else if(event.key.equals("Enter"))\r
- editor.removeAction(this);\r
+ editor.removeAction(this);\r
return true;\r
}\r
- else if(_event instanceof ClickEvent) {\r
- editor.removeAction(this);\r
- }\r
- return false;\r
+ else if(_event instanceof ClickEvent) {\r
+ List<IElement> elements = editor.getDiagram().getElements();\r
+ for(IElement e : elements) {\r
+ if(e instanceof TextElement) {\r
+ TextElement t = (TextElement) e;\r
+ if(t != TextElement.this && t.label.equals(textNode.getText())) {\r
+ label = oldText;\r
+ update();\r
+ editor.requestRepaint(); \r
+ }\r
+ }\r
+ }\r
+ editor.removeAction(this);\r
+ }\r
+ return false;\r
}\r
\r
- @Override\r
- public void textChanged() {\r
- label = textNode.getText();\r
+ @Override\r
+ public void textChanged() {\r
+ label = textNode.getText();\r
update();\r
editor.requestRepaint();\r
- }\r
- \r
- }\r
- \r
- public void beginRenameAction(IDiagramEditor editor) {\r
- editor.addAction(new TextEditingAction(editor));\r
- }\r
-\r
- @Override\r
+ }\r
+\r
+ }\r
+\r
+ public void beginRenameAction(IDiagramEditor editor) {\r
+ editor.addAction(new TextEditingAction(editor));\r
+ }\r
+\r
+ @Override\r
public boolean handle(IDiagramEditor editor, IEvent _event) {\r
if(_event instanceof KeyboardEvent) {\r
KeyboardEvent event = (KeyboardEvent)_event;\r
return false;\r
} \r
\r
- boolean oldHasEquation = false;\r
- boolean hasEquation = false;\r
- \r
- @UpdateMethod\r
+ boolean oldHasEquation = false;\r
+ boolean hasEquation = false;\r
+\r
+ @UpdateMethod\r
boolean readHasExpression(ReadGraph g, Resource resource) {\r
SysdynResource sr = SysdynResource.getInstance(g);\r
try {\r