KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > TextActionHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.actions;
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.graphics.Point;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.swt.widgets.Listener;
25 import org.eclipse.swt.widgets.Text;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
29 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
30
31 /**
32  * Handles the redirection of the global Cut, Copy, Paste, and
33  * Select All actions to either the current inline text control
34  * or the part's supplied action handler.
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p><p>
38  * Example usage:
39  * <pre>
40  * textActionHandler = new TextActionHandler(this.getViewSite().getActionBars());
41  * textActionHandler.addText((Text)textCellEditor1.getControl());
42  * textActionHandler.addText((Text)textCellEditor2.getControl());
43  * textActionHandler.setSelectAllAction(selectAllAction);
44  * </pre>
45  * </p>
46  */

47 public class TextActionHandler {
48     private DeleteActionHandler textDeleteAction = new DeleteActionHandler();
49
50     private CutActionHandler textCutAction = new CutActionHandler();
51
52     private CopyActionHandler textCopyAction = new CopyActionHandler();
53
54     private PasteActionHandler textPasteAction = new PasteActionHandler();
55
56     private SelectAllActionHandler textSelectAllAction = new SelectAllActionHandler();
57
58     private IAction deleteAction;
59
60     private IAction cutAction;
61
62     private IAction copyAction;
63
64     private IAction pasteAction;
65
66     private IAction selectAllAction;
67
68     private IPropertyChangeListener deleteActionListener = new PropertyChangeListener(
69             textDeleteAction);
70
71     private IPropertyChangeListener cutActionListener = new PropertyChangeListener(
72             textCutAction);
73
74     private IPropertyChangeListener copyActionListener = new PropertyChangeListener(
75             textCopyAction);
76
77     private IPropertyChangeListener pasteActionListener = new PropertyChangeListener(
78             textPasteAction);
79
80     private IPropertyChangeListener selectAllActionListener = new PropertyChangeListener(
81             textSelectAllAction);
82
83     private Listener textControlListener = new TextControlListener();
84
85     private Text activeTextControl;
86
87     private MouseAdapter mouseAdapter = new MouseAdapter() {
88         public void mouseUp(MouseEvent e) {
89             updateActionsEnableState();
90         }
91     };
92
93     private KeyAdapter keyAdapter = new KeyAdapter() {
94         public void keyReleased(KeyEvent e) {
95             updateActionsEnableState();
96         }
97     };
98
99     private class TextControlListener implements Listener {
100         public void handleEvent(Event event) {
101             switch (event.type) {
102             case SWT.Activate:
103                 activeTextControl = (Text) event.widget;
104                 updateActionsEnableState();
105                 break;
106             case SWT.Deactivate:
107                 activeTextControl = null;
108                 updateActionsEnableState();
109                 break;
110             default:
111                 break;
112             }
113         }
114     }
115
116     private class PropertyChangeListener implements IPropertyChangeListener {
117         private IAction actionHandler;
118
119         protected PropertyChangeListener(IAction actionHandler) {
120             super();
121             this.actionHandler = actionHandler;
122         }
123
124         public void propertyChange(PropertyChangeEvent event) {
125             if (activeTextControl != null) {
126                 return;
127             }
128             if (event.getProperty().equals(IAction.ENABLED)) {
129                 Boolean JavaDoc bool = (Boolean JavaDoc) event.getNewValue();
130                 actionHandler.setEnabled(bool.booleanValue());
131             }
132         }
133     }
134
135     private class DeleteActionHandler extends Action {
136         protected DeleteActionHandler() {
137             super(IDEWorkbenchMessages.Delete);
138             setId("TextDeleteActionHandler");//$NON-NLS-1$
139
setEnabled(false);
140             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
141                     IIDEHelpContextIds.TEXT_DELETE_ACTION);
142         }
143
144         public void runWithEvent(Event event) {
145             if (activeTextControl != null && !activeTextControl.isDisposed()) {
146                 String JavaDoc text = activeTextControl.getText();
147                 Point selection = activeTextControl.getSelection();
148                 if (selection.y == selection.x) {
149                     ++selection.y;
150                 }
151                 if (selection.y > text.length()) {
152                     return;
153                 }
154                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(text.substring(0,
155                         selection.x));
156                 buf.append(text.substring(selection.y));
157                 activeTextControl.setText(buf.toString());
158                 activeTextControl.setSelection(selection.x, selection.x);
159                 updateActionsEnableState();
160                 return;
161             }
162             if (deleteAction != null) {
163                 deleteAction.runWithEvent(event);
164                 return;
165             }
166         }
167
168         /**
169          * Update state.
170          */

171         public void updateEnabledState() {
172             if (activeTextControl != null && !activeTextControl.isDisposed()) {
173                 setEnabled(activeTextControl.getSelectionCount() > 0
174                         || activeTextControl.getCaretPosition() < activeTextControl
175                                 .getCharCount());
176                 return;
177             }
178             if (deleteAction != null) {
179                 setEnabled(deleteAction.isEnabled());
180                 return;
181             }
182             setEnabled(false);
183         }
184     }
185
186     private class CutActionHandler extends Action {
187         protected CutActionHandler() {
188             super(IDEWorkbenchMessages.Cut);
189             setId("TextCutActionHandler");//$NON-NLS-1$
190
setEnabled(false);
191             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
192                     IIDEHelpContextIds.TEXT_CUT_ACTION);
193         }
194
195         public void runWithEvent(Event event) {
196             if (activeTextControl != null && !activeTextControl.isDisposed()) {
197                 activeTextControl.cut();
198                 updateActionsEnableState();
199                 return;
200             }
201             if (cutAction != null) {
202                 cutAction.runWithEvent(event);
203                 return;
204             }
205         }
206
207         /**
208          * Update state.
209          */

