Project

General

Profile

Download (5.75 KB) Statistics
| Branch: | Tag: | Revision:
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.gef.handles;
12

    
13
import org.eclipse.swt.graphics.Cursor;
14

    
15
import org.eclipse.draw2d.AncestorListener;
16
import org.eclipse.draw2d.Figure;
17
import org.eclipse.draw2d.IFigure;
18
import org.eclipse.draw2d.Locator;
19
import org.eclipse.draw2d.geometry.Point;
20

    
21
import org.eclipse.gef.DragTracker;
22
import org.eclipse.gef.GraphicalEditPart;
23
import org.eclipse.gef.Handle;
24

    
25
/**
26
 * Base implementation for handles. This class keeps track of the typical data
27
 * needed by a handle, such as a drag tracker, a locator to place the handle, a
28
 * cursor, and the editpart to which the handle belongs. AbstractHandle will add
29
 * an {@link AncestorListener} to the owner's figure, and will automatically
30
 * revalidate this handle whenever the owner's figure moves.
31
 */
32
public abstract class AbstractHandle extends Figure implements Handle,
33
		AncestorListener {
34

    
35
	private GraphicalEditPart editpart;
36
	private DragTracker dragTracker;
37
	private Locator locator;
38

    
39
	/**
40
	 * Null constructor
41
	 */
42
	public AbstractHandle() {
43
	}
44

    
45
	/**
46
	 * Creates a handle for the given <code>GraphicalEditPart</code> using the
47
	 * given <code>Locator</code>.
48
	 * 
49
	 * @param owner
50
	 *            The editpart which provided this handle
51
	 * @param loc
52
	 *            The locator to position the handle
53
	 */
54
	public AbstractHandle(GraphicalEditPart owner, Locator loc) {
55
		setOwner(owner);
56
		setLocator(loc);
57
	}
58

    
59
	/**
60
	 * Creates a handle for the given <code>GraphicalEditPart</code> using the
61
	 * given <code>Locator</code> and <code>Cursor</code>.
62
	 * 
63
	 * @param owner
64
	 *            The editpart which provided this handle
65
	 * @param loc
66
	 *            The locator to position the handle
67
	 * @param c
68
	 *            The cursor to display when the mouse is over the handle
69
	 */
70
	public AbstractHandle(GraphicalEditPart owner, Locator loc, Cursor c) {
71
		this(owner, loc);
72
		setCursor(c);
73
	}
74

    
75
	/**
76
	 * Adds this as an {@link AncestorListener} to the owner's {@link Figure}.
77
	 */
78
	public void addNotify() {
79
		super.addNotify();
80
		// Listen to the owner figure so the handle moves when the
81
		// figure moves.
82
		getOwnerFigure().addAncestorListener(this);
83
	}
84

    
85
	/**
86
	 * @see org.eclipse.draw2d.AncestorListener#ancestorMoved(org.eclipse.draw2d.IFigure)
87
	 */
88
	public void ancestorMoved(IFigure ancestor) {
89
		revalidate();
90
	}
91

    
92
	/**
93
	 * @see org.eclipse.draw2d.AncestorListener#ancestorAdded(org.eclipse.draw2d.IFigure)
94
	 */
95
	public void ancestorAdded(IFigure ancestor) {
96
	}
97

    
98
	/**
99
	 * @see org.eclipse.draw2d.AncestorListener#ancestorRemoved(org.eclipse.draw2d.IFigure)
100
	 */
101
	public void ancestorRemoved(IFigure ancestor) {
102
	}
103

    
104
	/**
105
	 * Creates a new drag tracker to be returned by getDragTracker().
106
	 * 
107
	 * @return a new drag tracker
108
	 */
109
	protected abstract DragTracker createDragTracker();
110

    
111
	/**
112
	 * By default, the center of the handle is returned.
113
	 * 
114
	 * @see org.eclipse.gef.Handle#getAccessibleLocation()
115
	 */
116
	public Point getAccessibleLocation() {
117
		Point p = getBounds().getCenter();
118
		translateToAbsolute(p);
119
		return p;
120
	}
121

    
122
	/**
123
	 * Returns the cursor. The cursor is displayed whenever the mouse is over
124
	 * the handle.
125
	 * 
126
	 * @deprecated use getCursor()
127
	 * @return the cursor
128
	 */
129
	public Cursor getDragCursor() {
130
		return getCursor();
131
	}
132

    
133
	/**
134
	 * Returns the drag tracker to use when the user clicks on this handle. If
135
	 * the drag tracker has not been set, it will be lazily created by calling
136
	 * {@link #createDragTracker()}.
137
	 * 
138
	 * @return the drag tracker
139
	 */
140
	public DragTracker getDragTracker() {
141
		if (dragTracker == null)
142
			dragTracker = createDragTracker();
143
		return dragTracker;
144
	}
145

    
146
	/**
147
	 * Returns the <code>Locator</code> used to position this handle.
148
	 * 
149
	 * @return the locator
150
	 */
151
	public Locator getLocator() {
152
		return locator;
153
	}
154

    
155
	/**
156
	 * Returns the <code>GraphicalEditPart</code> associated with this handle.
157
	 * 
158
	 * @return the owner editpart
159
	 */
160
	protected GraphicalEditPart getOwner() {
161
		return editpart;
162
	}
163

    
164
	/**
165
	 * Convenience method to return the owner's figure.
166
	 * 
167
	 * @return the owner editpart's figure
168
	 */
169
	protected IFigure getOwnerFigure() {
170
		return getOwner().getFigure();
171
	}
172

    
173
	/**
174
	 * @see org.eclipse.draw2d.IFigure#removeNotify()
175
	 */
176
	public void removeNotify() {
177
		getOwnerFigure().removeAncestorListener(this);
178
		super.removeNotify();
179
	}
180

    
181
	/**
182
	 * Sets the Cursor for the handle.
183
	 * 
184
	 * @param c
185
	 *            the cursor
186
	 * @throws Exception
187
	 *             a bogus excpetion declaration
188
	 * @deprecated use setCursor()
189
	 */
190
	public void setDragCursor(Cursor c) throws Exception {
191
		setCursor(c);
192
	}
193

    
194
	/**
195
	 * Sets the drag tracker for this handle.
196
	 * 
197
	 * @param t
198
	 *            the drag tracker
199
	 */
200
	public void setDragTracker(DragTracker t) {
201
		dragTracker = t;
202
	}
203

    
204
	/**
205
	 * Sets the locator which position this handle.
206
	 * 
207
	 * @param locator
208
	 *            the new locator
209
	 */
210
	protected void setLocator(Locator locator) {
211
		this.locator = locator;
212
	}
213

    
214
	/**
215
	 * Sets the owner editpart associated with this handle.
216
	 * 
217
	 * @param editpart
218
	 *            the owner
219
	 */
220
	protected void setOwner(GraphicalEditPart editpart) {
221
		this.editpart = editpart;
222
	}
223

    
224
	/**
225
	 * Extends validate() to place the handle using its locator.
226
	 * 
227
	 * @see org.eclipse.draw2d.IFigure#validate()
228
	 */
229
	public void validate() {
230
		if (isValid())
231
			return;
232
		getLocator().relocate(this);
233
		super.validate();
234
	}
235

    
236
}
(1-1/19)