KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > SelectAllHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.handlers;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.ui.internal.ExceptionHandler;
25
26 /**
27  * This handler is an adaptation of the widget method handler allowing select
28  * all to work even in some cases where the "selectAll" method does not exist.
29  * This handler attempts to use "getTextLimit" and "setSelection" to do select
30  * all. If this doesn't work, then it finally fails.
31  *
32  * @since 3.0
33  */

34 public class SelectAllHandler extends WidgetMethodHandler {
35
36     /**
37      * The parameters for a single point select all.
38      */

39     private static final Class JavaDoc[] METHOD_PARAMETERS = { Point.class };
40
41     public final Object JavaDoc execute(final ExecutionEvent event)
42             throws ExecutionException {
43         final Method JavaDoc methodToExecute = getMethodToExecute();
44         if (methodToExecute != null) {
45             try {
46                 final Control focusControl = Display.getCurrent()
47                         .getFocusControl();
48                 
49                 final int numParams = methodToExecute.getParameterTypes().length;
50
51                 if ((focusControl instanceof Composite)
52                         && ((((Composite) focusControl).getStyle() & SWT.EMBEDDED) != 0)) {
53                     
54                     // we only support selectAll for swing components
55
if (numParams != 0) {
56                         return null;
57                     }
58
59                     /*
60                      * Okay. Have a seat. Relax a while. This is going to be a
61                      * bumpy ride. If it is an embedded widget, then it *might*
62                      * be a Swing widget. At the point where this handler is
63                      * executing, the key event is already bound to be
64                      * swallowed. If I don't do something, then the key will be
65                      * gone for good. So, I will try to forward the event to the
66                      * Swing widget. Unfortunately, we can't even count on the
67                      * Swing libraries existing, so I need to use reflection
68                      * everywhere. And, to top it off, I need to dispatch the
69                      * event on the Swing event queue, which means that it will
70                      * be carried out asynchronously to the SWT event queue.
71                      */

72                     try {
73                         final Object JavaDoc focusComponent = getFocusComponent();
74                         if (focusComponent != null) {
75                             Runnable JavaDoc methodRunnable = new Runnable JavaDoc() {
76                                 public void run() {
77                                     try {
78                                         methodToExecute.invoke(focusComponent,
79                                                 null);
80                                         // and back to the UI thread :-)
81
focusControl.getDisplay().asyncExec(
82                                                 new Runnable JavaDoc() {
83                                                     public void run() {
84                                                         if (!focusControl
85                                                                 .isDisposed()) {
86                                                             focusControl
87                                                                     .notifyListeners(
88                                                                             SWT.Selection,
89                                                                             null);
90                                                         }
91                                                     }
92                                                 });
93                                     } catch (final IllegalAccessException JavaDoc e) {
94                                         // The method is protected, so do
95
// nothing.
96
} catch (final InvocationTargetException JavaDoc e) {
97                                         /*
98                                          * I would like to log this exception --
99                                          * and possibly show a dialog to the
100                                          * user -- but I have to go back to the
101                                          * SWT event loop to do this. So, back
102                                          * we go....
103                                          */

104                                         focusControl.getDisplay().asyncExec(
105                                                 new Runnable JavaDoc() {
106                                                     public void run() {
107                                                         ExceptionHandler
108                                                                 .getInstance()
109                                                                 .handleException(
110                                                                         new ExecutionException(
111                                                                                 "An exception occurred while executing " //$NON-NLS-1$
112
+ methodToExecute
113                                                                                                 .getName(),
114                                                                                 e
115                                                                                         .getTargetException()));
116                                                     }
117                                                 });
118                                     }
119                                 }
120                             };
121
122                             swingInvokeLater(methodRunnable);
123                         }
124                     } catch (final ClassNotFoundException JavaDoc e) {
125                         // There is no Swing support, so do nothing.
126

127                     } catch (final NoSuchMethodException JavaDoc e) {
128                         // The API has changed, which seems amazingly unlikely.
129
throw new Error JavaDoc("Something is seriously wrong here"); //$NON-NLS-1$
130
}
131                 } else if (numParams == 0) {
132                     // This is a no-argument selectAll method.
133
methodToExecute.invoke(focusControl, null);
134                     focusControl.notifyListeners(SWT.Selection, null);
135
136                 } else if (numParams == 1) {
137                     // This is a single-point selection method.
138
final Method JavaDoc textLimitAccessor = focusControl.getClass()
139                             .getMethod("getTextLimit", NO_PARAMETERS); //$NON-NLS-1$
140
final Integer JavaDoc textLimit = (Integer JavaDoc) textLimitAccessor
141                             .invoke(focusControl, null);
142                     final Object JavaDoc[] parameters = { new Point(0, textLimit
143                             .intValue()) };
144                     methodToExecute.invoke(focusControl, parameters);
145                     focusControl.notifyListeners(SWT.Selection, null);
146
147                 } else {
148                     /*
149                      * This means that getMethodToExecute() has been changed,
150                      * while this method hasn't.
151                      */

152                     throw new ExecutionException(
153                             "Too many parameters on select all", new Exception JavaDoc()); //$NON-NLS-1$
154

155                 }
156
157             } catch (IllegalAccessException JavaDoc e) {
158                 // The method is protected, so do nothing.
159

160             } catch (InvocationTargetException JavaDoc e) {
161                 throw new ExecutionException(
162                         "An exception occurred while executing " //$NON-NLS-1$
163
+ getMethodToExecute(), e.getTargetException());
164
165             } catch (NoSuchMethodException JavaDoc e) {
166                 // I can't get the text limit. Do nothing.
167

168             }
169         }
170
171         return null;
172     }
173
174     /**
175      * Looks up the select all method on the given focus control.
176      *
177      * @return The method on the focus control; <code>null</code> if none.
178      */

179     protected Method JavaDoc getMethodToExecute() {
180         Method JavaDoc method = super.getMethodToExecute();
181
182         // Let's see if we have a control that supports point-based selection.
183
if (method == null) {
184             final Control focusControl = Display.getCurrent().getFocusControl();
185             if (focusControl != null) {
186                 try {
187                     method = focusControl.getClass().getMethod("setSelection", //$NON-NLS-1$
188
METHOD_PARAMETERS);
189                 } catch (NoSuchMethodException JavaDoc e) {
190                     // Do nothing.
191
}
192             }
193         }
194
195         return method;
196     }
197
198     /**
199      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
200      * java.lang.String, java.lang.Object)
201      */

202     public void setInitializationData(IConfigurationElement config,
203             String JavaDoc propertyName, Object JavaDoc data) {
204         // The name is always "selectAll".
205
methodName = "selectAll"; //$NON-NLS-1$
206
}
207 }
208
Popular Tags