]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/BoxClass.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / BoxClass.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/BoxClass.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/BoxClass.java
new file mode 100644 (file)
index 0000000..237d441
--- /dev/null
@@ -0,0 +1,242 @@
+/*******************************************************************************\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.g2d.elementclass;\r
+\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Rectangle;\r
+import java.awt.Shape;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Ellipse2D;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.Collection;\r
+\r
+import org.simantics.g2d.diagram.handler.Topology.Terminal;\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.handler.InternalSize;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.g2d.element.handler.TerminalLayout;\r
+import org.simantics.g2d.element.handler.TerminalTopology;\r
+import org.simantics.g2d.element.handler.impl.BorderColorImpl;\r
+import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
+import org.simantics.g2d.element.handler.impl.FillColorImpl;\r
+import org.simantics.g2d.element.handler.impl.Resizeable;\r
+import org.simantics.g2d.element.handler.impl.TextImpl;\r
+import org.simantics.g2d.element.handler.impl.TextPainter;\r
+import org.simantics.g2d.utils.geom.DirectionSet;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+/**\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class BoxClass  {\r
+\r
+    public static final ElementClass BOXCLASS =\r
+        ElementClass.compile(\r
+                DefaultTransform.INSTANCE,\r
+                Resizeable.UNCONSTRICTED,\r
+                BorderColorImpl.BLACK,\r
+                FillColorImpl.WHITE,\r
+                TextImpl.INSTANCE,\r
+                new RectangleSGNode(),\r
+                new BoxNode(),\r
+                new TextPainter()\r
+                //new RotatorHandler()\r
+        );\r
+\r
+    final static BasicStroke STROKE = new BasicStroke(1.0f);\r
+\r
+    static class RectangleSGNode implements SceneGraph {\r
+        /**\r
+         * \r
+         */\r
+        private static final long serialVersionUID = -1550908516884986157L;\r
+        private G2DParentNode node = null;\r
+\r
+        @Override\r
+        public void cleanup(IElement e) {\r
+            if(node != null) {\r
+                node.remove();\r
+            }\r
+            node = null;\r
+        }\r
+\r
+        @Override\r
+        public void init(IElement e, G2DParentNode parent) {\r
+            if(node == null) {\r
+                node = parent.addNode(G2DParentNode.class);\r
+            }\r
+            ShapeNode bgnode = node.getOrCreateNode("bg", ShapeNode.class);\r
+            bgnode.setZIndex(1);\r
+            ShapeNode fgnode = node.getOrCreateNode("fg", ShapeNode.class);\r
+            fgnode.setZIndex(2);\r
+\r
+            Color fc = ElementUtils.getFillColor(e);\r
+            Color bc = ElementUtils.getBorderColor(e);\r
+\r
+            Rectangle2D rect = ElementUtils.getElementBounds(e);\r
+            bgnode.setColor(fc);\r
+            bgnode.setFill(true);\r
+            bgnode.setShape(rect);\r
+\r
+            fgnode.setColor(bc);\r
+            fgnode.setFill(false);\r
+            fgnode.setStroke(STROKE);\r
+            fgnode.setScaleStroke(true);\r
+            fgnode.setShape(rect);\r
+        }\r
+    }\r
+\r
+\r
+\r
+    static class TerminalPoint implements Terminal {\r
+    }\r
+\r
+    public static class TerminalKeyOf extends KeyOf {\r
+        public final Terminal terminal;\r
+\r
+        private final int hashCode;\r
+\r
+        public TerminalKeyOf(Terminal terminal, Class<?> clazz) {\r
+            super(clazz);\r
+            this.terminal = terminal;\r
+            hashCode = terminal.hashCode() ^ clazz.hashCode();\r
+        }\r
+\r
+        @Override\r
+        public int hashCode() {\r
+            return hashCode;\r
+        }\r
+\r
+        @Override\r
+        public boolean equals(Object obj) {\r
+            if (!(obj instanceof TerminalKeyOf)) return false;\r
+            TerminalKeyOf other = (TerminalKeyOf) obj;\r
+            if (!other.terminal.equals(terminal)) return false;\r
+            return super.equals(obj);\r
+        }\r
+    }\r
+\r
+    public static class TerminalConnectKey extends TerminalKeyOf {\r
+        public TerminalConnectKey(Terminal terminal) {\r
+            super(terminal, IElement.class);\r
+        }\r
+\r
+    }\r
+\r
+    static class BoxNode implements TerminalTopology, TerminalLayout {\r
+        private static final long serialVersionUID = 4561446983606561224L;\r
+        public static final Terminal T0 = new TerminalPoint();\r
+        public static final Terminal T1 = new TerminalPoint();\r
+        public static final Terminal T2 = new TerminalPoint();\r
+        public static final Terminal T3 = new TerminalPoint();\r
+        public static final Terminal T4 = new TerminalPoint();\r
+        public static final Terminal T5 = new TerminalPoint();\r
+        public static final Terminal T6 = new TerminalPoint();\r
+        public static final Terminal T7 = new TerminalPoint();\r
+        public static final Terminal T8 = new TerminalPoint();\r
+\r
+        @Override\r
+        public void getTerminals(IElement e, Collection<Terminal> result) {\r
+            result.add(T0);\r
+            result.add(T1);\r
+            result.add(T2);\r
+            result.add(T3);\r
+            result.add(T4);\r
+            result.add(T5);\r
+            result.add(T6);\r
+            result.add(T7);\r
+            result.add(T8);\r
+        }\r
+\r
+        @Override\r
+        public AffineTransform getTerminalPosition(IElement node, Terminal t)\r
+        {\r
+            InternalSize b = node.getElementClass().getSingleItem(InternalSize.class);\r
+            Rectangle2D rect = b.getBounds(node, null);\r
+            Point2D pos = null;\r
+            if (rect == null) {\r
+                pos = new Point2D.Double(0, 0);\r
+            } else {\r
+                if (t==T0) pos = new Point2D.Double(rect.getCenterX(), rect.getCenterY());\r
+                if (t==T1) pos = new Point2D.Double(rect.getMinX(), rect.getMinY());\r
+                if (t==T2) pos = new Point2D.Double(rect.getCenterX(), rect.getMinY());\r
+                if (t==T3) pos = new Point2D.Double(rect.getMaxX(), rect.getMinY());\r
+                if (t==T4) pos = new Point2D.Double(rect.getMaxX(), rect.getCenterY());\r
+                if (t==T5) pos = new Point2D.Double(rect.getMaxX(), rect.getMaxY());\r
+                if (t==T6) pos = new Point2D.Double(rect.getCenterX(), rect.getMaxY());\r
+                if (t==T7) pos = new Point2D.Double(rect.getMinX(), rect.getMaxY());\r
+                if (t==T8) pos = new Point2D.Double(rect.getMinX(), rect.getCenterY());\r
+            }\r
+            AffineTransform result = new AffineTransform();\r
+            result.setToTranslation(pos.getX(), pos.getY());\r
+            return result;\r
+        }\r
+\r
+        @Override\r
+        public boolean getTerminalDirection(IElement node, Terminal t, DirectionSet directions) {\r
+            if (t==T0) {\r
+                directions.addAll(DirectionSet.NESW);\r
+                return true;\r
+            }\r
+            if (t==T1) {\r
+                directions.addAll(DirectionSet.NW2);\r
+                return true;\r
+            }\r
+            if (t==T2) {\r
+                directions.addAll(DirectionSet.N);\r
+                return true;\r
+            }\r
+            if (t==T3) {\r
+                directions.addAll(DirectionSet.NE2);\r
+                return true;\r
+            }\r
+            if (t==T4) {\r
+                directions.addAll(DirectionSet.E);\r
+                return true;\r
+            }\r
+            if (t==T5) {\r
+                directions.addAll(DirectionSet.SE2);\r
+                return true;\r
+            }\r
+            if (t==T6) {\r
+                directions.addAll(DirectionSet.S);\r
+                return true;\r
+            }\r
+            if (t==T7) {\r
+                directions.addAll(DirectionSet.SW2);\r
+                return true;\r
+            }\r
+            if (t==T8) {\r
+                directions.addAll(DirectionSet.W);\r
+                return true;\r
+            }\r
+            return false;\r
+        }\r
+\r
+        public static final Shape BOX_SHAPE = new Rectangle(-3, -3, 7, 7);\r
+        public static final Shape CIRCLE_SHAPE = new Ellipse2D.Double(-5, -5, 9, 9);\r
+\r
+        @Override\r
+        public Shape getTerminalShape(IElement node, Terminal t) {\r
+            return (t==T6 || t==T7 || t==T0) ? CIRCLE_SHAPE : null;\r
+        }\r
+\r
+    }\r
+\r
+}\r