]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.elementclass;\r
13 \r
14 import java.awt.BasicStroke;\r
15 import java.awt.Color;\r
16 import java.awt.Shape;\r
17 import java.awt.geom.AffineTransform;\r
18 import java.awt.geom.Ellipse2D;\r
19 import java.awt.geom.Rectangle2D;\r
20 \r
21 import org.simantics.g2d.element.ElementClass;\r
22 import org.simantics.g2d.element.ElementUtils;\r
23 import org.simantics.g2d.element.IElement;\r
24 import org.simantics.g2d.element.SceneGraphNodeKey;\r
25 import org.simantics.g2d.element.handler.InternalSize;\r
26 import org.simantics.g2d.element.handler.Outline;\r
27 import org.simantics.g2d.element.handler.SceneGraph;\r
28 import org.simantics.g2d.element.handler.impl.BorderColorImpl;\r
29 import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
30 import org.simantics.g2d.element.handler.impl.FillColorImpl;\r
31 import org.simantics.g2d.element.handler.impl.TextImpl;\r
32 import org.simantics.g2d.element.handler.impl.TextPainter;\r
33 import org.simantics.g2d.elementclass.wheel.RotatorHandler;\r
34 import org.simantics.scenegraph.Node;\r
35 import org.simantics.scenegraph.g2d.G2DParentNode;\r
36 import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
37 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
38 \r
39 /**\r
40  * @author Toni Kalajainen\r
41  */\r
42 public class ShapeClass {\r
43 \r
44     final static BasicStroke STROKE = new BasicStroke(1.0f);\r
45 \r
46     public static final ElementClass CIRCLE_CLASS = getShapeClass(new Ellipse2D.Double(-10, -10, 20, 20));\r
47 \r
48     public static ElementClass getShapeClass(Shape s)\r
49     {\r
50         return\r
51         ElementClass.compile(\r
52                 DefaultTransform.INSTANCE,\r
53                 BorderColorImpl.BLACK,\r
54                 FillColorImpl.WHITE,\r
55                 TextImpl.INSTANCE,\r
56                 new ShapePainter(s),\r
57                 new TextPainter(),\r
58                 new RotatorHandler()\r
59         );\r
60 \r
61     }\r
62 \r
63     static class ShapePainter implements SceneGraph, Outline, InternalSize\r
64     {\r
65         /**\r
66          * \r
67          */\r
68         private static final long serialVersionUID = 2328124349273536527L;\r
69         Shape shape;\r
70         Rectangle2D bounds;\r
71 \r
72         public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
73 \r
74         public ShapePainter(Shape shape) {\r
75             this.shape = shape;\r
76             this.bounds = shape.getBounds2D();\r
77         }\r
78 \r
79         @Override\r
80         public void cleanup(IElement e) {\r
81             Node node = e.removeHint(SG_NODE);\r
82             if (node != null)\r
83                 node.remove();\r
84         }\r
85 \r
86         @Override\r
87         public void init(IElement e, G2DParentNode parent) {\r
88             G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);\r
89             if (node == null) {\r
90                 node = parent.addNode(G2DParentNode.class);\r
91                 e.setHint(SG_NODE, node);\r
92             }\r
93                         AffineTransform at      = ElementUtils.getTransform(e);\r
94                         if(at != null) node.setTransform(at);\r
95 \r
96             ShapeNode bgnode = node.getOrCreateNode("bgnode", ShapeNode.class);\r
97             ShapeNode fgnode = node.getOrCreateNode("fgnode", ShapeNode.class);\r
98 \r
99             Color fc = ElementUtils.getFillColor(e);\r
100             Color bc = ElementUtils.getBorderColor(e);\r
101 \r
102             fgnode.setColor(fc);\r
103             fgnode.setFill(true);\r
104             fgnode.setShape(shape);\r
105 \r
106             bgnode.setColor(bc);\r
107             bgnode.setStroke(STROKE);\r
108             bgnode.setScaleStroke(true);\r
109             bgnode.setShape(shape);\r
110         }\r
111 \r
112         @Override\r
113         public Shape getElementShape(IElement e) {\r
114             return shape;\r
115         }\r
116 \r
117         @Override\r
118         public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
119             if (size==null) size = new Rectangle2D.Double();\r
120             size.setFrame(bounds);\r
121             return size;\r
122         }\r
123     }\r
124 }\r