Skip to main content
summaryrefslogtreecommitdiffstats
blob: c5da846062aafe8e063069b3d4ebf1e0e5e447f6 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*******************************************************************************
 * 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 org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.LocationRequest;
import org.eclipse.gef.tools.SelectionTool;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jst.pagedesigner.commands.DeleteNodeCommand;
import org.eclipse.jst.pagedesigner.commands.SwitchSelectionCommand;
import org.eclipse.jst.pagedesigner.commands.nav.HorizontalMoveCommand;
import org.eclipse.jst.pagedesigner.commands.nav.ICaretPositionMover;
import org.eclipse.jst.pagedesigner.commands.nav.VerticalMoveCommand;
import org.eclipse.jst.pagedesigner.commands.range.CopyCommand;
import org.eclipse.jst.pagedesigner.commands.range.CutCommand;
import org.eclipse.jst.pagedesigner.commands.range.DeleteCommand;
import org.eclipse.jst.pagedesigner.commands.range.InsertCommand;
import org.eclipse.jst.pagedesigner.commands.range.KeyboardData;
import org.eclipse.jst.pagedesigner.commands.range.PasteCommand;
import org.eclipse.jst.pagedesigner.commands.range.SelectAllCommand;
import org.eclipse.jst.pagedesigner.editors.pagedesigner.PageDesignerResources;
import org.eclipse.jst.pagedesigner.parts.DocumentEditPart;
import org.eclipse.jst.pagedesigner.parts.NodeEditPart;
import org.eclipse.jst.pagedesigner.requests.LocationModifierRequest;
import org.eclipse.jst.pagedesigner.viewer.HTMLGraphicalViewer;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Cursor;

/**
 * @author mengbo
 */
