KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > TextActionHandler


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 JavaDoc bool = (Boolean JavaDoc) 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             if (activeTextControl != null && !activeTextControl.isDisposed()) {
182                 activeTextControl.cut();
183                 return;
184             }
185             if (cutAction != null) {
186                 cutAction.runWithEvent(event);
187                 return;
188             }
189         }
190
191         /**
192          * Update state.
193          */

194         public void updateEnabledState() {
195             if (activeTextControl != null && !activeTextControl.isDisposed()) {
196                 setEnabled(activeTextControl.getSelectionCount() > 0);
197                 return;
198             }
199             if (cutAction != null) {
200                 setEnabled(cutAction.isEnabled());
201                 return;
202             }
203             setEnabled(false);
204         }
205     }
206
207     private class CopyActionHandler extends Action {
208         protected CopyActionHandler() {
209             super(CommonNavigatorMessages.Copy);
210             setId("TextCopyActionHandler");//$NON-NLS-1$
211
setEnabled(false);
212             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
213                     INavigatorHelpContextIds.TEXT_COPY_ACTION);
214         }
215
216         public void runWithEvent(Event event) {
217             if (activeTextControl != null && !activeTextControl.isDisposed()) {
218                 activeTextControl.copy();
219                 return;
220             }
221             if (copyAction != null) {
222                 copyAction.runWithEvent(event);
223                 return;
224             }
225         }
226
227         /**
228          * Update the state.
229          */

230         public void updateEnabledState() {
231             if (activeTextControl != null && !activeTextControl.isDisposed()) {
232                 setEnabled(activeTextControl.getSelectionCount() > 0);
233                 return;
234             }
235             if (copyAction != null) {
236                 setEnabled(copyAction.isEnabled());
237                 return;
238             }
239             setEnabled(false);
240         }
241     }
242
243     private class PasteActionHandler extends Action {
244         protected PasteActionHandler() {
245             super(CommonNavigatorMessages.Paste);
246             setId("TextPasteActionHandler");//$NON-NLS-1$
247
setEnabled(false);
248             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
249                     INavigatorHelpContextIds.TEXT_PASTE_ACTION);
250         }
251
252         public void runWithEvent(Event event) {
253             if (activeTextControl != null && !activeTextControl.isDisposed()) {
254                 activeTextControl.paste();
255                 return;
256             }
257             if (pasteAction != null) {
258                 pasteAction.runWithEvent(event);
259                 return;
260             }
261         }
262
263         /**
264          * Update the state
265          */

266         public void updateEnabledState() {
267             if (activeTextControl != null && !activeTextControl.isDisposed()) {
268                 setEnabled(true);
269                 return;
270             }
271             if (pasteAction != null) {
272                 setEnabled(pasteAction.isEnabled());
273                 return;
274             }
275             setEnabled(false);
276         }
277     }
278
279     private class SelectAllActionHandler extends Action {
280         protected SelectAllActionHandler() {
281             super(CommonNavigatorMessages.TextAction_selectAll);
282             setId("TextSelectAllActionHandler");//$NON-NLS-1$
283
setEnabled(false);
284             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
285                     INavigatorHelpContextIds.TEXT_SELECT_ALL_ACTION);
286         }
287
288         public void runWithEvent(Event event) {
289             if (activeTextControl != null && !activeTextControl.isDisposed()) {
290                 activeTextControl.selectAll();
291                 return;
292             }
293             if (selectAllAction != null) {
294                 selectAllAction.runWithEvent(event);
295                 return;
296             }
297         }
298
299         /**
300          * Update the state.
301          */

302         public void updateEnabledState() {
303             if (activeTextControl != null && !activeTextControl.isDisposed()) {
304                 setEnabled(true);
305                 return;
306             }
307             if (selectAllAction != null) {
308                 setEnabled(selectAllAction.isEnabled());
309                 return;
310             }
311             setEnabled(false);
312         }
313     }
314
315     /**
316      * Creates a <code>Text</code> control action handler
317      * for the global Cut, Copy, Paste, Delete, and Select All
318      * of the action bar.
319      *
320      * @param actionBar the action bar to register global
321      * action handlers for Cut, Copy, Paste, Delete,
322      * and Select All
323      */

324     public TextActionHandler(IActionBars actionBar) {
325         super();
326         actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(),
327                 textCutAction);
328         actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(),
329                 textCopyAction);
330         actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(),
331                 textPasteAction);
332         actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
333                 textSelectAllAction);
334         actionBar.setGlobalActionHandler(ActionFactory.DELETE.getId(),
335                 textDeleteAction);
336     }
337
338     /**
339      * Add a <code>Text</code> control to the handler
340      * so that the Cut, Copy, Paste, Delete, and Select All
341      * actions are redirected to it when active.
342      *
343      * @param textControl the inline <code>Text</code> control
344      */

345     public void addText(Text textControl) {
346         if (textControl == null) {
347             return;
348         }
349
350         activeTextControl = textControl;
351         textControl.addListener(SWT.Activate, textControlListener);
352         textControl.addListener(SWT.Deactivate, textControlListener);
353
354         // We really want a selection listener but it is not supported so we
355
// use a key listener and a mouse listener to know when selection changes
356
// may have occured
357
textControl.addKeyListener(keyAdapter);
358         textControl.addMouseListener(mouseAdapter);
359
360     }
361
362     /**
363      * Dispose of this action handler
364      */

