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 * Centralizes useful and generic methods concerning eclipse commands.
44 public class CommandUtils
{
47 * Factorizes command call that is quite verbose and always the same
49 * NOTE that none of the parameter can be null
51 public static void CallCommandWithOneParameter(String commandId
,
52 String paramId
, String paramValue
) {
54 IWorkbench iw
= ArgeoUiPlugin
.getDefault().getWorkbench();
56 IHandlerService handlerService
= (IHandlerService
) iw
57 .getService(IHandlerService
.class);
59 // get the command from plugin.xml
60 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
61 ICommandService cmdService
= (ICommandService
) window
62 .getService(ICommandService
.class);
64 Command cmd
= cmdService
.getCommand(commandId
);
66 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
69 IParameter iparam
= cmd
.getParameter(paramId
);
71 Parameterization params
= new Parameterization(iparam
, paramValue
);
72 parameters
.add(params
);
74 // build the parameterized command
75 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
76 parameters
.toArray(new Parameterization
[parameters
.size()]));
78 // execute the command
79 handlerService
= (IHandlerService
) window
80 .getService(IHandlerService
.class);
81 handlerService
.executeCommand(pc
, null);
82 } catch (Exception e
) {
83 throw new ArgeoException("Error while calling command of id :"
89 * Commodities the refresh of a single command with no parameter in a
90 * Menu.aboutToShow method to simplify further development
99 public static void refreshCommand(IMenuManager menuManager
,
100 IServiceLocator locator
, String cmdId
, String label
,
101 ImageDescriptor icon
, boolean showCommand
) {
102 refreshParametrizedCommand(menuManager
, locator
, cmdId
, label
, icon
,
107 * Commodities the refresh of a single command with a map of parameters in a
108 * Menu.aboutToShow method to simplify further development Rather use
109 * {@link refreshParameterizedCommand()}
112 public static void refreshParametrizedCommand(IMenuManager menuManager
,
113 IServiceLocator locator
, String cmdId
, String label
,
114 ImageDescriptor icon
, boolean showCommand
,
115 Map
<String
, String
> params
) {
116 refreshParameterizedCommand(menuManager
, locator
, cmdId
, label
, icon
,
117 showCommand
, params
);
121 * Commodities the refresh the contribution of a command with a map of
122 * parameters in a context menu
124 * The command ID is used has contribution item ID
133 public static void refreshParameterizedCommand(IMenuManager menuManager
,
134 IServiceLocator locator
, String cmdId
, String label
,
135 ImageDescriptor icon
, boolean showCommand
,
136 Map
<String
, String
> params
) {
137 refreshParameterizedCommand(menuManager
, locator
, cmdId
, cmdId
, label
,
138 icon
, showCommand
, params
);
142 * Commodities the refresh the contribution of a command with a map of
143 * parameters in a context menu
147 * @param contributionId
154 public static void refreshParameterizedCommand(IMenuManager menuManager
,
155 IServiceLocator locator
, String contributionId
, String commandId
,
156 String label
, ImageDescriptor icon
, boolean showCommand
,
157 Map
<String
, String
> params
) {
158 IContributionItem ici
= menuManager
.find(contributionId
);
160 menuManager
.remove(ici
);
162 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
163 locator
, null, commandId
, SWT
.PUSH
);
166 contributionItemParameter
.label
= label
;
167 contributionItemParameter
.icon
= icon
;
170 contributionItemParameter
.parameters
= params
;
172 CommandContributionItem cci
= new CommandContributionItem(
173 contributionItemParameter
);
174 cci
.setId(contributionId
);
175 menuManager
.add(cci
);
179 /** Helper to call a command without parameter easily */
180 public static void callCommand(String commandID
) {
181 callCommand(commandID
, null);
184 /** Helper to call a command with a single parameter easily */
185 public static void callCommand(String commandID
, String parameterID
,
186 String parameterValue
) {
187 Map
<String
, String
> params
= new HashMap
<String
, String
>();
188 params
.put(parameterID
, parameterValue
);
189 callCommand(commandID
, params
);
193 * Helper to call a command with a map of parameters easily
196 * a map that links various commands ids with corresponding
199 public static void callCommand(String commandID
,
200 Map
<String
, String
> paramMap
) {
202 IWorkbench iw
= ArgeoUiPlugin
.getDefault().getWorkbench();
203 IHandlerService handlerService
= (IHandlerService
) iw
204 .getService(IHandlerService
.class);
205 ICommandService cmdService
= (ICommandService
) iw
206 .getActiveWorkbenchWindow().getService(
207 ICommandService
.class);
208 Command cmd
= cmdService
.getCommand(commandID
);
210 ArrayList
<Parameterization
> parameters
= null;
211 ParameterizedCommand pc
;
213 if (paramMap
!= null) {
214 // Set parameters of the command to launch :
215 parameters
= new ArrayList
<Parameterization
>();
216 Parameterization parameterization
;
218 for (String id
: paramMap
.keySet()) {
219 parameterization
= new Parameterization(
220 cmd
.getParameter(id
), paramMap
.get(id
));
221 parameters
.add(parameterization
);
223 pc
= new ParameterizedCommand(cmd
,
224 parameters
.toArray(new Parameterization
[parameters
227 pc
= new ParameterizedCommand(cmd
, null);
229 // execute the command
230 handlerService
.executeCommand(pc
, null);
231 } catch (Exception e
) {
232 throw new ArgeoException("Unexpected error while"
233 + " calling the command " + commandID
, e
);