]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Attempting to implement some kind of hover support
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 28 Jul 2010 15:00:27 +0000 (15:00 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 28 Jul 2010 15:00:27 +0000 (15:00 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16863 ac1ea38d-2e2b-0410-8846-a27921b304fc

14 files changed:
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverShapeNode.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextNode.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ModuleFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementHints.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementUtils.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java

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