365     public void dispose() {
366         setCutAction(null);
367         setCopyAction(null);
368         setPasteAction(null);
369         setSelectAllAction(null);
370         setDeleteAction(null);
371     }
372
373     /**
374      * Removes a <code>Text</code> control from the handler
375      * so that the Cut, Copy, Paste, Delete, and Select All
376      * actions are no longer redirected to it when active.
377      *
378      * @param textControl the inline <code>Text</code> control
379      */

380     public void removeText(Text textControl) {
381         if (textControl == null) {
382             return;
383         }
384
385         textControl.removeListener(SWT.Activate, textControlListener);
386         textControl.removeListener(SWT.Deactivate, textControlListener);
387
388         textControl.removeMouseListener(mouseAdapter);
389         textControl.removeKeyListener(keyAdapter);
390
391         activeTextControl = null;
392         updateActionsEnableState();
393     }
394
395     /**
396      * Set the default <code>IAction</code> handler for the Copy
397      * action. This <code>IAction</code> is run only if no active
398      * inline text control.
399      *
400      * @param action the <code>IAction</code> to run for the
401      * Copy action, or <code>null</code> if not interested.
402      */

403     public void setCopyAction(IAction action) {
404         if (copyAction == action) {
405             return;
406         }
407
408         if (copyAction != null) {
409             copyAction.removePropertyChangeListener(copyActionListener);
410         }
411
412         copyAction = action;
413
414         if (copyAction != null) {
415             copyAction.addPropertyChangeListener(copyActionListener);
416         }
417
418         textCopyAction.updateEnabledState();
419     }
420
421     /**
422      * Set the default <code>IAction</code> handler for the Cut
423      * action. This <code>IAction</code> is run only if no active
424      * inline text control.
425      *
426      * @param action the <code>IAction</code> to run for the
427      * Cut action, or <code>null</code> if not interested.
428      */

429     public void setCutAction(IAction action) {
430         if (cutAction == action) {
431             return;
432         }
433
434         if (cutAction != null) {
435             cutAction.removePropertyChangeListener(cutActionListener);
436         }
437
438         cutAction = action;
439
440         if (cutAction != null) {
441             cutAction.addPropertyChangeListener(cutActionListener);
442         }
443
444         textCutAction.updateEnabledState();
445     }
446
447     /**
448      * Set the default <code>IAction</code> handler for the Paste
449      * action. This <code>IAction</code> is run only if no active
450      * inline text control.
451      *
452      * @param action the <code>IAction</code> to run for the
453      * Paste action, or <code>null</code> if not interested.
454      */

455     public void setPasteAction(IAction action) {
456         if (pasteAction == action) {
457             return;
458         }
459
460         if (pasteAction != null) {
461             pasteAction.removePropertyChangeListener(pasteActionListener);
462         }
463
464         pasteAction = action;
465
466         if (pasteAction != null) {
467             pasteAction.addPropertyChangeListener(pasteActionListener);
468         }
469
470         textPasteAction.updateEnabledState();
471     }
472
473     /**
474      * Set the default <code>IAction</code> handler for the Select All
475      * action. This <code>IAction</code> is run only if no active
476      * inline text control.
477      *
478      * @param action the <code>IAction</code> to run for the
479      * Select All action, or <code>null</code> if not interested.
480      */

481     public void setSelectAllAction(IAction action) {
482         if (selectAllAction == action) {
483             return;
484         }
485
486         if (selectAllAction != null) {
487             selectAllAction
488                     .removePropertyChangeListener(selectAllActionListener);
489         }
490
491         selectAllAction = action;
492
493         if (selectAllAction != null) {
494             selectAllAction.addPropertyChangeListener(selectAllActionListener);
495         }
496
497         textSelectAllAction.updateEnabledState();
498     }
499
500     /**
501      * Set the default <code>IAction</code> handler for the Delete
502      * action. This <code>IAction</code> is run only if no active
503      * inline text control.
504      *
505      * @param action the <code>IAction</code> to run for the
506      * Delete action, or <code>null</code> if not interested.
507      */

508     public void setDeleteAction(IAction action) {
509         if (deleteAction == action) {
510             return;
511         }
512
513         if (deleteAction != null) {
514             deleteAction.removePropertyChangeListener(deleteActionListener);
515         }
516
517         deleteAction = action;
518
519         if (deleteAction != null) {
520             deleteAction.addPropertyChangeListener(deleteActionListener);
521         }
522
523         textDeleteAction.updateEnabledState();
524     }
525
526     /**
527      * Update the enable state of the Cut, Copy,
528      * Paste, Delete, and Select All action handlers
529      */

530     private void updateActionsEnableState() {
531         textCutAction.updateEnabledState();
532         textCopyAction.updateEnabledState();
533         textPasteAction.updateEnabledState();
534         textSelectAllAction.updateEnabledState();
535         textDeleteAction.updateEnabledState();
536     }
537 }
538
539
Popular Tags