1 11 package org.eclipse.ui.internal.commands.ws; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.lang.reflect.Method ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.Map ; 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 32 public class WidgetMethodHandler extends AbstractHandler implements 33 IExecutableExtension { 34 35 38 private static final String ATTRIBUTE_ENABLED = "enabled"; 40 43 private static final String ATTRIBUTE_ID = "id"; 45 49 protected static final Class [] NO_PARAMETERS = new Class [0]; 50 51 55 protected String methodName; 56 57 62 public Object execute(Map parameterValuesByName) throws ExecutionException { 63 final Method methodToExecute = getMethodToExecute(); 64 if (methodToExecute != null) { 65 try { 66 final Control focusControl = Display.getCurrent() 67 .getFocusControl(); 68 methodToExecute.invoke(focusControl, null); 69 } catch (IllegalAccessException e) { 70 } catch (InvocationTargetException e) { 72 throw new ExecutionException( 73 "An exception occurred while executing " + getMethodToExecute(), e.getTargetException()); 75 } 76 } 77 78 return null; 79 } 80 81 public Map getAttributeValuesByName() { 82 Map attributeValuesByName = new HashMap (); 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 94 protected Method 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 e) { 101 } 103 104 return null; 105 } 106 107 113 public void setInitializationData(IConfigurationElement config, 114 String propertyName, Object data) { 115 methodName = data.toString(); 117 } 118 } 119 | Popular Tags |