2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.eclipse
.ui
.workbench
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
22 import org
.argeo
.ArgeoException
;
23 import org
.eclipse
.core
.commands
.Command
;
24 import org
.eclipse
.core
.commands
.Parameterization
;
25 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
26 import org
.eclipse
.jface
.action
.IContributionItem
;
27 import org
.eclipse
.jface
.action
.IMenuManager
;
28 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.ui
.IWorkbench
;
31 import org
.eclipse
.ui
.commands
.ICommandService
;
32 import org
.eclipse
.ui
.handlers
.IHandlerService
;
33 import org
.eclipse
.ui
.menus
.CommandContributionItem
;
34 import org
.eclipse
.ui
.menus
.CommandContributionItemParameter
;
35 import org
.eclipse
.ui
.services
.IServiceLocator
;
38 * Centralises useful and generic methods when dealing with commands in an
39 * Eclipse Workbench context
41 public class CommandUtils
{
44 * Commodities the refresh of a single command with no parameter in a
45 * Menu.aboutToShow method to simplify further development
47 * Note: that this method should be called with a false show command flag to
48 * remove a contribution that have been previously contributed
57 public static void refreshCommand(IMenuManager menuManager
,
58 IServiceLocator locator
, String cmdId
, String label
,
59 ImageDescriptor icon
, boolean showCommand
) {
60 refreshParameterizedCommand(menuManager
, locator
, cmdId
, label
, icon
,
65 * Commodities the refresh the contribution of a command with a map of
66 * parameters in a context menu
68 * The command ID is used has contribution item ID
77 public static void refreshParameterizedCommand(IMenuManager menuManager
,
78 IServiceLocator locator
, String cmdId
, String label
,
79 ImageDescriptor icon
, boolean showCommand
,
80 Map
<String
, String
> params
) {
81 refreshParameterizedCommand(menuManager
, locator
, cmdId
, cmdId
, label
,
82 icon
, showCommand
, params
);
86 * Commodities the refresh the contribution of a command with a map of
87 * parameters in a context menu
91 * @param contributionId
98 public static void refreshParameterizedCommand(IMenuManager menuManager
,
99 IServiceLocator locator
, String contributionId
, String commandId
,
100 String label
, ImageDescriptor icon
, boolean showCommand
,
101 Map
<String
, String
> params
) {
102 IContributionItem ici
= menuManager
.find(contributionId
);
104 menuManager
.remove(ici
);
106 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
107 locator
, null, commandId
, SWT
.PUSH
);
110 contributionItemParameter
.label
= label
;
111 contributionItemParameter
.icon
= icon
;
114 contributionItemParameter
.parameters
= params
;
116 CommandContributionItem cci
= new CommandContributionItem(
117 contributionItemParameter
);
118 cci
.setId(contributionId
);
119 menuManager
.add(cci
);
123 /** Helper to call a command without parameter easily */
124 public static void callCommand(String commandID
) {
125 callCommand(commandID
, null);
128 /** Helper to call a command with a single parameter easily */
129 public static void callCommand(String commandID
, String parameterID
,
130 String parameterValue
) {
131 Map
<String
, String
> params
= new HashMap
<String
, String
>();
132 params
.put(parameterID
, parameterValue
);
133 callCommand(commandID
, params
);
137 * Helper to call a command with a map of parameters easily
140 * a map that links various command IDs with corresponding String
143 public static void callCommand(String commandID
,
144 Map
<String
, String
> paramMap
) {
146 IWorkbench iw
= WorkbenchUiPlugin
.getDefault().getWorkbench();
147 IHandlerService handlerService
= (IHandlerService
) iw
148 .getService(IHandlerService
.class);
149 ICommandService cmdService
= (ICommandService
) iw
150 .getActiveWorkbenchWindow().getService(
151 ICommandService
.class);
152 Command cmd
= cmdService
.getCommand(commandID
);
154 ArrayList
<Parameterization
> parameters
= null;
155 ParameterizedCommand pc
;
157 if (paramMap
!= null) {
158 // Set parameters of the command to launch :
159 parameters
= new ArrayList
<Parameterization
>();
160 Parameterization parameterization
;
162 for (String id
: paramMap
.keySet()) {
163 parameterization
= new Parameterization(
164 cmd
.getParameter(id
), paramMap
.get(id
));
165 parameters
.add(parameterization
);
167 pc
= new ParameterizedCommand(cmd
,
168 parameters
.toArray(new Parameterization
[parameters
171 pc
= new ParameterizedCommand(cmd
, null);
173 // execute the command
174 handlerService
.executeCommand(pc
, null);
175 } catch (Exception e
) {
176 throw new ArgeoException("Unexpected error while"
177 + " calling the command " + commandID
, e
);