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
58 public static void refreshCommand(IMenuManager menuManager
,
59 IServiceLocator locator
, String cmdId
, String label
,
60 ImageDescriptor icon
, boolean showCommand
) {
61 refreshParameterizedCommand(menuManager
, locator
, cmdId
, label
, icon
,
66 * Commodities the refresh the contribution of a command with a map of
67 * parameters in a context menu
69 * The command ID is used has contribution item ID
78 public static void refreshParameterizedCommand(IMenuManager menuManager
,
79 IServiceLocator locator
, String cmdId
, String label
,
80 ImageDescriptor icon
, boolean showCommand
,
81 Map
<String
, String
> params
) {
82 refreshParameterizedCommand(menuManager
, locator
, cmdId
, cmdId
, label
,
83 icon
, showCommand
, params
);
87 * Commodities the refresh the contribution of a command with a map of
88 * parameters in a context menu
92 * @param contributionId
99 public static void refreshParameterizedCommand(IMenuManager menuManager
,
100 IServiceLocator locator
, String contributionId
, String commandId
,
101 String label
, ImageDescriptor icon
, boolean showCommand
,
102 Map
<String
, String
> params
) {
103 IContributionItem ici
= menuManager
.find(contributionId
);
105 menuManager
.remove(ici
);
107 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
108 locator
, null, commandId
, SWT
.PUSH
);
111 contributionItemParameter
.label
= label
;
112 contributionItemParameter
.icon
= icon
;
115 contributionItemParameter
.parameters
= params
;
117 CommandContributionItem cci
= new CommandContributionItem(
118 contributionItemParameter
);
119 cci
.setId(contributionId
);
120 menuManager
.add(cci
);
124 /** Helper to call a command without parameter easily */
125 public static void callCommand(String commandID
) {
126 callCommand(commandID
, null);
129 /** Helper to call a command with a single parameter easily */
130 public static void callCommand(String commandID
, String parameterID
,
131 String parameterValue
) {
132 Map
<String
, String
> params
= new HashMap
<String
, String
>();
133 params
.put(parameterID
, parameterValue
);
134 callCommand(commandID
, params
);
138 * Helper to call a command with a map of parameters easily
141 * a map that links various command IDs with corresponding String
144 public static void callCommand(String commandID
,
145 Map
<String
, String
> paramMap
) {
147 IWorkbench iw
= WorkbenchUiPlugin
.getDefault().getWorkbench();
148 IHandlerService handlerService
= (IHandlerService
) iw
149 .getService(IHandlerService
.class);
150 ICommandService cmdService
= (ICommandService
) iw
151 .getActiveWorkbenchWindow().getService(
152 ICommandService
.class);
153 Command cmd
= cmdService
.getCommand(commandID
);
155 ArrayList
<Parameterization
> parameters
= null;
156 ParameterizedCommand pc
;
158 if (paramMap
!= null) {
159 // Set parameters of the command to launch :
160 parameters
= new ArrayList
<Parameterization
>();
161 Parameterization parameterization
;
163 for (String id
: paramMap
.keySet()) {
164 parameterization
= new Parameterization(
165 cmd
.getParameter(id
), paramMap
.get(id
));
166 parameters
.add(parameterization
);
168 pc
= new ParameterizedCommand(cmd
,
169 parameters
.toArray(new Parameterization
[parameters
172 pc
= new ParameterizedCommand(cmd
, null);
174 // execute the command
175 handlerService
.executeCommand(pc
, null);
176 } catch (Exception e
) {
177 throw new EclipseUiException("Unexpected error while"
178 + " calling the command " + commandID
, e
);