public class RangeSelectionTool extends SelectionTool 
{
	private LocationRequest _hoverRequest;
    

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.tools.SelectionTool#handleKeyDown(org.eclipse.swt.events.KeyEvent)
	 */
	protected boolean handleKeyDown(KeyEvent e) {
		// resetHover() is not visible.
		if (isHoverActive()) {
			handleHoverStop();
		}
		setHoverActive(false);

		if ((e.stateMask & SWT.ALT) != 0) {
			return false;
		}
		Command command = null;
		KeyboardData keyCode = null;
		switch (e.keyCode) {
		case SWT.F2:
			command = new SwitchSelectionCommand((IHTMLGraphicalViewer) this
					.getCurrentViewer());

			break;
		case SWT.ARROW_UP:
			command = new VerticalMoveCommand((IHTMLGraphicalViewer) this
					.getCurrentViewer(), true, (e.stateMask & SWT.SHIFT) != 0);
			break;
		case SWT.ARROW_DOWN:
			command = new VerticalMoveCommand((IHTMLGraphicalViewer) this
					.getCurrentViewer(), false, (e.stateMask & SWT.SHIFT) != 0);
			break;
		case SWT.ARROW_LEFT:
			command = new HorizontalMoveCommand((IHTMLGraphicalViewer) this
					.getCurrentViewer(), false, (e.stateMask & SWT.SHIFT) != 0);
			break;
		case SWT.ARROW_RIGHT:
			command = new HorizontalMoveCommand((IHTMLGraphicalViewer) this
					.getCurrentViewer(), true, (e.stateMask & SWT.SHIFT) != 0);
			break;
		case SWT.DEL:
			if ((e.stateMask & SWT.SHIFT) == 0) {
				if (getCurrentViewer().getSelection() != null) {
					ISelection selection = getCurrentViewer().getSelection();
					if (selection instanceof StructuredSelection) {
						Object object = ((StructuredSelection) selection)
								.getFirstElement();
						if (!(object instanceof DocumentEditPart)) {
							// "delete node"
							command = new DeleteNodeCommand(
									(IHTMLGraphicalViewer) getCurrentViewer());
						}
					}
				}
				if (command == null) {
					// "delete"
					command = new DeleteCommand(true,
							(IHTMLGraphicalViewer) this.getCurrentViewer()); //$NON-NLS-1$
				}
			} else {
				// "cut"
				command = new CutCommand((IHTMLGraphicalViewer) this
						.getCurrentViewer()); //$NON-NLS-1$
				e.doit = false;
			}
			break;
		case SWT.BS:
			// "delete"
			command = new DeleteCommand(false, (IHTMLGraphicalViewer) this
					.getCurrentViewer()); //$NON-NLS-1$
			e.doit = false;
			break;
		case SWT.INSERT:
			if ((e.stateMask & SWT.SHIFT) != 0) {
				// "paste"
				command = new PasteCommand((IHTMLGraphicalViewer) this
						.getCurrentViewer()); //$NON-NLS-1$
				e.doit = false;
				break;
			} else if ((e.stateMask & SWT.CONTROL) != 0) {
				// "copy"
				command = new CopyCommand((IHTMLGraphicalViewer) this
						.getCurrentViewer()); //$NON-NLS-1$
				e.doit = false;
				break;
			}
			break;
		case SWT.LF:
		case SWT.CR:
			// "insert"
			keyCode = new KeyboardData(e.character, e.stateMask,
					(IHTMLGraphicalViewer) getCurrentViewer());
			command = new InsertCommand(
					PageDesignerResources.getInstance().getString(
							"RangeSelectionTool.CommandLabel.Insert"), (IHTMLGraphicalViewer) this.getCurrentViewer(), keyCode); //$NON-NLS-1$
			e.doit = false;
			break;
		default:
			if (e.keyCode == 'a' && (e.stateMask & SWT.CTRL) != 0) {
				command = new SelectAllCommand("selectAll",
						(IHTMLGraphicalViewer) this.getCurrentViewer());
				e.doit = false;
			} else {
				if (getCurrentViewer() instanceof HTMLGraphicalViewer
						&& ((HTMLGraphicalViewer) getCurrentViewer())
								.isInRangeMode()
						&& (!Character.isIdentifierIgnorable(e.character) && !Character
								.isISOControl(e.character))
						|| (e.character == '\r')) {
					keyCode = new KeyboardData(e.character, e.stateMask,
							(IHTMLGraphicalViewer) getCurrentViewer());
					// "insert"
					command = new InsertCommand(
							PageDesignerResources.getInstance().getString(
									"RangeSelectionTool.CommandLabel.Insert"), (IHTMLGraphicalViewer) this.getCurrentViewer(), keyCode); //$NON-NLS-1$
					e.doit = false;
					break;
				}
                return super.handleKeyDown(e);
			}
		}
		if (command != null) {
			command.execute();
			e.doit = false;
			if (command instanceof ICaretPositionMover) {
				if (getCurrentViewer() instanceof HTMLGraphicalViewer) {
					((HTMLGraphicalViewer) getCurrentViewer())
							.updateHorizontalPos();
				}
			}
			return true;
		}
		return false;
	}

	// /**
	// * @param e
	// * @return
	// */
	// protected boolean handleRangeModeKeyDown(KeyEvent e)
	// {
	// if (e.keyCode == SWT.SHIFT || e.keyCode == SWT.CONTROL || e.keyCode ==
	// SWT.ALT)
	// {
	// return false;
	// }
	//
	// dumpKey(e);
	// Command command = null;
	// switch (e.keyCode)
	// {
	// case SWT.ARROW_LEFT:
	// command = new HorizontalMoveCommand((IHTMLGraphicalViewer)
	// this.getCurrentViewer(), false,
	// (e.stateMask & SWT.SHIFT) != 0);
	// break;
	// case SWT.ARROW_RIGHT:
	// command = new HorizontalMoveCommand((IHTMLGraphicalViewer)
	// this.getCurrentViewer(), true,
	// (e.stateMask & SWT.SHIFT) != 0);
	// break;
	//
	// }
	// if (command != null)
	// {
	// command.execute();
	// return true;
	// }
	//
	// char content = e.character;
	//
	// // when reach here, should be standard content keys.
	// return handleRangeModeContentChar(content);
	// }

