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