2 * Copyright (C) 2007-2012 Mathieu Baudier
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
;
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
;
32 * Centralizes useful and generic methods concerning eclipse commands.
35 public class CommandUtils
{
38 * Factorizes command call that is quite verbose and always the same
40 * NOTE that none of the parameter can be null
42 public static void CallCommandWithOneParameter(String commandId
,
43 String paramId
, String paramValue
) {
45 IWorkbench iw
= ArgeoUiPlugin
.getDefault().getWorkbench();
47 IHandlerService handlerService
= (IHandlerService
) iw
48 .getService(IHandlerService
.class);
50 // get the command from plugin.xml
51 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
52 ICommandService cmdService
= (ICommandService
) window
53 .getService(ICommandService
.class);
55 Command cmd
= cmdService
.getCommand(commandId
);
57 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
60 IParameter iparam
= cmd
.getParameter(paramId
);
62 Parameterization params
= new Parameterization(iparam
, paramValue
);
63 parameters
.add(params
);
65 // build the parameterized command
66 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
67 parameters
.toArray(new Parameterization
[parameters
.size()]));
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 :"