]> 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
fix bug 145 ( https://www.argeo.org/bugzilla/show_bug.cgi?id=145 ).
[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
109 *
110 * @param menuManager
111 * @param locator
112 * @param cmdId
113 * @param label
114 * @param iconPath
115 * @param showCommand
116 */
117 public static void refreshParametrizedCommand(IMenuManager menuManager,
118 IServiceLocator locator, String cmdId, String label,
119 ImageDescriptor icon, boolean showCommand,
120 Map<String, String> params) {
121 IContributionItem ici = menuManager.find(cmdId);
122 if (ici != null)
123 menuManager.remove(ici);
124 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
125 locator, null, cmdId, SWT.PUSH);
126
127 if (showCommand) {
128 // Set Params
129 contributionItemParameter.label = label;
130 contributionItemParameter.icon = icon;
131
132 if (params != null)
133 contributionItemParameter.parameters = params;
134
135 CommandContributionItem cci = new CommandContributionItem(
136 contributionItemParameter);
137 cci.setId(cmdId);
138 menuManager.add(cci);
139 }
140 }
141
142 /** Helper to call a command without parameter easily */
143 public static void callCommand(String commandID) {
144 callCommand(commandID, null);
145 }
146
147 /** Helper to call a command with a single parameter easily */
148 public static void callCommand(String commandID, String parameterID,
149 String parameterValue) {
150 Map<String, String> params = new HashMap<String, String>();
151 params.put(parameterID, parameterValue);
152 callCommand(commandID, params);
153 }
154
155 /**
156 * Helper to call a command with a map of parameters easily
157 *
158 * @param paramMap
159 * a map that links various commands ids with corresponding
160 * String values.
161 */
162 public static void callCommand(String commandID,
163 Map<String, String> paramMap) {
164 try {
165 IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
166 IHandlerService handlerService = (IHandlerService) iw
167 .getService(IHandlerService.class);
168 ICommandService cmdService = (ICommandService) iw
169 .getActiveWorkbenchWindow().getService(
170 ICommandService.class);
171 Command cmd = cmdService.getCommand(commandID);
172
173 ArrayList<Parameterization> parameters = null;
174 ParameterizedCommand pc;
175
176 if (paramMap != null) {
177 // Set parameters of the command to launch :
178 parameters = new ArrayList<Parameterization>();
179 Parameterization parameterization;
180
181 for (String id : paramMap.keySet()) {
182 parameterization = new Parameterization(
183 cmd.getParameter(id), paramMap.get(id));
184 parameters.add(parameterization);
185 }
186 pc = new ParameterizedCommand(cmd,
187 parameters.toArray(new Parameterization[parameters
188 .size()]));
189 } else
190 pc = new ParameterizedCommand(cmd, null);
191
192 // execute the command
193 handlerService.executeCommand(pc, null);
194 } catch (Exception e) {
195 throw new ArgeoException("Unexpected error while"
196 + " calling the command " + commandID, e);
197 }
198 }
199 }