]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java
Update command utilities to enable the definition of multiple occurrences of the...
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / utils / CommandUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui.utils;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.argeo.ArgeoException;
23 import org.argeo.eclipse.ui.ArgeoUiPlugin;
24 import org.eclipse.core.commands.Command;
25 import org.eclipse.core.commands.IParameter;
26 import org.eclipse.core.commands.Parameterization;
27 import org.eclipse.core.commands.ParameterizedCommand;
28 import org.eclipse.jface.action.IContributionItem;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.commands.ICommandService;
35 import org.eclipse.ui.handlers.IHandlerService;
36 import org.eclipse.ui.menus.CommandContributionItem;
37 import org.eclipse.ui.menus.CommandContributionItemParameter;
38 import org.eclipse.ui.services.IServiceLocator;
39
40 /**
41 * Centralizes useful and generic methods concerning eclipse commands.
42 *
43 */
44 public class CommandUtils {
45
46 /**
47 * Factorizes command call that is quite verbose and always the same
48 *
49 * NOTE that none of the parameter can be null
50 */
51 public static void CallCommandWithOneParameter(String commandId,
52 String paramId, String paramValue) {
53 try {
54 IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
55
56 IHandlerService handlerService = (IHandlerService) iw
57 .getService(IHandlerService.class);
58
59 // get the command from plugin.xml
60 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
61 ICommandService cmdService = (ICommandService) window
62 .getService(ICommandService.class);
63
64 Command cmd = cmdService.getCommand(commandId);
65
66 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
67
68 // get the parameter
69 IParameter iparam = cmd.getParameter(paramId);
70
71 Parameterization params = new Parameterization(iparam, paramValue);
72 parameters.add(params);
73
74 // build the parameterized command
75 ParameterizedCommand pc = new ParameterizedCommand(cmd,
76 parameters.toArray(new Parameterization[parameters.size()]));
77
78 // execute the command
79 handlerService = (IHandlerService) window
80 .getService(IHandlerService.class);
81 handlerService.executeCommand(pc, null);
82 } catch (Exception e) {
83 throw new ArgeoException("Error while calling command of id :"
84 + commandId, e);
85 }
86 }
87
88 /**
89 * Commodities the refresh of a single command with no parameter in a
90 * Menu.aboutToShow method to simplify further development
91 *
92 * @param menuManager
93 * @param locator
94 * @param cmdId
95 * @param label
96 * @param iconPath
97 * @param showCommand
98 */
99 public static void refreshCommand(IMenuManager menuManager,
100 IServiceLocator locator, String cmdId, String label,
101 ImageDescriptor icon, boolean showCommand) {
102 refreshParametrizedCommand(menuManager, locator, cmdId, label, icon,
103 showCommand, null);
104 }
105
106 /**
107 * Commodities the refresh of a single command with a map of parameters in a
108 * Menu.aboutToShow method to simplify further development Rather use
109 * {@link refreshParameterizedCommand()}
110 */
111 @Deprecated
112 public static void refreshParametrizedCommand(IMenuManager menuManager,
113 IServiceLocator locator, String cmdId, String label,
114 ImageDescriptor icon, boolean showCommand,
115 Map<String, String> params) {
116 refreshParameterizedCommand(menuManager, locator, cmdId, label, icon,
117 showCommand, params);
118 }
119
120 /**
121 * Commodities the refresh the contribution of a command with a map of
122 * parameters in a context menu
123 *
124 * The command ID is used has contribution item ID
125 *
126 * @param menuManager
127 * @param locator
128 * @param cmdId
129 * @param label
130 * @param iconPath
131 * @param showCommand
132 */
133 public static void refreshParameterizedCommand(IMenuManager menuManager,
134 IServiceLocator locator, String cmdId, String label,
135 ImageDescriptor icon, boolean showCommand,
136 Map<String, String> params) {
137 refreshParameterizedCommand(menuManager, locator, cmdId, cmdId, label,
138 icon, showCommand, params);
139 }
140
141 /**
142 * Commodities the refresh the contribution of a command with a map of
143 * parameters in a context menu
144 *
145 * @param menuManager
146 * @param locator
147 * @param contributionId
148 * @param commandId
149 * @param label
150 * @param icon
151 * @param showCommand
152 * @param params
153 */
154 public static void refreshParameterizedCommand(IMenuManager menuManager,
155 IServiceLocator locator, String contributionId, String commandId,
156 String label, ImageDescriptor icon, boolean showCommand,
157 Map<String, String> params) {
158 IContributionItem ici = menuManager.find(contributionId);
159 if (ici != null)
160 menuManager.remove(ici);
161 if (showCommand) {
162 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
163 locator, null, commandId, SWT.PUSH);
164
165 // Set Params
166 contributionItemParameter.label = label;
167 contributionItemParameter.icon = icon;
168
169 if (params != null)
170 contributionItemParameter.parameters = params;
171
172 CommandContributionItem cci = new CommandContributionItem(
173 contributionItemParameter);
174 cci.setId(contributionId);
175 menuManager.add(cci);
176 }
177 }
178
179 /** Helper to call a command without parameter easily */
180 public static void callCommand(String commandID) {
181 callCommand(commandID, null);
182 }
183
184 /** Helper to call a command with a single parameter easily */
185 public static void callCommand(String commandID, String parameterID,
186 String parameterValue) {
187 Map<String, String> params = new HashMap<String, String>();
188 params.put(parameterID, parameterValue);
189 callCommand(commandID, params);
190 }
191
192 /**
193 * Helper to call a command with a map of parameters easily
194 *
195 * @param paramMap
196 * a map that links various commands ids with corresponding
197 * String values.
198 */
199 public static void callCommand(String commandID,
200 Map<String, String> paramMap) {
201 try {
202 IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
203 IHandlerService handlerService = (IHandlerService) iw
204 .getService(IHandlerService.class);
205 ICommandService cmdService = (ICommandService) iw
206 .getActiveWorkbenchWindow().getService(
207 ICommandService.class);
208 Command cmd = cmdService.getCommand(commandID);
209
210 ArrayList<Parameterization> parameters = null;
211 ParameterizedCommand pc;
212
213 if (paramMap != null) {
214 // Set parameters of the command to launch :
215 parameters = new ArrayList<Parameterization>();
216 Parameterization parameterization;
217
218 for (String id : paramMap.keySet()) {
219 parameterization = new Parameterization(
220 cmd.getParameter(id), paramMap.get(id));
221 parameters.add(parameterization);
222 }
223 pc = new ParameterizedCommand(cmd,
224 parameters.toArray(new Parameterization[parameters
225 .size()]));
226 } else
227 pc = new ParameterizedCommand(cmd, null);
228
229 // execute the command
230 handlerService.executeCommand(pc, null);
231 } catch (Exception e) {
232 throw new ArgeoException("Unexpected error while"
233 + " calling the command " + commandID, e);
234 }
235 }
236 }