1 package org
.argeo
.slc
.client
.ui
.dist
.utils
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
7 import org
.argeo
.ArgeoException
;
8 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
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
.swt
.SWT
;
15 import org
.eclipse
.ui
.IWorkbench
;
16 import org
.eclipse
.ui
.commands
.ICommandService
;
17 import org
.eclipse
.ui
.handlers
.IHandlerService
;
18 import org
.eclipse
.ui
.menus
.CommandContributionItem
;
19 import org
.eclipse
.ui
.menus
.CommandContributionItemParameter
;
20 import org
.eclipse
.ui
.services
.IServiceLocator
;
23 * Centralizes useful methods to manage command updates
25 public class CommandHelpers
{
28 * Refresh the given command.
30 public static void refreshCommand(IMenuManager menuManager
,
31 IServiceLocator locator
, String cmdId
, String label
, String iconPath
,
32 boolean showCommand
) {
33 IContributionItem ici
= menuManager
.find(cmdId
);
35 menuManager
.remove(ici
);
39 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
40 locator
, null, cmdId
, SWT
.PUSH
);
41 contributionItemParameter
.label
= label
;
42 contributionItemParameter
.icon
= DistPlugin
.getImageDescriptor(iconPath
);
43 CommandContributionItem cci
= new CommandContributionItem(
44 contributionItemParameter
);
51 * Refresh the given command and optionally corresponding parameters.
58 * Command must be explicitly removed from the context menu at
59 * each refresh setting this to false.
61 * maps a paramId with a String value
63 public static void refreshParameterizedCommand(IMenuManager menuManager
,
64 IServiceLocator locator
, String cmdId
, String label
, String iconPath
,
65 boolean showCommand
, Map
<String
, String
> params
) {
66 IContributionItem ici
= menuManager
.find(cmdId
);
68 menuManager
.remove(ici
);
72 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
73 locator
, null, cmdId
, SWT
.PUSH
);
74 contributionItemParameter
.label
= label
;
75 contributionItemParameter
.icon
= DistPlugin
.getImageDescriptor(iconPath
);
78 contributionItemParameter
.parameters
= params
;
80 CommandContributionItem cci
= new CommandContributionItem(
81 contributionItemParameter
);
87 /** Helper to call a command without parameter easily */
88 public static void callCommand(String commandID
) {
89 callCommand(commandID
, null);
92 /** Helper to call a command with a single parameter easily */
93 public static void callCommand(String commandID
, String parameterID
,
94 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 commands ids with corresponding
107 public static void callCommand(String commandID
,
108 Map
<String
, String
> paramMap
) {
110 IWorkbench iw
= DistPlugin
.getDefault().getWorkbench();
111 IHandlerService handlerService
= (IHandlerService
) iw
112 .getService(IHandlerService
.class);
113 ICommandService cmdService
= (ICommandService
) iw
114 .getActiveWorkbenchWindow().getService(
115 ICommandService
.class);
116 Command cmd
= cmdService
.getCommand(commandID
);
118 ArrayList
<Parameterization
> parameters
= null;
119 ParameterizedCommand pc
;
121 if (paramMap
!= null) {
122 // Set parameters of the command to launch :
123 parameters
= new ArrayList
<Parameterization
>();
124 Parameterization parameterization
;
125 for (String id
: paramMap
.keySet()) {
126 parameterization
= new Parameterization(
127 cmd
.getParameter(id
), paramMap
.get(id
));
128 parameters
.add(parameterization
);
130 pc
= new ParameterizedCommand(cmd
,
131 parameters
.toArray(new Parameterization
[parameters
.size()]));
133 pc
= new ParameterizedCommand(cmd
, null);
135 // build the parameterized command
136 // execute the command
137 handlerService
.executeCommand(pc
, null);
138 } catch (Exception e
) {
139 throw new ArgeoException(
140 "Unexepected exception while opening node editor", e
);