| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2007, 2008 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.Map; |
| 15 | |
| 16 | import org.eclipse.core.commands.Command; |
| 17 | import org.eclipse.core.commands.CommandEvent; |
| 18 | import org.eclipse.core.commands.ICommandListener; |
| 19 | import org.eclipse.core.commands.ParameterizedCommand; |
| 20 | import org.eclipse.core.commands.common.NotDefinedException; |
| 21 | import org.eclipse.jface.action.Action; |
| 22 | import org.eclipse.swt.widgets.Event; |
| 23 | import org.eclipse.ui.commands.ICommandImageService; |
| 24 | import org.eclipse.ui.commands.ICommandService; |
| 25 | import org.eclipse.ui.handlers.IHandlerService; |
| 26 | import org.eclipse.ui.internal.WorkbenchPlugin; |
| 27 | import org.eclipse.ui.services.IServiceLocator; |
| 28 | |
| 29 | /** |
| 30 | * Instantiate an action that will execute the command. |
| 31 | * <p> |
| 32 | * This is a legacy bridge class, and should not be used outside of the |
| 33 | * framework. Please use menu contributions to display a command in a menu or |
| 34 | * toolbar. |
| 35 | * </p> |
| 36 | * <p> |
| 37 | * <b>Note:</b> Clients my instantiate, but they must not subclass. |
| 38 | * </p> |
| 39 | * |
| 40 | * @since 3.3 |
| 41 | */ |
| 42 | public class CommandAction extends Action { |
| 43 | |
| 44 | private IHandlerService handlerService = null; |
| 45 | |
| 46 | private ParameterizedCommand parameterizedCommand = null; |
| 47 | |
| 48 | private ICommandListener commandListener; |
| 49 | |
| 50 | protected CommandAction() { |
| 51 | |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates the action backed by a command. For commands that don't take |
| 56 | * parameters. |
| 57 | * |
| 58 | * @param serviceLocator |
| 59 | * The service locator that is closest in lifecycle to this |
| 60 | * action. |
| 61 | * @param commandIdIn |
| 62 | * the command id. Must not be <code>null</code>. |
| 63 | */ |
| 64 | public CommandAction(IServiceLocator serviceLocator, String commandIdIn) { |
| 65 | this(serviceLocator, commandIdIn, null); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates the action backed by a parameterized command. The parameterMap |
| 70 | * must contain only all required parameters, and may contain the optional |
| 71 | * parameters. |
| 72 | * |
| 73 | * @param serviceLocator |
| 74 | * The service locator that is closest in lifecycle to this |
| 75 | * action. |
| 76 | * @param commandIdIn |
| 77 | * the command id. Must not be <code>null</code>. |
| 78 | * @param parameterMap |
| 79 | * the parameter map. May be <code>null</code>. |
| 80 | */ |
| 81 | public CommandAction(IServiceLocator serviceLocator, String commandIdIn, |
| 82 | Map parameterMap) { |
| 83 | if (commandIdIn == null) { |
| 84 | throw new NullPointerException("commandIdIn must not be null"); //$NON-NLS-1$ |
| 85 | } |
| 86 | init(serviceLocator, commandIdIn, parameterMap); |
| 87 | } |
| 88 | |
| 89 | protected ICommandListener getCommandListener() { |
| 90 | if (commandListener == null) { |
| 91 | commandListener = new ICommandListener() { |
| 92 | public void commandChanged(CommandEvent commandEvent) { |
| 93 | if (commandEvent.isHandledChanged() |
| 94 | || commandEvent.isEnabledChanged()) { |
| 95 | if (commandEvent.getCommand().isDefined()) { |
| 96 | setEnabled(commandEvent.getCommand().isEnabled()); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | } |
| 102 | return commandListener; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Build a command from the executable extension information. |
| 107 | * |
| 108 | * @param commandService |
| 109 | * to get the Command object |
| 110 | * @param commandId |
| 111 | * the command id for this action |
| 112 | * @param parameterMap |
| 113 | */ |
| 114 | private void createCommand(ICommandService commandService, |
| 115 | String commandId, Map parameterMap) { |
| 116 | Command cmd = commandService.getCommand(commandId); |
| 117 | if (!cmd.isDefined()) { |
| 118 | WorkbenchPlugin.log("Command " + commandId + " is undefined"); //$NON-NLS-1$//$NON-NLS-2$ |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (parameterMap == null) { |
| 123 | parameterizedCommand = new ParameterizedCommand(cmd, null); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | parameterizedCommand = ParameterizedCommand.generateCommand(cmd, |
| 128 | parameterMap); |
| 129 | } |
| 130 | |
| 131 | public void dispose() { |
| 132 | // not important for command ID, maybe for command though. |
| 133 | handlerService = null; |
| 134 | if (commandListener != null) { |
| 135 | parameterizedCommand.getCommand().removeCommandListener( |
| 136 | commandListener); |
| 137 | commandListener = null; |
| 138 | } |
| 139 | parameterizedCommand = null; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * (non-Javadoc) |
| 144 | * |
| 145 | * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event) |
| 146 | */ |
| 147 | public void runWithEvent(Event event) { |
| 148 | if (handlerService == null) { |
| 149 | String commandId = (parameterizedCommand == null ? "unknownCommand" //$NON-NLS-1$ |
| 150 | : parameterizedCommand.getId()); |
| 151 | WorkbenchPlugin.log("Cannot run " + commandId //$NON-NLS-1$ |
| 152 | + " before command action has been initialized"); //$NON-NLS-1$ |
| 153 | return; |
| 154 | } |
| 155 | try { |
| 156 | if (parameterizedCommand != null) { |
| 157 | handlerService.executeCommand(parameterizedCommand, event); |
| 158 | } |
| 159 | } catch (Exception e) { |
| 160 | WorkbenchPlugin.log(e); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * (non-Javadoc) |
| 166 | * |
| 167 | * @see org.eclipse.jface.action.Action#run() |
| 168 | */ |
| 169 | public void run() { |
| 170 | // hopefully this is never called |
| 171 | runWithEvent(null); |
| 172 | } |
| 173 | |
| 174 | protected void init(IServiceLocator serviceLocator, String commandIdIn, |
| 175 | Map parameterMap) { |
| 176 | if (handlerService != null) { |
| 177 | // already initialized |
| 178 | return; |
| 179 | } |
| 180 | handlerService = (IHandlerService) serviceLocator |
| 181 | .getService(IHandlerService.class); |
| 182 | ICommandService commandService = (ICommandService) serviceLocator |
| 183 | .getService(ICommandService.class); |
| 184 | ICommandImageService commandImageService = (ICommandImageService) serviceLocator |
| 185 | .getService(ICommandImageService.class); |
| 186 | |
| 187 | createCommand(commandService, commandIdIn, parameterMap); |
| 188 | if (parameterizedCommand != null) { |
| 189 | setId(parameterizedCommand.getId()); |
| 190 | setActionDefinitionId(parameterizedCommand.getId()); |
| 191 | try { |
| 192 | setText(parameterizedCommand.getName()); |
| 193 | } catch (NotDefinedException e) { |
| 194 | // if we get this far it shouldn't be a problem |
| 195 | } |
| 196 | parameterizedCommand.getCommand().addCommandListener( |
| 197 | getCommandListener()); |
| 198 | parameterizedCommand.getCommand().setEnabled( |
| 199 | handlerService.getCurrentState()); |
| 200 | setEnabled(parameterizedCommand.getCommand().isEnabled()); |
| 201 | setImageDescriptor(commandImageService.getImageDescriptor( |
| 202 | commandIdIn, ICommandImageService.TYPE_DEFAULT)); |
| 203 | setDisabledImageDescriptor(commandImageService.getImageDescriptor( |
| 204 | commandIdIn, ICommandImageService.TYPE_DISABLED)); |
| 205 | setHoverImageDescriptor(commandImageService.getImageDescriptor( |
| 206 | commandIdIn, ICommandImageService.TYPE_HOVER)); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | protected ParameterizedCommand getParameterizedCommand() { |
| 211 | return parameterizedCommand; |
| 212 | } |
| 213 | |
| 214 | public String getActionDefinitionId() { |
| 215 | return super.getActionDefinitionId(); |
| 216 | } |
| 217 | } |