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
.slc
.client
.ui
.dist
.utils
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
22 import org
.argeo
.ArgeoException
;
23 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
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
.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
;
38 * Centralizes useful methods to manage command updates
40 public class CommandHelpers
{
43 * Refresh the given command.
45 public static void refreshCommand(IMenuManager menuManager
,
46 IServiceLocator locator
, String cmdId
, String label
, String iconPath
,
47 boolean showCommand
) {
48 IContributionItem ici
= menuManager
.find(cmdId
);
50 menuManager
.remove(ici
);
54 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
55 locator
, null, cmdId
, SWT
.PUSH
);
56 contributionItemParameter
.label
= label
;
57 contributionItemParameter
.icon
= DistPlugin
.getImageDescriptor(iconPath
);
58 CommandContributionItem cci
= new CommandContributionItem(
59 contributionItemParameter
);
66 * Refresh the given command and optionally corresponding parameters.
73 * Command must be explicitly removed from the context menu at
74 * each refresh setting this to false.
76 * maps a paramId with a String value
78 public static void refreshParameterizedCommand(IMenuManager menuManager
,
79 IServiceLocator locator
, String cmdId
, String label
, String iconPath
,
80 boolean showCommand
, Map
<String
, String
> params
) {
81 IContributionItem ici
= menuManager
.find(cmdId
);
83 menuManager
.remove(ici
);
87 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
88 locator
, null, cmdId
, SWT
.PUSH
);
89 contributionItemParameter
.label
= label
;
90 contributionItemParameter
.icon
= DistPlugin
.getImageDescriptor(iconPath
);
93 contributionItemParameter
.parameters
= params
;
95 CommandContributionItem cci
= new CommandContributionItem(
96 contributionItemParameter
);
102 /** Helper to call a command without parameter easily */
103 public static void callCommand(String commandID
) {
104 callCommand(commandID
, null);
107 /** Helper to call a command with a single parameter easily */
108 public static void callCommand(String commandID
, String parameterID
,
109 String parameterValue
) {
110 Map
<String
, String
> params
= new HashMap
<String
, String
>();
111 params
.put(parameterID
, parameterValue
);
112 callCommand(commandID
, params
);
116 * Helper to call a command with a map of parameters easily
119 * a map that links various commands ids with corresponding
122 public static void callCommand(String commandID
,
123 Map
<String
, String
> paramMap
) {
125 IWorkbench iw
= DistPlugin
.getDefault().getWorkbench();
126 IHandlerService handlerService
= (IHandlerService
) iw
127 .getService(IHandlerService
.class);
128 ICommandService cmdService
= (ICommandService
) iw
129 .getActiveWorkbenchWindow().getService(
130 ICommandService
.class);
131 Command cmd
= cmdService
.getCommand(commandID
);
133 ArrayList
<Parameterization
> parameters
= null;
134 ParameterizedCommand pc
;
136 if (paramMap
!= null) {
137 // Set parameters of the command to launch :
138 parameters
= new ArrayList
<Parameterization
>();
139 Parameterization parameterization
;
140 for (String id
: paramMap
.keySet()) {
141 parameterization
= new Parameterization(
142 cmd
.getParameter(id
), paramMap
.get(id
));
143 parameters
.add(parameterization
);
145 pc
= new ParameterizedCommand(cmd
,
146 parameters
.toArray(new Parameterization
[parameters
.size()]));
148 pc
= new ParameterizedCommand(cmd
, null);
150 // build the parameterized command
151 // execute the command
152 handlerService
.executeCommand(pc
, null);
153 } catch (Exception e
) {
154 throw new ArgeoException(
155 "Unexepected exception while opening node editor", e
);