X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.client.ui.dist%2Fsrc%2Forg%2Fargeo%2Fslc%2Fclient%2Fui%2Fdist%2Futils%2FCommandHelpers.java;fp=org.argeo.slc.client.ui.dist%2Fsrc%2Forg%2Fargeo%2Fslc%2Fclient%2Fui%2Fdist%2Futils%2FCommandHelpers.java;h=ad2cfdb58bbf6deb67e9efc01327ac421dfae37a;hb=2db415932b071525adb52c6374e021174512a924;hp=0000000000000000000000000000000000000000;hpb=7e2f6c6ae08e97925955184aaa29035ac05de149;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/CommandHelpers.java b/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/CommandHelpers.java new file mode 100644 index 000000000..ad2cfdb58 --- /dev/null +++ b/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/CommandHelpers.java @@ -0,0 +1,159 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.argeo.slc.client.ui.dist.utils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.argeo.ArgeoException; +import org.argeo.slc.client.ui.dist.DistPlugin; +import org.eclipse.core.commands.Command; +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.swt.SWT; +import org.eclipse.ui.IWorkbench; +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 methods to manage command updates + */ +public class CommandHelpers { + + /** + * Refresh the given command. + */ + public static void refreshCommand(IMenuManager menuManager, + IServiceLocator locator, String cmdId, String label, String iconPath, + boolean showCommand) { + IContributionItem ici = menuManager.find(cmdId); + if (ici != null) + menuManager.remove(ici); + + if (showCommand) { + // Set Params + CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter( + locator, null, cmdId, SWT.PUSH); + contributionItemParameter.label = label; + contributionItemParameter.icon = DistPlugin.getImageDescriptor(iconPath); + CommandContributionItem cci = new CommandContributionItem( + contributionItemParameter); + cci.setId(cmdId); + menuManager.add(cci); + } + } + + /** + * Refresh the given command and optionally corresponding parameters. + * + * @param menuManager + * @param locator + * @param cmdId + * @param label + * @param showCommand + * Command must be explicitly removed from the context menu at + * each refresh setting this to false. + * @param params + * maps a paramId with a String value + */ + public static void refreshParameterizedCommand(IMenuManager menuManager, + IServiceLocator locator, String cmdId, String label, String iconPath, + boolean showCommand, Map params) { + IContributionItem ici = menuManager.find(cmdId); + if (ici != null) + menuManager.remove(ici); + + if (showCommand) { + // Set Params + CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter( + locator, null, cmdId, SWT.PUSH); + contributionItemParameter.label = label; + contributionItemParameter.icon = DistPlugin.getImageDescriptor(iconPath); + + 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 = DistPlugin.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); + + // build the parameterized command + // execute the command + handlerService.executeCommand(pc, null); + } catch (Exception e) { + throw new ArgeoException( + "Unexepected exception while opening node editor", e); + } + } + +}