1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
40  
41  
42  public class CommandUtils {
43  
44  	
45  
46  
47  
48  
49  
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  
58  
59  
60  
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  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
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  			
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 	
104 	public static void callCommand(String commandID) {
105 		callCommand(commandID, null);
106 	}
107 
108 	
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 
117 
118 
119 
120 
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 				
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 			
147 			handlerService.executeCommand(pc, null);
148 		} catch (Exception e) {
149 			throw new EclipseUiException("Unexpected error while" + " calling the command " + commandID, e);
150 		}
151 	}
152 }