]> 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 and clean comments
[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 * Centralises useful and generic methods when dealing with commands in an
42 * Eclipse Workbench context
43 */
44 public class CommandUtils {
45
46 /**
47 * Commodities the refresh of a single command with no parameter in a
48 * Menu.aboutToShow method to simplify further development
49 *
50 * Note: that this method should be called with a false show command flag to
51 * remove a contribution that have been previously contributed
52 *
53 * @param menuManager
54 * @param locator
55 * @param cmdId
56 * @param label
57 * @param icon
58 * @param showCommand
59 */
60 public static void refreshCommand(IMenuManager menuManager,
61 IServiceLocator locator, String cmdId, String label,
62 ImageDescriptor icon, boolean showCommand) {
63 refreshParameterizedCommand(menuManager, locator, cmdId, label, icon,
64 showCommand, null);
65 }
66
67 /**
68 * Commodities the refresh the contribution of a command with a map of
69 * parameters in a context menu
70 *
71 * The command ID is used has contribution item ID
72 *
73 * @param menuManager
74 * @param locator
75 * @param cmdId
76 * @param label
77 * @param iconPath
78 * @param showCommand
79 */
80 public static void refreshParameterizedCommand(IMenuManager menuManager,
81 IServiceLocator locator, String cmdId, String label,
82 ImageDescriptor icon, boolean showCommand,
83 Map<String, String> params) {
84 refreshParameterizedCommand(menuManager, locator, cmdId, cmdId, label,
85 icon, showCommand, params);
86 }
87
88 /**
89 * Commodities the refresh the contribution of a command with a map of
90 * parameters in a context menu
91 *
92 * @param menuManager
93 * @param locator
94 * @param contributionId
95 * @param commandId
96 * @param label
97 * @param icon
98 * @param showCommand
99 * @param params
100 */
101 public static void refreshParameterizedCommand(IMenuManager menuManager,
102 IServiceLocator locator, String contributionId, String commandId,
103 String label, ImageDescriptor icon, boolean showCommand,
104 Map<String, String> params) {
105 IContributionItem ici = menuManager.find(contributionId);
106 if (ici != null)
107 menuManager.remove(ici);
108 if (showCommand) {
109 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
110 locator, null, commandId, SWT.PUSH);
111
112 // Set Params
113 contributionItemParameter.label = label;
114 contributionItemParameter.icon = icon;
115
116 if (params != null)
117 contributionItemParameter.parameters = params;
118
119 CommandContributionItem cci = new CommandContributionItem(
120 contributionItemParameter);
121 cci.setId(contributionId);
122 menuManager.add(cci);
123 }
124 }
125
126 /** Helper to call a command without parameter easily */
127 public static void callCommand(String commandID) {
128 callCommand(commandID, null);
129 }
130
131 /** Helper to call a command with a single parameter easily */
132 public static void callCommand(String commandID, String parameterID,
133 String parameterValue) {
134 Map<String, String> params = new HashMap<String, String>();
135 params.put(parameterID, parameterValue);
136 callCommand(commandID, params);
137 }
138
139 /**
140 * Helper to call a command with a map of parameters easily
141 *
142 * @param paramMap
143 * a map that links various command IDs with corresponding String
144 * values.
145 */
146 public static void callCommand(String commandID,
147 Map<String, String> paramMap) {
148 try {
149 IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
150 IHandlerService handlerService = (IHandlerService) iw
151 .getService(IHandlerService.class);
152 ICommandService cmdService = (ICommandService) iw
153 .getActiveWorkbenchWindow().getService(
154 ICommandService.class);
155 Command cmd = cmdService.getCommand(commandID);
156
157 ArrayList<Parameterization> parameters = null;
158 ParameterizedCommand pc;
159
160 if (paramMap != null) {
161 // Set parameters of the command to launch :
162 parameters = new ArrayList<Parameterization>();
163 Parameterization parameterization;
164
165 for (String id : paramMap.keySet()) {
166 parameterization = new Parameterization(
167 cmd.getParameter(id), paramMap.get(id));
168 parameters.add(parameterization);
169 }
170 pc = new ParameterizedCommand(cmd,
171 parameters.toArray(new Parameterization[parameters
172 .size()]));
173 } else
174 pc = new ParameterizedCommand(cmd, null);
175
176 // execute the command
177 handlerService.executeCommand(pc, null);
178 } catch (Exception e) {
179 throw new ArgeoException("Unexpected error while"
180 + " calling the command " + commandID, e);
181 }
182 }
183
184 // legacy methods. Should be removed soon
185
186 /**
187 * Shortcut to call a command with a single parameter.
188 *
189 * WARNING: none of the parameter can be null
190 *
191 * @deprecated rather use <code>callCommand(commandID,parameterID,
192 parameterValue)</code>
193 */
194 public static void CallCommandWithOneParameter(String commandId,
195 String paramId, String paramValue) {
196 try {
197 IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
198 IHandlerService handlerService = (IHandlerService) iw
199 .getService(IHandlerService.class);
200
201 // Gets a command that must have been previously registered
202 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
203 ICommandService cmdService = (ICommandService) window
204 .getService(ICommandService.class);
205 Command cmd = cmdService.getCommand(commandId);
206
207 // Manages the single parameter
208 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
209 IParameter iparam = cmd.getParameter(paramId);
210 Parameterization params = new Parameterization(iparam, paramValue);
211 parameters.add(params);
212
213 // Create and execute the command
214 ParameterizedCommand pc = new ParameterizedCommand(cmd,
215 parameters.toArray(new Parameterization[parameters.size()]));
216 handlerService = (IHandlerService) window
217 .getService(IHandlerService.class);
218 handlerService.executeCommand(pc, null);
219 } catch (Exception e) {
220 throw new ArgeoException(
221 "Error calling command of id:" + commandId, e);
222 }
223 }
224
225 /**
226 * Commodities the refresh of a single command with a map of parameters in a
227 * Menu.aboutToShow method to simplify further development Rather use
228 * {@link refreshParameterizedCommand()}
229 */
230 @Deprecated
231 public static void refreshParametrizedCommand(IMenuManager menuManager,
232 IServiceLocator locator, String cmdId, String label,
233 ImageDescriptor icon, boolean showCommand,
234 Map<String, String> params) {
235 refreshParameterizedCommand(menuManager, locator, cmdId, label, icon,
236 showCommand, params);
237 }
238 }