Project

General

Profile

Download (7.08 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.editpolicies;
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15

    
16
import org.eclipse.draw2d.Cursors;
17
import org.eclipse.draw2d.PositionConstants;
18

    
19
import org.eclipse.gef.GraphicalEditPart;
20
import org.eclipse.gef.Request;
21
import org.eclipse.gef.commands.Command;
22
import org.eclipse.gef.handles.ResizableHandleKit;
23
import org.eclipse.gef.requests.ChangeBoundsRequest;
24
import org.eclipse.gef.tools.ResizeTracker;
25

    
26
/**
27
 * Provides support for selecting, positioning, and resizing an edit part.
28
 * Selection is indicated via eight square handles along the editpart's figure,
29
 * and a rectangular handle that outlines the edit part with a 1-pixel black
30
 * line. The eight square handles will resize the current selection in the eight
31
 * primary directions. The rectangular handle will drag the current selection
32
 * using a {@link org.eclipse.gef.tools.DragEditPartsTracker}.
33
 * <p>
34
 * During feedback, a rectangle filled using XOR and outlined with dashes is
35
 * drawn. Subclasses may tailor the feedback.
36
 * 
37
 * @author hudsonr
38
 * @author msorens
39
 * @author anyssen
40
 */
41
public class ResizableEditPolicy extends NonResizableEditPolicy {
42

    
43
	private int resizeDirections = PositionConstants.NSEW;
44

    
45
	/**
46
	 * Constructs a new {@link ResizableEditPolicy}.
47
	 * 
48
	 * @since 3.7
49
	 */
50
	public ResizableEditPolicy() {
51
	}
52

    
53
	/**
54
	 * @see org.eclipse.gef.editpolicies.SelectionHandlesEditPolicy#createSelectionHandles()
55
	 */
56
	protected List createSelectionHandles() {
57
		if (resizeDirections == PositionConstants.NONE) {
58
			// non resizable, so delegate to super implementation
59
			return super.createSelectionHandles();
60
		}
61

    
62
		// resizable in at least one direction
63
		List list = new ArrayList();
64
		createMoveHandle(list);
65
		createResizeHandle(list, PositionConstants.NORTH);
66
		createResizeHandle(list, PositionConstants.EAST);
67
		createResizeHandle(list, PositionConstants.SOUTH);
68
		createResizeHandle(list, PositionConstants.WEST);
69
		createResizeHandle(list, PositionConstants.SOUTH_EAST);
70
		createResizeHandle(list, PositionConstants.SOUTH_WEST);
71
		createResizeHandle(list, PositionConstants.NORTH_WEST);
72
		createResizeHandle(list, PositionConstants.NORTH_EAST);
73
		return list;
74
	}
75

    
76
	/**
77
	 * Creates a 'resize' handle, which uses a {@link ResizeTracker} in case
78
	 * resizing is allowed in the respective direction, otherwise returns a drag
79
	 * handle by delegating to
80
	 * {@link NonResizableEditPolicy#createDragHandle(List, int)}.
81
	 * 
82
	 * @param handles
83
	 *            The list of handles to add the resize handle to
84
	 * @param direction
85
	 *            A position constant indicating the direction to create the
86
	 *            handle for
87
	 * @since 3.7
88
	 */
89
	protected void createResizeHandle(List handles, int direction) {
90
		if ((resizeDirections & direction) == direction) {
91
			ResizableHandleKit.addHandle((GraphicalEditPart) getHost(),
92
					handles, direction, getResizeTracker(direction), Cursors
93
							.getDirectionalCursor(direction, getHostFigure()
94
									.isMirrored()));
95
		} else {
96
			// display 'resize' handle to allow dragging or indicate selection
97
			// only
98
			createDragHandle(handles, direction);
99
		}
100
	}
101

    
102
	/**
103
	 * Returns a resize tracker for the given direction to be used by a resize
104
	 * handle.
105
	 * 
106
	 * @param direction
107
	 *            the resize direction for the {@link ResizeTracker}.
108
	 * @return a new {@link ResizeTracker}
109
	 * @since 3.7
110
	 */
111
	protected ResizeTracker getResizeTracker(int direction) {
112
		return new ResizeTracker((GraphicalEditPart) getHost(), direction);
113
	}
114

    
115
	/**
116
	 * Dispatches erase requests to more specific methods.
117
	 * 
118
	 * @see org.eclipse.gef.EditPolicy#eraseSourceFeedback(org.eclipse.gef.Request)
119
	 */
120
	public void eraseSourceFeedback(Request request) {
121
		if (REQ_RESIZE.equals(request.getType()))
122
			eraseChangeBoundsFeedback((ChangeBoundsRequest) request);
123
		else
124
			super.eraseSourceFeedback(request);
125
	}
126

    
127
	/**
128
	 * @see org.eclipse.gef.EditPolicy#getCommand(org.eclipse.gef.Request)
129
	 */
130
	public Command getCommand(Request request) {
131
		if (REQ_RESIZE.equals(request.getType())) {
132
			return getResizeCommand((ChangeBoundsRequest) request);
133
		}
134
		return super.getCommand(request);
135
	}
136

    
137
	/**
138
	 * Returns the command contribution for the given resize request. By
139
	 * default, the request is re-dispatched to the host's parent as a
140
	 * {@link org.eclipse.gef.RequestConstants#REQ_RESIZE_CHILDREN}. The
141
	 * parent's edit policies determine how to perform the resize based on the
142
	 * layout manager in use.
143
	 * 
144
	 * @param request
145
	 *            the resize request
146
	 * @return the command contribution obtained from the parent
147
	 */
148
	protected Command getResizeCommand(ChangeBoundsRequest request) {
149
		ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_RESIZE_CHILDREN);
150
		req.setEditParts(getHost());
151

    
152
		req.setMoveDelta(request.getMoveDelta());
153
		req.setSizeDelta(request.getSizeDelta());
154
		req.setLocation(request.getLocation());
155
		req.setExtendedData(request.getExtendedData());
156
		req.setResizeDirection(request.getResizeDirection());
157
		return getHost().getParent().getCommand(req);
158
	}
159

    
160
	/**
161
	 * Sets the directions in which handles should allow resizing. Valid values
162
	 * are bit-wise combinations of:
163
	 * <UL>
164
	 * <LI>{@link PositionConstants#NORTH}
165
	 * <LI>{@link PositionConstants#SOUTH}
166
	 * <LI>{@link PositionConstants#EAST}
167
	 * <LI>{@link PositionConstants#WEST}
168
	 * </UL>
169
	 * 
170
	 * @param newDirections
171
	 *            the direction in which resizing is allowed
172
	 */
173
	public void setResizeDirections(int newDirections) {
174
		resizeDirections = newDirections;
175
	}
176

    
177
	/**
178
	 * @see org.eclipse.gef.EditPolicy#showSourceFeedback(org.eclipse.gef.Request)
179
	 */
180
	public void showSourceFeedback(Request request) {
181
		if (REQ_RESIZE.equals(request.getType())) {
182
			showChangeBoundsFeedback((ChangeBoundsRequest) request);
183
		} else {
184
			super.showSourceFeedback(request);
185
		}
186
	}
187

    
188
	/**
189
	 * @see org.eclipse.gef.EditPolicy#understandsRequest(org.eclipse.gef.Request)
190
	 */
191
	public boolean understandsRequest(Request request) {
192
		if (REQ_RESIZE.equals(request.getType())) {
193
			// check all resize directions of the request are supported
194
			int resizeDirections = ((ChangeBoundsRequest) request)
195
					.getResizeDirection();
196
			return (resizeDirections & getResizeDirections()) == resizeDirections;
197
		}
198
		return super.understandsRequest(request);
199
	}
200

    
201
	/**
202
	 * Returns the directions in which resizing should be allowed
203
	 * 
204
	 * Valid values are bit-wise combinations of:
205
	 * <UL>
206
	 * <LI>{@link PositionConstants#NORTH}
207
	 * <LI>{@link PositionConstants#SOUTH}
208
	 * <LI>{@link PositionConstants#EAST}
209
	 * <LI>{@link PositionConstants#WEST}
210
	 * </UL>
211
	 * or {@link PositionConstants#NONE}.
212
	 * 
213
	 */
214
	public int getResizeDirections() {
215
		return resizeDirections;
216
	}
217
}
(17-17/26)