KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > CommandAction


1 /*******************************************************************************
2  * Copyright (c) 2007 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.actions;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.commands.Command;
19 import org.eclipse.core.commands.CommandEvent;
20 import org.eclipse.core.commands.ICommandListener;
21 import org.eclipse.core.commands.IParameter;
22 import org.eclipse.core.commands.Parameterization;
23 import org.eclipse.core.commands.ParameterizedCommand;
24 import org.eclipse.core.commands.common.NotDefinedException;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.swt.widgets.Event;
27 import org.eclipse.ui.commands.ICommandService;
28 import org.eclipse.ui.handlers.IHandlerService;
29 import org.eclipse.ui.internal.WorkbenchPlugin;
30 import org.eclipse.ui.services.IServiceLocator;
31
32 /**
33  * Instantiate an action that will execute the command.
34  * <p>
35  * This is a legacy bridge class, and should not be used outside of the
36  * framework. Please use menu contributions to display a command in a menu or
37  * toolbar.
38  * </p>
39  * <p>
40  * <b>Note:</b> Clients my instantiate, but they must not subclass.
41  * </p>
42  *
43  * @since 3.3
44  */

45 public class CommandAction extends Action {
46
47     private IHandlerService handlerService = null;
48
49     private ParameterizedCommand parameterizedCommand = null;
50
51     private ICommandListener commandListener;
52     
53     protected CommandAction() {
54         
55     }
56
57     /**
58      * Creates the action backed by a command. For commands that don't take
59      * parameters.
60      *
61      * @param serviceLocator
62      * The service locator that is closest in lifecycle to this
63      * action.
64      * @param commandIdIn
65      * the command id. Must not be <code>null</code>.
66      */

67     public CommandAction(IServiceLocator serviceLocator, String JavaDoc commandIdIn) {
68         this(serviceLocator, commandIdIn, null);
69     }
70
71     /**
72      * Creates the action backed by a parameterized command. The parameterMap
73      * must contain only all required parameters, and may contain the optional
74      * parameters.
75      *
76      * @param serviceLocator
77      * The service locator that is closest in lifecycle to this
78      * action.
79      * @param commandIdIn
80      * the command id. Must not be <code>null</code>.
81      * @param parameterMap
82      * the parameter map. May be <code>null</code>.
83      */

84     public CommandAction(IServiceLocator serviceLocator, String JavaDoc commandIdIn,
85             Map JavaDoc parameterMap) {
86         if (commandIdIn == null) {
87             throw new NullPointerException JavaDoc("commandIdIn must not be null"); //$NON-NLS-1$
88
}
89         init(serviceLocator, commandIdIn, parameterMap);
90     }
91
92     protected ICommandListener getCommandListener() {
93         if (commandListener == null) {
94             commandListener = new ICommandListener() {
95                 public void commandChanged(CommandEvent commandEvent) {
96                     if (commandEvent.isHandledChanged()
97                             || commandEvent.isEnabledChanged()) {
98                         if (commandEvent.getCommand().isDefined()) {
99                             setEnabled(commandEvent.getCommand().isEnabled());
100                         }
101                     }
102                 }
103             };
104         }
105         return commandListener;
106     }
107
108     /**
109      * Build a command from the executable extension information.
110      *
111      * @param commandService
112      * to get the Command object
113      * @param commandId
114      * the command id for this action
115      * @param parameterMap
116      */

117     private void createCommand(ICommandService commandService,
118             String JavaDoc commandId, Map JavaDoc parameterMap) {
119         try {
120             Command cmd = commandService.getCommand(commandId);
121             if (!cmd.isDefined()) {
122                 WorkbenchPlugin.log("Command " + commandId + " is undefined"); //$NON-NLS-1$//$NON-NLS-2$
123
return;
124             }
125
126             if (parameterMap == null) {
127                 parameterizedCommand = new ParameterizedCommand(cmd, null);
128                 return;
129             }
130
131             ArrayList JavaDoc parameters = new ArrayList JavaDoc();
132             Iterator JavaDoc i = parameterMap.keySet().iterator();
133             while (i.hasNext()) {
134                 String JavaDoc parmName = (String JavaDoc) i.next();
135                 IParameter parm = cmd.getParameter(parmName);
136                 if (parm == null) {
137                     WorkbenchPlugin.log("Invalid parameter \'" + parmName //$NON-NLS-1$
138
+ "\' for command " + commandId); //$NON-NLS-1$
139
return;
140                 }
141                 parameters.add(new Parameterization(parm, (String JavaDoc) parameterMap
142                         .get(parmName)));
143             }
144             parameterizedCommand = new ParameterizedCommand(cmd,
145                     (Parameterization[]) parameters
146                             .toArray(new Parameterization[parameters.size()]));
147         } catch (NotDefinedException e) {
148             WorkbenchPlugin.log(e);
149         }
150     }
151
152     public void dispose() {
153         // not important for command ID, maybe for command though.
154
handlerService = null;
155         if (commandListener != null) {
156             parameterizedCommand.getCommand().removeCommandListener(
157                     commandListener);
158             commandListener = null;
159         }
160         parameterizedCommand = null;
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
167      */

168     public void runWithEvent(Event event) {
169         if (handlerService == null) {
170             String JavaDoc commandId = (parameterizedCommand == null ? "unknownCommand" //$NON-NLS-1$
171
: parameterizedCommand.getId());
172             WorkbenchPlugin.log("Cannot run " + commandId //$NON-NLS-1$
173
+ " before command action has been initialized"); //$NON-NLS-1$
174
return;
175         }
176         try {
177             if (parameterizedCommand != null) {
178                 handlerService.executeCommand(parameterizedCommand, event);
179             }
180         } catch (Exception JavaDoc e) {
181             WorkbenchPlugin.log(e);
182         }
183     }
184
185     /*
186      * (non-Javadoc)
187      *
188      * @see org.eclipse.jface.action.Action#run()
189      */

190     public void run() {
191         // hopefully this is never called
192
runWithEvent(null);
193     }
194
195     protected void init(IServiceLocator serviceLocator, String JavaDoc commandIdIn,
196             Map JavaDoc parameterMap) {
197         if (handlerService != null) {
198             // already initialized
199
return;
200         }
201         handlerService = (IHandlerService) serviceLocator
202                 .getService(IHandlerService.class);
203         ICommandService commandService = (ICommandService) serviceLocator
204                 .getService(ICommandService.class);
205         createCommand(commandService, commandIdIn, parameterMap);
206         if (parameterizedCommand != null) {
207             setId(parameterizedCommand.getId());
208             try {
209                 setText(parameterizedCommand.getName());
210             } catch (NotDefinedException e) {
211                 // if we get this far it shouldn't be a problem
212
}
213             parameterizedCommand.getCommand().addCommandListener(
214                     getCommandListener());
215             setEnabled(parameterizedCommand.getCommand().isEnabled());
216         }
217     }
218
219     protected ParameterizedCommand getParameterizedCommand() {
220         return parameterizedCommand;
221     }
222
223     public String JavaDoc getActionDefinitionId() {
224         if (parameterizedCommand != null) {
225             return parameterizedCommand.getId();
226         }
227         return null;
228     }
229 }
230
Popular Tags