]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/SymbolMonitorClass.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / SymbolMonitorClass.java
diff --git a/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/SymbolMonitorClass.java b/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/SymbolMonitorClass.java
new file mode 100644 (file)
index 0000000..96be9b1
--- /dev/null
@@ -0,0 +1,263 @@
+/*******************************************************************************\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.scenegraph.swing;\r
+\r
+import java.awt.Rectangle;\r
+import java.awt.Shape;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.Collection;\r
+import java.util.EnumSet;\r
+import java.util.Map;\r
+\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementHints;\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.InternalSize;\r
+import org.simantics.g2d.element.handler.Outline;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.g2d.element.handler.StaticSymbol;\r
+import org.simantics.g2d.element.handler.Transform;\r
+import org.simantics.g2d.element.handler.impl.SimpleElementLayers;\r
+import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;\r
+import org.simantics.g2d.elementclass.MonitorHandler;\r
+import org.simantics.g2d.image.Image;\r
+import org.simantics.g2d.image.ProviderUtils;\r
+import org.simantics.g2d.image.impl.AbstractImage;\r
+import org.simantics.scenegraph.Node;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.utils.datastructures.cache.IFactory;\r
+import org.simantics.utils.datastructures.cache.IProvider;\r
+import org.simantics.utils.datastructures.cache.ProvisionException;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SymbolMonitorClass {\r
+\r
+    public static final Key  KEY_SG_NODE             = new SceneGraphNodeKey(Node.class, "SYMBOL_MONITOR_SG_NODE");\r
+\r
+    public static class MonitorHandlerImpl implements MonitorHandler {\r
+        private static final long          serialVersionUID = -4258875745321808416L;\r
+        public static final MonitorHandler INSTANCE         = new MonitorHandlerImpl();\r
+    }\r
+\r
+    public static void update(IElement e) {\r
+        SymbolMonitorSGNode node = e.getElementClass().getAtMostOneItemOfClass(SymbolMonitorSGNode.class);\r
+        node.update(e);\r
+    }\r
+\r
+    public static void cleanup(IElement e) {\r
+        SymbolMonitorSGNode node = e.getElementClass().getAtMostOneItemOfClass(SymbolMonitorSGNode.class);\r
+        node.cleanup(e);\r
+    }\r
+\r
+    static final Rectangle2D DEFAULT_BOX = new Rectangle2D.Double(0, 0, 0, 0);\r
+\r
+    static Shape createMonitor(IElement e) {\r
+\r
+        return DEFAULT_BOX;\r
+\r
+    }\r
+\r
+    static Path2D makePath(double x, double y, double w, double h) {\r
+        Path2D path = new Path2D.Double();\r
+        path.moveTo(x, y);\r
+        path.lineTo(x+w, y);\r
+        path.lineTo(x+w, y+h);\r
+        path.lineTo(x, y+h);\r
+        path.closePath();\r
+        return path;\r
+    }\r
+\r
+    public static final Shape BOX_SHAPE = new Rectangle(-1, -1, 2, 2);\r
+\r
+    public static class SymbolMonitorSGNode implements SceneGraph, InternalSize, Outline {\r
+\r
+        private static final long serialVersionUID = -106278359626957687L;\r
+\r
+        static final SymbolMonitorSGNode INSTANCE = new SymbolMonitorSGNode();\r
+\r
+        @Override\r
+        public void init(final IElement e, final G2DParentNode parent) {\r
+            // Create node if it doesn't exist yet\r
+            SymbolMonitorNode node = (SymbolMonitorNode)e.getHint(KEY_SG_NODE);\r
+            if(node == null || node.getBounds() == null || node.getParent() != parent) {\r
+                // FIXME : symbol monitor node is not disposed properly, so currently this code is commented out.\r
+               //node = parent.addNode(ElementUtils.generateNodeId(e), SymbolMonitorNode.class);\r
+                //e.setHint(KEY_SG_NODE, node);\r
+            }\r
+            update(e);\r
+        }\r
+\r
+        public void update(IElement e) {\r
+        }\r
+\r
+        @Override\r
+        public void cleanup(IElement e) {\r
+            SymbolMonitorNode node = (SymbolMonitorNode)e.removeHint(KEY_SG_NODE);\r
+            if (node != null)\r
+                node.remove();\r
+        }\r
+\r
+        @Override\r
+        public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
+            Rectangle2D shape = new Rectangle2D.Double(0, 0, 0, 0);\r
+\r
+            SymbolMonitorNode node = (SymbolMonitorNode)e.getHint(KEY_SG_NODE);\r
+            if(node != null && node.getBounds() != null) {\r
+                shape = node.getBounds().getBounds2D();\r
+            }\r
+\r
+            if(size != null) size.setRect(shape);\r
+            return shape;\r
+        }\r
+\r
+        @Override\r
+        public Shape getElementShape(IElement e) {\r
+            Shape shape = new Rectangle2D.Double(0, 0, 0, 0);\r
+\r
+            SymbolMonitorNode node = (SymbolMonitorNode)e.getHint(KEY_SG_NODE);\r
+            if(node != null && node.getBounds() != null) {\r
+                shape = node.getBounds();\r
+            }\r
+\r
+            return shape;\r
+        }\r
+\r
+    }\r
+\r
+    public static class Transformer implements Transform {\r
+\r
+        private static final long serialVersionUID = -3704887325602085677L;\r
+\r
+        public static final Transformer INSTANCE = new Transformer(null);\r
+\r
+        Double aspectRatio;\r
+\r
+        public Transformer() {\r
+            this(null);\r
+        }\r
+\r
+        public Transformer(Double aspectRatio) {\r
+            this.aspectRatio = aspectRatio;\r
+        }\r
+\r
+        @Override\r
+        public AffineTransform getTransform(IElement e) {\r
+            AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);\r
+\r
+            IElement parentElement = e.getHint(ElementHints.KEY_PARENT_ELEMENT);\r
+            if (parentElement == null)\r
+                return at;\r
+\r
+            Transform parentTransform = parentElement.getElementClass().getSingleItem(Transform.class);\r
+            assert(parentTransform!=null);\r
+\r
+            AffineTransform result = (AffineTransform)at.clone();\r
+            result.preConcatenate(parentTransform.getTransform(parentElement));\r
+\r
+            return result;\r
+        }\r
+\r
+        @Override\r
+        public void setTransform(IElement e, AffineTransform at) {\r
+            e.setHint(ElementHints.KEY_TRANSFORM, at.clone());\r
+        }\r
+\r
+    }\r
+\r
+    static class MonitorImageFactory implements IFactory<Image> {\r
+\r
+        private double staticScaleX = 1, staticScaleY = 1;\r
+\r
+        public MonitorImageFactory(double staticScaleX, double staticScaleY) {\r
+            this.staticScaleX = staticScaleX;\r
+            this.staticScaleY = staticScaleY;\r
+        }\r
+\r
+        @Override\r
+        public Image get() throws ProvisionException {\r
+\r
+            return new AbstractImage() {\r
+\r
+                Shape path = BOX_SHAPE;\r
+\r
+                @Override\r
+                public Rectangle2D getBounds() {\r
+                    return path.getBounds2D();\r
+                }\r
+\r
+                @Override\r
+                public EnumSet<Feature> getFeatures() {\r
+                    return EnumSet.of(Feature.Vector);\r
+                }\r
+\r
+                @Override\r
+                public Shape getOutline() {\r
+                    return path;\r
+                }\r
+\r
+                @Override\r
+                public Node init(G2DParentNode parent) {\r
+                    SymbolMonitorNode node = parent.getOrCreateNode(""+hashCode(), SymbolMonitorNode.class);\r
+                    node.setText("");\r
+                    node.setBounds(new Rectangle2D.Double(0, 0, 50, 22));\r
+                    node.setText("Drop Me");\r
+                    node.setTransform(AffineTransform.getScaleInstance(staticScaleX, staticScaleY));\r
+                    return node;\r
+                }\r
+            };\r
+        }\r
+    }\r
+\r
+    static final IProvider<Image> MONITOR_IMAGE =\r
+        ProviderUtils.reference(\r
+                ProviderUtils.cache(\r
+                        ProviderUtils.rasterize(\r
+                                new MonitorImageFactory(0.5, 0.5)\r
+                        )));\r
+\r
+    static final StaticSymbol MONITOR_SYMBOL = new StaticSymbolImpl( MONITOR_IMAGE.get() );\r
+\r
+    public static final ElementClass MONITOR_CLASS =\r
+        ElementClass.compile(\r
+                MonitorHandlerImpl.INSTANCE,\r
+                Transformer.INSTANCE,\r
+                SymbolMonitorSGNode.INSTANCE,\r
+                SimpleElementLayers.INSTANCE,\r
+                MONITOR_SYMBOL\r
+        );\r
+\r
+    // staticScale{X,Y} define the scale of the static monitor image\r
+    public static ElementClass create(IElement parentElement, Map<String, String> substitutions, Collection<Object> path, double staticScaleX, double staticScaleY, ElementHandler... extraHandlers) {\r
+        // Bit of a hack to be able to define the scale\r
+        IProvider<Image> staticMonitorSymbolProvider = ProviderUtils.reference(\r
+                ProviderUtils.cache(\r
+                        ProviderUtils\r
+                        .rasterize(\r
+                                new MonitorImageFactory(staticScaleX, staticScaleY))));\r
+        StaticSymbol staticMonitorSymbol = new StaticSymbolImpl( staticMonitorSymbolProvider.get() );\r
+        return ElementClass.compile(\r
+                MonitorHandlerImpl.INSTANCE,\r
+                Transformer.INSTANCE,\r
+                SymbolMonitorSGNode.INSTANCE,\r
+                SimpleElementLayers.INSTANCE,\r
+                staticMonitorSymbol\r
+        ).newClassWith(extraHandlers);\r
+    }\r
+\r
+}\r