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
.cms
.ui
.workbench
.util
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
22 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
23 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
24 import org
.eclipse
.core
.commands
.Command
;
25 import org
.eclipse
.core
.commands
.Parameterization
;
26 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
27 import org
.eclipse
.jface
.action
.IContributionItem
;
28 import org
.eclipse
.jface
.action
.IMenuManager
;
29 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.ui
.IWorkbench
;
32 import org
.eclipse
.ui
.commands
.ICommandService
;
33 import org
.eclipse
.ui
.handlers
.IHandlerService
;
34 import org
.eclipse
.ui
.menus
.CommandContributionItem
;
35 import org
.eclipse
.ui
.menus
.CommandContributionItemParameter
;
36 import org
.eclipse
.ui
.services
.IServiceLocator
;
39 * Centralises useful and generic methods when dealing with commands in an
40 * Eclipse Workbench context
42 public class CommandUtils
{
45 * Commodities the refresh of a single command with no parameter in a
46 * Menu.aboutToShow method to simplify further development
48 * Note: that this method should be called with a false show command flag to
49 * remove a contribution that have been previously contributed
51 public static void refreshCommand(IMenuManager menuManager
, IServiceLocator locator
, String cmdId
, String label
,
52 ImageDescriptor icon
, boolean showCommand
) {
53 refreshParameterizedCommand(menuManager
, locator
, cmdId
, label
, icon
, showCommand
, null);
57 * Commodities the refresh the contribution of a command with a map of
58 * parameters in a context menu
60 * The command ID is used has contribution item ID
62 public static void refreshParameterizedCommand(IMenuManager menuManager
, IServiceLocator locator
, String cmdId
,
63 String label
, ImageDescriptor icon
, boolean showCommand
, Map
<String
, String
> params
) {
64 refreshParameterizedCommand(menuManager
, locator
, cmdId
, cmdId
, label
, icon
, showCommand
, params
);
68 * Commodities the refresh the contribution of a command with a map of
69 * parameters in a context menu
73 * @param contributionId
80 public static void refreshParameterizedCommand(IMenuManager menuManager
, IServiceLocator locator
,
81 String contributionId
, String commandId
, String label
, ImageDescriptor icon
, boolean showCommand
,
82 Map
<String
, String
> params
) {
83 IContributionItem ici
= menuManager
.find(contributionId
);
85 menuManager
.remove(ici
);
87 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(locator
,
88 null, commandId
, SWT
.PUSH
);
91 contributionItemParameter
.label
= label
;
92 contributionItemParameter
.icon
= icon
;
95 contributionItemParameter
.parameters
= params
;
97 CommandContributionItem cci
= new CommandContributionItem(contributionItemParameter
);
98 cci
.setId(contributionId
);
103 /** Helper to call a command without parameter easily */
104 public static void callCommand(String commandID
) {
105 callCommand(commandID
, null);
108 /** Helper to call a command with a single parameter easily */
109 public static void callCommand(String commandID
, String parameterID
, String parameterValue
) {
110 Map
<String
, String
> params
= new HashMap
<String
, String
>();
111 params
.put(parameterID
, parameterValue
);
112 callCommand(commandID
, params
);
116 * Helper to call a command with a map of parameters easily
119 * a map that links various command IDs with corresponding String
122 public static void callCommand(String commandID
, Map
<String
, String
> paramMap
) {
124 IWorkbench iw
= WorkbenchUiPlugin
.getDefault().getWorkbench();
125 IHandlerService handlerService
= (IHandlerService
) iw
.getService(IHandlerService
.class);
126 ICommandService cmdService
= (ICommandService
) iw
.getActiveWorkbenchWindow()
127 .getService(ICommandService
.class);
128 Command cmd
= cmdService
.getCommand(commandID
);
130 ArrayList
<Parameterization
> parameters
= null;
131 ParameterizedCommand pc
;
133 if (paramMap
!= null) {
134 // Set parameters of the command to launch :
135 parameters
= new ArrayList
<Parameterization
>();
136 Parameterization parameterization
;
138 for (String id
: paramMap
.keySet()) {
139 parameterization
= new Parameterization(cmd
.getParameter(id
), paramMap
.get(id
));
140 parameters
.add(parameterization
);
142 pc
= new ParameterizedCommand(cmd
, parameters
.toArray(new Parameterization
[parameters
.size()]));
144 pc
= new ParameterizedCommand(cmd
, null);
146 // execute the command
147 handlerService
.executeCommand(pc
, null);
148 } catch (Exception e
) {
149 throw new EclipseUiException("Unexpected error while" + " calling the command " + commandID
, e
);