Skip to main content
summaryrefslogtreecommitdiffstats
blob: 03e690ee261cfd0cd9c14dd1baf62bfd3940be14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.tools;

import java.util.Collections;

import org.eclipse.draw2d.Cursors;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.SelectionRequest;
import org.eclipse.gef.tools.TargetingTool;
import org.eclipse.jst.pagedesigner.parts.NodeEditPart;
import org.eclipse.jst.pagedesigner.parts.TextEditPart;
import org.eclipse.jst.pagedesigner.range.RangeUtil;
import org.eclipse.jst.pagedesigner.validation.caret.ActionData;
import org.eclipse.jst.pagedesigner.validation.caret.IPositionMediator;
import org.eclipse.jst.pagedesigner.validation.caret.InlineEditingPositionMediator;
import org.eclipse.jst.pagedesigner.validation.caret.Target;
import org.eclipse.jst.pagedesigner.viewer.DesignPosition;
import org.eclipse.jst.pagedesigner.viewer.DesignRange;
import org.eclipse.jst.pagedesigner.viewer.EditPartPositionHelper;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.eclipse.swt.graphics.Cursor;

/**
 * @author mengbo
 */
public class RangeDragTracker extends TargetingTool implements DragTracker {
	/** Flag to indicate selection has been performed. */
	private static final int FLAG_SELECTION_PERFORMED = TargetingTool.MAX_FLAG << 1;

	private EditPart editpart;

	/**
	 * Constructs a new SelectEditPartTracker with the given edit part as the
	 * source.
	 * 
	 * @param owner
	 *            the source edit part
	 */
	public RangeDragTracker(EditPart owner) {
		setSourceEditPart(owner);
	}

	/**
	 * @see org.eclipse.gef.tools.AbstractTool#calculateCursor()
	 */
	protected Cursor calculateCursor() {
        return Cursors.IBEAM;
//		if (isInState(STATE_INITIAL))
//        {
//            return Cursors.IBEAM;
//        }
//        else if (isInState(STATE_DRAG | STATE_ACCESSIBLE_DRAG))
//        {
//			return getDefaultCursor();
//		}
//        return Cursors.IBEAM;
//		return super.calculateCursor();
	}

