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.PrecisionRectangle;
|
15
|
import org.eclipse.draw2d.geometry.Rectangle;
|
16
|
|
17
|
/**
|
18
|
* Places a handle relative to a figure's bounds. The placement is determined by
|
19
|
* indicating the figure to which the placement is relative, and two
|
20
|
* floating-point value indicating the horizontal and vertical offset from that
|
21
|
* figure's top-left corner. The values (0.0, 0.0) would indicate the figure's
|
22
|
* top-left corner, while the values (1.0, 1.0) would indicate the figure's
|
23
|
* bottom-right corner.
|
24
|
* <P>
|
25
|
* Constants such as {@link PositionConstants#NORTH NORTH} and
|
26
|
* {@link PositionConstants#SOUTH SOUTH} can be used to set the placement.
|
27
|
*/
|
28
|
public class RelativeLocator implements Locator {
|
29
|
|
30
|
private double relativeX;
|
31
|
private double relativeY;
|
32
|
private IFigure reference;
|
33
|
|
34
|
/**
|
35
|
* Null constructor. The reference figure must be set before use. The
|
36
|
* relative locations will default to (0.0, 0.0).
|
37
|
*
|
38
|
* @since 2.0
|
39
|
*/
|
40
|
public RelativeLocator() {
|
41
|
relativeX = 0.0;
|
42
|
relativeY = 0.0;
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Constructs a RelativeLocator with the given reference figure and relative
|
47
|
* location. The location is a constant from {@link PositionConstants} used
|
48
|
* as a convenient and readable way to set both the relativeX and relativeY
|
49
|
* values.
|
50
|
*
|
51
|
* @param reference
|
52
|
* the reference figure
|
53
|
* @param location
|
54
|
* one of NORTH, NORTH_EAST, etc.
|
55
|
* @since 2.0
|
56
|
*/
|
57
|
public RelativeLocator(IFigure reference, int location) {
|
58
|
setReferenceFigure(reference);
|
59
|
switch (location & PositionConstants.NORTH_SOUTH) {
|
60
|
case PositionConstants.NORTH:
|
61
|
relativeY = 0;
|
62
|
break;
|
63
|
case PositionConstants.SOUTH:
|
64
|
relativeY = 1.0;
|
65
|
break;
|
66
|
default:
|
67
|
relativeY = 0.5;
|
68
|
}
|
69
|
|
70
|
switch (location & PositionConstants.EAST_WEST) {
|
71
|
case PositionConstants.WEST:
|
72
|
relativeX = 0;
|
73
|
break;
|
74
|
case PositionConstants.EAST:
|
75
|
relativeX = 1.0;
|
76
|
break;
|
77
|
default:
|
78
|
relativeX = 0.5;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Constructs a RelativeLocator with the given reference Figure and offset
|
84
|
* ratios.
|
85
|
*
|
86
|
* @param reference
|
87
|
* the reference figure
|
88
|
* @param relativeX
|
89
|
* the relative X offset
|
90
|
* @param relativeY
|
91
|
* the relative Y offset
|
92
|
* @since 2.0
|
93
|
*/
|
94
|
public RelativeLocator(IFigure reference, double relativeX, double relativeY) {
|
95
|
setReferenceFigure(reference);
|
96
|
this.relativeX = relativeX;
|
97
|
this.relativeY = relativeY;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Returns the Reference Box in the Reference Figure's coordinate system.
|
102
|
* The returned Rectangle may be by reference, and should <b>not</b> be
|
103
|
* modified.
|
104
|
*
|
105
|
* @return the reference box
|
106
|
* @since 2.0
|
107
|
*/
|
108
|
protected Rectangle getReferenceBox() {
|
109
|
return getReferenceFigure().getBounds();
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Returns the Figure this locator is relative to.
|
114
|
*
|
115
|
* @return the reference figure
|
116
|
* @since 2.0
|
117
|
*/
|
118
|
protected IFigure getReferenceFigure() {
|
119
|
return reference;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Relocates the target using the relative offset locations.
|
124
|
*
|
125
|
* @see org.eclipse.draw2d.Locator#relocate(org.eclipse.draw2d.IFigure)
|
126
|
*/
|
127
|
public void relocate(IFigure target) {
|
128
|
IFigure reference = getReferenceFigure();
|
129
|
Rectangle targetBounds = new PrecisionRectangle(getReferenceBox()
|
130
|
.getResized(-1, -1));
|
131
|
reference.translateToAbsolute(targetBounds);
|
132
|
target.translateToRelative(targetBounds);
|
133
|
targetBounds.resize(1, 1);
|
134
|
|
135
|
Dimension targetSize = target.getPreferredSize();
|
136
|
|
137
|
targetBounds.x += (int) (targetBounds.width * relativeX - ((targetSize.width + 1) / 2));
|
138
|
targetBounds.y += (int) (targetBounds.height * relativeY - ((targetSize.height + 1) / 2));
|
139
|
targetBounds.setSize(targetSize);
|
140
|
target.setBounds(targetBounds);
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Sets the reference figure this locator uses to place the target figure.
|
145
|
*
|
146
|
* @param reference
|
147
|
* the reference figure
|
148
|
* @since 2.0
|
149
|
*/
|
150
|
public void setReferenceFigure(IFigure reference) {
|
151
|
this.reference = reference;
|
152
|
}
|
153
|
|
154
|
}
|