1 package org
.argeo
.cms
.ui
.workbench
.util
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
7 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
8 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
9 import org
.eclipse
.core
.commands
.Command
;
10 import org
.eclipse
.core
.commands
.Parameterization
;
11 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
12 import org
.eclipse
.jface
.action
.IContributionItem
;
13 import org
.eclipse
.jface
.action
.IMenuManager
;
14 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.ui
.IWorkbench
;
17 import org
.eclipse
.ui
.commands
.ICommandService
;
18 import org
.eclipse
.ui
.handlers
.IHandlerService
;
19 import org
.eclipse
.ui
.menus
.CommandContributionItem
;
20 import org
.eclipse
.ui
.menus
.CommandContributionItemParameter
;
21 import org
.eclipse
.ui
.services
.IServiceLocator
;
24 * Centralises useful and generic methods when dealing with commands in an
25 * Eclipse Workbench context
27 public class CommandUtils
{
30 * Commodities the refresh of a single command with no parameter in a
31 * Menu.aboutToShow method to simplify further development
33 * Note: that this method should be called with a false show command flag to
34 * remove a contribution that have been previously contributed
36 public static void refreshCommand(IMenuManager menuManager
, IServiceLocator locator
, String cmdId
, String label
,
37 ImageDescriptor icon
, boolean showCommand
) {
38 refreshParameterizedCommand(menuManager
, locator
, cmdId
, label
, icon
, showCommand
, null);
42 * Commodities the refresh the contribution of a command with a map of
43 * parameters in a context menu
45 * The command ID is used has contribution item ID
47 public static void refreshParameterizedCommand(IMenuManager menuManager
, IServiceLocator locator
, String cmdId
,
48 String label
, ImageDescriptor icon
, boolean showCommand
, Map
<String
, String
> params
) {
49 refreshParameterizedCommand(menuManager
, locator
, cmdId
, cmdId
, label
, icon
, showCommand
, params
);
53 * Commodities the refresh the contribution of a command with a map of
54 * parameters in a context menu
58 * @param contributionId
65 public static void refreshParameterizedCommand(IMenuManager menuManager
, IServiceLocator locator
,
66 String contributionId
, String commandId
, String label
, ImageDescriptor icon
, boolean showCommand
,
67 Map
<String
, String
> params
) {
68 IContributionItem ici
= menuManager
.find(contributionId
);
70 menuManager
.remove(ici
);
72 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(locator
,
73 null, commandId
, SWT
.PUSH
);
76 contributionItemParameter
.label
= label
;
77 contributionItemParameter
.icon
= icon
;
80 contributionItemParameter
.parameters
= params
;
82 CommandContributionItem cci
= new CommandContributionItem(contributionItemParameter
);
83 cci
.setId(contributionId
);
88 /** Helper to call a command without parameter easily */
89 public static void callCommand(String commandID
) {
90 callCommand(commandID
, null);
93 /** Helper to call a command with a single parameter easily */
94 public static void callCommand(String commandID
, String parameterID
, String parameterValue
) {
95 Map
<String
, String
> params
= new HashMap
<String
, String
>();
96 params
.put(parameterID
, parameterValue
);
97 callCommand(commandID
, params
);
101 * Helper to call a command with a map of parameters easily
104 * a map that links various command IDs with corresponding String
107 public static void callCommand(String commandID
, Map
<String
, String
> paramMap
) {
109 IWorkbench iw
= WorkbenchUiPlugin
.getDefault().getWorkbench();
110 IHandlerService handlerService
= (IHandlerService
) iw
.getService(IHandlerService
.class);
111 ICommandService cmdService
= (ICommandService
) iw
.getActiveWorkbenchWindow()
112 .getService(ICommandService
.class);
113 Command cmd
= cmdService
.getCommand(commandID
);
115 ArrayList
<Parameterization
> parameters
= null;
116 ParameterizedCommand pc
;
118 if (paramMap
!= null) {
119 // Set parameters of the command to launch :
120 parameters
= new ArrayList
<Parameterization
>();
121 Parameterization parameterization
;
123 for (String id
: paramMap
.keySet()) {
124 parameterization
= new Parameterization(cmd
.getParameter(id
), paramMap
.get(id
));
125 parameters
.add(parameterization
);
127 pc
= new ParameterizedCommand(cmd
, parameters
.toArray(new Parameterization
[parameters
.size()]));
129 pc
= new ParameterizedCommand(cmd
, null);
131 // execute the command
132 handlerService
.executeCommand(pc
, null);
133 } catch (Exception e
) {
134 throw new EclipseUiException("Unexpected error while" + " calling the command " + commandID
, e
);