From 63e79522c1ed7a79c2deb8aa7cae47d6f0385e80 Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 29 Jul 2010 10:43:02 +0000 Subject: [PATCH] git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16868 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/elements2/HoverTextElementHandler.java | 4 +-- .../elements2/HoverTextElementNoBounds.java | 30 ++++++++++++------- .../sysdyn/ui/elements2/HoverTextNode.java | 8 +++++ .../sysdyn/ui/elements2/InputFactory.java | 16 +++++----- .../sysdyn/ui/elements2/ModuleFactory.java | 4 +-- .../sysdyn/ui/elements2/StockFactory.java | 4 +-- .../ui/elements2/SysdynElementFactory.java | 2 +- .../sysdyn/ui/elements2/ValveFactory.java | 5 +++- 8 files changed, 47 insertions(+), 26 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java index 92f70a41..479a6884 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java @@ -25,8 +25,8 @@ public class HoverTextElementHandler extends HoverTextElementNoBounds implements } public HoverTextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth, - boolean editable) { - super(originX, originY, horizontalAlignment, borderWidth, editable); + double paddingX, double paddingY, boolean editable) { + super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable); } @Override 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 ebdea3bf..f27813ad 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 @@ -57,6 +57,9 @@ public class HoverTextElementNoBounds implements SceneGraph, HandleMouseEvent { private final Alignment horizontalAlignment; private final double borderWidth; private final boolean editable; + private final double paddingX; + private final double paddingY; + private IHintListener hoverHintListener ; public HoverTextElementNoBounds() { this(0, 0, Alignment.LEADING, 0); @@ -67,10 +70,10 @@ public class HoverTextElementNoBounds implements SceneGraph, HandleMouseEvent { } public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) { - this(originX, originY, horizontalAlignment, borderWidth, true); + this(originX, originY, horizontalAlignment, borderWidth, 2.0, 2.0, true); } - public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, boolean editable) { + public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable) { if (horizontalAlignment == null) throw new NullPointerException("null horizontal alignment"); @@ -79,6 +82,8 @@ public class HoverTextElementNoBounds implements SceneGraph, HandleMouseEvent { this.horizontalAlignment = horizontalAlignment; this.borderWidth = borderWidth; this.editable = editable; + this.paddingX = paddingX; + this.paddingY = paddingY; } @Override @@ -125,11 +130,10 @@ public class HoverTextElementNoBounds implements SceneGraph, HandleMouseEvent { node.setBackgroundColor(fillColor); node.setBorderColor(borderColor); node.setHorizontalAlignment((byte) horizontalAlignment.ordinal()); - node.setBorderWidth((float) borderWidth); + node.setBorderWidth((float) borderWidth * 0.235); node.setEditable(editable); - - e.addHintListener(new IHintListener() { - + node.setPadding(paddingX, paddingY); + hoverHintListener = new IHintListener() { @Override public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { @@ -137,16 +141,21 @@ public class HoverTextElementNoBounds implements SceneGraph, HandleMouseEvent { @Override public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { - IElement e = (IElement)sender; - HoverTextNode name = (HoverTextNode) e.getHint(SG_NODE); - name.setHover(SysdynElementUtils.isHovering(e)); + if(key == SysdynElementHints.KEY_HOVER) { + IElement e = (IElement)sender; + HoverTextNode name = (HoverTextNode) e.getHint(SG_NODE); + name.setHover(SysdynElementUtils.isHovering(e)); + } } - }); + }; + + e.addHintListener(hoverHintListener); } @Override public void cleanup(IElement e) { ElementUtils.removePossibleNode(e, SG_NODE); + e.removeHintListener(hoverHintListener); } // FIXME: hazardous with TextElementHandler.INSTANCE @@ -227,5 +236,4 @@ public class HoverTextElementNoBounds implements SceneGraph, HandleMouseEvent { return false; return true; } - } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java index 4a432786..b4fa4a61 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java @@ -3,9 +3,11 @@ package org.simantics.sysdyn.ui.elements2; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; +import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import org.simantics.diagram.elements.TextNode; +import org.simantics.g2d.utils.Alignment; import org.simantics.scenegraph.utils.NodeUtil; public class HoverTextNode extends TextNode { @@ -21,6 +23,12 @@ public class HoverTextNode extends TextNode { @Override public void render(Graphics2D g) { + + if(horizontalAlignment == Alignment.CENTER.ordinal()) { + FontRenderContext frc = g.getFontRenderContext(); + Rectangle2D stringBounds = font.getStringBounds(getText(), frc); + g.translate(- stringBounds.getWidth() / 2 * scale, 0); + } Rectangle2D r = getBoundsInLocal(); boolean selected = NodeUtil.isSelected(this, 1); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java index 005fbfba..366ab5b8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java @@ -74,7 +74,7 @@ public class InputFactory extends SysdynElementFactory { new StaticSymbolImpl(INPUT_IMAGE), StaticSymbolImageInitializer.INSTANCE, Input.INSTANCE, - new InputSceneGraph(0, 0, Alignment.CENTER), + new InputSceneGraph(0, 0, Alignment.LEADING), BoundsOutline.INSTANCE, HoverHandler.INSTANCE, new WholeElementTerminals(terminals) @@ -98,9 +98,11 @@ public class InputFactory extends SysdynElementFactory { dependency = graph.getPossibleObject(component, sr.IsHeadOf); if(dependency != null) { refersTo = graph.getPossibleObject(dependency, sr.RefersTo); - referenceName = (String) graph.getPossibleRelatedValue(refersTo, b.HasName); - module = graph.getPossibleObject(dependency, sr.HasTail); - moduleName = (String) graph.getPossibleRelatedValue(module, b.HasName); + if(refersTo != null) { + referenceName = (String) graph.getPossibleRelatedValue(refersTo, b.HasName); + module = graph.getPossibleObject(dependency, sr.HasTail); + moduleName = (String) graph.getPossibleRelatedValue(module, b.HasName); + } } else { Resource runtime = diagram.getHint((DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE)); DiagramResource dr = DiagramResource.getInstance(graph); @@ -138,7 +140,7 @@ public class InputFactory extends SysdynElementFactory { } SysdynElementUtils.setInputReference(e, inputReference); - + Font font = ElementUtils.getTextFont(e); font = font.deriveFont(font.getStyle() + Font.ITALIC); ElementUtils.setTextFont(e, font); @@ -154,7 +156,7 @@ public class InputFactory extends SysdynElementFactory { private final Alignment horizontalAlignment; public InputSceneGraph(double originX, double originY, Alignment horizontalAlignment) { - super(originX, originY, horizontalAlignment, (double) 0.0, true); + super(originX, originY, horizontalAlignment, 0.0, 2.0, 2.0, true); this.originX = originX; this.horizontalAlignment = horizontalAlignment; } @@ -196,6 +198,6 @@ public class InputFactory extends SysdynElementFactory { size.setFrame(0, 0, 0, 0); return size; } - + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java index 1661b9b9..c0d76dbb 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java @@ -30,6 +30,7 @@ import org.simantics.g2d.element.handler.impl.TextFontImpl; import org.simantics.g2d.element.handler.impl.TextImpl; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; +import org.simantics.g2d.utils.Alignment; public class ModuleFactory extends SysdynElementFactory { @@ -48,8 +49,7 @@ public class ModuleFactory extends SysdynElementFactory { new StaticObjectAdapter(elementType), new StaticSymbolImpl(DEFAULT_IMAGE), StaticSymbolImageInitializer.INSTANCE, - HoverTextElementHandler.INSTANCE, - BorderSceneGraph.INSTANCE, + new HoverTextElementHandler(0, 0, Alignment.LEADING, 1f), BoundsOutline.INSTANCE, HoverHandler.INSTANCE, new WholeElementTerminals(terminals) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java index f26a8541..f94a2d84 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java @@ -30,6 +30,7 @@ import org.simantics.g2d.element.handler.impl.TextFontImpl; import org.simantics.g2d.element.handler.impl.TextImpl; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; +import org.simantics.g2d.utils.Alignment; public class StockFactory extends SysdynElementFactory { @@ -48,8 +49,7 @@ public class StockFactory extends SysdynElementFactory { new StaticObjectAdapter(elementType), new StaticSymbolImpl(DEFAULT_IMAGE), StaticSymbolImageInitializer.INSTANCE, - HoverTextElementHandler.INSTANCE, - BorderSceneGraph.INSTANCE, + new HoverTextElementHandler(0, 0, Alignment.LEADING, 2f), BoundsOutline.INSTANCE, HoverHandler.INSTANCE, new WholeElementTerminals(terminals) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java index 2797342c..9b739a50 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java @@ -82,7 +82,7 @@ public abstract class SysdynElementFactory extends SyncElementFactory { ElementUtils.setTextColor(e, G2DUtils.getColor(graph, colorResource)); } - if (graph.hasStatement(component, SysdynResource.getInstance(graph).IsOutput)) { + if (component != null && graph.hasStatement(component, SysdynResource.getInstance(graph).IsOutput)) { Font font = ElementUtils.getTextFont(e); font = font.deriveFont(Font.BOLD); ElementUtils.setTextFont(e, font); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java index 6b435c33..545dfc74 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java @@ -107,7 +107,7 @@ public class ValveFactory extends SysdynElementFactory { private static final Key NODE = new SceneGraphNodeKey(ShapeNode.class, "VALVE_NODE"); public ValveSceneGraph() { - super(0, VALVE_SIZE + 2, Alignment.CENTER); + super(0, VALVE_SIZE + 3.0, Alignment.CENTER); } @Override public void init(IElement e, G2DParentNode parent) { @@ -150,6 +150,9 @@ public class ValveFactory extends SysdynElementFactory { if (size == null) size = new Rectangle2D.Double(); size.setFrame(createShape(VALVE_SIZE, Boolean.TRUE.equals(e.getHint(KEY_ROTATED))).getBounds2D()); + double paddingX = 2.0; + double paddingY = 2.0; + size.setRect(size.getX() - paddingX, size.getY() -paddingY, size.getWidth() + paddingX + paddingX, size.getHeight() + paddingY + paddingY); return size; } -- 2.47.1