KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExecutableExtension;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.ui.commands.AbstractHandler;
24 import org.eclipse.ui.commands.ExecutionException;
25
26 /**
27  * Handles the cut command in both dialogs and windows. This handler is enabled
28  * if the focus control supports the "cut" method.
29  *
30  * @since 3.0
31  */

32 public class WidgetMethodHandler extends AbstractHandler implements
33         IExecutableExtension {
34
35     /**
36      * The name of the attribute controlling the enabled state.
37      */

38     private static final String JavaDoc ATTRIBUTE_ENABLED = "enabled"; //$NON-NLS-1$
39

40     /**
41      * The name of the attribute for the identifier.
42      */

43     private static final String JavaDoc ATTRIBUTE_ID = "id"; //$NON-NLS-1$
44

45     /**
46      * The parameters to pass to the method this handler invokes. This handler
47      * always passes no parameters.
48      */

49     protected static final Class JavaDoc[] NO_PARAMETERS = new Class JavaDoc[0];
50
51     /**
52      * The name of the method to be invoked by this handler. This value should
53      * never be <code>null</code>.
54      */

55     protected String JavaDoc methodName;
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see org.eclipse.ui.commands.IHandler#execute(java.lang.Object)
61      */

62     public Object JavaDoc execute(Map JavaDoc parameterValuesByName) throws ExecutionException {
63         final Method JavaDoc methodToExecute = getMethodToExecute();
64         if (methodToExecute != null) {
65             try {
66                 final Control focusControl = Display.getCurrent()
67                         .getFocusControl();
68                 methodToExecute.invoke(focusControl, null);
69             } catch (IllegalAccessException JavaDoc e) {
70                 // The method is protected, so do nothing.
71
} catch (InvocationTargetException JavaDoc e) {
72                 throw new ExecutionException(
73                         "An exception occurred while executing " //$NON-NLS-1$
74
+ getMethodToExecute(), e.getTargetException());
75             }
76         }
77
78         return null;
79     }
80
81     public Map JavaDoc getAttributeValuesByName() {
82         Map JavaDoc attributeValuesByName = new HashMap JavaDoc();
83         attributeValuesByName.put(ATTRIBUTE_ENABLED,
84                 getMethodToExecute() == null ? Boolean.FALSE : Boolean.TRUE);
85         attributeValuesByName.put(ATTRIBUTE_ID, null);
86         return Collections.unmodifiableMap(attributeValuesByName);
87     }
88
89     /**
90      * Looks up the method on the focus control.
91      *
92      * @return The method on the focus control; <code>null</code> if none.
93      */

94     protected Method JavaDoc getMethodToExecute() {
95         final Control focusControl = Display.getCurrent().getFocusControl();
96         try {
97             if (focusControl != null) {
98                 return focusControl.getClass().getMethod(methodName, NO_PARAMETERS);
99             }
100         } catch (NoSuchMethodException JavaDoc e) {
101             // Fall through....
102
}
103         
104         return null;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
111      * java.lang.String, java.lang.Object)
112      */

113     public void setInitializationData(IConfigurationElement config,
114             String JavaDoc propertyName, Object JavaDoc data) {
115         // The data is really just a string (i.e., the method name).
116
methodName = data.toString();
117     }
118 }
119
Popular Tags