001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Platform;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.jface.action.IAction;
016: import org.eclipse.jface.util.IPropertyChangeListener;
017: import org.eclipse.jface.util.PropertyChangeEvent;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.ui.cheatsheets.ICheatSheetAction;
020: import org.eclipse.ui.internal.cheatsheets.data.Action;
021: import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager;
022: import org.osgi.framework.Bundle;
023:
024: /**
025: * Class which can run actions and determine the outcome
026: */
027: public class ActionRunner {
028: public IStatus runAction(Action cheatSheetAction,
029: CheatSheetManager csm) {
030:
031: IStatus status = Status.OK_STATUS;
032: String pluginId = cheatSheetAction.getPluginID();
033: String className = cheatSheetAction.getActionClass();
034: String[] params = cheatSheetAction.getParams();
035: Bundle bundle = Platform.getBundle(pluginId);
036: if (bundle == null) {
037: String message = NLS.bind(
038: Messages.ERROR_FINDING_PLUGIN_FOR_ACTION,
039: (new Object[] { pluginId }));
040: return new Status(IStatus.ERROR,
041: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
042: IStatus.OK, message, null);
043: }
044: Class actionClass;
045: IAction action;
046: try {
047: actionClass = bundle.loadClass(className);
048: } catch (Exception e) {
049: String message = NLS.bind(
050: Messages.ERROR_LOADING_CLASS_FOR_ACTION,
051: (new Object[] { className }));
052: return new Status(IStatus.ERROR,
053: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
054: IStatus.OK, message, e);
055: }
056: try {
057: action = (IAction) actionClass.newInstance();
058: } catch (Exception e) {
059: String message = NLS.bind(
060: Messages.ERROR_CREATING_CLASS_FOR_ACTION,
061: (new Object[] { className }));
062: return new Status(IStatus.ERROR,
063: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
064: IStatus.OK, message, e);
065: }
066:
067: final boolean[] listenerFired = { false };
068: final boolean[] listenerResult = { false };
069: IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
070: public void propertyChange(PropertyChangeEvent event) {
071: if (event.getProperty().equals(IAction.RESULT)
072: && event.getNewValue() instanceof Boolean) {
073: listenerFired[0] = true;
074: listenerResult[0] = ((Boolean) event.getNewValue())
075: .booleanValue();
076: }
077: }
078: };
079:
080: // Add PropertyChangeListener to the action, so we can detemine if a action was succesfull
081: action.addPropertyChangeListener(propertyChangeListener);
082:
083: // Run the action for this ViewItem
084: if (action instanceof ICheatSheetAction) {
085: // Prepare parameters
086: String[] clonedParams = null;
087: if (params != null && params.length > 0) {
088: clonedParams = new String[params.length];
089: System.arraycopy(params, 0, clonedParams, 0,
090: params.length);
091: for (int i = 0; i < clonedParams.length; i++) {
092: String param = clonedParams[i];
093: if (param != null
094: && param.startsWith("${") && param.endsWith("}")) { //$NON-NLS-1$ //$NON-NLS-2$
095: param = param.substring(2, param.length() - 1);
096: String value = csm.getDataQualified(param);
097: clonedParams[i] = value == null ? ICheatSheetResource.EMPTY_STRING
098: : value;
099: }
100: }
101: }
102: ((ICheatSheetAction) action).run(clonedParams, csm);
103: } else {
104: try {
105: action.run();
106: } catch (Throwable e) {
107: status = new Status(IStatus.ERROR,
108: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
109: IStatus.OK, Messages.EXCEPTION_RUNNING_ACTION,
110: e);
111: }
112: }
113:
114: // Remove the PropertyChangeListener
115: action.removePropertyChangeListener(propertyChangeListener);
116:
117: if (status.isOK() && listenerFired[0]) {
118: if (!listenerResult[0]) {
119: status = new Status(IStatus.WARNING,
120: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
121: IStatus.OK, Messages.ACTION_FAILED, null);
122: }
123: }
124:
125: return status;
126: }
127:
128: }
|