210         public void updateEnabledState() {
211             if (activeTextControl != null && !activeTextControl.isDisposed()) {
212                 setEnabled(activeTextControl.getSelectionCount() > 0);
213                 return;
214             }
215             if (cutAction != null) {
216                 setEnabled(cutAction.isEnabled());
217                 return;
218             }
219             setEnabled(false);
220         }
221     }
222
223     private class CopyActionHandler extends Action {
224         protected CopyActionHandler() {
225             super(IDEWorkbenchMessages.Copy);
226             setId("TextCopyActionHandler");//$NON-NLS-1$
227
setEnabled(false);
228             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
229                     IIDEHelpContextIds.TEXT_COPY_ACTION);
230         }
231
232         public void runWithEvent(Event event) {
233             if (activeTextControl != null && !activeTextControl.isDisposed()) {
234                 activeTextControl.copy();
235                 updateActionsEnableState();
236                 return;
237             }
238             if (copyAction != null) {
239                 copyAction.runWithEvent(event);
240                 return;
241             }
242         }
243
244         /**
245          * Update the state.
246          */

247         public void updateEnabledState() {
248             if (activeTextControl != null && !activeTextControl.isDisposed()) {
249                 setEnabled(activeTextControl.getSelectionCount() > 0);
250                 return;
251             }
252             if (copyAction != null) {
253                 setEnabled(copyAction.isEnabled());
254                 return;
255             }
256             setEnabled(false);
257         }
258     }
259
260     private class PasteActionHandler extends Action {
261         protected PasteActionHandler() {
262             super(IDEWorkbenchMessages.Paste);
263             setId("TextPasteActionHandler");//$NON-NLS-1$
264
setEnabled(false);
265             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
266                     IIDEHelpContextIds.TEXT_PASTE_ACTION);
267         }
268
269         public void runWithEvent(Event event) {
270             if (activeTextControl != null && !activeTextControl.isDisposed()) {
271                 activeTextControl.paste();
272                 updateActionsEnableState();
273                 return;
274             }
275             if (pasteAction != null) {
276                 pasteAction.runWithEvent(event);
277                 return;
278             }
279         }
280
281         /**
282          * Update the state
283          */

