]> 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
Remove TreeObject
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / utils / CommandUtils.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.eclipse.ui.utils;
17
18 import java.util.ArrayList;
19
20 import org.argeo.ArgeoException;
21 import org.argeo.eclipse.ui.ArgeoUiPlugin;
22 import org.eclipse.core.commands.Command;
23 import org.eclipse.core.commands.IParameter;
24 import org.eclipse.core.commands.Parameterization;
25 import org.eclipse.core.commands.ParameterizedCommand;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.commands.ICommandService;
29 import org.eclipse.ui.handlers.IHandlerService;
30
31 /**
32 * Centralizes useful and generic methods concerning eclipse commands.
33 *
34 */
35 public class CommandUtils {
36
37 /**
38 * Factorizes command call that is quite verbose and always the same
39 *
40 * NOTE that none of the parameter can be null
41 */
42 public static void CallCommandWithOneParameter(String commandId,
43 String paramId, String paramValue) {
44 try {
45 IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
46
47 IHandlerService handlerService = (IHandlerService) iw
48 .getService(IHandlerService.class);
49
50 // get the command from plugin.xml
51 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
52 ICommandService cmdService = (ICommandService) window
53 .getService(ICommandService.class);
54
55 Command cmd = cmdService.getCommand(commandId);
56
57 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
58
59 // get the parameter
60 IParameter iparam = cmd.getParameter(paramId);
61
62 Parameterization params = new Parameterization(iparam, paramValue);
63 parameters.add(params);
64
65 // build the parameterized command
66 ParameterizedCommand pc = new ParameterizedCommand(cmd,
67 parameters.toArray(new Parameterization[parameters.size()]));
68
69 // execute the command
70 handlerService = (IHandlerService) window
71 .getService(IHandlerService.class);
72 handlerService.executeCommand(pc, null);
73 } catch (Exception e) {
74 throw new ArgeoException("Error while calling command of id :"
75 + commandId, e);
76 }
77 }
78 }