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
.utils
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
22 import org
.argeo
.ArgeoException
;
23 import org
.argeo
.eclipse
.ui
.ArgeoUiPlugin
;
24 import org
.eclipse
.core
.commands
.Command
;
25 import org
.eclipse
.core
.commands
.IParameter
;
26 import org
.eclipse
.core
.commands
.Parameterization
;
27 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
28 import org
.eclipse
.jface
.action
.IContributionItem
;
29 import org
.eclipse
.jface
.action
.IMenuManager
;
30 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.ui
.IWorkbench
;
33 import org
.eclipse
.ui
.IWorkbenchWindow
;
34 import org
.eclipse
.ui
.commands
.ICommandService
;
35 import org
.eclipse
.ui
.handlers
.IHandlerService
;
36 import org
.eclipse
.ui
.menus
.CommandContributionItem
;
37 import org
.eclipse
.ui
.menus
.CommandContributionItemParameter
;
38 import org
.eclipse
.ui
.services
.IServiceLocator
;
41 * Centralises useful and generic methods when dealing with commands in an
42 * Eclipse Workbench context
44 public class CommandUtils
{
47 * Commodities the refresh of a single command with no parameter in a
48 * Menu.aboutToShow method to simplify further development
50 * Note: that this method should be called with a false show command flag to
51 * remove a contribution that have been previously contributed
60 public static void refreshCommand(IMenuManager menuManager
,
61 IServiceLocator locator
, String cmdId
, String label
,
62 ImageDescriptor icon
, boolean showCommand
) {
63 refreshParameterizedCommand(menuManager
, locator
, cmdId
, label
, icon
,
68 * Commodities the refresh the contribution of a command with a map of
69 * parameters in a context menu
71 * The command ID is used has contribution item ID
80 public static void refreshParameterizedCommand(IMenuManager menuManager
,
81 IServiceLocator locator
, String cmdId
, String label
,
82 ImageDescriptor icon
, boolean showCommand
,
83 Map
<String
, String
> params
) {
84 refreshParameterizedCommand(menuManager
, locator
, cmdId
, cmdId
, label
,
85 icon
, showCommand
, params
);
89 * Commodities the refresh the contribution of a command with a map of
90 * parameters in a context menu
94 * @param contributionId
101 public static void refreshParameterizedCommand(IMenuManager menuManager
,
102 IServiceLocator locator
, String contributionId
, String commandId
,
103 String label
, ImageDescriptor icon
, boolean showCommand
,
104 Map
<String
, String
> params
) {
105 IContributionItem ici
= menuManager
.find(contributionId
);
107 menuManager
.remove(ici
);
109 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
110 locator
, null, commandId
, SWT
.PUSH
);
113 contributionItemParameter
.label
= label
;
114 contributionItemParameter
.icon
= icon
;
117 contributionItemParameter
.parameters
= params
;
119 CommandContributionItem cci
= new CommandContributionItem(
120 contributionItemParameter
);
121 cci
.setId(contributionId
);
122 menuManager
.add(cci
);
126 /** Helper to call a command without parameter easily */
127 public static void callCommand(String commandID
) {
128 callCommand(commandID
, null);
131 /** Helper to call a command with a single parameter easily */
132 public static void callCommand(String commandID
, String parameterID
,
133 String parameterValue
) {
134 Map
<String
, String
> params
= new HashMap
<String
, String
>();
135 params
.put(parameterID
, parameterValue
);
136 callCommand(commandID
, params
);
140 * Helper to call a command with a map of parameters easily
143 * a map that links various command IDs with corresponding String
146 public static void callCommand(String commandID
,
147 Map
<String
, String
> paramMap
) {
149 IWorkbench iw
= ArgeoUiPlugin
.getDefault().getWorkbench();
150 IHandlerService handlerService
= (IHandlerService
) iw
151 .getService(IHandlerService
.class);
152 ICommandService cmdService
= (ICommandService
) iw
153 .getActiveWorkbenchWindow().getService(
154 ICommandService
.class);
155 Command cmd
= cmdService
.getCommand(commandID
);
157 ArrayList
<Parameterization
> parameters
= null;
158 ParameterizedCommand pc
;
160 if (paramMap
!= null) {
161 // Set parameters of the command to launch :
162 parameters
= new ArrayList
<Parameterization
>();
163 Parameterization parameterization
;
165 for (String id
: paramMap
.keySet()) {
166 parameterization
= new Parameterization(
167 cmd
.getParameter(id
), paramMap
.get(id
));
168 parameters
.add(parameterization
);
170 pc
= new ParameterizedCommand(cmd
,
171 parameters
.toArray(new Parameterization
[parameters
174 pc
= new ParameterizedCommand(cmd
, null);
176 // execute the command
177 handlerService
.executeCommand(pc
, null);
178 } catch (Exception e
) {
179 throw new ArgeoException("Unexpected error while"
180 + " calling the command " + commandID
, e
);
184 // legacy methods. Should be removed soon
187 * Shortcut to call a command with a single parameter.
189 * WARNING: none of the parameter can be null
191 * @deprecated rather use <code>callCommand(commandID,parameterID,
192 parameterValue)</code>
194 public static void CallCommandWithOneParameter(String commandId
,
195 String paramId
, String paramValue
) {
197 IWorkbench iw
= ArgeoUiPlugin
.getDefault().getWorkbench();
198 IHandlerService handlerService
= (IHandlerService
) iw
199 .getService(IHandlerService
.class);
201 // Gets a command that must have been previously registered
202 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
203 ICommandService cmdService
= (ICommandService
) window
204 .getService(ICommandService
.class);
205 Command cmd
= cmdService
.getCommand(commandId
);
207 // Manages the single parameter
208 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
209 IParameter iparam
= cmd
.getParameter(paramId
);
210 Parameterization params
= new Parameterization(iparam
, paramValue
);
211 parameters
.add(params
);
213 // Create and execute the command
214 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
215 parameters
.toArray(new Parameterization
[parameters
.size()]));
216 handlerService
= (IHandlerService
) window
217 .getService(IHandlerService
.class);
218 handlerService
.executeCommand(pc
, null);
219 } catch (Exception e
) {
220 throw new ArgeoException(
221 "Error calling command of id:" + commandId
, e
);
226 * Commodities the refresh of a single command with a map of parameters in a
227 * Menu.aboutToShow method to simplify further development Rather use
228 * {@link refreshParameterizedCommand()}
231 public static void refreshParametrizedCommand(IMenuManager menuManager
,
232 IServiceLocator locator
, String cmdId
, String label
,
233 ImageDescriptor icon
, boolean showCommand
,
234 Map
<String
, String
> params
) {
235 refreshParameterizedCommand(menuManager
, locator
, cmdId
, label
, icon
,
236 showCommand
, params
);