1 11 package org.eclipse.ui.internal.handlers; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 16 import org.eclipse.core.commands.AbstractHandler; 17 import org.eclipse.core.commands.ExecutionEvent; 18 import org.eclipse.core.commands.ExecutionException; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExecutableExtension; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Display; 25 import org.eclipse.ui.internal.ExceptionHandler; 26 27 33 public class WidgetMethodHandler extends AbstractHandler implements 34 IExecutableExtension { 35 36 40 protected static final Class [] NO_PARAMETERS = new Class [0]; 41 42 46 protected String methodName; 47 48 public Object execute(final ExecutionEvent event) throws ExecutionException { 49 final Method methodToExecute = getMethodToExecute(); 50 if (methodToExecute != null) { 51 try { 52 final Control focusControl = Display.getCurrent() 53 .getFocusControl(); 54 if ((focusControl instanceof Composite) 55 && ((((Composite) focusControl).getStyle() & SWT.EMBEDDED) != 0)) { 56 69 try { 70 final Object focusComponent = getFocusComponent(); 71 if (focusComponent != null) { 72 Runnable methodRunnable = new Runnable () { 73 public void run() { 74 try { 75 methodToExecute.invoke(focusComponent, 76 null); 77 } catch (final IllegalAccessException e) { 78 } catch (final InvocationTargetException e) { 81 88 focusControl.getDisplay().asyncExec( 89 new Runnable () { 90 public void run() { 91 ExceptionHandler 92 .getInstance() 93 .handleException( 94 new ExecutionException( 95 "An exception occurred while executing " + methodToExecute 97 .getName(), 98 e 99 .getTargetException())); 100 } 101 }); 102 } 103 } 104 }; 105 106 swingInvokeLater(methodRunnable); 107 } 108 } catch (final ClassNotFoundException e) { 109 111 } catch (final NoSuchMethodException e) { 112 throw new Error ("Something is seriously wrong here"); } 115 116 } else { 117 118 methodToExecute.invoke(focusControl, null); 119 } 120 121 } catch (IllegalAccessException e) { 122 124 } catch (InvocationTargetException e) { 125 throw new ExecutionException( 126 "An exception occurred while executing " + methodToExecute.getName(), e 128 .getTargetException()); 129 130 } 131 } 132 133 return null; 134 } 135 136 145 protected void swingInvokeLater(Runnable methodRunnable) 146 throws ClassNotFoundException , NoSuchMethodException , 147 IllegalAccessException , InvocationTargetException { 148 final Class swingUtilitiesClass = Class 149 .forName("javax.swing.SwingUtilities"); final Method swingUtilitiesInvokeLaterMethod = swingUtilitiesClass 151 .getMethod("invokeLater", new Class [] { Runnable .class }); 153 swingUtilitiesInvokeLaterMethod.invoke( 154 swingUtilitiesClass, 155 new Object [] { methodRunnable }); 156 } 157 158 167 protected Object getFocusComponent() throws ClassNotFoundException , 168 NoSuchMethodException , IllegalAccessException , 169 InvocationTargetException { 170 final Class focusManagerClass = Class 171 .forName("javax.swing.FocusManager"); final Method focusManagerGetCurrentManagerMethod = focusManagerClass 173 .getMethod("getCurrentManager", null); final Object focusManager = focusManagerGetCurrentManagerMethod 175 .invoke(focusManagerClass, null); 176 final Method focusManagerGetFocusOwner = focusManagerClass 177 .getMethod("getFocusOwner", null); final Object focusComponent = focusManagerGetFocusOwner 179 .invoke(focusManager, null); 180 return focusComponent; 181 } 182 183 public final boolean isEnabled() { 184 return getMethodToExecute() != null; 185 } 186 187 192 protected Method getMethodToExecute() { 193 final Control focusControl = Display.getCurrent().getFocusControl(); 194 Method method = null; 195 196 if (focusControl != null) { 197 final Class clazz = focusControl.getClass(); 198 try { 199 method = clazz.getMethod(methodName, NO_PARAMETERS); 200 } catch (NoSuchMethodException e) { 201 } 203 } 204 205 if ((method == null) 206 && (focusControl instanceof Composite) 207 && ((((Composite) focusControl).getStyle() & SWT.EMBEDDED) != 0)) { 208 216 try { 217 final Object focusComponent = getFocusComponent(); 218 if (focusComponent != null) { 219 final Class clazz = focusComponent.getClass(); 220 221 try { 222 method = clazz.getMethod(methodName, NO_PARAMETERS); 223 } catch (NoSuchMethodException e) { 224 } 226 } 227 } catch (final ClassNotFoundException e) { 228 230 } catch (final NoSuchMethodException e) { 231 throw new Error ("Something is seriously wrong here"); } catch (IllegalAccessException e) { 234 throw new Error ("Something is seriously wrong here"); } catch (InvocationTargetException e) { 237 throw new Error ("Something is seriously wrong here"); } 240 } 241 242 return method; 243 } 244 245 251 public void setInitializationData(IConfigurationElement config, 252 String propertyName, Object data) { 253 methodName = data.toString(); 255 } 256 } 257 | Popular Tags |