From 2c779a108900dd823973d89bf982d135aff96c7e Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 10 Mar 2010 09:43:38 +0000 Subject: [PATCH] Text elements don't allow to change a name to a name that already exists git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@14752 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/elements/TextElement.java | 199 ++++++++++-------- 1 file changed, 106 insertions(+), 93 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java index 5f8404c3..8586a66c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java @@ -5,12 +5,14 @@ import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; +import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.h2d.action.IAction; import org.simantics.h2d.editor.IDiagramEditor; +import org.simantics.h2d.element.IElement; import org.simantics.h2d.element.handler.Connectable; import org.simantics.h2d.element.handler.Movable; import org.simantics.h2d.event.ClickEvent; @@ -27,11 +29,11 @@ import org.simantics.sysdyn.SysdynResource; @GraphType("http://www.simantics.org/Sysdyn#Auxiliary") public class TextElement extends RectangularElement implements Movable, Connectable, IEventHandler { - - static final Font FONT = new Font("sans-serif", Font.PLAIN, 12); - static final double FONT_SCALE = 0.3; - static final AffineTransform FONT_TRANSFORM = new AffineTransform(FONT_SCALE, 0.0, 0.0, FONT_SCALE, 0.0, 0.0); - + + static final Font FONT = new Font("sans-serif", Font.PLAIN, 12); + static final double FONT_SCALE = 0.3; + static final AffineTransform FONT_TRANSFORM = new AffineTransform(FONT_SCALE, 0.0, 0.0, FONT_SCALE, 0.0, 0.0); + static final double XPADDING = 4.0; static final double YPADDING = 2.0; static final FontRenderContext FRC = new FontRenderContext(FONT_TRANSFORM, true, true); @@ -39,54 +41,54 @@ public class TextElement extends RectangularElement implements Movable, Connecta // Properties @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName") public String label = "Unnamed"; - + @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasType") public String type = "Real"; - - // Auxiliary fields - double textX; - double textY; - - TextNode textNode; - - public TextElement() { - } - - public double getTextDeltaX() { - return 0.0; - } - - public double getTextDeltaY() { - return 0.0; - } - - public TextElement(String label, double x, double y) { - this.label = label; - this.posX = x; - this.posY = y; - } - - @Override - public void remove() { - textNode.remove(); - super.remove(); - } - - @Override - public void init(G2DParentNode parent) { - textNode = parent.addNode(TextNode.class); - update(); - } - - protected void updateText() { - Rectangle2D textBounds = FONT.getStringBounds(label, FRC); + + // Auxiliary fields + double textX; + double textY; + + TextNode textNode; + + public TextElement() { + } + + public double getTextDeltaX() { + return 0.0; + } + + public double getTextDeltaY() { + return 0.0; + } + + public TextElement(String label, double x, double y) { + this.label = label; + this.posX = x; + this.posY = y; + } + + @Override + public void remove() { + textNode.remove(); + super.remove(); + } + + @Override + public void init(G2DParentNode parent) { + textNode = parent.addNode(TextNode.class); + update(); + } + + protected void updateText() { + Rectangle2D textBounds = FONT.getStringBounds(label, FRC); textBounds.setFrame( textBounds.getX()*FONT_SCALE, textBounds.getY()*FONT_SCALE, textBounds.getWidth()*FONT_SCALE, textBounds.getHeight()*FONT_SCALE - ); - + ); + textX = posX+getTextDeltaX()-textBounds.getCenterX(); textY = posY+getTextDeltaY()-textBounds.getCenterY(); bounds.setFrame( @@ -94,80 +96,91 @@ public class TextElement extends RectangularElement implements Movable, Connecta posY+getTextDeltaY()-textBounds.getHeight()*0.5-YPADDING, textBounds.getWidth()+XPADDING*2, textBounds.getHeight()+YPADDING*2 - ); - + ); + if(textNode != null) { textNode.init(label, FONT, hasEquation ? Color.BLACK : Color.RED, textX, textY, FONT_SCALE); oldHasEquation = hasEquation; } - } - - protected void update() { - updateText(); - fireElementUpdated(); - } - - class TextEditingAction implements IAction, ITextListener { - - String oldText; - IDiagramEditor editor; + } + + protected void update() { + updateText(); + fireElementUpdated(); + } + + class TextEditingAction implements IAction, ITextListener { + + String oldText; + IDiagramEditor editor; public TextEditingAction(IDiagramEditor editor) { - super(); - this.editor = editor; - } + super(); + this.editor = editor; + } - @Override + @Override public void init(G2DParentNode parent) { - oldText = textNode.getText(); - textNode.setEditMode(true); - textNode.setTextListener(this); + oldText = textNode.getText(); + textNode.setEditMode(true); + textNode.setTextListener(this); /*textFieldNode = parent.addNode(TextFieldNode.class); textFieldNode.init(10.0, 10.0);*/ } @Override public void remove() { - textNode.setEditMode(false); - textNode.setTextListener(null); + textNode.setEditMode(false); + textNode.setTextListener(null); //textFieldNode.remove(); } @Override public boolean handle(IDiagramEditor editor, IEvent _event) { - if(_event instanceof KeyboardEvent) { + if(_event instanceof KeyboardEvent) { KeyboardEvent event = (KeyboardEvent)_event; if(event.key.equals("Escape")) { - label = oldText; - update(); - editor.removeAction(this); + label = oldText; + update(); + editor.removeAction(this); editor.requestRepaint(); } else if(event.key.equals("Enter")) - editor.removeAction(this); + editor.removeAction(this); return true; } - else if(_event instanceof ClickEvent) { - editor.removeAction(this); - } - return false; + else if(_event instanceof ClickEvent) { + List elements = editor.getDiagram().getElements(); + for(IElement e : elements) { + if(e instanceof TextElement) { + TextElement t = (TextElement) e; + if(t != TextElement.this && t.label.equals(textNode.getText())) { + label = oldText; + update(); + editor.requestRepaint(); + } + } + } + editor.removeAction(this); + } + return false; } - @Override - public void textChanged() { - label = textNode.getText(); + @Override + public void textChanged() { + label = textNode.getText(); update(); editor.requestRepaint(); - } - - } - - public void beginRenameAction(IDiagramEditor editor) { - editor.addAction(new TextEditingAction(editor)); - } - - @Override + } + + } + + public void beginRenameAction(IDiagramEditor editor) { + editor.addAction(new TextEditingAction(editor)); + } + + @Override public boolean handle(IDiagramEditor editor, IEvent _event) { if(_event instanceof KeyboardEvent) { KeyboardEvent event = (KeyboardEvent)_event; @@ -179,10 +192,10 @@ public class TextElement extends RectangularElement implements Movable, Connecta return false; } - boolean oldHasEquation = false; - boolean hasEquation = false; - - @UpdateMethod + boolean oldHasEquation = false; + boolean hasEquation = false; + + @UpdateMethod boolean readHasExpression(ReadGraph g, Resource resource) { SysdynResource sr = SysdynResource.getInstance(g); try { -- 2.47.1