+++ /dev/null
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.toolbar;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import org.eclipse.core.commands.Command;\r
-import org.eclipse.core.commands.State;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.eclipse.ui.commands.ICommandService;\r
-import org.eclipse.ui.handlers.IHandlerService;\r
-import org.eclipse.ui.handlers.RadioState;\r
-import org.eclipse.ui.handlers.RegistryToggleState;\r
-\r
-/**\r
- * Registry for storing command states separately for each IEditorPart\r
- * \r
- * TODO : how to change toggle/radios state from editor?\r
- * TODO : how to update visible buttons (ToolbarContributor)\r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- */\r
-public class CommandStateRegistry {\r
-\r
- \r
- private static CommandStateRegistry instance;\r
- \r
- \r
- public static CommandStateRegistry getInstance() {\r
- if (instance == null)\r
- instance = new CommandStateRegistry();\r
- return instance;\r
- }\r
- \r
- \r
- \r
- private Map<IWorkbenchPart,Map<String,Boolean>> toggleStates = new HashMap<IWorkbenchPart, Map<String,Boolean>>();\r
- private Map<String,Boolean> defaultToggleStates = new HashMap<String, Boolean>();\r
- \r
- private Map<String,String> defaultRadioStates = new HashMap<String, String>();\r
- private Map<IWorkbenchPart,Map<String,String>> radioStates = new HashMap<IWorkbenchPart, Map<String,String>>();\r
-\r
- private ICommandService service;\r
- private IHandlerService handlerService;\r
- \r
- private CommandStateRegistry() {\r
- service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
- handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);\r
- }\r
- \r
- /**\r
- * Stores default state for a command.\r
- * \r
- * Note: uses current state as default. \r
- * \r
- * @param commandId\r
- */\r
- public void storeDefaultState(String commandId) {\r
- Command command = service.getCommand(commandId);\r
- State toggleState = command.getState(RegistryToggleState.STATE_ID);\r
- State radioState = command.getState(RadioState.STATE_ID);\r
- if (toggleState != null) {\r
- if (!defaultToggleStates.containsKey(commandId))\r
- defaultToggleStates.put(commandId, getToggleState(command));\r
- } else if (radioState != null) {\r
- String value = (String) radioState.getValue();\r
- if (!defaultRadioStates.containsKey(commandId))\r
- defaultRadioStates.put(commandId, value);\r
- } else {\r
- throw new IllegalArgumentException("Command " + commandId + " does not have a state");\r
- }\r
- }\r
- /**\r
- * Stores toggle state of a command.\r
- * @param part\r
- * @param commandId\r
- * @param checked\r
- */\r
- public void setEditorState(IWorkbenchPart part, String commandId, boolean checked) {\r
- Map<String,Boolean> editorStates = toggleStates.get(part);\r
- if (editorStates == null) {\r
- editorStates = new HashMap<String, Boolean>();\r
- toggleStates.put(part, editorStates);\r
- }\r
- editorStates.put(commandId, checked);\r
- }\r
- \r
- /**\r
- * Stores radio state of a command.\r
- * @param part\r
- * @param commandId\r
- * @param value\r
- */\r
- public void setEditorState(IWorkbenchPart part, String commandId, String value) {\r
- Map<String,String> editorStates = radioStates.get(part);\r
- if (editorStates == null) {\r
- editorStates = new HashMap<String, String>();\r
- radioStates.put(part, editorStates);\r
- }\r
- editorStates.put(commandId, value);\r
- }\r
- \r
- public Map<String, Boolean> getDefaultToggleStates() {\r
- return defaultToggleStates;\r
- }\r
- \r
- public Map<String, String> getDefaultRadioStates() {\r
- return defaultRadioStates;\r
- }\r
- \r
- public Map<String, Boolean> getEditorToggleStates(IWorkbenchPart part) {\r
- return toggleStates.get(part);\r
- }\r
- \r
- public Map<String, String> getEditorRadioStates(IWorkbenchPart part) {\r
- return radioStates.get(part);\r
- }\r
- \r
- public Boolean getToggleState(IWorkbenchPart part, String commandId) {\r
- if (part == null)\r
- return defaultToggleStates.get(commandId);\r
- Map<String,Boolean> editorStates = toggleStates.get(part);\r
- if (editorStates == null) {\r
- return defaultToggleStates.get(commandId);\r
- }\r
- return editorStates.get(commandId);\r
- }\r
- \r
- public String getRadioState(IWorkbenchPart part, String commandId) {\r
- if (part == null)\r
- return defaultRadioStates.get(commandId);\r
- Map<String,String> editorStates = radioStates.get(part);\r
- if (editorStates == null) {\r
- return defaultRadioStates.get(commandId);\r
- }\r
- return editorStates.get(commandId);\r
- }\r
- \r
- public void clearStates(IWorkbenchPart part) {\r
- toggleStates.remove(part);\r
- radioStates.remove(part);\r
- }\r
- \r
- private boolean getToggleState(Command command) {\r
- State toggleState = command.getState(RegistryToggleState.STATE_ID);\r
- return (Boolean)toggleState.getValue();\r
- }\r
- \r
- \r
-}\r
+++ /dev/null
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.toolbar;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.core.runtime.IConfigurationElement;\r
-import org.eclipse.core.runtime.IExtension;\r
-import org.eclipse.core.runtime.IExtensionPoint;\r
-import org.eclipse.core.runtime.Platform;\r
-import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;\r
-import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;\r
-import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;\r
-import org.eclipse.core.runtime.dynamichelpers.IFilter;\r
-import org.simantics.g3d.Activator;\r
-\r
-\r
-\r
-\r
-\r
-public class ToolBarCommandRegistry implements IExtensionChangeHandler {\r
- private final static String NAMESPACE = Activator.PLUGIN_ID;\r
-\r
- private final static String EP_NAME = "toolbarCommand";\r
- \r
- private ExtensionTracker tracker;\r
- \r
- private List<ToolbarCommandExtension> extensions = new ArrayList<ToolbarCommandExtension>();\r
- \r
- \r
- private static ToolBarCommandRegistry INSTANCE;\r
- \r
- public static synchronized ToolBarCommandRegistry getInstance() {\r
- if (INSTANCE == null)\r
- INSTANCE = new ToolBarCommandRegistry();\r
- return INSTANCE;\r
- }\r
- \r
- public static synchronized void dispose() {\r
- if (INSTANCE != null) {\r
- INSTANCE.close();\r
- INSTANCE = null;\r
- } \r
- }\r
- \r
- public ToolBarCommandRegistry() {\r
- tracker = new ExtensionTracker();\r
- \r
- IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE,EP_NAME);\r
- loadExtensions(ep.getConfigurationElements());\r
- \r
- IFilter filter = ExtensionTracker.createExtensionPointFilter(ep);\r
- tracker.registerHandler(this, filter);\r
- }\r
- \r
- private void close() {\r
- tracker.close();\r
- tracker = null;\r
- extensions.clear();\r
- }\r
- \r
- public synchronized List<ToolbarCommandExtension> getExtensions() {\r
- return Collections.unmodifiableList(extensions);\r
- }\r
- \r
- private synchronized void loadExtensions(IConfigurationElement[] elements) {\r
- for (IConfigurationElement el : elements) {\r
- String commandId = el.getAttribute("commandId");\r
- \r
- ToolbarCommandExtension ext = new ToolbarCommandExtension(commandId);\r
- ext.toolbarId = el.getAttribute("toolbarId");\r
- ext.image = el.getAttribute("image");\r
- ext.name = el.getAttribute("name");\r
- ext.type = el.getAttribute("type");\r
- ext.value = el.getAttribute("value");\r
- ext.contributorId = el.getContributor().getName();\r
- tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
- extensions.add(ext);\r
- \r
- }\r
- }\r
- \r
- @Override\r
- public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
- loadExtensions(extension.getConfigurationElements());\r
- }\r
- \r
- @Override\r
- public synchronized void removeExtension(IExtension extension, Object[] objects) {\r
- for (Object o : objects) {\r
- ToolbarCommandExtension ext = (ToolbarCommandExtension) o;\r
- tracker.unregisterObject(extension, ext);\r
- extensions.remove(ext);\r
- }\r
- }\r
- \r
- public synchronized List<ToolbarCommandExtension> getExtensions(String toolbarId) {\r
- List<ToolbarCommandExtension> list = new ArrayList<ToolbarCommandExtension>();\r
- for (ToolbarCommandExtension ext : extensions)\r
- if (ext.toolbarId.equals(toolbarId))\r
- list.add(ext);\r
- return list;\r
- \r
- }\r
- \r
- public class ToolbarCommandExtension {\r
- public String commandId;\r
- public String toolbarId;\r
- public String name;\r
- public String type;\r
- public String value;\r
- public String image;\r
- public String contributorId;\r
- public ToolbarCommandExtension(String commandId) {\r
- super();\r
- this.commandId = commandId;\r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "ToolbarCommandExtension, commandId= " + commandId + " toolbarId= " + toolbarId + " type= " + type + " value= " + value + " contributor= " + contributorId;\r
- }\r
- }\r
- \r
- \r
-}\r
+++ /dev/null
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.toolbar;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.eclipse.core.commands.Command;\r
-import org.eclipse.core.commands.CommandEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.core.commands.ICommandListener;\r
-import org.eclipse.core.commands.State;\r
-import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.action.ActionContributionItem;\r
-import org.eclipse.jface.action.IContributionItem;\r
-import org.eclipse.jface.action.ICoolBarManager;\r
-import org.eclipse.jface.action.IToolBarManager;\r
-import org.eclipse.jface.action.ToolBarContributionItem;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.nebula.widgets.tablecombo.TableCombo;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.layout.GridData;\r
-import org.eclipse.swt.layout.GridLayout;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.TableItem;\r
-import org.eclipse.ui.IEditorPart;\r
-import org.eclipse.ui.IPartListener;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.eclipse.ui.commands.ICommandService;\r
-import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.eclipse.ui.handlers.IHandlerService;\r
-import org.eclipse.ui.handlers.RadioState;\r
-import org.eclipse.ui.handlers.RegistryToggleState;\r
-import org.eclipse.ui.menus.WorkbenchWindowControlContribution;\r
-import org.eclipse.ui.part.EditorActionBarContributor;\r
-import org.simantics.db.common.utils.ErrorLogger;\r
-import org.simantics.g3d.Activator;\r
-import org.simantics.g3d.toolbar.ToolBarCommandRegistry.ToolbarCommandExtension;\r
-import org.simantics.utils.datastructures.MapList;\r
-\r
-\r
-/**\r
- * EditorBarContributor, which tracks toggle states separately for each command.\r
- * \r
- * @see org.simantics.g3d.toolbarCommand Extension Point\r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- * \r
- * \r
- * \r
- * TODO : test radio buttons.\r
- * TODO : configuring the position of buttons. \r
- *\r
- */\r
-public abstract class ToolbarContributor extends EditorActionBarContributor implements ICommandListener, IPartListener {\r
-\r
- private static final String PLATFORM = "platform:/plugin/";\r
- \r
- private IEditorPart activePart;\r
- private Set<IEditorPart> parts = new HashSet<IEditorPart>();\r
- IToolBarManager mgr;\r
- \r
- \r
- ICommandService service;\r
- IHandlerService handlerService;\r
- List<IContributionItem> items = new ArrayList<IContributionItem>();\r
- MapList<String, CommandAction> actions = new MapList<String, ToolbarContributor.CommandAction>();\r
- \r
- CommandStateRegistry stateRegistry;\r
- \r
- private Map<String,ComboContribution> menus = new HashMap<String, ComboContribution>();\r
- \r
- public ToolbarContributor() {\r
- service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); \r
- handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);\r
- stateRegistry = CommandStateRegistry.getInstance();\r
- }\r
- \r
- public abstract String getToolbarId();\r
- \r
- public void contributeToCoolBar(ICoolBarManager coolBarManager) {\r
- IContributionItem toolBar = coolBarManager.find(getToolbarId());\r
- if (toolBar instanceof ToolBarContributionItem)\r
- mgr = ((ToolBarContributionItem) toolBar).getToolBarManager();\r
- if (mgr == null)\r
- return;\r
- \r
- createCommands();\r
- \r
- mgr.markDirty();\r
- \r
- }\r
- \r
- private void createCommands() {\r
- for (ToolbarCommandExtension ext : ToolBarCommandRegistry.getInstance().getExtensions(getToolbarId())) {\r
- addCommand(ext);\r
- }\r
- }\r
- \r
- private void addCommand(ToolbarCommandExtension ext) {\r
- String commandId = ext.commandId;\r
- Command command = service.getCommand(commandId);\r
- \r
- String type = ext.type;\r
- \r
- State toggleState = command.getState(RegistryToggleState.STATE_ID);\r
- State radioState = command.getState(RadioState.STATE_ID);\r
- \r
- String name = ext.name;\r
- \r
- \r
- ImageDescriptor image = getImage(ext);\r
- \r
- CommandAction a = null;\r
- if (type.equals("toggle") && toggleState != null) {\r
- a = new CommandCheckboxAction(command,name,image);\r
-\r
- stateRegistry.storeDefaultState(commandId);\r
- } else if (radioState != null && ext.value != null) {\r
- stateRegistry.storeDefaultState(commandId);\r
- if (type.equals("radio")) {\r
- a = new CommandRadioAction(command,name,ext.value,image);\r
- } else if (type.equals("combo")) {\r
- a = new CommandRadioAction(command,name,ext.value,image);\r
- ComboContribution combo = menus.get(commandId);\r
- if (combo == null) {\r
- combo = new ComboContribution();\r
- menus.put(commandId, combo);\r
- items.add(combo);\r
- mgr.add(combo);\r
- a.getCommand().addCommandListener(this);\r
- }\r
- actions.add(commandId,a);\r
- combo.addAction(a);\r
- return;\r
- }\r
- } else if (type.equals("push")) {\r
- a = new CommandPushAction(command,name,image);\r
- } else {\r
- ErrorLogger.defaultLogError(ext + " is not valid.");\r
- return;\r
- }\r
- a.getCommand().addCommandListener(this);\r
- IContributionItem item = new ActionContributionItem(a);\r
- actions.add(commandId,a);\r
- items.add(item);\r
- mgr.add(item);\r
- }\r
- \r
- private ImageDescriptor getImage(ToolbarCommandExtension ext) {\r
- ImageDescriptor image = null;\r
- if (ext.image != null) {\r
- String plugin = null;\r
- String file = null;\r
- if (ext.image.startsWith(PLATFORM)) {\r
- String s = ext.image.substring(PLATFORM.length());\r
- int i = s.indexOf("/");\r
- plugin = s.substring(0,i);\r
- file = s.substring(i+1);\r
- } else {\r
- plugin = ext.contributorId;\r
- file = ext.image;\r
- }\r
- image = Activator.imageDescriptorFromPlugin(plugin, file);\r
- }\r
- return image;\r
- }\r
- \r
-\r
- \r
- @Override\r
- public void commandChanged(CommandEvent commandEvent) {\r
- if (commandEvent.isHandledChanged()||commandEvent.isEnabledChanged()) {\r
- Command command = commandEvent.getCommand();\r
- String commandId = command.getId();\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- a.setEnabled(command.isHandled() && command.isEnabled());\r
- }\r
- }\r
- }\r
- \r
- @Override\r
- public void setActiveEditor(IEditorPart targetEditor) {\r
- if (targetEditor == activePart)\r
- return;\r
- setContext(targetEditor);\r
- }\r
- \r
- private void setContext(IEditorPart part) {\r
- this.activePart = part;\r
- if (!parts.contains(activePart)) {\r
- activePart.getSite().getPage().addPartListener(this);\r
- }\r
- if (part != null) {\r
- for (String commandId : actions.getKeys()) {\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- a.setEnabled(true);\r
- }\r
- }\r
- updateActionBars(part);\r
- } else {\r
- for (String commandId : actions.getKeys()) {\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- a.setEnabled(false);\r
- }\r
- }\r
- }\r
- }\r
-\r
- private void updateActionBars(IEditorPart part) {\r
- restoreActionStates();\r
- part.getEditorSite().getActionBars().updateActionBars();\r
- }\r
- \r
- @Override\r
- public void dispose() {\r
- if (mgr != null) {\r
- for (IContributionItem item : items) {\r
- mgr.remove(item);\r
- item.dispose();\r
- }\r
- mgr.markDirty();\r
- mgr.update(true);\r
- if (activePart != null) {\r
- activePart.getEditorSite().getActionBars().updateActionBars();\r
- }\r
- \r
- for (String commandId : actions.getKeys()) {\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- a.getCommand().removeCommandListener(this);\r
- }\r
- }\r
- actions.clear();\r
- }\r
- \r
- super.dispose();\r
- activePart = null;\r
- }\r
- \r
-\r
- private void storeRadioActionState(CommandRadioAction action, boolean checked) {\r
- if (activePart == null)\r
- return;\r
- stateRegistry.setEditorState(activePart, action.getCommandId(), action.getValue());\r
- }\r
- \r
- private void storeToggleActionState(CommandAction action, boolean checked) {\r
- if (activePart == null)\r
- return;\r
- stateRegistry.setEditorState(activePart, action.getCommandId(), checked);\r
- }\r
- \r
- private void restoreActionStates() {\r
- if (activePart == null)\r
- return;\r
- // toggles\r
- Map<String,Boolean> defaultToggleStates = stateRegistry.getDefaultToggleStates();\r
- for (String commandId : defaultToggleStates.keySet()) {\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- a.setChecked(defaultToggleStates.get(commandId));\r
- }\r
- }\r
- Map<String,Boolean> editorStates = stateRegistry.getEditorToggleStates(activePart);//toggleStates.get(activePart);\r
- if (editorStates != null) {\r
- for (String commandId : editorStates.keySet()) {\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- a.setChecked(editorStates.get(commandId));\r
- }\r
- }\r
- }\r
- // radios\r
- Map<String,String> defaultRadioStates = stateRegistry.getDefaultRadioStates();\r
- for (String commandId : defaultRadioStates.keySet()) {\r
- String defaultValue = defaultRadioStates.get(commandId);\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- CommandRadioAction r = (CommandRadioAction)a;\r
- r.setChecked(r.getValue().equals(defaultValue));\r
- }\r
- }\r
- \r
- Map<String,String> editorRadioStates = stateRegistry.getEditorRadioStates(activePart);//radioStates.get(activePart);\r
- if (editorRadioStates != null) {\r
- for (String commandId : editorRadioStates.keySet()) {\r
- String defaultValue = editorRadioStates.get(commandId);\r
- for (CommandAction a : actions.getValues(commandId)) {\r
- CommandRadioAction r = (CommandRadioAction)a;\r
- r.setChecked(r.getValue().equals(defaultValue));\r
- }\r
- }\r
- }\r
- \r
- for (ComboContribution c : menus.values()) {\r
- c.updateSelection();\r
- }\r
- \r
-\r
- }\r
- \r
- @Override\r
- public void partActivated(IWorkbenchPart part) {\r
-\r
- }\r
- \r
- @Override\r
- public void partBroughtToTop(IWorkbenchPart part) {\r
- \r
- }\r
- \r
- @Override\r
- public void partClosed(IWorkbenchPart part) {\r
- parts.remove(part);\r
- stateRegistry.clearStates(part);\r
- part.getSite().getPage().removePartListener(this);\r
- }\r
- \r
- @Override\r
- public void partDeactivated(IWorkbenchPart part) {\r
- \r
- }\r
- \r
- @Override\r
- public void partOpened(IWorkbenchPart part) {\r
- \r
- }\r
- \r
- private boolean getToggleState(Command command) {\r
- State toggleState = command.getState(RegistryToggleState.STATE_ID);\r
- return (Boolean)toggleState.getValue();\r
- }\r
- \r
- private abstract class CommandAction extends Action {\r
- private Command command;\r
- \r
- public CommandAction(Command command, String name, ImageDescriptor image, int style) {\r
- super(name,style);\r
- this.command = command;\r
- if (image != null)\r
- setImageDescriptor(image);\r
- }\r
- \r
- @Override\r
- public void run() {\r
- try {\r
- handlerService.executeCommand(command.getId(), null);\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- public Command getCommand() {\r
- return command;\r
- }\r
- \r
- public String getCommandId() {\r
- return command.getId();\r
- }\r
- }\r
- \r
- private class CommandCheckboxAction extends CommandAction {\r
- \r
- public CommandCheckboxAction(Command command, String name, ImageDescriptor image) {\r
- super(command,name,image,Action.AS_CHECK_BOX);\r
- \r
- }\r
- \r
- @Override\r
- public void run() {\r
- boolean checked = isChecked(); \r
- storeToggleActionState(this, checked);\r
- try {\r
- if (checked == getToggleState(getCommand()))\r
- HandlerUtil.toggleCommandState(getCommand());\r
- } catch (ExecutionException e) {\r
- e.printStackTrace();\r
- }\r
- super.run();\r
- }\r
- \r
- }\r
- \r
- private class CommandRadioAction extends CommandAction {\r
-\r
- private String value;\r
- \r
- public CommandRadioAction(Command command, String name, String value, ImageDescriptor image) {\r
- super(command,name,image,Action.AS_RADIO_BUTTON);\r
- this.value = value;\r
- }\r
- \r
- @Override\r
- public void run() {\r
- boolean checked = isChecked(); \r
- storeRadioActionState(this, checked);\r
- try {\r
- HandlerUtil.updateRadioState(getCommand(), value);\r
- } catch (ExecutionException e) {\r
- e.printStackTrace();\r
- return;\r
- }\r
- super.run();\r
- }\r
- \r
- public String getValue() {\r
- return value;\r
- }\r
- \r
- }\r
-\r
- private class CommandPushAction extends CommandAction {\r
- \r
- public CommandPushAction(Command command, String name, ImageDescriptor image) {\r
- super(command,name,image,Action.AS_PUSH_BUTTON);\r
- }\r
-\r
- }\r
- \r
- private class ComboContribution extends WorkbenchWindowControlContribution {\r
- private TableCombo combo;\r
- \r
- private List<Action> actions = new ArrayList<Action>();\r
- \r
- @Override\r
- protected Control createControl(Composite parent) {\r
- Composite container = new Composite(parent, SWT.NONE);\r
- GridLayout glContainer = new GridLayout(1, false);\r
- glContainer.marginTop = 0;\r
- glContainer.marginHeight = 0;\r
- glContainer.marginWidth = 0;\r
- container.setLayout(glContainer);\r
- GridData glReader = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);\r
- combo = new TableCombo(container, SWT.BORDER | SWT.READ_ONLY);\r
- combo.setLayoutData(glReader);\r
-\r
- for (Action a : actions) {\r
- TableItem item = new TableItem(combo.getTable(), SWT.NONE);\r
- item.setText(a.getText());\r
- if (a.getImageDescriptor() != null)\r
- item.setImage(a.getImageDescriptor().createImage());\r
- }\r
- \r
- combo.addSelectionListener(new SelectionListener() {\r
- \r
- @Override\r
- public void widgetSelected(SelectionEvent e) {\r
- int index = combo.getSelectionIndex();\r
- if (index == -1)\r
- return;\r
- actions.get(index).run();\r
- }\r
- \r
- @Override\r
- public void widgetDefaultSelected(SelectionEvent e) {\r
- \r
- }\r
- });\r
- updateSelection();\r
- return container;\r
- }\r
- \r
- public void addAction(Action a) {\r
- actions.add(a);\r
- }\r
- \r
- void updateSelection() {\r
- if (combo == null)\r
- return;\r
- for (int i = 0; i < actions.size(); i++) {\r
- if (actions.get(i).isChecked()) {\r
- combo.select(i);\r
- return;\r
- }\r
- }\r
- }\r
- }\r
- \r
-}\r