]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ShapeClass.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / ShapeClass.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ShapeClass.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/ShapeClass.java
new file mode 100644 (file)
index 0000000..0afa2bc
--- /dev/null
@@ -0,0 +1,124 @@
+/*******************************************************************************\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.Shape;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Ellipse2D;\r
+import java.awt.geom.Rectangle2D;\r
+\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.InternalSize;\r
+import org.simantics.g2d.element.handler.Outline;\r
+import org.simantics.g2d.element.handler.SceneGraph;\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.TextImpl;\r
+import org.simantics.g2d.element.handler.impl.TextPainter;\r
+import org.simantics.g2d.elementclass.wheel.RotatorHandler;\r
+import org.simantics.scenegraph.Node;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * @author Toni Kalajainen\r
+ */\r
+public class ShapeClass {\r
+\r
+    final static BasicStroke STROKE = new BasicStroke(1.0f);\r
+\r
+    public static final ElementClass CIRCLE_CLASS = getShapeClass(new Ellipse2D.Double(-10, -10, 20, 20));\r
+\r
+    public static ElementClass getShapeClass(Shape s)\r
+    {\r
+        return\r
+        ElementClass.compile(\r
+                DefaultTransform.INSTANCE,\r
+                BorderColorImpl.BLACK,\r
+                FillColorImpl.WHITE,\r
+                TextImpl.INSTANCE,\r
+                new ShapePainter(s),\r
+                new TextPainter(),\r
+                new RotatorHandler()\r
+        );\r
+\r
+    }\r
+\r
+    static class ShapePainter implements SceneGraph, Outline, InternalSize\r
+    {\r
+        /**\r
+         * \r
+         */\r
+        private static final long serialVersionUID = 2328124349273536527L;\r
+        Shape shape;\r
+        Rectangle2D bounds;\r
+\r
+        public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
+\r
+        public ShapePainter(Shape shape) {\r
+            this.shape = shape;\r
+            this.bounds = shape.getBounds2D();\r
+        }\r
+\r
+        @Override\r
+        public void cleanup(IElement e) {\r
+            Node node = e.removeHint(SG_NODE);\r
+            if (node != null)\r
+                node.remove();\r
+        }\r
+\r
+        @Override\r
+        public void init(IElement e, G2DParentNode parent) {\r
+            G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);\r
+            if (node == null) {\r
+                node = parent.addNode(G2DParentNode.class);\r
+                e.setHint(SG_NODE, node);\r
+            }\r
+                       AffineTransform at      = ElementUtils.getTransform(e);\r
+                       if(at != null) node.setTransform(at);\r
+\r
+            ShapeNode bgnode = node.getOrCreateNode("bgnode", ShapeNode.class);\r
+            ShapeNode fgnode = node.getOrCreateNode("fgnode", ShapeNode.class);\r
+\r
+            Color fc = ElementUtils.getFillColor(e);\r
+            Color bc = ElementUtils.getBorderColor(e);\r
+\r
+            fgnode.setColor(fc);\r
+            fgnode.setFill(true);\r
+            fgnode.setShape(shape);\r
+\r
+            bgnode.setColor(bc);\r
+            bgnode.setStroke(STROKE);\r
+            bgnode.setScaleStroke(true);\r
+            bgnode.setShape(shape);\r
+        }\r
+\r
+        @Override\r
+        public Shape getElementShape(IElement e) {\r
+            return shape;\r
+        }\r
+\r
+        @Override\r
+        public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
+            if (size==null) size = new Rectangle2D.Double();\r
+            size.setFrame(bounds);\r
+            return size;\r
+        }\r
+    }\r
+}\r