	/**
	 * @see org.eclipse.gef.tools.AbstractTool#getCommandName()
	 */
	protected String getCommandName() {
		return "Range Drag Tracker";//$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.gef.tools.AbstractTool#getDebugName()
	 */
	protected String getDebugName() {
		return "Range Drag Tracker";//$NON-NLS-1$
	}

	/**
	 * Returns the source edit part.
	 * 
	 * @return the source edit part
	 */
	protected EditPart getSourceEditPart() {
		return editpart;
	}

	/**
	 * Performs a conditional selection if needed (if right or left mouse button
	 * have been pressed) and goes into the drag state. If any other button has
	 * been pressed, the tool goes into the invalid state.
	 * 
	 * @see org.eclipse.gef.tools.AbstractTool#handleButtonDown(int)
	 */
	protected boolean handleButtonDown(int button) {
		if (button == 3 && isInState(STATE_INITIAL)) {
			EditPart sourcePart = this.getSourceEditPart();
			IHTMLGraphicalViewer viewer = (IHTMLGraphicalViewer) sourcePart
					.getViewer();
			if (viewer != null && viewer.isInRangeMode()) {
				DesignRange range = viewer.getRangeSelection();
				if (range != null && range.isValid()) {
					if (RangeUtil.intersect(range, sourcePart)) {
						return true;
					}
				}
			}
		}

		if ((button == 1 || button == 3) && isInState(STATE_INITIAL)) {
			peroformSelectionBegin();
		}

		if (button != 1) {
			setState(STATE_INVALID);
			if (button == 3)
				setState(STATE_TERMINAL);
			handleInvalidInput();
		} else {
			stateTransition(STATE_INITIAL, STATE_DRAG);
		}
		return true;
	}

	/**
	 * If the
	 * source is selected and there are no modifier keys pressed (i.e. the user
	 * isn't selecting multiple edit parts or deselecting edit parts), sets the
	 * direct edit flag so that when the mouse is released, a direct edit will
	 * be performed.
	 */
	protected void peroformSelectionBegin() {
		// if (getCurrentInput().isControlKeyDown())
		// {
		// // when control key is down, switch to object selection mode.
		// getHTMLGraphicalViewer().ensureObjectSelectionMode();
		// setFlag(FLAG_SELECTION_PERFORMED, true);
		// EditPartViewer viewer = getCurrentViewer();
		// List selectedObjects = viewer.getSelectedEditParts();
		//
		// if (selectedObjects.contains(getSourceEditPart()))
		// viewer.deselect(getSourceEditPart());
		// else
		// viewer.appendSelection(getSourceEditPart());
		// }
		// else
		if (getCurrentInput().isShiftKeyDown()) {
			getHTMLGraphicalViewer().ensureRangeSelectionMode();
			rangeSelection(true);
		} else {
			if (shouldStartRangeSelection()) {
				rangeSelection(false);
			} else {
				getCurrentViewer().select(getSourceEditPart());
			}
		}
	}

	/**
	 * If in the drag state, the tool selects the source edit part.  If the
	 * edit part is newly selected and not completely visible,
	 * {@link org.eclipse.gef.EditPartViewer#reveal(EditPart)}is called to show the selected
	 * edit part.
	 * 
	 * @see org.eclipse.gef.tools.AbstractTool#handleButtonUp(int)
	 */
	protected boolean handleButtonUp(int button) {
		if (isInState(STATE_DRAG)) {
			// XXX: commented the following two line (lium)
			// performSelection();
			// if (button == 1 && getSourceEditPart().getSelected() !=
			// EditPart.SELECTED_NONE)
			// getCurrentViewer().reveal(getSourceEditPart());
			setState(STATE_TERMINAL);
			return true;
		}
		return false;
	}

	/**
	 * Calls {@link #performOpen()}if the double click was with mouse button 1.
	 * 
	 * @see org.eclipse.gef.tools.AbstractTool#handleDoubleClick(int)
	 */
	protected boolean handleDoubleClick(int button) {
		if (button == 1) {
			performOpen();
		}
		return true;
	}

	/**
	 * @see org.eclipse.gef.tools.AbstractTool#handleDragStarted()
	 */
	protected boolean handleDragStarted() {
		return stateTransition(STATE_DRAG, STATE_DRAG_IN_PROGRESS);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.tools.AbstractTool#handleDragInProgress()
	 */
	protected boolean handleDragInProgress() {
		if (getHTMLGraphicalViewer().isInRangeMode()) {
			rangeSelection(true);
			return true;
		}
        return super.handleDragInProgress();
	}

	/**
	 * Returns <code>true</code> if selection has already occured.
	 * 
	 * @return <code>true</code> if selection has occured
	 */
	protected boolean hasSelectionOccurred() {
		return getFlag(FLAG_SELECTION_PERFORMED);
	}

	/**
	 * Creates a {@link SelectionRequest}and sends it to the source edit part
	 * via {@link EditPart#performRequest(org.eclipse.gef.Request)}. Possible uses are to open
	 * the selected item in another editor or replace the current editor's
	 * contents based on the selected item.
	 */
	protected void performOpen() {
		SelectionRequest request = new SelectionRequest();
		request.setLocation(getLocation());
		request.setType(RequestConstants.REQ_OPEN);
		getSourceEditPart().performRequest(request);
	}

	/**
	 * @see org.eclipse.gef.tools.AbstractTool#resetFlags()
	 */
	protected void resetFlags() {
		super.resetFlags();
		setFlag(FLAG_SELECTION_PERFORMED, false);
	}

	/**
	 * Sets the source edit part.
	 * 
	 * @param part
	 *            the source edit part
	 */
	protected void setSourceEditPart(EditPart part) {
		this.editpart = part;
	}

	/**
	 * @return the html graphical viewer
	 */
	public IHTMLGraphicalViewer getHTMLGraphicalViewer() {
		return (IHTMLGraphicalViewer) getCurrentViewer();
	}

	/**
	 * @return
	 */
	private boolean shouldStartRangeSelection() {
		IPositionMediator positionMediator = new InlineEditingPositionMediator(
				new ActionData(ActionData.INLINE_EDIT, null));
		if (positionMediator.isEditable(new Target(getSourceEditPart()))) {
			return getSourceEditPart() instanceof TextEditPart
					|| !(((NodeEditPart) getSourceEditPart()).isWidget());
		}
        return false;
	}

	/**
	 * @param b
	 *            true means remain the old range start position.
	 */
	private void rangeSelection(boolean b) {
		// XXX: not using updateTargetEditPartUnderMouse. Maybe should. Don't
		// want to
		// go through the request mechanism, so simple implementation for now.
		
		//to avoid 219038 and possibility of current viewer changing
		final EditPartViewer viewer = getCurrentViewer();
		final IHTMLGraphicalViewer graphicalViewer = (IHTMLGraphicalViewer)viewer;
		EditPart editPart = viewer.findObjectAtExcluding(
				getLocation(), Collections.EMPTY_LIST);
		IPositionMediator positionMediator = new InlineEditingPositionMediator(
				new ActionData(ActionData.INLINE_EDIT, null));
		ExposeHelper exposeHelper = new ExposeHelper(graphicalViewer);
		exposeHelper.adjustVertical(getCurrentInput().getMouseLocation());
		DesignPosition position = EditPartPositionHelper.findEditPartPosition(
				editPart, getCurrentInput().getMouseLocation(),
				positionMediator);
		if (b) {
			graphicalViewer.setRangeEndPosition(position);
		} else {
			graphicalViewer.setRange(position, position);
		}
		graphicalViewer.updateHorizontalPos();
	}

    protected boolean handleHover() {
        boolean retValue = super.handleHover();
        refreshCursor();
        return retValue;
    }
}

Back to the top