View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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.cms.ui.workbench.util;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
23  import org.argeo.eclipse.ui.EclipseUiException;
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.jface.resource.ImageDescriptor;
30  import org.eclipse.swt.SWT;
31  import org.eclipse.ui.IWorkbench;
32  import org.eclipse.ui.commands.ICommandService;
33  import org.eclipse.ui.handlers.IHandlerService;
34  import org.eclipse.ui.menus.CommandContributionItem;
35  import org.eclipse.ui.menus.CommandContributionItemParameter;
36  import org.eclipse.ui.services.IServiceLocator;
37  
38  /**
39   * Centralises useful and generic methods when dealing with commands in an
40   * Eclipse Workbench context
41   */
42  public class CommandUtils {
43  
44  	/**
45  	 * Commodities the refresh of a single command with no parameter in a
46  	 * Menu.aboutToShow method to simplify further development
47  	 * 
48  	 * Note: that this method should be called with a false show command flag to
49  	 * remove a contribution that have been previously contributed
50  	 */
51  	public static void refreshCommand(IMenuManager menuManager, IServiceLocator locator, String cmdId, String label,
52  			ImageDescriptor icon, boolean showCommand) {
53  		refreshParameterizedCommand(menuManager, locator, cmdId, label, icon, showCommand, null);
54  	}
55  
56  	/**
57  	 * Commodities the refresh the contribution of a command with a map of
58  	 * parameters in a context menu
59  	 * 
60  	 * The command ID is used has contribution item ID
61  	 */
62  	public static void refreshParameterizedCommand(IMenuManager menuManager, IServiceLocator locator, String cmdId,
63  			String label, ImageDescriptor icon, boolean showCommand, Map<String, String> params) {
64  		refreshParameterizedCommand(menuManager, locator, cmdId, cmdId, label, icon, showCommand, params);
65  	}
66  
67  	/**
68  	 * Commodities the refresh the contribution of a command with a map of
69  	 * parameters in a context menu
70  	 * 
71  	 * @param menuManager
72  	 * @param locator
73  	 * @param contributionId
74  	 * @param commandId
75  	 * @param label
76  	 * @param icon
77  	 * @param showCommand
78  	 * @param params
79  	 */
80  	public static void refreshParameterizedCommand(IMenuManager menuManager, IServiceLocator locator,
81  			String contributionId, String commandId, String label, ImageDescriptor icon, boolean showCommand,
82  			Map<String, String> params) {
83  		IContributionItem ici = menuManager.find(contributionId);
84  		if (ici != null)
85  			menuManager.remove(ici);
86  		if (showCommand) {
87  			CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(locator,
88  					null, commandId, SWT.PUSH);
89  
90  			// Set Params
91  			contributionItemParameter.label = label;
92  			contributionItemParameter.icon = icon;
93  
94  			if (params != null)
95  				contributionItemParameter.parameters = params;
96  
97  			CommandContributionItem cci = new CommandContributionItem(contributionItemParameter);
98  			cci.setId(contributionId);
99  			menuManager.add(cci);
100 		}
101 	}
102 
103 	/** Helper to call a command without parameter easily */
104 	public static void callCommand(String commandID) {
105 		callCommand(commandID, null);
106 	}
107 
108 	/** Helper to call a command with a single parameter easily */
109 	public static void callCommand(String commandID, String parameterID, String parameterValue) {
110 		Map<String, String> params = new HashMap<String, String>();
111 		params.put(parameterID, parameterValue);
112 		callCommand(commandID, params);
113 	}
114 
115 	/**
116 	 * Helper to call a command with a map of parameters easily
117 	 * 
118 	 * @param paramMap
119 	 *            a map that links various command IDs with corresponding String
120 	 *            values.
121 	 */
122 	public static void callCommand(String commandID, Map<String, String> paramMap) {
123 		try {
124 			IWorkbench iw = WorkbenchUiPlugin.getDefault().getWorkbench();
125 			IHandlerService handlerService = (IHandlerService) iw.getService(IHandlerService.class);
126 			ICommandService cmdService = (ICommandService) iw.getActiveWorkbenchWindow()
127 					.getService(ICommandService.class);
128 			Command cmd = cmdService.getCommand(commandID);
129 
130 			ArrayList<Parameterization> parameters = null;
131 			ParameterizedCommand pc;
132 
133 			if (paramMap != null) {
134 				// Set parameters of the command to launch :
135 				parameters = new ArrayList<Parameterization>();
136 				Parameterization parameterization;
137 
138 				for (String id : paramMap.keySet()) {
139 					parameterization = new Parameterization(cmd.getParameter(id), paramMap.get(id));
140 					parameters.add(parameterization);
141 				}
142 				pc = new ParameterizedCommand(cmd, parameters.toArray(new Parameterization[parameters.size()]));
143 			} else
144 				pc = new ParameterizedCommand(cmd, null);
145 
146 			// execute the command
147 			handlerService.executeCommand(pc, null);
148 		} catch (Exception e) {
149 			throw new EclipseUiException("Unexpected error while" + " calling the command " + commandID, e);
150 		}
151 	}
152 }