1 11 12 package org.eclipse.ui.internal.cheatsheets; 13 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.Platform; 16 import org.eclipse.core.runtime.Status; 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.util.IPropertyChangeListener; 19 import org.eclipse.jface.util.PropertyChangeEvent; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.ui.cheatsheets.ICheatSheetAction; 22 import org.eclipse.ui.internal.cheatsheets.data.Action; 23 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager; 24 import org.osgi.framework.Bundle; 25 26 29 public class ActionRunner { 30 public IStatus runAction(Action cheatSheetAction, CheatSheetManager csm) { 31 32 IStatus status = Status.OK_STATUS; 33 String pluginId = cheatSheetAction.getPluginID(); 34 String className = cheatSheetAction.getActionClass(); 35 String [] params = cheatSheetAction.getParams(); 36 Bundle bundle = Platform.getBundle(pluginId); 37 if (bundle == null) { 38 String message = NLS.bind(Messages.ERROR_FINDING_PLUGIN_FOR_ACTION, (new Object [] {pluginId})); 39 return new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, null); 40 } 41 Class actionClass; 42 IAction action; 43 try { 44 actionClass = bundle.loadClass(className); 45 } catch (Exception e) { 46 String message = NLS.bind(Messages.ERROR_LOADING_CLASS_FOR_ACTION, (new Object [] {className})); 47 return new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e); 48 } 49 try { 50 action = (IAction) actionClass.newInstance(); 51 } catch (Exception e) { 52 String message = NLS.bind(Messages.ERROR_CREATING_CLASS_FOR_ACTION, (new Object [] {className})); 53 return new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e); 54 } 55 56 final boolean[] listenerFired = { false }; 57 final boolean[] listenerResult = { false }; 58 IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() { 59 public void propertyChange(PropertyChangeEvent event) { 60 if(event.getProperty().equals(IAction.RESULT) && event.getNewValue() instanceof Boolean ) { 61 listenerFired[0] = true; 62 listenerResult[0] = ((Boolean )event.getNewValue()).booleanValue(); 63 } 64 } 65 }; 66 67 action.addPropertyChangeListener(propertyChangeListener); 69 70 if (action instanceof ICheatSheetAction) { 72 String [] clonedParams = null; 74 if(params != null && params.length > 0) { 75 clonedParams = new String [params.length]; 76 System.arraycopy(params, 0, clonedParams, 0, params.length); 77 for (int i = 0; i < clonedParams.length; i++) { 78 String param = clonedParams[i]; 79 if(param != null && param.startsWith("${") && param.endsWith("}")) { param = param.substring(2,param.length()-1); 81 String value = csm.getDataQualified(param); 82 clonedParams[i] = value == null ? ICheatSheetResource.EMPTY_STRING : value; 83 } 84 } 85 } 86 ((ICheatSheetAction) action).run(clonedParams, csm); 87 } else { 88 try { 89 action.run(); 90 } catch (Throwable e) { 91 status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, Messages.EXCEPTION_RUNNING_ACTION, e); 92 } 93 } 94 95 action.removePropertyChangeListener(propertyChangeListener); 97 98 if (status.isOK() && listenerFired[0]) { 99 if (!listenerResult[0]) { 100 status =new Status(IStatus.WARNING, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, Messages.ACTION_FAILED, null); 101 } 102 } 103 104 return status; 105 } 106 107 } 108 | Popular Tags |