1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.draw2d;
|
12
|
|
13
|
import org.eclipse.draw2d.geometry.Dimension;
|
14
|
import org.eclipse.draw2d.geometry.Point;
|
15
|
import org.eclipse.draw2d.geometry.PointList;
|
16
|
|
17
|
/**
|
18
|
* Repositions a {@link Figure} attached to a {@link Connection} when the
|
19
|
* Connection is moved. Provides for alignment at the start (source), middle, or
|
20
|
* end (target) of the Connection.
|
21
|
*/
|
22
|
public class ConnectionLocator extends AbstractLocator {
|
23
|
|
24
|
/** @deprecated Use {@link #SOURCE} */
|
25
|
public static final int START = 2;
|
26
|
/** The start (or source) of the Connection */
|
27
|
public static final int SOURCE = 2;
|
28
|
|
29
|
/** @deprecated Use {@link #TARGET} */
|
30
|
public static final int END = 3;
|
31
|
/** The end (or target) of the Connection */
|
32
|
public static final int TARGET = 3;
|
33
|
|
34
|
/**
|
35
|
* @deprecated Use {@link #MIDDLE} instead, since the location is not the
|
36
|
* midpoint of a line-segment, but the middle of a polyline.
|
37
|
*/
|
38
|
public static final int MIDPOINT = 4;
|
39
|
/** The middle of the Connection */
|
40
|
public static final int MIDDLE = 4;
|
41
|
|
42
|
private Connection connection;
|
43
|
private int alignment;
|
44
|
|
45
|
/**
|
46
|
* Constructs a ConnectionLocator with the passed connection and
|
47
|
* {@link #MIDDLE} alignment.
|
48
|
*
|
49
|
* @param connection
|
50
|
* The Connection
|
51
|
* @since 2.0
|
52
|
*/
|
53
|
public ConnectionLocator(Connection connection) {
|
54
|
this(connection, MIDDLE);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Constructs a ConnectionLocator with the passed Connection and alignment.
|
59
|
* Valid values for the alignment are integer constants {@link #SOURCE},
|
60
|
* {@link #MIDDLE}, and {@link #TARGET}.
|
61
|
*
|
62
|
* @param connection
|
63
|
* The Connection
|
64
|
* @param align
|
65
|
* The alignment
|
66
|
*
|
67
|
* @since 2.0
|
68
|
*/
|
69
|
public ConnectionLocator(Connection connection, int align) {
|
70
|
setConnection(connection);
|
71
|
setAlignment(align);
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Returns the alignment of ConnectionLocator.
|
76
|
*
|
77
|
* @return The alignment
|
78
|
* @since 2.0
|
79
|
*/
|
80
|
public int getAlignment() {
|
81
|
return alignment;
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Returns connection associated with ConnectionLocator.
|
86
|
*
|
87
|
* @return The Connection
|
88
|
* @since 2.0
|
89
|
*/
|
90
|
protected Connection getConnection() {
|
91
|
return connection;
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Returns ConnectionLocator's reference point in absolute coordinates.
|
96
|
*
|
97
|
* @return The reference point
|
98
|
* @since 2.0
|
99
|
*/
|
100
|
protected Point getReferencePoint() {
|
101
|
Point p = getLocation(getConnection().getPoints());
|
102
|
getConnection().translateToAbsolute(p);
|
103
|
return p;
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Returns a point from the passed PointList, dependent on
|
108
|
* ConnectionLocator's alignment. If the alignment is {@link #SOURCE}, it
|
109
|
* returns the first point in <i>points</i>. If {@link #TARGET}, it returns
|
110
|
* the last point in <i>points</i>. If {@link #MIDDLE}, it returns the
|
111
|
* middle of line represented by <i>points</i>.
|
112
|
*
|
113
|
* @param points
|
114
|
* The points in the Connection
|
115
|
* @return The location
|
116
|
* @since 2.0
|
117
|
*/
|
118
|
protected Point getLocation(PointList points) {
|
119
|
switch (getAlignment()) {
|
120
|
case SOURCE:
|
121
|
return points.getPoint(Point.SINGLETON, 0);
|
122
|
case TARGET:
|
123
|
return points.getPoint(Point.SINGLETON, points.size() - 1);
|
124
|
case MIDDLE:
|
125
|
if (points.size() % 2 == 0) {
|
126
|
int i = points.size() / 2;
|
127
|
int j = i - 1;
|
128
|
Point p1 = points.getPoint(j);
|
129
|
Point p2 = points.getPoint(i);
|
130
|
Dimension d = p2.getDifference(p1);
|
131
|
return Point.SINGLETON.setLocation(p1.x + d.width / 2, p1.y
|
132
|
+ d.height / 2);
|
133
|
}
|
134
|
int i = (points.size() - 1) / 2;
|
135
|
return points.getPoint(Point.SINGLETON, i);
|
136
|
default:
|
137
|
return new Point();
|
138
|
}
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Sets the alignment. Possible values are {@link #SOURCE}, {@link #MIDDLE},
|
143
|
* and {@link #TARGET}.
|
144
|
*
|
145
|
* @param align
|
146
|
* The alignment
|
147
|
* @since 2.0
|
148
|
*/
|
149
|
protected void setAlignment(int align) {
|
150
|
alignment = align;
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Sets the Connection to be associated with this ConnectionLocator.
|
155
|
*
|
156
|
* @param connection
|
157
|
* The Connection
|
158
|
* @since 2.0
|
159
|
*/
|
160
|
protected void setConnection(Connection connection) {
|
161
|
this.connection = connection;
|
162
|
}
|
163
|
|
164
|
}
|