X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=base%2Fruntime%2Forg.argeo.eclipse.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Futils%2FCommandUtils.java;h=22a139f134aa565b119106ed292511d3693e29d5;hb=e2584ebcf56759309bc139f974e79bb31af23132;hp=eb0de934f74837f8ec4c7f6ff9e3f8caa58492cf;hpb=07b21659cba964ffdc81403718d0962c2d09175d;p=lgpl%2Fargeo-commons.git diff --git a/base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java b/base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java index eb0de934f..22a139f13 100644 --- a/base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java +++ b/base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/utils/CommandUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2012 Mathieu Baudier + * Copyright (C) 2007-2012 Argeo GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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,56 +25,214 @@ 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. - * + * 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 + * Commodities the refresh of a single command with no parameter in a + * Menu.aboutToShow method to simplify further development * - * NOTE that none of the parameter can be null + * 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 icon + * @param showCommand + */ + public static void refreshCommand(IMenuManager menuManager, + IServiceLocator locator, String cmdId, String label, + ImageDescriptor icon, boolean showCommand) { + refreshParameterizedCommand(menuManager, locator, cmdId, label, icon, + showCommand, null); + } + + /** + * 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 + * @param cmdId + * @param label + * @param iconPath + * @param showCommand + */ + public static void refreshParameterizedCommand(IMenuManager menuManager, + IServiceLocator locator, String cmdId, String label, + ImageDescriptor icon, boolean showCommand, + Map params) { + 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 params) { + IContributionItem ici = menuManager.find(contributionId); + if (ici != null) + menuManager.remove(ici); + if (showCommand) { + CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter( + locator, null, commandId, SWT.PUSH); + + // Set Params + contributionItemParameter.label = label; + contributionItemParameter.icon = icon; + + if (params != null) + contributionItemParameter.parameters = params; + + CommandContributionItem cci = new CommandContributionItem( + contributionItemParameter); + cci.setId(contributionId); + 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 params = new HashMap(); + 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 command IDs with corresponding String + * values. + */ + public static void callCommand(String commandID, + Map 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 parameters = null; + ParameterizedCommand pc; + + if (paramMap != null) { + // Set parameters of the command to launch : + parameters = new ArrayList(); + Parameterization parameterization; + + for (String id : paramMap.keySet()) { + parameterization = new Parameterization( + cmd.getParameter(id), paramMap.get(id)); + parameters.add(parameterization); + } + pc = new ParameterizedCommand(cmd, + parameters.toArray(new Parameterization[parameters + .size()])); + } else + pc = new ParameterizedCommand(cmd, null); + + // 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 callCommand(commandID,parameterID, + parameterValue) */ 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 + // 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 parameters = new ArrayList(); - - // get the parameter IParameter iparam = cmd.getParameter(paramId); - Parameterization params = new Parameterization(iparam, paramValue); parameters.add(params); - // build the parameterized command + // Create and execute the 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); + throw new ArgeoException( + "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 params) { + refreshParameterizedCommand(menuManager, locator, cmdId, label, icon, + showCommand, params); + } +} \ No newline at end of file