284         public void updateEnabledState() {
285             if (activeTextControl != null && !activeTextControl.isDisposed()) {
286                 setEnabled(true);
287                 return;
288             }
289             if (pasteAction != null) {
290                 setEnabled(pasteAction.isEnabled());
291                 return;
292             }
293             setEnabled(false);
294         }
295     }
296
297     private class SelectAllActionHandler extends Action {
298         protected SelectAllActionHandler() {
299             super(IDEWorkbenchMessages.TextAction_selectAll);
300             setId("TextSelectAllActionHandler");//$NON-NLS-1$
301
setEnabled(false);
302             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
303                     IIDEHelpContextIds.TEXT_SELECT_ALL_ACTION);
304         }
305
306         public void runWithEvent(Event event) {
307             if (activeTextControl != null && !activeTextControl.isDisposed()) {
308                 activeTextControl.selectAll();
309                 updateActionsEnableState();
310                 return;
311             }
312             if (selectAllAction != null) {
313                 selectAllAction.runWithEvent(event);
314                 return;
315             }
316         }
317
318         /**
319          * Update the state.
320          */

321         public void updateEnabledState() {
322             if (activeTextControl != null && !activeTextControl.isDisposed()) {
323                 setEnabled(true);
324                 return;
325             }
326             if (selectAllAction != null) {
327                 setEnabled(selectAllAction.isEnabled());
328                 return;
329             }
330             setEnabled(false);
331         }
332     }
333
334     /**
335      * Creates a <code>Text</code> control action handler
336      * for the global Cut, Copy, Paste, Delete, and Select All
337      * of the action bar.
338      *
339      * @param actionBar the action bar to register global
340      * action handlers for Cut, Copy, Paste, Delete,
341      * and Select All
342      */

343     public TextActionHandler(IActionBars actionBar) {
344         super();
345         actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(),
346                 textCutAction);
347         actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(),
348                 textCopyAction);
349         actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(),
350                 textPasteAction);
351         actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
352                 textSelectAllAction);
353         actionBar.setGlobalActionHandler(ActionFactory.DELETE.getId(),
354                 textDeleteAction);
355     }
356
357     /**
358      * Add a <code>Text</code> control to the handler
359      * so that the Cut, Copy, Paste, Delete, and Select All
360      * actions are redirected to it when active.
361      *
362      * @param textControl the inline <code>Text</code> control
363      */

364     public void addText(Text textControl) {
365         if (textControl == null) {
366             return;
367         }
368
369         activeTextControl = textControl;
370         textControl.addListener(SWT.Activate, textControlListener);
371         textControl.addListener(SWT.Deactivate, textControlListener);
372
373         // We really want a selection listener but it is not supported so we
374
// use a key listener and a mouse listener to know when selection changes
375
// may have occured
376
textControl.addKeyListener(keyAdapter);
377         textControl.addMouseListener(mouseAdapter);
378
379     }
380
381     /**
382      * Dispose of this action handler
383      */

384     public void dispose() {
385         setCutAction(null);
386         setCopyAction(null);
387         setPasteAction(null);
388         setSelectAllAction(null);
389         setDeleteAction(null);
390     }
391
392     /**
393      * Removes a <code>Text</code> control from the handler
394      * so that the Cut, Copy, Paste, Delete, and Select All
395      * actions are no longer redirected to it when active.
396      *
397      * @param textControl the inline <code>Text</code> control
398      */

399     public void removeText(Text textControl) {
400         if (textControl == null) {
401             return;
402         }
403
404         textControl.removeListener(SWT.Activate, textControlListener);
405         textControl.removeListener(SWT.Deactivate, textControlListener);
406
407         textControl.removeMouseListener(mouseAdapter);
408         textControl.removeKeyListener(keyAdapter);
409
410         activeTextControl = null;
411         updateActionsEnableState();
412     }
413
414     /**
415      * Set the default <code>IAction</code> handler for the Copy
416      * action. This <code>IAction</code> is run only if no active
417      * inline text control.
418      *
419      * @param action the <code>IAction</code> to run for the
420      * Copy action, or <code>null</code> if not interested.
421      */

