]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java
Update and clean comments
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / utils / CommandUtils.java
index 8b7658626cfd0e60bca77ac99becb92909d1a071..22a139f134aa565b119106ed292511d3693e29d5 100644 (file)
@@ -38,74 +38,37 @@ import org.eclipse.ui.menus.CommandContributionItemParameter;
 import org.eclipse.ui.services.IServiceLocator;
 
 /**
- * Centralizes useful and generic methods concerning eclipse commands.
- * 
+ * Centralises useful and generic methods when dealing with commands in an
+ * Eclipse Workbench context
  */
 public class CommandUtils {
 
-       /**
-        * Factorizes command call that is quite verbose and always the same
-        * 
-        * NOTE that none of the parameter can be null
-        */
-       public static void CallCommandWithOneParameter(String commandId,
-                       String paramId, String paramValue) {
-               try {
-                       IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
-
-                       IHandlerService handlerService = (IHandlerService) iw
-                                       .getService(IHandlerService.class);
-
-                       // get the command from plugin.xml
-                       IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
-                       ICommandService cmdService = (ICommandService) window
-                                       .getService(ICommandService.class);
-
-                       Command cmd = cmdService.getCommand(commandId);
-
-                       ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
-
-                       // get the parameter
-                       IParameter iparam = cmd.getParameter(paramId);
-
-                       Parameterization params = new Parameterization(iparam, paramValue);
-                       parameters.add(params);
-
-                       // build the parameterized command
-                       ParameterizedCommand pc = new ParameterizedCommand(cmd,
-                                       parameters.toArray(new Parameterization[parameters.size()]));
-
-                       // execute the command
-                       handlerService = (IHandlerService) window
-                                       .getService(IHandlerService.class);
-                       handlerService.executeCommand(pc, null);
-               } catch (Exception e) {
-                       throw new ArgeoException("Error while calling command of id :"
-                                       + commandId, e);
-               }
-       }
-
        /**
         * Commodities the refresh of a single command with no parameter in a
         * Menu.aboutToShow method to simplify further development
         * 
+        * Note: that this method should be called with a false show command flag to
+        * remove a contribution that have been previously contributed
+        * 
         * @param menuManager
         * @param locator
         * @param cmdId
         * @param label
-        * @param iconPath
+        * @param icon
         * @param showCommand
         */
        public static void refreshCommand(IMenuManager menuManager,
                        IServiceLocator locator, String cmdId, String label,
                        ImageDescriptor icon, boolean showCommand) {
-               refreshParametrizedCommand(menuManager, locator, cmdId, label, icon,
+               refreshParameterizedCommand(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
+        * Commodities the refresh the contribution of a command with a map of
+        * parameters in a context menu
+        * 
+        * The command ID is used has contribution item ID
         * 
         * @param menuManager
         * @param locator
@@ -114,17 +77,38 @@ public class CommandUtils {
         * @param iconPath
         * @param showCommand
         */
-       public static void refreshParametrizedCommand(IMenuManager menuManager,
+       public static void refreshParameterizedCommand(IMenuManager menuManager,
                        IServiceLocator locator, String cmdId, String label,
                        ImageDescriptor icon, boolean showCommand,
                        Map<String, String> params) {
-               IContributionItem ici = menuManager.find(cmdId);
+               refreshParameterizedCommand(menuManager, locator, cmdId, cmdId, label,
+                               icon, showCommand, params);
+       }
+
+       /**
+        * Commodities the refresh the contribution of a command with a map of
+        * parameters in a context menu
+        * 
+        * @param menuManager
+        * @param locator
+        * @param contributionId
+        * @param commandId
+        * @param label
+        * @param icon
+        * @param showCommand
+        * @param params
+        */
+       public static void refreshParameterizedCommand(IMenuManager menuManager,
+                       IServiceLocator locator, String contributionId, String commandId,
+                       String label, ImageDescriptor icon, boolean showCommand,
+                       Map<String, String> params) {
+               IContributionItem ici = menuManager.find(contributionId);
                if (ici != null)
                        menuManager.remove(ici);
-               CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
-                               locator, null, cmdId, SWT.PUSH);
-
                if (showCommand) {
+                       CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
+                                       locator, null, commandId, SWT.PUSH);
+
                        // Set Params
                        contributionItemParameter.label = label;
                        contributionItemParameter.icon = icon;
@@ -134,7 +118,7 @@ public class CommandUtils {
 
                        CommandContributionItem cci = new CommandContributionItem(
                                        contributionItemParameter);
-                       cci.setId(cmdId);
+                       cci.setId(contributionId);
                        menuManager.add(cci);
                }
        }
@@ -156,8 +140,8 @@ public class CommandUtils {
         * Helper to call a command with a map of parameters easily
         * 
         * @param paramMap
-        *            a map that links various commands ids with corresponding
-        *            String values.
+        *            a map that links various command IDs with corresponding String
+        *            values.
         */
        public static void callCommand(String commandID,
                        Map<String, String> paramMap) {
@@ -191,9 +175,64 @@ public class CommandUtils {
 
                        // execute the command
                        handlerService.executeCommand(pc, null);
+               } catch (Exception e) {
+                       throw new ArgeoException("Unexpected error while"
+                                       + " calling the command " + commandID, e);
+               }
+       }
+
+       // legacy methods. Should be removed soon
+
+       /**
+        * Shortcut to call a command with a single parameter.
+        * 
+        * WARNING: none of the parameter can be null
+        * 
+        * @deprecated rather use <code>callCommand(commandID,parameterID,
+                       parameterValue)</code>
+        */
+       public static void CallCommandWithOneParameter(String commandId,
+                       String paramId, String paramValue) {
+               try {
+                       IWorkbench iw = ArgeoUiPlugin.getDefault().getWorkbench();
+                       IHandlerService handlerService = (IHandlerService) iw
+                                       .getService(IHandlerService.class);
+
+                       // Gets a command that must have been previously registered
+                       IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
+                       ICommandService cmdService = (ICommandService) window
+                                       .getService(ICommandService.class);
+                       Command cmd = cmdService.getCommand(commandId);
+
+                       // Manages the single parameter
+                       ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
+                       IParameter iparam = cmd.getParameter(paramId);
+                       Parameterization params = new Parameterization(iparam, paramValue);
+                       parameters.add(params);
+
+                       // Create and execute the command
+                       ParameterizedCommand pc = new ParameterizedCommand(cmd,
+                                       parameters.toArray(new Parameterization[parameters.size()]));
+                       handlerService = (IHandlerService) window
+                                       .getService(IHandlerService.class);
+                       handlerService.executeCommand(pc, null);
                } catch (Exception e) {
                        throw new ArgeoException(
-                                       "Unexepected exception while opening node editor", e);
+                                       "Error calling command of id:" + commandId, e);
                }
        }
+
+       /**
+        * Commodities the refresh of a single command with a map of parameters in a
+        * Menu.aboutToShow method to simplify further development Rather use
+        * {@link refreshParameterizedCommand()}
+        */
+       @Deprecated
+       public static void refreshParametrizedCommand(IMenuManager menuManager,
+                       IServiceLocator locator, String cmdId, String label,
+                       ImageDescriptor icon, boolean showCommand,
+                       Map<String, String> params) {
+               refreshParameterizedCommand(menuManager, locator, cmdId, label, icon,
+                               showCommand, params);
+       }
 }
\ No newline at end of file