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