2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.utils
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
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
;
41 * Centralizes useful and generic methods concerning eclipse commands.
44 public class CommandUtils
{
47 * Factorizes command call that is quite verbose and always the same
49 * NOTE that none of the parameter can be null
51 public static void CallCommandWithOneParameter(String commandId
,
52 String paramId
, String paramValue
) {
54 IWorkbench iw
= ArgeoUiPlugin
.getDefault().getWorkbench();
56 IHandlerService handlerService
= (IHandlerService
) iw
57 .getService(IHandlerService
.class);
59 // get the command from plugin.xml
60 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
61 ICommandService cmdService
= (ICommandService
) window
62 .getService(ICommandService
.class);
64 Command cmd
= cmdService
.getCommand(commandId
);
66 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
69 IParameter iparam
= cmd
.getParameter(paramId
);
71 Parameterization params
= new Parameterization(iparam
, paramValue
);
72 parameters
.add(params
);
74 // build the parameterized command
75 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
76 parameters
.toArray(new Parameterization
[parameters
.size()]));
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 :"
89 * Commodities the refresh of a single command with no parameter in a
90 * Menu.aboutToShow method to simplify further development
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
,
107 * Commodities the refresh of a single command with a map of parameters in a
108 * Menu.aboutToShow method to simplify further development
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
);
123 menuManager
.remove(ici
);
124 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
125 locator
, null, cmdId
, SWT
.PUSH
);
129 contributionItemParameter
.label
= label
;
130 contributionItemParameter
.icon
= icon
;
133 contributionItemParameter
.parameters
= params
;
135 CommandContributionItem cci
= new CommandContributionItem(
136 contributionItemParameter
);
138 menuManager
.add(cci
);
142 /** Helper to call a command without parameter easily */
143 public static void callCommand(String commandID
) {
144 callCommand(commandID
, null);
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
);
156 * Helper to call a command with a map of parameters easily
159 * a map that links various commands ids with corresponding
162 public static void callCommand(String commandID
,
163 Map
<String
, String
> paramMap
) {
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
);
173 ArrayList
<Parameterization
> parameters
= null;
174 if (paramMap
!= null) {
175 // Set parameters of the command to launch :
176 parameters
= new ArrayList
<Parameterization
>();
177 Parameterization parameterization
;
179 for (String id
: paramMap
.keySet()) {
180 parameterization
= new Parameterization(
181 cmd
.getParameter(id
), paramMap
.get(id
));
182 parameters
.add(parameterization
);
185 // build the parameterized command
186 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
187 parameters
.toArray(new Parameterization
[parameters
.size()]));
188 // execute the command
189 handlerService
.executeCommand(pc
, null);
190 } catch (Exception e
) {
191 throw new ArgeoException(
192 "Unexepected exception while opening node editor", e
);