Add some utils method to simplify command call
authorBruno Sinou <bsinou@argeo.org>
Wed, 22 Aug 2012 14:46:29 +0000 (14:46 +0000)
committerBruno Sinou <bsinou@argeo.org>
Wed, 22 Aug 2012 14:46:29 +0000 (14:46 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@5532 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java

index eb0de934f74837f8ec4c7f6ff9e3f8caa58492cf..7bb5c27916c504669562f4f723f0d95ae2fb9236 100644 (file)
@@ -16,6 +16,8 @@
 package org.argeo.eclipse.ui.utils;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.ArgeoUiPlugin;
@@ -23,10 +25,17 @@ import org.eclipse.core.commands.Command;
 import org.eclipse.core.commands.IParameter;
 import org.eclipse.core.commands.Parameterization;
 import org.eclipse.core.commands.ParameterizedCommand;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.swt.SWT;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.commands.ICommandService;
 import org.eclipse.ui.handlers.IHandlerService;
+import org.eclipse.ui.menus.CommandContributionItem;
+import org.eclipse.ui.menus.CommandContributionItemParameter;
+import org.eclipse.ui.services.IServiceLocator;
 
 /**
  * Centralizes useful and generic methods concerning eclipse commands.
@@ -75,4 +84,112 @@ public class CommandUtils {
                                        + commandId, e);
                }
        }
-}
+
+       /**
+        * Commodities the refresh of a single command with no parameter in a
+        * Menu.aboutToShow method to simplify further development
+        * 
+        * @param menuManager
+        * @param locator
+        * @param cmdId
+        * @param label
+        * @param iconPath
+        * @param showCommand
+        */
+       public static void refreshCommand(IMenuManager menuManager,
+                       IServiceLocator locator, String cmdId, String label,
+                       ImageDescriptor icon, boolean showCommand) {
+               refreshParametrizedCommand(menuManager, locator, cmdId, label, icon,
+                               showCommand, null);
+       }
+
+       /**
+        * Commodities the refresh of a single command with a map of parameters in a
+        * Menu.aboutToShow method to simplify further development
+        * 
+        * @param menuManager
+        * @param locator
+        * @param cmdId
+        * @param label
+        * @param iconPath
+        * @param showCommand
+        */
+       public static void refreshParametrizedCommand(IMenuManager menuManager,
+                       IServiceLocator locator, String cmdId, String label,
+                       ImageDescriptor icon, boolean showCommand,
+                       Map<String, String> params) {
+               IContributionItem ici = menuManager.find(cmdId);
+               if (ici != null)
+                       menuManager.remove(ici);
+               CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
+                               locator, null, cmdId, SWT.PUSH);
+
+               if (showCommand) {
+                       // Set Params
+                       contributionItemParameter.label = label;
+                       contributionItemParameter.icon = icon;
+
+                       if (params != null)
+                               contributionItemParameter.parameters = params;
+
+                       CommandContributionItem cci = new CommandContributionItem(
+                                       contributionItemParameter);
+                       cci.setId(cmdId);
+                       menuManager.add(cci);
+               }
+       }
+
+       /** Helper to call a command without parameter easily */
+       public static void callCommand(String commandID) {
+               callCommand(commandID, null);
+       }
+
+       /** Helper to call a command with a single parameter easily */
+       public static void callCommand(String commandID, String parameterID,
+                       String parameterValue) {
+               Map<String, String> params = new HashMap<String, String>();
+               params.put(parameterID, parameterValue);
+               callCommand(commandID, params);
+       }
+
+       /**
+        * Helper to call a command with a map of parameters easily
+        * 
+        * @param paramMap
+        *            a map that links various commands ids with corresponding
+        *            String values.
+        */
+       public static void callCommand(String commandID,
+                       Map<String, String> paramMap) {
+               try {
+                       IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
+                       IHandlerService handlerService = (IHandlerService) iw
+                                       .getService(IHandlerService.class);
+                       ICommandService cmdService = (ICommandService) iw
+                                       .getActiveWorkbenchWindow().getService(
+                                                       ICommandService.class);
+                       Command cmd = cmdService.getCommand(commandID);
+
+                       ArrayList<Parameterization> parameters = null;
+                       if (paramMap != null) {
+                               // Set parameters of the command to launch :
+                               parameters = new ArrayList<Parameterization>();
+                               Parameterization parameterization;
+
+                               for (String id : paramMap.keySet()) {
+                                       parameterization = new Parameterization(
+                                                       cmd.getParameter(id), paramMap.get(id));
+                                       parameters.add(parameterization);
+                               }
+                       }
+                       // build the parameterized command
+                       ParameterizedCommand pc = new ParameterizedCommand(cmd,
+                                       parameters.toArray(new Parameterization[parameters.size()]));
+                       // execute the command
+                       handlerService.executeCommand(pc, null);
+               } catch (Exception e) {
+                       throw new ArgeoException(
+                                       "Unexepected exception while opening node editor", e);
+               }
+       }
+}
\ No newline at end of file