	/**
	 * For inner debuging.
	 * 
	 * @param e
	 */
    
    // TODO: dead?
//	private void dumpKey(KeyEvent e) {
//		String message = "code:" + Integer.toBinaryString(e.keyCode) + "char:" + e.character + "key:" + Integer.toBinaryString(e.stateMask); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
//		_log.info(message);
//	}
//
//	/**
//	 * @param content
//	 */
//	private boolean handleRangeModeContentChar(char content) {
//		IHTMLGraphicalViewer viewer = (IHTMLGraphicalViewer) this
//				.getCurrentViewer();
//		ContentCommand c = new ContentCommand(viewer, content);
//		c.execute();
//		return true;
//	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.tools.SelectionTool#createHoverRequest()
	 */
	protected void createHoverRequest() {
		this._hoverRequest = new LocationModifierRequest();
		_hoverRequest.setType(RequestConstants.REQ_SELECTION_HOVER);
	}

	protected Request getTargetHoverRequest() {
		if (_hoverRequest == null) {
			createHoverRequest();
		}
		return _hoverRequest;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.tools.SelectionTool#updateHoverRequest()
	 */
	protected void updateHoverRequest() {
		LocationModifierRequest request = (LocationModifierRequest) getTargetHoverRequest();
		request.setLocation(getLocation());
		request.setControlKeyPressed(getCurrentInput().isControlKeyDown());
	}

    /* 
     * If the target edit part is a NodeEditPart, then inform it if of the current
     * drag status before calling its hover feedback
     */
    protected void showHoverFeedback() 
    {
        if (getTargetEditPart() instanceof NodeEditPart)
        {
            ((NodeEditPart)getTargetEditPart())
                .setDragActive(
                        isInState(STATE_DRAG_IN_PROGRESS 
                                | STATE_ACCESSIBLE_DRAG_IN_PROGRESS 
                                | STATE_DRAG));
        }
        super.showHoverFeedback();
    }

    // TODO : this method is for debug purposes and should
    // be removed in production
//    protected boolean updateTargetUnderMouse() {
//        EditPart editPart = getTargetEditPart();
//        boolean retValue =  super.updateTargetUnderMouse();
//        if (getTargetEditPart() != editPart)
//        {
//            System.out.println("New target editpart:  "+getTargetEditPart()+" Old edit part: "+editPart);
//        }
//        return retValue;
//    }

    protected boolean handleMove() {
        boolean handled =  super.handleMove();
        EditPart targetEditPart = getTargetEditPart();
        
        if (isInState(STATE_INITIAL)
                && targetEditPart instanceof NodeEditPart)
        {
            LocationRequest request = new LocationRequest(org.eclipse.jst.pagedesigner.requests.PageDesignerRequestConstants.REQ_SELECTION_TRACKER);
            request.setLocation(getLocation());
            DragTracker selectionTracker = targetEditPart.getDragTracker(request);
            setDragTracker(selectionTracker);
        }
        
        return handled;
    }

    protected Cursor calculateCursor() {
        EditPart targetEditPart = getTargetEditPart();
        if (isInState(STATE_INITIAL)
               && targetEditPart instanceof NodeEditPart)
        {
            final Cursor  nodeCursor = 
                ((NodeEditPart)targetEditPart).getCursor(getLocation());
            
            // if the edit part specified a custom cursor and there is no
            // active drag tracker (which would otherwise manage cursor)
            // then set the custom cursor
            // if we fall-through, the default behaviour will be used
            if (nodeCursor != null
                    && getDragTracker() == null)
            {
                return nodeCursor;
            }
        }
        // otherwise, use super's defaults
        return super.calculateCursor();
    }
}

Back to the top