]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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.Rectangle;\r
17 import java.awt.Shape;\r
18 import java.awt.geom.AffineTransform;\r
19 import java.awt.geom.Ellipse2D;\r
20 import java.awt.geom.Point2D;\r
21 import java.awt.geom.Rectangle2D;\r
22 import java.util.Collection;\r
23 \r
24 import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
25 import org.simantics.g2d.element.ElementClass;\r
26 import org.simantics.g2d.element.ElementUtils;\r
27 import org.simantics.g2d.element.IElement;\r
28 import org.simantics.g2d.element.handler.InternalSize;\r
29 import org.simantics.g2d.element.handler.SceneGraph;\r
30 import org.simantics.g2d.element.handler.TerminalLayout;\r
31 import org.simantics.g2d.element.handler.TerminalTopology;\r
32 import org.simantics.g2d.element.handler.impl.BorderColorImpl;\r
33 import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
34 import org.simantics.g2d.element.handler.impl.FillColorImpl;\r
35 import org.simantics.g2d.element.handler.impl.Resizeable;\r
36 import org.simantics.g2d.element.handler.impl.TextImpl;\r
37 import org.simantics.g2d.element.handler.impl.TextPainter;\r
38 import org.simantics.g2d.utils.geom.DirectionSet;\r
39 import org.simantics.scenegraph.g2d.G2DParentNode;\r
40 import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
41 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
42 \r
43 /**\r
44  * \r
45  * @author Toni Kalajainen\r
46  */\r
47 public class BoxClass  {\r
48 \r
49     public static final ElementClass BOXCLASS =\r
50         ElementClass.compile(\r
51                 DefaultTransform.INSTANCE,\r
52                 Resizeable.UNCONSTRICTED,\r
53                 BorderColorImpl.BLACK,\r
54                 FillColorImpl.WHITE,\r
55                 TextImpl.INSTANCE,\r
56                 new RectangleSGNode(),\r
57                 new BoxNode(),\r
58                 new TextPainter()\r
59                 //new RotatorHandler()\r
60         );\r
61 \r
62     final static BasicStroke STROKE = new BasicStroke(1.0f);\r
63 \r
64     static class RectangleSGNode implements SceneGraph {\r
65         /**\r
66          * \r
67          */\r
68         private static final long serialVersionUID = -1550908516884986157L;\r
69         private G2DParentNode node = null;\r
70 \r
71         @Override\r
72         public void cleanup(IElement e) {\r
73             if(node != null) {\r
74                 node.remove();\r
75             }\r
76             node = null;\r
77         }\r
78 \r
79         @Override\r
80         public void init(IElement e, G2DParentNode parent) {\r
81             if(node == null) {\r
82                 node = parent.addNode(G2DParentNode.class);\r
83             }\r
84             ShapeNode bgnode = node.getOrCreateNode("bg", ShapeNode.class);\r
85             bgnode.setZIndex(1);\r
86             ShapeNode fgnode = node.getOrCreateNode("fg", ShapeNode.class);\r
87             fgnode.setZIndex(2);\r
88 \r
89             Color fc = ElementUtils.getFillColor(e);\r
90             Color bc = ElementUtils.getBorderColor(e);\r
91 \r
92             Rectangle2D rect = ElementUtils.getElementBounds(e);\r
93             bgnode.setColor(fc);\r
94             bgnode.setFill(true);\r
95             bgnode.setShape(rect);\r
96 \r
97             fgnode.setColor(bc);\r
98             fgnode.setFill(false);\r
99             fgnode.setStroke(STROKE);\r
100             fgnode.setScaleStroke(true);\r
101             fgnode.setShape(rect);\r
102         }\r
103     }\r
104 \r
105 \r
106 \r
107     static class TerminalPoint implements Terminal {\r
108     }\r
109 \r
110     public static class TerminalKeyOf extends KeyOf {\r
111         public final Terminal terminal;\r
112 \r
113         private final int hashCode;\r
114 \r
115         public TerminalKeyOf(Terminal terminal, Class<?> clazz) {\r
116             super(clazz);\r
117             this.terminal = terminal;\r
118             hashCode = terminal.hashCode() ^ clazz.hashCode();\r
119         }\r
120 \r
121         @Override\r
122         public int hashCode() {\r
123             return hashCode;\r
124         }\r
125 \r
126         @Override\r
127         public boolean equals(Object obj) {\r
128             if (!(obj instanceof TerminalKeyOf)) return false;\r
129             TerminalKeyOf other = (TerminalKeyOf) obj;\r
130             if (!other.terminal.equals(terminal)) return false;\r
131             return super.equals(obj);\r
132         }\r
133     }\r
134 \r
135     public static class TerminalConnectKey extends TerminalKeyOf {\r
136         public TerminalConnectKey(Terminal terminal) {\r
137             super(terminal, IElement.class);\r
138         }\r
139 \r
140     }\r
141 \r
142     static class BoxNode implements TerminalTopology, TerminalLayout {\r
143         private static final long serialVersionUID = 4561446983606561224L;\r
144         public static final Terminal T0 = new TerminalPoint();\r
145         public static final Terminal T1 = new TerminalPoint();\r
146         public static final Terminal T2 = new TerminalPoint();\r
147         public static final Terminal T3 = new TerminalPoint();\r
148         public static final Terminal T4 = new TerminalPoint();\r
149         public static final Terminal T5 = new TerminalPoint();\r
150         public static final Terminal T6 = new TerminalPoint();\r
151         public static final Terminal T7 = new TerminalPoint();\r
152         public static final Terminal T8 = new TerminalPoint();\r
153 \r
154         @Override\r
155         public void getTerminals(IElement e, Collection<Terminal> result) {\r
156             result.add(T0);\r
157             result.add(T1);\r
158             result.add(T2);\r
159             result.add(T3);\r
160             result.add(T4);\r
161             result.add(T5);\r
162             result.add(T6);\r
163             result.add(T7);\r
164             result.add(T8);\r
165         }\r
166 \r
167         @Override\r
168         public AffineTransform getTerminalPosition(IElement node, Terminal t)\r
169         {\r
170             InternalSize b = node.getElementClass().getSingleItem(InternalSize.class);\r
171             Rectangle2D rect = b.getBounds(node, null);\r
172             Point2D pos = null;\r
173             if (rect == null) {\r
174                 pos = new Point2D.Double(0, 0);\r
175             } else {\r
176                 if (t==T0) pos = new Point2D.Double(rect.getCenterX(), rect.getCenterY());\r
177                 if (t==T1) pos = new Point2D.Double(rect.getMinX(), rect.getMinY());\r
178                 if (t==T2) pos = new Point2D.Double(rect.getCenterX(), rect.getMinY());\r
179                 if (t==T3) pos = new Point2D.Double(rect.getMaxX(), rect.getMinY());\r
180                 if (t==T4) pos = new Point2D.Double(rect.getMaxX(), rect.getCenterY());\r
181                 if (t==T5) pos = new Point2D.Double(rect.getMaxX(), rect.getMaxY());\r
182                 if (t==T6) pos = new Point2D.Double(rect.getCenterX(), rect.getMaxY());\r
183                 if (t==T7) pos = new Point2D.Double(rect.getMinX(), rect.getMaxY());\r
184                 if (t==T8) pos = new Point2D.Double(rect.getMinX(), rect.getCenterY());\r
185             }\r
186             AffineTransform result = new AffineTransform();\r
187             result.setToTranslation(pos.getX(), pos.getY());\r
188             return result;\r
189         }\r
190 \r
191         @Override\r
192         public boolean getTerminalDirection(IElement node, Terminal t, DirectionSet directions) {\r
193             if (t==T0) {\r
194                 directions.addAll(DirectionSet.NESW);\r
195                 return true;\r
196             }\r
197             if (t==T1) {\r
198                 directions.addAll(DirectionSet.NW2);\r
199                 return true;\r
200             }\r
201             if (t==T2) {\r
202                 directions.addAll(DirectionSet.N);\r
203                 return true;\r
204             }\r
205             if (t==T3) {\r
206                 directions.addAll(DirectionSet.NE2);\r
207                 return true;\r
208             }\r
209             if (t==T4) {\r
210                 directions.addAll(DirectionSet.E);\r
211                 return true;\r
212             }\r
213             if (t==T5) {\r
214                 directions.addAll(DirectionSet.SE2);\r
215                 return true;\r
216             }\r
217             if (t==T6) {\r
218                 directions.addAll(DirectionSet.S);\r
219                 return true;\r
220             }\r
221             if (t==T7) {\r
222                 directions.addAll(DirectionSet.SW2);\r
223                 return true;\r
224             }\r
225             if (t==T8) {\r
226                 directions.addAll(DirectionSet.W);\r
227                 return true;\r
228             }\r
229             return false;\r
230         }\r
231 \r
232         public static final Shape BOX_SHAPE = new Rectangle(-3, -3, 7, 7);\r
233         public static final Shape CIRCLE_SHAPE = new Ellipse2D.Double(-5, -5, 9, 9);\r
234 \r
235         @Override\r
236         public Shape getTerminalShape(IElement node, Terminal t) {\r
237             return (t==T6 || t==T7 || t==T0) ? CIRCLE_SHAPE : null;\r
238         }\r
239 \r
240     }\r
241 \r
242 }\r