1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2005, 2006 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.ui.internal.navigator;
|
12
|
|
13
|
import org.eclipse.jface.action.Action;
|
14
|
import org.eclipse.jface.action.IAction;
|
15
|
import org.eclipse.jface.util.IPropertyChangeListener;
|
16
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
17
|
import org.eclipse.swt.SWT;
|
18
|
import org.eclipse.swt.events.KeyAdapter;
|
19
|
import org.eclipse.swt.events.KeyEvent;
|
20
|
import org.eclipse.swt.events.MouseAdapter;
|
21
|
import org.eclipse.swt.events.MouseEvent;
|
22
|
import org.eclipse.swt.widgets.Event;
|
23
|
import org.eclipse.swt.widgets.Listener;
|
24
|
import org.eclipse.swt.widgets.Text;
|
25
|
import org.eclipse.ui.IActionBars;
|
26
|
import org.eclipse.ui.PlatformUI;
|
27
|
import org.eclipse.ui.actions.ActionFactory;
|
28
|
|
29
|
/**
|
30
|
* Handles the redirection of the global Cut, Copy, Paste, and
|
31
|
* Select All actions to either the current inline text control
|
32
|
* or the part's supplied action handler.
|
33
|
* <p>
|
34
|
* This class may be instantiated; it is not intended to be subclassed.
|
35
|
* </p><p>
|
36
|
* Example usage:
|
37
|
* <pre>
|
38
|
* textActionHandler = new TextActionHandler(this.getViewSite().getActionBars());
|
39
|
* textActionHandler.addText((Text)textCellEditor1.getControl());
|
40
|
* textActionHandler.addText((Text)textCellEditor2.getControl());
|
41
|
* textActionHandler.setSelectAllAction(selectAllAction);
|
42
|
* </pre>
|
43
|
* </p>
|
44
|
*/
|
45
|
public class TextActionHandler {
|
46
|
private DeleteActionHandler textDeleteAction = new DeleteActionHandler();
|
47
|
|
48
|
private CutActionHandler textCutAction = new CutActionHandler();
|
49
|
|
50
|
private CopyActionHandler textCopyAction = new CopyActionHandler();
|
51
|
|
52
|
private PasteActionHandler textPasteAction = new PasteActionHandler();
|
53
|
|
54
|
private SelectAllActionHandler textSelectAllAction = new SelectAllActionHandler();
|
55
|
|
56
|
private IAction deleteAction;
|
57
|
|
58
|
private IAction cutAction;
|
59
|
|
60
|
private IAction copyAction;
|
61
|
|
62
|
private IAction pasteAction;
|
63
|
|
64
|
private IAction selectAllAction;
|
65
|
|
66
|
private IPropertyChangeListener deleteActionListener = new PropertyChangeListener(
|
67
|
textDeleteAction);
|
68
|
|
69
|
private IPropertyChangeListener cutActionListener = new PropertyChangeListener(
|
70
|
textCutAction);
|
71
|
|
72
|
private IPropertyChangeListener copyActionListener = new PropertyChangeListener(
|
73
|
textCopyAction);
|
74
|
|
75
|
private IPropertyChangeListener pasteActionListener = new PropertyChangeListener(
|
76
|
textPasteAction);
|
77
|
|
78
|
private IPropertyChangeListener selectAllActionListener = new PropertyChangeListener(
|
79
|
textSelectAllAction);
|
80
|
|
81
|
private Listener textControlListener = new TextControlListener();
|
82
|
|
83
|
private Text activeTextControl;
|
84
|
|
85
|
private MouseAdapter mouseAdapter = new MouseAdapter() {
|
86
|
public void mouseUp(MouseEvent e) {
|
87
|
updateActionsEnableState();
|
88
|
}
|
89
|
};
|
90
|
|
91
|
private KeyAdapter keyAdapter = new KeyAdapter() {
|
92
|
public void keyReleased(KeyEvent e) {
|
93
|
updateActionsEnableState();
|
94
|
}
|
95
|
};
|
96
|
|
97
|
private class TextControlListener implements Listener {
|
98
|
public void handleEvent(Event event) {
|
99
|
switch (event.type) {
|
100
|
case SWT.Activate:
|
101
|
activeTextControl = (Text) event.widget;
|
102
|
updateActionsEnableState();
|
103
|
break;
|
104
|
case SWT.Deactivate:
|
105
|
activeTextControl = null;
|
106
|
updateActionsEnableState();
|
107
|
break;
|
108
|
default:
|
109
|
break;
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
|
114
|
private class PropertyChangeListener implements IPropertyChangeListener {
|
115
|
private IAction actionHandler;
|
116
|
|
117
|
protected PropertyChangeListener(IAction actionHandler) {
|
118
|
super();
|
119
|
this.actionHandler = actionHandler;
|
120
|
}
|
121
|
|
122
|
public void propertyChange(PropertyChangeEvent event) {
|
123
|
if (activeTextControl != null) {
|
124
|
return;
|
125
|
}
|
126
|
if (event.getProperty().equals(IAction.ENABLED)) {
|
127
|
Boolean bool = (Boolean) event.getNewValue();
|
128
|
actionHandler.setEnabled(bool.booleanValue());
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
|
133
|
private class DeleteActionHandler extends Action {
|
134
|
protected DeleteActionHandler() {
|
135
|
super(CommonNavigatorMessages.Delete);
|
136
|
setId("TextDeleteActionHandler");//$NON-NLS-1$
|
137
|
setEnabled(false);
|
138
|
PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
|
139
|
INavigatorHelpContextIds.TEXT_DELETE_ACTION);
|
140
|
}
|
141
|
|
142
|
public void runWithEvent(Event event) {
|
143
|
if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
144
|
activeTextControl.clearSelection();
|
145
|
return;
|
146
|
}
|
147
|
if (deleteAction != null) {
|
148
|
deleteAction.runWithEvent(event);
|
149
|
return;
|
150
|
}
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Update state.
|
155
|
*/
|
156
|
public void updateEnabledState() {
|
157
|
if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
158
|
setEnabled(activeTextControl.getSelectionCount() > 0
|
159
|
|| activeTextControl.getCaretPosition() < activeTextControl
|
160
|
.getCharCount());
|
161
|
return;
|
162
|
}
|
163
|
if (deleteAction != null) {
|
164
|
setEnabled(deleteAction.isEnabled());
|
165
|
return;
|
166
|
}
|
167
|
setEnabled(false);
|
168
|
}
|
169
|
}
|
170
|
|
171
|
private class CutActionHandler extends Action {
|
172
|
protected CutActionHandler() {
|
173
|
super(CommonNavigatorMessages.Cut);
|
174
|
setId("TextCutActionHandler");//$NON-NLS-1$
|
175
|
setEnabled(false);
|
176
|
PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
|
177
|
INavigatorHelpContextIds.TEXT_CUT_ACTION);
|
178
|
}
|
179
|
|
180
|
public void runWithEvent(Event event) {
|
181
|
// RAP [bmichalik]: text action
|
182
|
// if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
183
|
// activeTextControl.cut();
|
184
|
// return;
|
185
|
// }
|
186
|
if (cutAction != null) {
|
187
|
cutAction.runWithEvent(event);
|
188
|
return;
|
189
|
}
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Update state.
|
194
|
*/
|
195
|
public void updateEnabledState() {
|
196
|
if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
197
|
setEnabled(activeTextControl.getSelectionCount() > 0);
|
198
|
return;
|
199
|
}
|
200
|
if (cutAction != null) {
|
201
|
setEnabled(cutAction.isEnabled());
|
202
|
return;
|
203
|
}
|
204
|
setEnabled(false);
|
205
|
}
|
206
|
}
|
207
|
|
208
|
private class CopyActionHandler extends Action {
|
209
|
protected CopyActionHandler() {
|
210
|
super(CommonNavigatorMessages.Copy);
|
211
|
setId("TextCopyActionHandler");//$NON-NLS-1$
|
212
|
setEnabled(false);
|
213
|
PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
|
214
|
INavigatorHelpContextIds.TEXT_COPY_ACTION);
|
215
|
}
|
216
|
|
217
|
public void runWithEvent(Event event) {
|
218
|
// RAP [bmichalik]: text action
|
219
|
// if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
220
|
// activeTextControl.copy();
|
221
|
// return;
|
222
|
// }
|
223
|
if (copyAction != null) {
|
224
|
copyAction.runWithEvent(event);
|
225
|
return;
|
226
|
}
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Update the state.
|
231
|
*/
|
232
|
public void updateEnabledState() {
|
233
|
if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
234
|
setEnabled(activeTextControl.getSelectionCount() > 0);
|
235
|
return;
|
236
|
}
|
237
|
if (copyAction != null) {
|
238
|
setEnabled(copyAction.isEnabled());
|
239
|
return;
|
240
|
}
|
241
|
setEnabled(false);
|
242
|
}
|
243
|
}
|
244
|
|
245
|
private class PasteActionHandler extends Action {
|
246
|
protected PasteActionHandler() {
|
247
|
super(CommonNavigatorMessages.Paste);
|
248
|
setId("TextPasteActionHandler");//$NON-NLS-1$
|
249
|
setEnabled(false);
|
250
|
PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
|
251
|
INavigatorHelpContextIds.TEXT_PASTE_ACTION);
|
252
|
}
|
253
|
|
254
|
public void runWithEvent(Event event) {
|
255
|
// RAP [bmichalik]: text action
|
256
|
// if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
257
|
// activeTextControl.paste();
|
258
|
// return;
|
259
|
// }
|
260
|
if (pasteAction != null) {
|
261
|
pasteAction.runWithEvent(event);
|
262
|
return;
|
263
|
}
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Update the state
|
268
|
*/
|
269
|
public void updateEnabledState() {
|
270
|
if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
271
|
setEnabled(true);
|
272
|
return;
|
273
|
}
|
274
|
if (pasteAction != null) {
|
275
|
setEnabled(pasteAction.isEnabled());
|
276
|
return;
|
277
|
}
|
278
|
setEnabled(false);
|
279
|
}
|
280
|
}
|
281
|
|
282
|
private class SelectAllActionHandler extends Action {
|
283
|
protected SelectAllActionHandler() {
|
284
|
super(CommonNavigatorMessages.TextAction_selectAll);
|
285
|
setId("TextSelectAllActionHandler");//$NON-NLS-1$
|
286
|
setEnabled(false);
|
287
|
PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
|
288
|
INavigatorHelpContextIds.TEXT_SELECT_ALL_ACTION);
|
289
|
}
|
290
|
|
291
|
public void runWithEvent(Event event) {
|
292
|
if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
293
|
activeTextControl.selectAll();
|
294
|
return;
|
295
|
}
|
296
|
if (selectAllAction != null) {
|
297
|
selectAllAction.runWithEvent(event);
|
298
|
return;
|
299
|
}
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Update the state.
|
304
|
*/
|
305
|
public void updateEnabledState() {
|
306
|
if (activeTextControl != null && !activeTextControl.isDisposed()) {
|
307
|
setEnabled(true);
|
308
|
return;
|
309
|
}
|
310
|
if (selectAllAction != null) {
|
311
|
setEnabled(selectAllAction.isEnabled());
|
312
|
return;
|
313
|
}
|
314
|
setEnabled(false);
|
315
|
}
|
316
|
}
|
317
|
|
318
|
/**
|
319
|
* Creates a <code>Text</code> control action handler
|
320
|
* for the global Cut, Copy, Paste, Delete, and Select All
|
321
|
* of the action bar.
|
322
|
*
|
323
|
* @param actionBar the action bar to register global
|
324
|
* action handlers for Cut, Copy, Paste, Delete,
|
325
|
* and Select All
|
326
|
*/
|
327
|
public TextActionHandler(IActionBars actionBar) {
|
328
|
super();
|
329
|
actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(),
|
330
|
textCutAction);
|
331
|
actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(),
|
332
|
textCopyAction);
|
333
|
actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(),
|
334
|
textPasteAction);
|
335
|
actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
|
336
|
textSelectAllAction);
|
337
|
actionBar.setGlobalActionHandler(ActionFactory.DELETE.getId(),
|
338
|
textDeleteAction);
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* Add a <code>Text</code> control to the handler
|
343
|
* so that the Cut, Copy, Paste, Delete, and Select All
|
344
|
* actions are redirected to it when active.
|
345
|
*
|
346
|
* @param textControl the inline <code>Text</code> control
|
347
|
*/
|
348
|
public void addText(Text textControl) {
|
349
|
if (textControl == null) {
|
350
|
return;
|
351
|
}
|
352
|
|
353
|
activeTextControl = textControl;
|
354
|
textControl.addListener(SWT.Activate, textControlListener);
|
355
|
textControl.addListener(SWT.Deactivate, textControlListener);
|
356
|
|
357
|
// We really want a selection listener but it is not supported so we
|
358
|
// use a key listener and a mouse listener to know when selection changes
|
359
|
// may have occured
|
360
|
textControl.addKeyListener(keyAdapter);
|
361
|
textControl.addMouseListener(mouseAdapter);
|
362
|
|
363
|
}
|
364
|
|
365
|
/**
|
366
|
* Dispose of this action handler
|
367
|
*/
|
368
|
public void dispose() {
|
369
|
setCutAction(null);
|
370
|
setCopyAction(null);
|
371
|
setPasteAction(null);
|
372
|
setSelectAllAction(null);
|
373
|
setDeleteAction(null);
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* Removes a <code>Text</code> control from the handler
|
378
|
* so that the Cut, Copy, Paste, Delete, and Select All
|
379
|
* actions are no longer redirected to it when active.
|
380
|
*
|
381
|
* @param textControl the inline <code>Text</code> control
|
382
|
*/
|
383
|
public void removeText(Text textControl) {
|
384
|
if (textControl == null) {
|
385
|
return;
|
386
|
}
|
387
|
|
388
|
textControl.removeListener(SWT.Activate, textControlListener);
|
389
|
textControl.removeListener(SWT.Deactivate, textControlListener);
|
390
|
|
391
|
textControl.removeMouseListener(mouseAdapter);
|
392
|
textControl.removeKeyListener(keyAdapter);
|
393
|
|
394
|
activeTextControl = null;
|
395
|
updateActionsEnableState();
|
396
|
}
|
397
|
|
398
|
/**
|
399
|
* Set the default <code>IAction</code> handler for the Copy
|
400
|
* action. This <code>IAction</code> is run only if no active
|
401
|
* inline text control.
|
402
|
*
|
403
|
* @param action the <code>IAction</code> to run for the
|
404
|
* Copy action, or <code>null</code> if not interested.
|
405
|
*/
|
406
|
public void setCopyAction(IAction action) {
|
407
|
if (copyAction == action) {
|
408
|
return;
|
409
|
}
|
410
|
|
411
|
if (copyAction != null) {
|
412
|
copyAction.removePropertyChangeListener(copyActionListener);
|
413
|
}
|
414
|
|
415
|
copyAction = action;
|
416
|
|
417
|
if (copyAction != null) {
|
418
|
copyAction.addPropertyChangeListener(copyActionListener);
|
419
|
}
|
420
|
|
421
|
textCopyAction.updateEnabledState();
|
422
|
}
|
423
|
|
424
|
/**
|
425
|
* Set the default <code>IAction</code> handler for the Cut
|
426
|
* action. This <code>IAction</code> is run only if no active
|
427
|
* inline text control.
|
428
|
*
|
429
|
* @param action the <code>IAction</code> to run for the
|
430
|
* Cut action, or <code>null</code> if not interested.
|
431
|
*/
|
432
|
public void setCutAction(IAction action) {
|
433
|
if (cutAction == action) {
|
434
|
return;
|
435
|
}
|
436
|
|
437
|
if (cutAction != null) {
|
438
|
cutAction.removePropertyChangeListener(cutActionListener);
|
439
|
}
|
440
|
|
441
|
cutAction = action;
|
442
|
|
443
|
if (cutAction != null) {
|
444
|
cutAction.addPropertyChangeListener(cutActionListener);
|
445
|
}
|
446
|
|
447
|
textCutAction.updateEnabledState();
|
448
|
}
|
449
|
|
450
|
/**
|
451
|
* Set the default <code>IAction</code> handler for the Paste
|
452
|
* action. This <code>IAction</code> is run only if no active
|
453
|
* inline text control.
|
454
|
*
|
455
|
* @param action the <code>IAction</code> to run for the
|
456
|
* Paste action, or <code>null</code> if not interested.
|
457
|
*/
|
458
|
public void setPasteAction(IAction action) {
|
459
|
if (pasteAction == action) {
|
460
|
return;
|
461
|
}
|
462
|
|
463
|
if (pasteAction != null) {
|
464
|
pasteAction.removePropertyChangeListener(pasteActionListener);
|
465
|
}
|
466
|
|
467
|
pasteAction = action;
|
468
|
|
469
|
if (pasteAction != null) {
|
470
|
pasteAction.addPropertyChangeListener(pasteActionListener);
|
471
|
}
|
472
|
|
473
|
textPasteAction.updateEnabledState();
|
474
|
}
|
475
|
|
476
|
/**
|
477
|
* Set the default <code>IAction</code> handler for the Select All
|
478
|
* action. This <code>IAction</code> is run only if no active
|
479
|
* inline text control.
|
480
|
*
|
481
|
* @param action the <code>IAction</code> to run for the
|
482
|
* Select All action, or <code>null</code> if not interested.
|
483
|
*/
|
484
|
public void setSelectAllAction(IAction action) {
|
485
|
if (selectAllAction == action) {
|
486
|
return;
|
487
|
}
|
488
|
|
489
|
if (selectAllAction != null) {
|
490
|
selectAllAction
|
491
|
.removePropertyChangeListener(selectAllActionListener);
|
492
|
}
|
493
|
|
494
|
selectAllAction = action;
|
495
|
|
496
|
if (selectAllAction != null) {
|
497
|
selectAllAction.addPropertyChangeListener(selectAllActionListener);
|
498
|
}
|
499
|
|
500
|
textSelectAllAction.updateEnabledState();
|
501
|
}
|
502
|
|
503
|
/**
|
504
|
* Set the default <code>IAction</code> handler for the Delete
|
505
|
* action. This <code>IAction</code> is run only if no active
|
506
|
* inline text control.
|
507
|
*
|
508
|
* @param action the <code>IAction</code> to run for the
|
509
|
* Delete action, or <code>null</code> if not interested.
|
510
|
*/
|
511
|
public void setDeleteAction(IAction action) {
|
512
|
if (deleteAction == action) {
|
513
|
return;
|
514
|
}
|
515
|
|
516
|
if (deleteAction != null) {
|
517
|
deleteAction.removePropertyChangeListener(deleteActionListener);
|
518
|
}
|
519
|
|
520
|
deleteAction = action;
|
521
|
|
522
|
if (deleteAction != null) {
|
523
|
deleteAction.addPropertyChangeListener(deleteActionListener);
|
524
|
}
|
525
|
|
526
|
textDeleteAction.updateEnabledState();
|
527
|
}
|
528
|
|
529
|
/**
|
530
|
* Update the enable state of the Cut, Copy,
|
531
|
* Paste, Delete, and Select All action handlers
|
532
|
*/
|
533
|
private void updateActionsEnableState() {
|
534
|
textCutAction.updateEnabledState();
|
535
|
textCopyAction.updateEnabledState();
|
536
|
textPasteAction.updateEnabledState();
|
537
|
textSelectAllAction.updateEnabledState();
|
538
|
textDeleteAction.updateEnabledState();
|
539
|
}
|
540
|
}
|
541
|
|