From 497be98fedf3e753cd4f2580463cc7081effe05b Mon Sep 17 00:00:00 2001 From: lempinen Date: Wed, 28 Jul 2010 15:00:27 +0000 Subject: [PATCH] Attempting to implement some kind of hover support git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16863 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/elements2/AuxiliaryFactory.java | 4 +- .../sysdyn/ui/elements2/CloudFactory.java | 40 ++- .../sysdyn/ui/elements2/HoverHandler.java | 20 ++ .../sysdyn/ui/elements2/HoverShapeNode.java | 46 +++ .../ui/elements2/HoverTextElementHandler.java | 43 +++ .../elements2/HoverTextElementNoBounds.java | 271 ++++++++++++++++++ .../sysdyn/ui/elements2/HoverTextNode.java | 44 +++ .../sysdyn/ui/elements2/InputFactory.java | 5 +- .../sysdyn/ui/elements2/ModuleFactory.java | 4 +- .../sysdyn/ui/elements2/StockFactory.java | 4 +- .../ui/elements2/SysdynElementFactory.java | 2 + .../ui/elements2/SysdynElementHints.java | 2 + .../ui/elements2/SysdynElementUtils.java | 12 + .../sysdyn/ui/elements2/ValveFactory.java | 40 ++- 14 files changed, 516 insertions(+), 21 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverShapeNode.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java index e0f6838f..8c353038 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java @@ -16,7 +16,6 @@ import java.awt.geom.Ellipse2D; import java.util.Collection; import org.simantics.db.Resource; -import org.simantics.diagram.elements.TextElementHandler; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; @@ -52,8 +51,9 @@ public class AuxiliaryFactory extends SysdynElementFactory { new StaticObjectAdapter(elementType), new StaticSymbolImpl(AUX_STATIC_IMAGE), StaticSymbolImageInitializer.INSTANCE, - TextElementHandler.INSTANCE, + HoverTextElementHandler.INSTANCE, BoundsOutline.INSTANCE, + HoverHandler.INSTANCE, new WholeElementTerminals(terminals) ).setId(AuxiliaryFactory.class.getSimpleName()); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java index 93c9c5bf..034b02af 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java @@ -19,10 +19,12 @@ import java.awt.geom.Rectangle2D; import java.util.Collection; import org.simantics.db.Resource; +import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.HandleMouseEvent; import org.simantics.g2d.element.handler.InternalSize; import org.simantics.g2d.element.handler.SceneGraph; import org.simantics.g2d.element.handler.impl.BoundsOutline; @@ -36,10 +38,15 @@ import org.simantics.g2d.element.handler.impl.StaticSymbolImpl; import org.simantics.g2d.element.handler.impl.TextColorImpl; import org.simantics.g2d.element.handler.impl.TextFontImpl; import org.simantics.g2d.element.handler.impl.TextImpl; +import org.simantics.g2d.event.MouseEvent; +import org.simantics.g2d.event.MouseEvent.MouseEnterEvent; +import org.simantics.g2d.event.MouseEvent.MouseExitEvent; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.utils.datastructures.hints.IHintListener; +import org.simantics.utils.datastructures.hints.IHintObservable; import org.simantics.utils.datastructures.hints.IHintContext.Key; public class CloudFactory extends SysdynElementFactory { @@ -85,13 +92,13 @@ public class CloudFactory extends SysdynElementFactory { new StaticSymbolImpl(DEFAULT_IMAGE), StaticSymbolImageInitializer.INSTANCE, CloudSceneGraph.INSTANCE, + HoverHandler.INSTANCE, BoundsOutline.INSTANCE, new WholeElementTerminals(terminals) ).setId(CloudFactory.class.getSimpleName()); } - - public static class CloudSceneGraph implements SceneGraph, InternalSize { + public static class CloudSceneGraph implements SceneGraph, InternalSize, HandleMouseEvent { private static final long serialVersionUID = 5544256245734478634L; @@ -101,12 +108,27 @@ public class CloudFactory extends SysdynElementFactory { @Override public void init(IElement e, G2DParentNode parent) { - ShapeNode node = ElementUtils.getOrCreateNode(e, parent, NODE, "valve", ShapeNode.class); + HoverShapeNode node = ElementUtils.getOrCreateNode(e, parent, NODE, "valve", HoverShapeNode.class); node.setStroke(STROKE); node.setScaleStroke(true); node.setColor(Color.BLACK); node.setShape(getCloudShape()); + + e.addHintListener(new IHintListener() { + + @Override + public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { + + } + + @Override + public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { + IElement e = (IElement)sender; + HoverShapeNode shape = (HoverShapeNode) e.getHint(NODE); + shape.setHover(SysdynElementUtils.isHovering(e)); + } + }); } @Override @@ -122,6 +144,18 @@ public class CloudFactory extends SysdynElementFactory { return size; } + @Override + public boolean handleMouseEvent(IElement e, ICanvasContext ctx, MouseEvent me) { + if (me instanceof MouseEnterEvent) { + SysdynElementUtils.setHover(e, true); + return false; + } else if (me instanceof MouseExitEvent) { + SysdynElementUtils.setHover(e, false); + return false; + } + return false; + } + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverHandler.java new file mode 100644 index 00000000..8673fab2 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverHandler.java @@ -0,0 +1,20 @@ +package org.simantics.sysdyn.ui.elements2; + +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.ElementHandler; + +public class HoverHandler implements ElementHandler { + + private static final long serialVersionUID = -6392685799523448598L; + + public static final HoverHandler INSTANCE = new HoverHandler(); + + public boolean isHovering(IElement e) { + return e.getHint(SysdynElementHints.KEY_HOVER); + } + + + public void setHover(IElement e, boolean hover) { + e.setHint(SysdynElementHints.KEY_HOVER, hover); + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverShapeNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverShapeNode.java new file mode 100644 index 00000000..1db4490c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverShapeNode.java @@ -0,0 +1,46 @@ +package org.simantics.sysdyn.ui.elements2; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Shape; +import java.awt.Stroke; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; + +public class HoverShapeNode extends ShapeNode { + + private static final long serialVersionUID = -4580969977763722602L; + + transient public boolean hover = false; + + @Override + protected void renderShape(Graphics2D g2d, Shape s) { + boolean fill = Boolean.TRUE.equals(dynamicFill) ? true : this.fill; + if (fill) + g2d.fill(s); + + if(hover) { + BasicStroke oldStroke = (BasicStroke)g2d.getStroke(); + Color oldColor = g2d.getColor(); + g2d.setColor(Color.LIGHT_GRAY); + float lineWidth = oldStroke.getLineWidth() * 6; + g2d.setStroke(new BasicStroke(lineWidth < 1.0f ? lineWidth : 1.0f)); + g2d.draw(s); + g2d.setColor(oldColor); + g2d.setStroke(oldStroke); + } + + Stroke stroke = dynamicStroke != null ? dynamicStroke : this.stroke; + if (stroke != null) + g2d.draw(s); + + } + + public void setHover(boolean hover) { + this.hover = hover; + repaint(); + } + + + +} 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 new file mode 100644 index 00000000..92f70a41 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java @@ -0,0 +1,43 @@ +package org.simantics.sysdyn.ui.elements2; + +import java.awt.geom.Rectangle2D; + +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.InternalSize; +import org.simantics.g2d.utils.Alignment; + +public class HoverTextElementHandler extends HoverTextElementNoBounds implements InternalSize { + + private static final long serialVersionUID = 8800738238681432901L; + + public static final HoverTextElementHandler INSTANCE = new HoverTextElementHandler(); + + public HoverTextElementHandler() { + super(); + } + + public HoverTextElementHandler(double originX, double originY, Alignment horizontalAlignment) { + super(originX, originY, horizontalAlignment); + } + + public HoverTextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth) { + super(originX, originY, horizontalAlignment, borderWidth); + } + + public HoverTextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth, + boolean editable) { + super(originX, originY, horizontalAlignment, borderWidth, editable); + } + + @Override + public Rectangle2D getBounds(IElement e, Rectangle2D size) { + HoverTextNode node = (HoverTextNode) e.getHint(SG_NODE); + if (size == null) + size = new Rectangle2D.Double(); + if (node != null) + size.setRect(node.getBoundsInLocal()); + else + size.setFrame(0, 0, 0, 0); + return size; + } +} 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 new file mode 100644 index 00000000..ffab9354 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java @@ -0,0 +1,271 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.elements2; + +import java.awt.Color; +import java.awt.Font; +import java.awt.geom.Point2D; +import java.util.Collection; + +import org.simantics.diagram.elements.ITextListener; +import org.simantics.diagram.elements.TextNode; +import org.simantics.diagram.participant.ContextUtil; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.canvas.IMouseCaptureHandle; +import org.simantics.g2d.diagram.DiagramUtils; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.diagram.participant.ElementInteractor; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.HandleMouseEvent; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.event.MouseEvent; +import org.simantics.g2d.event.MouseEvent.MouseEnterEvent; +import org.simantics.g2d.event.MouseEvent.MouseExitEvent; +import org.simantics.g2d.participant.MouseUtil.MouseClickEvent; +import org.simantics.g2d.utils.Alignment; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.datastructures.hints.IHintListener; +import org.simantics.utils.datastructures.hints.IHintObservable; +import org.simantics.utils.datastructures.hints.IHintContext.Key; + +/** + * ElementHandler for text elements + * In-line editing supported. + * + * @author Marko Luukkainen + */ +public class HoverTextElementNoBounds implements SceneGraph, HandleMouseEvent { + + private static final long serialVersionUID = -148784588840819612L; + + + public static final HoverTextElementNoBounds INSTANCE = new HoverTextElementNoBounds(); + + public static final Key SG_NODE = new SceneGraphNodeKey(TextNode.class, "TEXT_SG_NODE"); + + private final double originX; + private final double originY; + private final Alignment horizontalAlignment; + private final double borderWidth; + private final boolean editable; + + public HoverTextElementNoBounds() { + this(0, 0, Alignment.LEADING, 0); + } + + public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) { + this(originX, originY, horizontalAlignment, 0); + } + + public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) { + this(originX, originY, horizontalAlignment, borderWidth, true); + } + + public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, boolean editable) { + if (horizontalAlignment == null) + throw new NullPointerException("null horizontal alignment"); + + this.originX = originX; + this.originY = originY; + this.horizontalAlignment = horizontalAlignment; + this.borderWidth = borderWidth; + this.editable = 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); + node.init(text, font, color, originX, originY, 0.235); + node.setBackgroundColor(fillColor); + node.setBorderColor(borderColor); + node.setHorizontalAlignment((byte) horizontalAlignment.ordinal()); + node.setBorderWidth((float) borderWidth); + node.setEditable(editable); + + e.addHintListener(new IHintListener() { + + @Override + public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { + + } + + @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)); + } + }); + } + + @Override + public void cleanup(IElement e) { + ElementUtils.removePossibleNode(e, SG_NODE); + } + + // FIXME: hazardous with TextElementHandler.INSTANCE + TextEditActivation editActivation = null; + + @Override + public boolean handleMouseEvent(IElement e, ICanvasContext ctx, MouseEvent me) { + if (me instanceof MouseEnterEvent) { + SysdynElementUtils.setHover(e, true); + } else if (me instanceof MouseExitEvent) { + SysdynElementUtils.setHover(e, false); + } else if (me instanceof MouseClickEvent) { + MouseClickEvent mce = (MouseClickEvent)me; + if ((mce.button & MouseClickEvent.LEFT_BUTTON) > 0) { + TextNode node = (TextNode) e.getHint(SG_NODE); + if (node == null) + return false; + if (node.isEditMode()) { + Point2D p = new Point2D.Double(); + ElementUtils.controlToCanvasCoordinate(ctx, me.controlPosition, p); + if (!ElementUtils.getElementBoundsOnDiagram(e).contains(p)) { + endEdit(node); + } + } else if (!node.isEditable()) { + return false; + } else if (mce.clickCount > 1) { + editActivation = new TextEditActivation(mce.mouseId, e, ctx); + node.setEditMode(true); + node.repaint(); + } + } + } + + return false; + } + + private void endEdit(TextNode node) { + if (editActivation != null) { + editActivation.release(); + editActivation = null; + + node.setEditMode(false); + node.repaint(); + } + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(borderWidth); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + horizontalAlignment.hashCode(); + temp = Double.doubleToLongBits(originX); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(originY); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + HoverTextElementNoBounds other = (HoverTextElementNoBounds) obj; + if (Double.doubleToLongBits(borderWidth) != Double.doubleToLongBits(other.borderWidth)) + return false; + if (horizontalAlignment != other.horizontalAlignment) + return false; + if (Double.doubleToLongBits(originX) != Double.doubleToLongBits(other.originX)) + return false; + if (Double.doubleToLongBits(originY) != Double.doubleToLongBits(other.originY)) + return false; + return true; + } + + + + + + + + class TextEditActivation { + + ICanvasContext ctx; + IMouseCaptureHandle capture; + ContextUtil ctxUtil; + Collection originalContexts; + + public TextEditActivation(int mouseId, IElement e, ICanvasContext ctx) { + ElementInteractor ei = ctx.getSingleItem(ElementInteractor.class); + this.ctxUtil = ctx.getSingleItem(ContextUtil.class); + + // Capture mouse events (otherwise we would not get mouse events when the cursor is outside the text widget) + this.capture = ei.captureMouse(e, mouseId); + + // activate in-line editing context + this.originalContexts = ctxUtil.getActivatedContextIds(); + ctxUtil.deactivateAll(); + ctxUtil.activate("org.simantics.modeling.ui.diagramming.inlineEdit"); + } + + public void release() { + capture.release(); + capture = null; + + // de-activate in-line editing context + ctxUtil.deactivateAll(); + ctxUtil.activate(originalContexts); + } + + } + +} \ 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 new file mode 100644 index 00000000..4a432786 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java @@ -0,0 +1,44 @@ +package org.simantics.sysdyn.ui.elements2; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.geom.Rectangle2D; + +import org.simantics.diagram.elements.TextNode; +import org.simantics.scenegraph.utils.NodeUtil; + +public class HoverTextNode extends TextNode { + + private static final long serialVersionUID = 3539499125943249895L; + + transient public boolean hover = false; + + public void setHover(boolean hover) { + this.hover = hover; + repaint(); + } + + @Override + public void render(Graphics2D g) { + Rectangle2D r = getBoundsInLocal(); + + boolean selected = NodeUtil.isSelected(this, 1); + if (!selected && hover){ + + BasicStroke oldStroke = (BasicStroke)g.getStroke(); + Color oldColor = g.getColor(); + g.setColor(Color.LIGHT_GRAY); + g.setStroke(new BasicStroke((float)(2.0f * scale))); + g.translate(x, y); + g.draw(r); + g.translate(-x, -y); + g.setColor(oldColor); + g.setStroke(oldStroke); + } + + super.render(g); + + + } +} 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 4c0da883..005fbfba 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 @@ -12,7 +12,6 @@ import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; -import org.simantics.diagram.elements.TextElementNoBounds; import org.simantics.diagram.elements.TextNode; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.diagram.ui.DiagramModelHints; @@ -77,6 +76,7 @@ public class InputFactory extends SysdynElementFactory { Input.INSTANCE, new InputSceneGraph(0, 0, Alignment.CENTER), BoundsOutline.INSTANCE, + HoverHandler.INSTANCE, new WholeElementTerminals(terminals) ).setId(InputFactory.class.getSimpleName()); } @@ -145,7 +145,7 @@ public class InputFactory extends SysdynElementFactory { } - public static class InputSceneGraph extends TextElementNoBounds implements InternalSize { + public static class InputSceneGraph extends HoverTextElementNoBounds implements InternalSize { private static final long serialVersionUID = -3713275157729126409L; public static final Key INPUT_SG_NODE = new SceneGraphNodeKey(TextNode.class, "INPUT_SG_NODE"); @@ -196,5 +196,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 4976c7dc..1661b9b9 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 @@ -16,7 +16,6 @@ import java.awt.geom.Rectangle2D; import java.util.Collection; import org.simantics.db.Resource; -import org.simantics.diagram.elements.TextElementHandler; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; @@ -49,9 +48,10 @@ public class ModuleFactory extends SysdynElementFactory { new StaticObjectAdapter(elementType), new StaticSymbolImpl(DEFAULT_IMAGE), StaticSymbolImageInitializer.INSTANCE, - TextElementHandler.INSTANCE, + HoverTextElementHandler.INSTANCE, BorderSceneGraph.INSTANCE, BoundsOutline.INSTANCE, + HoverHandler.INSTANCE, new WholeElementTerminals(terminals) ).setId(ModuleFactory.class.getSimpleName()); } 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 4f2e4905..f26a8541 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 @@ -16,7 +16,6 @@ import java.awt.geom.Rectangle2D; import java.util.Collection; import org.simantics.db.Resource; -import org.simantics.diagram.elements.TextElementHandler; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; @@ -49,9 +48,10 @@ public class StockFactory extends SysdynElementFactory { new StaticObjectAdapter(elementType), new StaticSymbolImpl(DEFAULT_IMAGE), StaticSymbolImageInitializer.INSTANCE, - TextElementHandler.INSTANCE, + HoverTextElementHandler.INSTANCE, BorderSceneGraph.INSTANCE, BoundsOutline.INSTANCE, + HoverHandler.INSTANCE, new WholeElementTerminals(terminals) ).setId(StockFactory.class.getSimpleName()); } 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 1863fc62..2797342c 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 @@ -93,6 +93,8 @@ public abstract class SysdynElementFactory extends SyncElementFactory { // This synchronizes only text and transformation (not font and color) e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, SYNCHRONIZER); + + SysdynElementUtils.setHover(e, false); } @Override diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementHints.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementHints.java index 44d5fef4..5c187767 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementHints.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementHints.java @@ -6,4 +6,6 @@ import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; public class SysdynElementHints { public static final Key KEY_INPUT_REFERENCE = new KeyOf(String.class, "INPUT_REFERENCE"); + + public static final Key KEY_HOVER = new KeyOf(Boolean.class, "HOVER"); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementUtils.java index ba1bc733..a77038b3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementUtils.java @@ -17,4 +17,16 @@ public class SysdynElementUtils extends ElementUtils { Input i = e.getElementClass().getSingleItem(Input.class); return i.getInputReference(e); } + + public static void setHover(IElement e, boolean hover) + { + HoverHandler hh = e.getElementClass().getSingleItem(HoverHandler.class); + hh.setHover(e, hover); + } + + public static boolean isHovering(IElement e) + { + HoverHandler hh = e.getElementClass().getSingleItem(HoverHandler.class); + return hh.isHovering(e); + } } 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 a2fb18f9..6b435c33 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 @@ -15,18 +15,15 @@ import java.awt.BasicStroke; import java.awt.Color; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; -import java.awt.geom.Rectangle2D.Double; import java.util.Collection; import org.simantics.db.Resource; -import org.simantics.diagram.elements.TextElementNoBounds; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.SceneGraphNodeKey; -import org.simantics.g2d.element.handler.ElementHandler; +import org.simantics.g2d.element.handler.HandleMouseEvent; import org.simantics.g2d.element.handler.InternalSize; -import org.simantics.g2d.element.handler.SceneGraph; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.ObjectTerminal; @@ -43,6 +40,8 @@ import org.simantics.g2d.image.impl.ShapeImage; import org.simantics.g2d.utils.Alignment; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.utils.datastructures.hints.IHintListener; +import org.simantics.utils.datastructures.hints.IHintObservable; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; @@ -53,8 +52,6 @@ public class ValveFactory extends SysdynElementFactory { public static final double VALVE_SIZE = 2.5; - private static final ElementHandler TEXT_NODE = new TextElementNoBounds(0, VALVE_SIZE + 2, Alignment.CENTER); - private static final BasicStroke STROKE = new BasicStroke(1f); private static final Image VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, STROKE, true); @@ -73,13 +70,13 @@ public class ValveFactory extends SysdynElementFactory { new StaticObjectAdapter(elementType), new StaticSymbolImpl(VALVE_STATIC_IMAGE), StaticSymbolImageInitializer.INSTANCE, - TEXT_NODE, + HoverHandler.INSTANCE, ValveSceneGraph.INSTANCE, BoundsOutline.INSTANCE, new WholeElementTerminals(terminals) ).setId(ValveFactory.class.getSimpleName()); } - + /** * @param valveSize * @param rotated true for vertical valve, false @@ -101,7 +98,7 @@ public class ValveFactory extends SysdynElementFactory { return path; } - public static class ValveSceneGraph implements SceneGraph, InternalSize { + public static class ValveSceneGraph extends HoverTextElementNoBounds implements InternalSize, HandleMouseEvent { private static final long serialVersionUID = 5544256245734478634L; @@ -109,15 +106,38 @@ 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); + } @Override public void init(IElement e, G2DParentNode parent) { - ShapeNode node = ElementUtils.getOrCreateNode(e, parent, NODE, "valve", ShapeNode.class); + super.init(e, parent); + final HoverShapeNode node = ElementUtils.getOrCreateNode(e, parent, NODE, "valve", HoverShapeNode.class); // Calculate borders from text node bounds. node.setStroke(STROKE); node.setScaleStroke(true); node.setColor(Color.BLACK); node.setShape(createShape(VALVE_SIZE, Boolean.TRUE.equals(e.getHint(KEY_ROTATED)))); + node.setHover(SysdynElementUtils.isHovering(e)); + + e.addHintListener(new IHintListener() { + + @Override + public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { + + } + + @Override + public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { + IElement e = (IElement)sender; +// TextNode name = (TextNode) e.getHint(SG_NODE); + HoverShapeNode shape = (HoverShapeNode) e.getHint(NODE); + boolean hover = SysdynElementUtils.isHovering(e); + shape.setHover(hover); +// name.setHover(hover); + } + }); } @Override -- 2.47.1