]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/utils/CommandHelpers.java
Restructure SLC
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / utils / CommandHelpers.java
1 package org.argeo.slc.client.ui.dist.utils;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.argeo.ArgeoException;
8 import org.argeo.slc.client.ui.dist.DistPlugin;
9 import org.eclipse.core.commands.Command;
10 import org.eclipse.core.commands.Parameterization;
11 import org.eclipse.core.commands.ParameterizedCommand;
12 import org.eclipse.jface.action.IContributionItem;
13 import org.eclipse.jface.action.IMenuManager;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.ui.IWorkbench;
16 import org.eclipse.ui.commands.ICommandService;
17 import org.eclipse.ui.handlers.IHandlerService;
18 import org.eclipse.ui.menus.CommandContributionItem;
19 import org.eclipse.ui.menus.CommandContributionItemParameter;
20 import org.eclipse.ui.services.IServiceLocator;
21
22 /**
23 * Centralizes useful methods to manage command updates
24 */
25 public class CommandHelpers {
26
27 /**
28 * Refresh the given command.
29 */
30 public static void refreshCommand(IMenuManager menuManager,
31 IServiceLocator locator, String cmdId, String label, String iconPath,
32 boolean showCommand) {
33 IContributionItem ici = menuManager.find(cmdId);
34 if (ici != null)
35 menuManager.remove(ici);
36
37 if (showCommand) {
38 // Set Params
39 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
40 locator, null, cmdId, SWT.PUSH);
41 contributionItemParameter.label = label;
42 contributionItemParameter.icon = DistPlugin.getImageDescriptor(iconPath);
43 CommandContributionItem cci = new CommandContributionItem(
44 contributionItemParameter);
45 cci.setId(cmdId);
46 menuManager.add(cci);
47 }
48 }
49
50 /**
51 * Refresh the given command and optionally corresponding parameters.
52 *
53 * @param menuManager
54 * @param locator
55 * @param cmdId
56 * @param label
57 * @param showCommand
58 * Command must be explicitly removed from the context menu at
59 * each refresh setting this to false.
60 * @param params
61 * maps a paramId with a String value
62 */
63 public static void refreshParameterizedCommand(IMenuManager menuManager,
64 IServiceLocator locator, String cmdId, String label, String iconPath,
65 boolean showCommand, Map<String, String> params) {
66 IContributionItem ici = menuManager.find(cmdId);
67 if (ici != null)
68 menuManager.remove(ici);
69
70 if (showCommand) {
71 // Set Params
72 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
73 locator, null, cmdId, SWT.PUSH);
74 contributionItemParameter.label = label;
75 contributionItemParameter.icon = DistPlugin.getImageDescriptor(iconPath);
76
77 if (params != null)
78 contributionItemParameter.parameters = params;
79
80 CommandContributionItem cci = new CommandContributionItem(
81 contributionItemParameter);
82 cci.setId(cmdId);
83 menuManager.add(cci);
84 }
85 }
86
87 /** Helper to call a command without parameter easily */
88 public static void callCommand(String commandID) {
89 callCommand(commandID, null);
90 }
91
92 /** Helper to call a command with a single parameter easily */
93 public static void callCommand(String commandID, String parameterID,
94 String parameterValue) {
95 Map<String, String> params = new HashMap<String, String>();
96 params.put(parameterID, parameterValue);
97 callCommand(commandID, params);
98 }
99
100 /**
101 * Helper to call a command with a map of parameters easily
102 *
103 * @param paramMap
104 * a map that links various commands ids with corresponding
105 * String values.
106 */
107 public static void callCommand(String commandID,
108 Map<String, String> paramMap) {
109 try {
110 IWorkbench iw = DistPlugin.getDefault().getWorkbench();
111 IHandlerService handlerService = (IHandlerService) iw
112 .getService(IHandlerService.class);
113 ICommandService cmdService = (ICommandService) iw
114 .getActiveWorkbenchWindow().getService(
115 ICommandService.class);
116 Command cmd = cmdService.getCommand(commandID);
117
118 ArrayList<Parameterization> parameters = null;
119 ParameterizedCommand pc;
120
121 if (paramMap != null) {
122 // Set parameters of the command to launch :
123 parameters = new ArrayList<Parameterization>();
124 Parameterization parameterization;
125 for (String id : paramMap.keySet()) {
126 parameterization = new Parameterization(
127 cmd.getParameter(id), paramMap.get(id));
128 parameters.add(parameterization);
129 }
130 pc = new ParameterizedCommand(cmd,
131 parameters.toArray(new Parameterization[parameters.size()]));
132 } else
133 pc = new ParameterizedCommand(cmd, null);
134
135 // build the parameterized command
136 // execute the command
137 handlerService.executeCommand(pc, null);
138 } catch (Exception e) {
139 throw new ArgeoException(
140 "Unexepected exception while opening node editor", e);
141 }
142 }
143
144 }