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.slc.client.ui.dist.utils;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.argeo.slc.SlcException;
23  import org.argeo.slc.client.ui.dist.DistPlugin;
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.swt.SWT;
30  import org.eclipse.ui.IWorkbench;
31  import org.eclipse.ui.commands.ICommandService;
32  import org.eclipse.ui.handlers.IHandlerService;
33  import org.eclipse.ui.menus.CommandContributionItem;
34  import org.eclipse.ui.menus.CommandContributionItemParameter;
35  import org.eclipse.ui.services.IServiceLocator;
36  
37  /**
38   * Centralizes useful methods to manage command updates
39   */
40  public class CommandHelpers {
41  
42  	/**
43  	 * Refresh the given command. 
44  	 */
45  	public static void refreshCommand(IMenuManager menuManager,
46  			IServiceLocator locator, String cmdId, String label, String iconPath,
47  			boolean showCommand) {
48  		IContributionItem ici = menuManager.find(cmdId);
49  		if (ici != null)
50  			menuManager.remove(ici);
51  
52  		if (showCommand) {
53  			// Set Params
54  			CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
55  					locator, null, cmdId, SWT.PUSH);
56  			contributionItemParameter.label = label;
57  			contributionItemParameter.icon = DistPlugin.getImageDescriptor(iconPath);
58  			CommandContributionItem cci = new CommandContributionItem(
59  					contributionItemParameter);
60  			cci.setId(cmdId);
61  			menuManager.add(cci);
62  		}
63  	}
64  
65  	/**
66  	 * Refresh the given command and optionally corresponding parameters.
67  	 * 
68  	 * @param menuManager
69  	 * @param locator
70  	 * @param cmdId
71  	 * @param label
72  	 * @param showCommand
73  	 *            Command must be explicitly removed from the context menu at
74  	 *            each refresh setting this to false.
75  	 * @param params
76  	 *            maps a paramId with a String value
77  	 */
78  	public static void refreshParameterizedCommand(IMenuManager menuManager,
79  			IServiceLocator locator, String cmdId, String label, String iconPath,
80  			boolean showCommand, Map<String, String> params) {
81  		IContributionItem ici = menuManager.find(cmdId);
82  		if (ici != null)
83  			menuManager.remove(ici);
84  		
85  		if (showCommand) {
86  			// Set Params
87  			CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
88  					locator, null, cmdId, SWT.PUSH);
89  			contributionItemParameter.label = label;
90  			contributionItemParameter.icon = DistPlugin.getImageDescriptor(iconPath);
91  
92  			if (params != null)
93  				contributionItemParameter.parameters = params;
94  
95  			CommandContributionItem cci = new CommandContributionItem(
96  					contributionItemParameter);
97  			cci.setId(cmdId);
98  			menuManager.add(cci);
99  		}
100 	}
101 
102 	/** Helper to call a command without parameter easily */
103 	public static void callCommand(String commandID) {
104 		callCommand(commandID, null);
105 	}
106 
107 	/** Helper to call a command with a single parameter easily */
108 	public static void callCommand(String commandID, String parameterID,
109 			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 commands ids with corresponding
120 	 *            String values.
121 	 */
122 	public static void callCommand(String commandID,
123 			Map<String, String> paramMap) {
124 		try {
125 			IWorkbench iw = DistPlugin.getDefault().getWorkbench();
126 			IHandlerService handlerService = (IHandlerService) iw
127 					.getService(IHandlerService.class);
128 			ICommandService cmdService = (ICommandService) iw
129 					.getActiveWorkbenchWindow().getService(
130 							ICommandService.class);
131 			Command cmd = cmdService.getCommand(commandID);
132 
133 			ArrayList<Parameterization> parameters = null;
134 			ParameterizedCommand pc; 
135 
136 			if (paramMap != null) {
137 				// Set parameters of the command to launch :
138 				parameters = new ArrayList<Parameterization>();
139 				Parameterization parameterization;
140 				for (String id : paramMap.keySet()) {
141 					parameterization = new Parameterization(
142 							cmd.getParameter(id), paramMap.get(id));
143 					parameters.add(parameterization);
144 				}
145 				pc = new ParameterizedCommand(cmd,
146 						parameters.toArray(new Parameterization[parameters.size()]));
147 			} else 
148 				pc = new ParameterizedCommand(cmd, null);
149 			
150 			// build the parameterized command
151 			// execute the command
152 			handlerService.executeCommand(pc, null);
153 		} catch (Exception e) {
154 			throw new SlcException(
155 					"Unexepected exception while opening node editor", e);
156 		}
157 	}
158 
159 }