]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/ObjectTerminal.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / ObjectTerminal.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.element.handler.impl;\r
13 \r
14 import java.awt.Shape;\r
15 import java.awt.geom.AffineTransform;\r
16 \r
17 import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
18 import org.simantics.g2d.utils.geom.DirectionSet;\r
19 import org.simantics.utils.Container;\r
20 import org.simantics.utils.DataContainer;\r
21 \r
22 /**\r
23  * An arbitrary object based implementation of the g2d {@link Terminal}\r
24  * interface.\r
25  * \r
26  * <p>\r
27  * Elements that intend to contain terminal and use\r
28  * <code>DiagramGraphSynchronizer</code> must use this implementation to\r
29  * represent the terminals of the element.\r
30  * </p>\r
31  * \r
32  * <p>\r
33  * Also contains a relative transformation with respect to its parent, a set of\r
34  * allowed drawing directions and a graphical shape to depict the terminal.\r
35  * </p>\r
36  * \r
37  * @author Tuukka Lehtonen\r
38  */\r
39 public abstract class ObjectTerminal implements Terminal {\r
40 \r
41     private static final Container<Shape> NO_SHAPE = new DataContainer<Shape>(null);\r
42 \r
43     private final Object          data;\r
44     private final AffineTransform transform;\r
45     private final DirectionSet    ds;\r
46     private final Container<Shape> shape;\r
47 \r
48     public ObjectTerminal(Object data, AffineTransform transform, DirectionSet ds, Container<Shape> shape) {\r
49         assert data != null;\r
50         this.data = data;\r
51         this.transform = transform;\r
52         this.ds = ds;\r
53         this.shape = shape != null ? shape : NO_SHAPE;\r
54     }\r
55 \r
56     public ObjectTerminal(Object data, AffineTransform transform, DirectionSet ds, Shape shape) {\r
57         this(data, transform, ds, new DataContainer<Shape>(shape));\r
58     }\r
59 \r
60     public ObjectTerminal(Object data, AffineTransform transform, DirectionSet ds) {\r
61         this(data, transform, ds, NO_SHAPE);\r
62     }\r
63 \r
64     public ObjectTerminal(Object data, AffineTransform transform) {\r
65         this(data, transform, DirectionSet.ANY, NO_SHAPE);\r
66     }\r
67 \r
68     public ObjectTerminal(Object data) {\r
69         this(data, new AffineTransform(), DirectionSet.ANY, NO_SHAPE);\r
70     }\r
71 \r
72     @Override\r
73     public String toString() {\r
74         return getClass().getSimpleName() + "[" + data + ", " + transform + ", " + shape + "]";\r
75     }\r
76 \r
77     @Override\r
78     public int hashCode() {\r
79         return data.hashCode();\r
80     }\r
81 \r
82     @Override\r
83     public boolean equals(Object obj) {\r
84         if (this == obj)\r
85             return true;\r
86         if (obj == null || getClass() != obj.getClass())\r
87             return false;\r
88         ObjectTerminal other = (ObjectTerminal) obj;\r
89         return data.equals(other.data);\r
90     }\r
91 \r
92     public Object getData() {\r
93         return data;\r
94     }\r
95 \r
96     public AffineTransform getTransform() {\r
97         return transform;\r
98     }\r
99 \r
100     public DirectionSet getDirections() {\r
101         return ds;\r
102     }\r
103 \r
104     public Shape getShape() {\r
105         return shape.get();\r
106     }\r
107 \r
108 }