KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > ActionRunner


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
27  * Class which can run actions and determine the outcome
28  */

29 public class ActionRunner {
30     public IStatus runAction(Action cheatSheetAction, CheatSheetManager csm) {
31
32         IStatus status = Status.OK_STATUS;
33         String JavaDoc pluginId = cheatSheetAction.getPluginID();
34         String JavaDoc className = cheatSheetAction.getActionClass();
35         String JavaDoc[] params = cheatSheetAction.getParams();
36         Bundle bundle = Platform.getBundle(pluginId);
37         if (bundle == null) {
38             String JavaDoc message = NLS.bind(Messages.ERROR_FINDING_PLUGIN_FOR_ACTION, (new Object JavaDoc[] {pluginId}));
39             return new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, null);
40         }
41         Class JavaDoc actionClass;
42         IAction action;
43         try {
44             actionClass = bundle.loadClass(className);
45         } catch (Exception JavaDoc e) {
46             String JavaDoc message = NLS.bind(Messages.ERROR_LOADING_CLASS_FOR_ACTION, (new Object JavaDoc[] {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 JavaDoc e) {
52             String JavaDoc message = NLS.bind(Messages.ERROR_CREATING_CLASS_FOR_ACTION, (new Object JavaDoc[] {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 JavaDoc) {
61                     listenerFired[0] = true;
62                     listenerResult[0] = ((Boolean JavaDoc)event.getNewValue()).booleanValue();
63                 }
64             }
65         };
66
67         // Add PropertyChangeListener to the action, so we can detemine if a action was succesfull
68
action.addPropertyChangeListener(propertyChangeListener);
69
70         // Run the action for this ViewItem
71
if (action instanceof ICheatSheetAction) {
72             // Prepare parameters
73
String JavaDoc[] clonedParams = null;
74             if(params != null && params.length > 0) {
75                 clonedParams = new String JavaDoc[params.length];
76                 System.arraycopy(params, 0, clonedParams, 0, params.length);
77                 for (int i = 0; i < clonedParams.length; i++) {
78                     String JavaDoc param = clonedParams[i];
79                     if(param != null && param.startsWith("${") && param.endsWith("}")) { //$NON-NLS-1$ //$NON-NLS-2$
80
param = param.substring(2,param.length()-1);
81                         String JavaDoc 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 JavaDoc e) {
91                 status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, Messages.EXCEPTION_RUNNING_ACTION, e);
92             }
93         }
94
95         // Remove the PropertyChangeListener
96
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