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.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 34 public class SelectAllHandler extends WidgetMethodHandler { 35 36 39 private static final Class [] METHOD_PARAMETERS = { Point.class }; 40 41 public final Object execute(final ExecutionEvent event) 42 throws ExecutionException { 43 final Method 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 if (numParams != 0) { 56 return null; 57 } 58 59 72 try { 73 final Object focusComponent = getFocusComponent(); 74 if (focusComponent != null) { 75 Runnable methodRunnable = new Runnable () { 76 public void run() { 77 try { 78 methodToExecute.invoke(focusComponent, 79 null); 80 focusControl.getDisplay().asyncExec( 82 new Runnable () { 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 e) { 94 } catch (final InvocationTargetException e) { 97 104 focusControl.getDisplay().asyncExec( 105 new Runnable () { 106 public void run() { 107 ExceptionHandler 108 .getInstance() 109 .handleException( 110 new ExecutionException( 111 "An exception occurred while executing " + methodToExecute 113 .getName(), 114 e 115 .getTargetException())); 116 } 117 }); 118 } 119 } 120 }; 121 122 swingInvokeLater(methodRunnable); 123 } 124 } catch (final ClassNotFoundException e) { 125 127 } catch (final NoSuchMethodException e) { 128 throw new Error ("Something is seriously wrong here"); } 131 } else if (numParams == 0) { 132 methodToExecute.invoke(focusControl, null); 134 focusControl.notifyListeners(SWT.Selection, null); 135 136 } else if (numParams == 1) { 137 final Method textLimitAccessor = focusControl.getClass() 139 .getMethod("getTextLimit", NO_PARAMETERS); final Integer textLimit = (Integer ) textLimitAccessor 141 .invoke(focusControl, null); 142 final Object [] parameters = { new Point(0, textLimit 143 .intValue()) }; 144 methodToExecute.invoke(focusControl, parameters); 145 focusControl.notifyListeners(SWT.Selection, null); 146 147 } else { 148 152 throw new ExecutionException( 153 "Too many parameters on select all", new Exception ()); 155 } 156 157 } catch (IllegalAccessException e) { 158 160 } catch (InvocationTargetException e) { 161 throw new ExecutionException( 162 "An exception occurred while executing " + getMethodToExecute(), e.getTargetException()); 164 165 } catch (NoSuchMethodException e) { 166 168 } 169 } 170 171 return null; 172 } 173 174 179 protected Method getMethodToExecute() { 180 Method method = super.getMethodToExecute(); 181 182 if (method == null) { 184 final Control focusControl = Display.getCurrent().getFocusControl(); 185 if (focusControl != null) { 186 try { 187 method = focusControl.getClass().getMethod("setSelection", METHOD_PARAMETERS); 189 } catch (NoSuchMethodException e) { 190 } 192 } 193 } 194 195 return method; 196 } 197 198 202 public void setInitializationData(IConfigurationElement config, 203 String propertyName, Object data) { 204 methodName = "selectAll"; } 207 } 208 | Popular Tags |