1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.elementclass;
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;
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;
45 * @author Toni Kalajainen
47 public class BoxClass {
49 public static final ElementClass BOXCLASS =
51 DefaultTransform.INSTANCE,
52 Resizeable.UNCONSTRICTED,
53 BorderColorImpl.BLACK,
56 new RectangleSGNode(),
59 //new RotatorHandler()
62 final static BasicStroke STROKE = new BasicStroke(1.0f);
64 static class RectangleSGNode implements SceneGraph {
68 private static final long serialVersionUID = -1550908516884986157L;
69 private G2DParentNode node = null;
72 public void cleanup(IElement e) {
80 public void init(IElement e, G2DParentNode parent) {
82 node = parent.addNode(G2DParentNode.class);
84 ShapeNode bgnode = node.getOrCreateNode("bg", ShapeNode.class);
86 ShapeNode fgnode = node.getOrCreateNode("fg", ShapeNode.class);
89 Color fc = ElementUtils.getFillColor(e);
90 Color bc = ElementUtils.getBorderColor(e);
92 Rectangle2D rect = ElementUtils.getElementBounds(e);
95 bgnode.setShape(rect);
98 fgnode.setFill(false);
99 fgnode.setStroke(STROKE);
100 fgnode.setScaleStroke(true);
101 fgnode.setShape(rect);
107 static class TerminalPoint implements Terminal {
110 public static class TerminalKeyOf extends KeyOf {
111 public final Terminal terminal;
113 private final int hashCode;
115 public TerminalKeyOf(Terminal terminal, Class<?> clazz) {
117 this.terminal = terminal;
118 hashCode = terminal.hashCode() ^ clazz.hashCode();
122 public int hashCode() {
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);
135 public static class TerminalConnectKey extends TerminalKeyOf {
136 public TerminalConnectKey(Terminal terminal) {
137 super(terminal, IElement.class);
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();
155 public void getTerminals(IElement e, Collection<Terminal> result) {
168 public AffineTransform getTerminalPosition(IElement node, Terminal t)
170 InternalSize b = node.getElementClass().getSingleItem(InternalSize.class);
171 Rectangle2D rect = b.getBounds(node, null);
174 pos = new Point2D.Double(0, 0);
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());
186 AffineTransform result = new AffineTransform();
187 result.setToTranslation(pos.getX(), pos.getY());
192 public boolean getTerminalDirection(IElement node, Terminal t, DirectionSet directions) {
194 directions.addAll(DirectionSet.NESW);
198 directions.addAll(DirectionSet.NW2);
202 directions.addAll(DirectionSet.N);
206 directions.addAll(DirectionSet.NE2);
210 directions.addAll(DirectionSet.E);
214 directions.addAll(DirectionSet.SE2);
218 directions.addAll(DirectionSet.S);
222 directions.addAll(DirectionSet.SW2);
226 directions.addAll(DirectionSet.W);
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);
236 public Shape getTerminalShape(IElement node, Terminal t) {
237 return (t==T6 || t==T7 || t==T0) ? CIRCLE_SHAPE : null;