001: /*******************************************************************************
002: * Copyright (c) 2007 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.actions;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.eclipse.core.commands.Command;
017: import org.eclipse.core.commands.CommandEvent;
018: import org.eclipse.core.commands.ICommandListener;
019: import org.eclipse.core.commands.IParameter;
020: import org.eclipse.core.commands.Parameterization;
021: import org.eclipse.core.commands.ParameterizedCommand;
022: import org.eclipse.core.commands.common.NotDefinedException;
023: import org.eclipse.jface.action.Action;
024: import org.eclipse.swt.widgets.Event;
025: import org.eclipse.ui.commands.ICommandService;
026: import org.eclipse.ui.handlers.IHandlerService;
027: import org.eclipse.ui.internal.WorkbenchPlugin;
028: import org.eclipse.ui.services.IServiceLocator;
029:
030: /**
031: * Instantiate an action that will execute the command.
032: * <p>
033: * This is a legacy bridge class, and should not be used outside of the
034: * framework. Please use menu contributions to display a command in a menu or
035: * toolbar.
036: * </p>
037: * <p>
038: * <b>Note:</b> Clients my instantiate, but they must not subclass.
039: * </p>
040: *
041: * @since 3.3
042: */
043: public class CommandAction extends Action {
044:
045: private IHandlerService handlerService = null;
046:
047: private ParameterizedCommand parameterizedCommand = null;
048:
049: private ICommandListener commandListener;
050:
051: protected CommandAction() {
052:
053: }
054:
055: /**
056: * Creates the action backed by a command. For commands that don't take
057: * parameters.
058: *
059: * @param serviceLocator
060: * The service locator that is closest in lifecycle to this
061: * action.
062: * @param commandIdIn
063: * the command id. Must not be <code>null</code>.
064: */
065: public CommandAction(IServiceLocator serviceLocator,
066: String commandIdIn) {
067: this (serviceLocator, commandIdIn, null);
068: }
069:
070: /**
071: * Creates the action backed by a parameterized command. The parameterMap
072: * must contain only all required parameters, and may contain the optional
073: * parameters.
074: *
075: * @param serviceLocator
076: * The service locator that is closest in lifecycle to this
077: * action.
078: * @param commandIdIn
079: * the command id. Must not be <code>null</code>.
080: * @param parameterMap
081: * the parameter map. May be <code>null</code>.
082: */
083: public CommandAction(IServiceLocator serviceLocator,
084: String commandIdIn, Map parameterMap) {
085: if (commandIdIn == null) {
086: throw new NullPointerException(
087: "commandIdIn must not be null"); //$NON-NLS-1$
088: }
089: init(serviceLocator, commandIdIn, parameterMap);
090: }
091:
092: protected ICommandListener getCommandListener() {
093: if (commandListener == null) {
094: commandListener = new ICommandListener() {
095: public void commandChanged(CommandEvent commandEvent) {
096: if (commandEvent.isHandledChanged()
097: || commandEvent.isEnabledChanged()) {
098: if (commandEvent.getCommand().isDefined()) {
099: setEnabled(commandEvent.getCommand()
100: .isEnabled());
101: }
102: }
103: }
104: };
105: }
106: return commandListener;
107: }
108:
109: /**
110: * Build a command from the executable extension information.
111: *
112: * @param commandService
113: * to get the Command object
114: * @param commandId
115: * the command id for this action
116: * @param parameterMap
117: */
118: private void createCommand(ICommandService commandService,
119: String commandId, Map parameterMap) {
120: try {
121: Command cmd = commandService.getCommand(commandId);
122: if (!cmd.isDefined()) {
123: WorkbenchPlugin
124: .log("Command " + commandId + " is undefined"); //$NON-NLS-1$//$NON-NLS-2$
125: return;
126: }
127:
128: if (parameterMap == null) {
129: parameterizedCommand = new ParameterizedCommand(cmd,
130: null);
131: return;
132: }
133:
134: ArrayList parameters = new ArrayList();
135: Iterator i = parameterMap.keySet().iterator();
136: while (i.hasNext()) {
137: String parmName = (String) i.next();
138: IParameter parm = cmd.getParameter(parmName);
139: if (parm == null) {
140: WorkbenchPlugin
141: .log("Invalid parameter \'" + parmName //$NON-NLS-1$
142: + "\' for command " + commandId); //$NON-NLS-1$
143: return;
144: }
145: parameters.add(new Parameterization(parm,
146: (String) parameterMap.get(parmName)));
147: }
148: parameterizedCommand = new ParameterizedCommand(cmd,
149: (Parameterization[]) parameters
150: .toArray(new Parameterization[parameters
151: .size()]));
152: } catch (NotDefinedException e) {
153: WorkbenchPlugin.log(e);
154: }
155: }
156:
157: public void dispose() {
158: // not important for command ID, maybe for command though.
159: handlerService = null;
160: if (commandListener != null) {
161: parameterizedCommand.getCommand().removeCommandListener(
162: commandListener);
163: commandListener = null;
164: }
165: parameterizedCommand = null;
166: }
167:
168: /*
169: * (non-Javadoc)
170: *
171: * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
172: */
173: public void runWithEvent(Event event) {
174: if (handlerService == null) {
175: String commandId = (parameterizedCommand == null ? "unknownCommand" //$NON-NLS-1$
176: : parameterizedCommand.getId());
177: WorkbenchPlugin.log("Cannot run " + commandId //$NON-NLS-1$
178: + " before command action has been initialized"); //$NON-NLS-1$
179: return;
180: }
181: try {
182: if (parameterizedCommand != null) {
183: handlerService.executeCommand(parameterizedCommand,
184: event);
185: }
186: } catch (Exception e) {
187: WorkbenchPlugin.log(e);
188: }
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see org.eclipse.jface.action.Action#run()
195: */
196: public void run() {
197: // hopefully this is never called
198: runWithEvent(null);
199: }
200:
201: protected void init(IServiceLocator serviceLocator,
202: String commandIdIn, Map parameterMap) {
203: if (handlerService != null) {
204: // already initialized
205: return;
206: }
207: handlerService = (IHandlerService) serviceLocator
208: .getService(IHandlerService.class);
209: ICommandService commandService = (ICommandService) serviceLocator
210: .getService(ICommandService.class);
211: createCommand(commandService, commandIdIn, parameterMap);
212: if (parameterizedCommand != null) {
213: setId(parameterizedCommand.getId());
214: try {
215: setText(parameterizedCommand.getName());
216: } catch (NotDefinedException e) {
217: // if we get this far it shouldn't be a problem
218: }
219: parameterizedCommand.getCommand().addCommandListener(
220: getCommandListener());
221: setEnabled(parameterizedCommand.getCommand().isEnabled());
222: }
223: }
224:
225: protected ParameterizedCommand getParameterizedCommand() {
226: return parameterizedCommand;
227: }
228:
229: public String getActionDefinitionId() {
230: if (parameterizedCommand != null) {
231: return parameterizedCommand.getId();
232: }
233: return null;
234: }
235: }
|