]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/argeo-commons/org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/util/CommandUtils.java
Merge remote-tracking branch 'origin/master' into testing
[gpl/argeo-slc.git] / legacy / argeo-commons / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / util / CommandUtils.java
1 package org.argeo.cms.ui.workbench.util;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6
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;
22
23 /**
24 * Centralises useful and generic methods when dealing with commands in an
25 * Eclipse Workbench context
26 */
27 public class CommandUtils {
28
29 /**
30 * Commodities the refresh of a single command with no parameter in a
31 * Menu.aboutToShow method to simplify further development
32 *
33 * Note: that this method should be called with a false show command flag to
34 * remove a contribution that have been previously contributed
35 */
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);
39 }
40
41 /**
42 * Commodities the refresh the contribution of a command with a map of
43 * parameters in a context menu
44 *
45 * The command ID is used has contribution item ID
46 */
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);
50 }
51
52 /**
53 * Commodities the refresh the contribution of a command with a map of
54 * parameters in a context menu
55 *
56 * @param menuManager
57 * @param locator
58 * @param contributionId
59 * @param commandId
60 * @param label
61 * @param icon
62 * @param showCommand
63 * @param params
64 */
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);
69 if (ici != null)
70 menuManager.remove(ici);
71 if (showCommand) {
72 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(locator,
73 null, commandId, SWT.PUSH);
74
75 // Set Params
76 contributionItemParameter.label = label;
77 contributionItemParameter.icon = icon;
78
79 if (params != null)
80 contributionItemParameter.parameters = params;
81
82 CommandContributionItem cci = new CommandContributionItem(contributionItemParameter);
83 cci.setId(contributionId);
84 menuManager.add(cci);
85 }
86 }
87
88 /** Helper to call a command without parameter easily */
89 public static void callCommand(String commandID) {
90 callCommand(commandID, null);
91 }
92
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);
98 }
99
100 /**
101 * Helper to call a command with a map of parameters easily
102 *
103 * @param paramMap
104 * a map that links various command IDs with corresponding String
105 * values.
106 */
107 public static void callCommand(String commandID, Map<String, String> paramMap) {
108 try {
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);
114
115 ArrayList<Parameterization> parameters = null;
116 ParameterizedCommand pc;
117
118 if (paramMap != null) {
119 // Set parameters of the command to launch :
120 parameters = new ArrayList<Parameterization>();
121 Parameterization parameterization;
122
123 for (String id : paramMap.keySet()) {
124 parameterization = new Parameterization(cmd.getParameter(id), paramMap.get(id));
125 parameters.add(parameterization);
126 }
127 pc = new ParameterizedCommand(cmd, parameters.toArray(new Parameterization[parameters.size()]));
128 } else
129 pc = new ParameterizedCommand(cmd, null);
130
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);
135 }
136 }
137 }