From d9981e639bcf212209cddb86ddc7d703859efdea Mon Sep 17 00:00:00 2001 From: Bruno Sinou Date: Wed, 22 Aug 2012 14:46:29 +0000 Subject: [PATCH] Add some utils method to simplify command call git-svn-id: https://svn.argeo.org/commons/trunk@5532 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../argeo/eclipse/ui/utils/CommandUtils.java | 119 +++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) 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..7bb5c2791 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 @@ -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 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 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 commands 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; + 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); + } + } + // 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 -- 2.30.2