KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > 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.internal.navigator.resources.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.actions.ActionFactory;
29
30 /**
31  * Handles the redirection of the global Cut, Copy, Paste, and
32  * Select All actions to either the current inline text control
33  * or the part's supplied action handler.
34  * <p>
35  * This class may be instantiated; it is not intended to be subclassed.
36  * </p><p>
37  * Example usage:
38  * <pre>
39  * textActionHandler = new TextActionHandler(this.getViewSite().getActionBars());
40  * textActionHandler.addText((Text)textCellEditor1.getControl());
41  * textActionHandler.addText((Text)textCellEditor2.getControl());
42  * textActionHandler.setSelectAllAction(selectAllAction);
43  * </pre>
44  * </p>
45  */

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

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

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

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

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

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

344     public TextActionHandler(IActionBars theActionBars) {
345         super(theActionBars);
346         actionBars = theActionBars;
347         updateActionBars();
348     }
349
350     /**
351      *
352      */

353     public void updateActionBars() {
354         actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(),
355                 textCutAction);
356         actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),
357                 textCopyAction);
358         actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(),
359                 textPasteAction);
360         actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
361                 textSelectAllAction);
362         actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),
363                 textDeleteAction);
364     }
365
366     /**
367      * Add a <code>Text</code> control to the handler
368      * so that the Cut, Copy, Paste, Delete, and Select All
369      * actions are redirected to it when active.
370      *
371      * @param textControl the inline <code>Text</code> control
372      */

373     public void addText(Text textControl) {
374         if (textControl == null) {
375             return;
376         }
377
378         activeTextControl = textControl;
379         textControl.addListener(SWT.Activate, textControlListener);
380         textControl.addListener(SWT.Deactivate, textControlListener);
381
382         // We really want a selection listener but it is not supported so we
383
// use a key listener and a mouse listener to know when selection changes
384
// may have occured
385
textControl.addKeyListener(keyAdapter);
386         textControl.addMouseListener(mouseAdapter);
387
388     }
389
390     /**
391      * Dispose of this action handler
392      */

393     public void dispose() {
394         setCutAction(null);
395         setCopyAction(null);
396         setPasteAction(null);
397         setSelectAllAction(null);
398         setDeleteAction(null);
399     }
400
401     /**
402      * Removes a <code>Text</code> control from the handler
403      * so that the Cut, Copy, Paste, Delete, and Select All
404      * actions are no longer redirected to it when active.
405      *
406      * @param textControl the inline <code>Text</code> control
407      */

408     public void removeText(Text textControl) {
409         if (textControl == null) {
410             return;
411         }
412
413         textControl.removeListener(SWT.Activate, textControlListener);
414         textControl.removeListener(SWT.Deactivate, textControlListener);
415
416         textControl.removeMouseListener(mouseAdapter);
417         textControl.removeKeyListener(keyAdapter);
418
419         activeTextControl = null;
420         updateActionsEnableState();
421     }
422
423     /**
424      * Set the default <code>IAction</code> handler for the Copy
425      * action. This <code>IAction</code> is run only if no active
426      * inline text control.
427      *
428      * @param action the <code>IAction</code> to run for the
429      * Copy action, or <code>null</code> if not interested.
430      */

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

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

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

509     public void setSelectAllAction(IAction action) {
510         if (selectAllAction == action) {
511             return;
512         }
513
514         if (selectAllAction != null) {
515             selectAllAction
516                     .removePropertyChangeListener(selectAllActionListener);
517         }
518
519         selectAllAction = action;
520
521         if (selectAllAction != null) {
522             selectAllAction.addPropertyChangeListener(selectAllActionListener);
523         }
524
525         textSelectAllAction.updateEnabledState();
526     }
527
528     /**
529      * Set the default <code>IAction</code> handler for the Delete
530      * action. This <code>IAction</code> is run only if no active
531      * inline text control.
532      *
533      * @param action the <code>IAction</code> to run for the
534      * Delete action, or <code>null</code> if not interested.
535      */

536     public void setDeleteAction(IAction action) {
537         if (deleteAction == action) {
538             return;
539         }
540
541         if (deleteAction != null) {
542             deleteAction.removePropertyChangeListener(deleteActionListener);
543         }
544
545         deleteAction = action;
546
547         if (deleteAction != null) {
548             deleteAction.addPropertyChangeListener(deleteActionListener);
549         }
550
551         textDeleteAction.updateEnabledState();
552     }
553
554     /**
555      * Update the enable state of the Cut, Copy,
556      * Paste, Delete, and Select All action handlers
557      */

558     private void updateActionsEnableState() {
559         textCutAction.updateEnabledState();
560         textCopyAction.updateEnabledState();
561         textPasteAction.updateEnabledState();
562         textSelectAllAction.updateEnabledState();
563         textDeleteAction.updateEnabledState();
564     }
565 }
566
Popular Tags