KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > ws > SelectAllHandler


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11 package org.eclipse.ui.internal.commands.ws;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.ui.commands.ExecutionException;
22
23 /**
24  * This handler is an adaptation of the widget method handler allowing select
25  * all to work even in some cases where the "selectAll" method does not exist.
26  * This handler attempts to use "getTextLimit" and "setSelection" to do select
27  * all. If this doesn't work, then it finally fails.
28  *
29  * @since 3.0
30  */

31 public class SelectAllHandler extends WidgetMethodHandler {
32
33     /**
34      * The parameters for a single point select all.
35      */

36     private static final Class JavaDoc[] METHOD_PARAMETERS = { Point.class };
37
38     /**
39      * @see org.eclipse.ui.commands.IHandler#execute(Map)
40      */

41     public Object JavaDoc execute(Map JavaDoc parameterValuesByName) throws ExecutionException {
42         final Method JavaDoc methodToExecute = getMethodToExecute();
43         if (methodToExecute != null) {
44             try {
45                 final Control focusControl = Display.getCurrent()
46                         .getFocusControl();
47                 final int numParams = methodToExecute.getParameterTypes().length;
48
49                 if (numParams == 0) {
50                     // This is a no-argument selectAll method.
51
methodToExecute.invoke(focusControl, null);
52
53                 } else if (numParams == 1) {
54                     // This is a single-point selection method.
55
final Method JavaDoc textLimitAccessor = focusControl.getClass()
56                             .getMethod("getTextLimit", NO_PARAMETERS); //$NON-NLS-1$
57
final Integer JavaDoc textLimit = (Integer JavaDoc) textLimitAccessor
58                             .invoke(focusControl, null);
59                     final Object JavaDoc[] parameters = { new Point(0, textLimit
60                             .intValue()) };
61                     methodToExecute.invoke(focusControl, parameters);
62
63                 } else {
64                     /*
65                      * This means that getMethodToExecute() has been changed,
66                      * while this method hasn't.
67                      */

68                     throw new ExecutionException(
69                             "Too many parameters on select all", new Exception JavaDoc()); //$NON-NLS-1$
70

71                 }
72
73             } catch (IllegalAccessException JavaDoc e) {
74                 // The method is protected, so do nothing.
75

76             } catch (InvocationTargetException JavaDoc e) {
77                 throw new ExecutionException(
78                         "An exception occurred while executing " //$NON-NLS-1$
79
+ getMethodToExecute(), e.getTargetException());
80
81             } catch (NoSuchMethodException JavaDoc e) {
82                 // I can't get the text limit. Do nothing.
83

84             }
85         }
86
87         return null;
88     }
89
90     /**
91      * Looks up the select all method on the given focus control.
92      *
93      * @return The method on the focus control; <code>null</code> if none.
94      */

95     protected Method JavaDoc getMethodToExecute() {
96         Method JavaDoc method = super.getMethodToExecute();
97
98         // Let's see if we have a control that supports point-based selection.
99
if (method == null) {
100             final Control focusControl = Display.getCurrent().getFocusControl();
101             try {
102                 method = focusControl.getClass().getMethod("setSelection", //$NON-NLS-1$
103
METHOD_PARAMETERS);
104             } catch (NoSuchMethodException JavaDoc e) {
105                 // Do nothing.
106
}
107         }
108
109         return method;
110     }
111
112     /**
113      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
114      * java.lang.String, java.lang.Object)
115      */

116     public void setInitializationData(IConfigurationElement config,
117             String JavaDoc propertyName, Object JavaDoc data) {
118         // The name is always "selectAll".
119
methodName = "selectAll"; //$NON-NLS-1$
120
}
121 }
Popular Tags