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