]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CommandUtils.java
3670efbe586a277bf7cbbdf5b6ae28bf190843b4
[lgpl/argeo-commons.git] / 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.workbench;
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.eclipse.core.commands.Command;
24 import org.eclipse.core.commands.Parameterization;
25 import org.eclipse.core.commands.ParameterizedCommand;
26 import org.eclipse.jface.action.IContributionItem;
27 import org.eclipse.jface.action.IMenuManager;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.commands.ICommandService;
32 import org.eclipse.ui.handlers.IHandlerService;
33 import org.eclipse.ui.menus.CommandContributionItem;
34 import org.eclipse.ui.menus.CommandContributionItemParameter;
35 import org.eclipse.ui.services.IServiceLocator;
36
37 /**
38 * Centralises useful and generic methods when dealing with commands in an
39 * Eclipse Workbench context
40 */
41 public class CommandUtils {
42
43 /**
44 * Commodities the refresh of a single command with no parameter in a
45 * Menu.aboutToShow method to simplify further development
46 *
47 * Note: that this method should be called with a false show command flag to
48 * remove a contribution that have been previously contributed
49 *
50 * @param menuManager
51 * @param locator
52 * @param cmdId
53 * @param label
54 * @param icon
55 * @param showCommand
56 */
57 public static void refreshCommand(IMenuManager menuManager,
58 IServiceLocator locator, String cmdId, String label,
59 ImageDescriptor icon, boolean showCommand) {
60 refreshParameterizedCommand(menuManager, locator, cmdId, label, icon,
61 showCommand, null);
62 }
63
64 /**
65 * Commodities the refresh the contribution of a command with a map of
66 * parameters in a context menu
67 *
68 * The command ID is used has contribution item ID
69 *
70 * @param menuManager
71 * @param locator
72 * @param cmdId
73 * @param label
74 * @param iconPath
75 * @param showCommand
76 */
77 public static void refreshParameterizedCommand(IMenuManager menuManager,
78 IServiceLocator locator, String cmdId, String label,
79 ImageDescriptor icon, boolean showCommand,
80 Map<String, String> params) {
81 refreshParameterizedCommand(menuManager, locator, cmdId, cmdId, label,
82 icon, showCommand, params);
83 }
84
85 /**
86 * Commodities the refresh the contribution of a command with a map of
87 * parameters in a context menu
88 *
89 * @param menuManager
90 * @param locator
91 * @param contributionId
92 * @param commandId
93 * @param label
94 * @param icon
95 * @param showCommand
96 * @param params
97 */
98 public static void refreshParameterizedCommand(IMenuManager menuManager,
99 IServiceLocator locator, String contributionId, String commandId,
100 String label, ImageDescriptor icon, boolean showCommand,
101 Map<String, String> params) {
102 IContributionItem ici = menuManager.find(contributionId);
103 if (ici != null)
104 menuManager.remove(ici);
105 if (showCommand) {
106 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
107 locator, null, commandId, SWT.PUSH);
108
109 // Set Params
110 contributionItemParameter.label = label;
111 contributionItemParameter.icon = icon;
112
113 if (params != null)
114 contributionItemParameter.parameters = params;
115
116 CommandContributionItem cci = new CommandContributionItem(
117 contributionItemParameter);
118 cci.setId(contributionId);
119 menuManager.add(cci);
120 }
121 }
122
123 /** Helper to call a command without parameter easily */
124 public static void callCommand(String commandID) {
125 callCommand(commandID, null);
126 }
127
128 /** Helper to call a command with a single parameter easily */
129 public static void callCommand(String commandID, String parameterID,
130 String parameterValue) {
131 Map<String, String> params = new HashMap<String, String>();
132 params.put(parameterID, parameterValue);
133 callCommand(commandID, params);
134 }
135
136 /**
137 * Helper to call a command with a map of parameters easily
138 *
139 * @param paramMap
140 * a map that links various command IDs with corresponding String
141 * values.
142 */
143 public static void callCommand(String commandID,
144 Map<String, String> paramMap) {
145 try {
146 IWorkbench iw = WorkbenchUiPlugin.getDefault().getWorkbench();
147 IHandlerService handlerService = (IHandlerService) iw
148 .getService(IHandlerService.class);
149 ICommandService cmdService = (ICommandService) iw
150 .getActiveWorkbenchWindow().getService(
151 ICommandService.class);
152 Command cmd = cmdService.getCommand(commandID);
153
154 ArrayList<Parameterization> parameters = null;
155 ParameterizedCommand pc;
156
157 if (paramMap != null) {
158 // Set parameters of the command to launch :
159 parameters = new ArrayList<Parameterization>();
160 Parameterization parameterization;
161
162 for (String id : paramMap.keySet()) {
163 parameterization = new Parameterization(
164 cmd.getParameter(id), paramMap.get(id));
165 parameters.add(parameterization);
166 }
167 pc = new ParameterizedCommand(cmd,
168 parameters.toArray(new Parameterization[parameters
169 .size()]));
170 } else
171 pc = new ParameterizedCommand(cmd, null);
172
173 // execute the command
174 handlerService.executeCommand(pc, null);
175 } catch (Exception e) {
176 throw new ArgeoException("Unexpected error while"
177 + " calling the command " + commandID, e);
178 }
179 }
180 }