422     public void setCopyAction(IAction action) {
423         if (copyAction == action) {
424             return;
425         }
426
427         if (copyAction != null) {
428             copyAction.removePropertyChangeListener(copyActionListener);
429         }
430
431         copyAction = action;
432
433         if (copyAction != null) {
434             copyAction.addPropertyChangeListener(copyActionListener);
435         }
436
437         textCopyAction.updateEnabledState();
438     }
439
440     /**
441      * Set the default <code>IAction</code> handler for the Cut
442      * action. This <code>IAction</code> is run only if no active
443      * inline text control.
444      *
445      * @param action the <code>IAction</code> to run for the
446      * Cut action, or <code>null</code> if not interested.
447      */

448     public void setCutAction(IAction action) {
449         if (cutAction == action) {
450             return;
451         }
452
453         if (cutAction != null) {
454             cutAction.removePropertyChangeListener(cutActionListener);
455         }
456
457         cutAction = action;
458
459         if (cutAction != null) {
460             cutAction.addPropertyChangeListener(cutActionListener);
461         }
462
463         textCutAction.updateEnabledState();
464     }
465
466     /**
467      * Set the default <code>IAction</code> handler for the Paste
468      * action. This <code>IAction</code> is run only if no active
469      * inline text control.
470      *
471      * @param action the <code>IAction</code> to run for the
472      * Paste action, or <code>null</code> if not interested.
473      */

474     public void setPasteAction(IAction action) {
475         if (pasteAction == action) {
476             return;
477         }
478
479         if (pasteAction != null) {
480             pasteAction.removePropertyChangeListener(pasteActionListener);
481         }
482
483         pasteAction = action;
484
485         if (pasteAction != null) {
486             pasteAction.addPropertyChangeListener(pasteActionListener);
487         }
488
489         textPasteAction.updateEnabledState();
490     }
491
492     /**
493      * Set the default <code>IAction</code> handler for the Select All
494      * action. This <code>IAction</code> is run only if no active
495      * inline text control.
496      *
497      * @param action the <code>IAction</code> to run for the
498      * Select All action, or <code>null</code> if not interested.
499      */

500     public void setSelectAllAction(IAction action) {
501         if (selectAllAction == action) {
502             return;
503         }
504
505         if (selectAllAction != null) {
506             selectAllAction
507                     .removePropertyChangeListener(selectAllActionListener);
508         }
509
510         selectAllAction = action;
511
512         if (selectAllAction != null) {
513             selectAllAction.addPropertyChangeListener(selectAllActionListener);
514         }
515
516         textSelectAllAction.updateEnabledState();
517     }
518
519     /**
520      * Set the default <code>IAction</code> handler for the Delete
521      * action. This <code>IAction</code> is run only if no active
522      * inline text control.
523      *
524      * @param action the <code>IAction</code> to run for the
525      * Delete action, or <code>null</code> if not interested.
526      */

527     public void setDeleteAction(IAction action) {
528         if (deleteAction == action) {
529             return;
530         }
531
532         if (deleteAction != null) {
533             deleteAction.removePropertyChangeListener(deleteActionListener);
534         }
535
536         deleteAction = action;
537
538         if (deleteAction != null) {
539             deleteAction.addPropertyChangeListener(deleteActionListener);
540         }
541
542         textDeleteAction.updateEnabledState();
543     }
544
545     /**
546      * Update the enable state of the Cut, Copy,
547      * Paste, Delete, and Select All action handlers
548      */

549     private void updateActionsEnableState() {
550         textCutAction.updateEnabledState();
551         textCopyAction.updateEnabledState();
552         textPasteAction.updateEnabledState();
553         textSelectAllAction.updateEnabledState();
554         textDeleteAction.updateEnabledState();
555     }
556 }
557
Popular Tags