]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java
Deprecate TreeObject (not used anywhere anymore)
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / utils / CommandUtils.java
1 package org.argeo.eclipse.ui.utils;
2
3 import java.util.ArrayList;
4
5 import org.argeo.ArgeoException;
6 import org.argeo.eclipse.ui.ArgeoUiPlugin;
7 import org.eclipse.core.commands.Command;
8 import org.eclipse.core.commands.IParameter;
9 import org.eclipse.core.commands.Parameterization;
10 import org.eclipse.core.commands.ParameterizedCommand;
11 import org.eclipse.ui.IWorkbench;
12 import org.eclipse.ui.IWorkbenchWindow;
13 import org.eclipse.ui.commands.ICommandService;
14 import org.eclipse.ui.handlers.IHandlerService;
15
16 /**
17 * Centralizes useful and generic methods concerning eclipse commands.
18 *
19 */
20 public class CommandUtils {
21
22 /**
23 * Factorizes command call that is quite verbose and always the same
24 *
25 * NOTE that none of the parameter can be null
26 */
27 public static void CallCommandWithOneParameter(String commandId,
28 String paramId, String paramValue) {
29 try {
30 IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
31
32 IHandlerService handlerService = (IHandlerService) iw
33 .getService(IHandlerService.class);
34
35 // get the command from plugin.xml
36 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
37 ICommandService cmdService = (ICommandService) window
38 .getService(ICommandService.class);
39
40 Command cmd = cmdService.getCommand(commandId);
41
42 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
43
44 // get the parameter
45 IParameter iparam = cmd.getParameter(paramId);
46
47 Parameterization params = new Parameterization(iparam, paramValue);
48 parameters.add(params);
49
50 // build the parameterized command
51 ParameterizedCommand pc = new ParameterizedCommand(cmd,
52 parameters.toArray(new Parameterization[parameters.size()]));
53
54 // execute the command
55 handlerService = (IHandlerService) window
56 .getService(IHandlerService.class);
57 handlerService.executeCommand(pc, null);
58 } catch (Exception e) {
59 throw new ArgeoException("Error while calling command of id :"
60 + commandId, e);
61 }
62 }
63 }