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.IParameter;\r
+import org.eclipse.core.commands.Parameterization;\r
+import org.eclipse.core.commands.ParameterizedCommand;\r
import org.eclipse.core.commands.State;\r
import org.eclipse.core.runtime.CoreException;\r
import org.eclipse.core.runtime.IConfigurationElement;\r
import org.eclipse.ui.handlers.RegistryToggleState;\r
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;\r
import org.eclipse.ui.part.EditorActionBarContributor;\r
-\r
import org.simantics.db.common.utils.Logger;\r
import org.simantics.ui.internal.Activator;\r
+import org.simantics.ui.toolbar.ToolBarCommandRegistry.Parameter;\r
import org.simantics.ui.toolbar.ToolBarCommandRegistry.ToolbarCommandExtension;\r
import org.simantics.utils.datastructures.MapList;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
\r
\r
/**\r
public class ToolbarContributor extends EditorActionBarContributor implements ICommandListener, IPartListener, CommandStateListener, IExecutableExtension {\r
\r
private static boolean DEBUG = false; // Print debug messages to console\r
- private static boolean REUSE = true; // true: Reuse contribution items / false: delete items on dispose \r
+ private boolean REUSE = true; // true: Reuse contribution items (leave toolbar in disabled state when editor closes)\r
+ // false: delete items on dispose (remove toolbar editor closes) \r
\r
private static final String PLATFORM = "platform:/plugin/";\r
\r
MapList<String, CommandAction> actions = new MapList<String, ToolbarContributor.CommandAction>();\r
\r
CommandStateRegistry stateRegistry;\r
+ \r
+ IPartListener partListener;\r
\r
private Map<String,ComboContribution> menus = new HashMap<String, ComboContribution>();\r
\r
service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); \r
handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);\r
stateRegistry = CommandStateRegistry.getInstance();\r
+ // we need part listener to be notified for other editor activations (i.e editors that do not use this contributor).\r
+ partListener = new IPartListener() {\r
+ \r
+ @Override\r
+ public void partOpened(IWorkbenchPart part) {}\r
+ \r
+ @Override\r
+ public void partDeactivated(IWorkbenchPart part) {}\r
+ \r
+ @Override\r
+ public void partClosed(IWorkbenchPart part) {}\r
+ \r
+ @Override\r
+ public void partBroughtToTop(IWorkbenchPart part) {}\r
+ \r
+ @Override\r
+ public void partActivated(IWorkbenchPart part) {\r
+ if (part instanceof IEditorPart)\r
+ setContext2((IEditorPart)part);\r
+ }\r
+ };\r
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(partListener);\r
\r
}\r
\r
\r
if ("toolbar".equals(key)) {\r
toolbarId = value;\r
- } \r
+ } else if ("hide".equals(key)) {\r
+ REUSE = !Boolean.parseBoolean(value);\r
+ }\r
}\r
}\r
\r
if (DEBUG) System.out.println("Adding command to toolbar " +getToolbarId() + " " + ext);\r
String commandId = ext.commandId;\r
Command command = service.getCommand(commandId);\r
+ ICommandWrapper wrapper = new CommandWrapper(command);\r
+ \r
+ ParameterizedCommand parameterizedCommand = null;\r
+ \r
+ if (ext.parameters.size() > 0) {\r
+ try {\r
+ Parameterization parameterizations[] = new Parameterization[ext.parameters.size()];\r
+ for (int i = 0; i < ext.parameters.size(); i++) {\r
+ Parameter param = ext.parameters.get(i);\r
+ IParameter parameter = command.getParameter(param.name);\r
+ parameterizations[i] = new Parameterization(parameter, param.value);\r
+ }\r
+ parameterizedCommand = new ParameterizedCommand(command, parameterizations);\r
+ wrapper = new ParameterizedCommandWrapper(parameterizedCommand);\r
+ } catch (org.eclipse.core.commands.common.NotDefinedException e) {\r
+ e.printStackTrace();\r
+ ExceptionUtils.logError(e);\r
+ return;\r
+ }\r
+ }\r
\r
String type = ext.type;\r
\r
\r
CommandAction a = null;\r
if (type.equals("toggle") && toggleState != null) {\r
- a = new CommandCheckboxAction(command,name,image);\r
+ a = new CommandCheckboxAction(wrapper,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
+ a = new CommandRadioAction(wrapper,name,ext.value,image);\r
} else if (type.equals("combo")) {\r
- a = new CommandRadioAction(command,name,ext.value,image);\r
+ a = new CommandRadioAction(wrapper,name,ext.value,image);\r
ComboContribution combo = menus.get(commandId);\r
if (REUSE && combo == null) {\r
combo = (ComboContribution)mgr.find(commandId);\r
return;\r
}\r
} else if (type.equals("push")) {\r
- a = new CommandPushAction(command,name,image);\r
+ a = new CommandPushAction(wrapper,name,image);\r
} else {\r
if (DEBUG) System.out.println(ext + " is not valid.");\r
Logger.defaultLogError(ext + " is not valid.");\r
setContext((IEditorPart)part);\r
}\r
\r
+ private void setContext2(IEditorPart part) {\r
+ if (REUSE)\r
+ return;\r
+ if (this.activePart == part)\r
+ return;\r
+ setContext(null);\r
+ }\r
+ \r
private void setContext(IEditorPart part) {\r
this.activePart = part;\r
if (activePart != null && !parts.contains(activePart)) {\r
a.setEnabled(false);\r
}\r
ComboContribution menu = menus.get(commandId);\r
- if (menu != null) \r
+ if (menu != null) {\r
menu.setEnabled(false);\r
+ }\r
\r
}\r
}\r
actions.clear();\r
\r
// without this the contributed toolbar widgets would continue reserve the space even when they are destroyed.\r
- // TODO : how to make the toolbar fix its layout?\r
- // Note: Using REUSE flag alleviates the problem, since the widgets are not removed.\r
coolBarManager.update(true);\r
+ mgr.update(true);\r
}\r
CommandStateRegistry.getInstance().removeListener(this);\r
super.dispose();\r
activePart = null;\r
+ if (partListener != null) {\r
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(partListener);\r
+ partListener = null;\r
+ }\r
}\r
\r
boolean settingState = false;\r
private void restoreActionStates() {\r
if (activePart == null)\r
return;\r
+ if (DEBUG)System.out.println("Restore " + activePart);\r
// toggles\r
Map<String,Boolean> defaultToggleStates = stateRegistry.getDefaultToggleStates();\r
for (String commandId : defaultToggleStates.keySet()) {\r
for (CommandAction a : actions.getValues(commandId)) {\r
+ if (DEBUG)System.out.println(commandId + " def " + defaultToggleStates.get(commandId));\r
a.setChecked(defaultToggleStates.get(commandId));\r
}\r
}\r
if (editorStates != null) {\r
for (String commandId : editorStates.keySet()) {\r
for (CommandAction a : actions.getValues(commandId)) {\r
+ if (DEBUG)System.out.println(commandId + " " + editorStates.get(commandId));\r
a.setChecked(editorStates.get(commandId));\r
}\r
}\r
String defaultValue = defaultRadioStates.get(commandId);\r
for (CommandAction a : actions.getValues(commandId)) {\r
CommandRadioAction r = (CommandRadioAction)a;\r
+ if (DEBUG)System.out.println(commandId + " def " + r.getValue().equals(defaultValue) +" " + r.getValue());\r
r.setChecked(r.getValue().equals(defaultValue));\r
}\r
}\r
String defaultValue = editorRadioStates.get(commandId);\r
for (CommandAction a : actions.getValues(commandId)) {\r
CommandRadioAction r = (CommandRadioAction)a;\r
+ if (DEBUG)System.out.println(commandId + " " + r.getValue().equals(defaultValue) +" " + r.getValue());\r
r.setChecked(r.getValue().equals(defaultValue));\r
}\r
}\r
return (Boolean)toggleState.getValue();\r
}\r
\r
- private abstract class CommandAction extends Action {\r
+ private interface ICommandWrapper {\r
+ \r
+ public Command getCommand();\r
+ public String getCommandId();\r
+ public void run();\r
+ }\r
+ \r
+ private class CommandWrapper implements ICommandWrapper{\r
private Command command;\r
\r
- public CommandAction(Command command, String name, ImageDescriptor image, int style) {\r
- super(name,style);\r
+ public CommandWrapper(Command command) {\r
this.command = command;\r
- if (image != null)\r
- setImageDescriptor(image);\r
+ }\r
+ \r
+ @Override\r
+ public Command getCommand() {\r
+ return command;\r
+ }\r
+ \r
+ @Override\r
+ public String getCommandId() {\r
+ return command.getId();\r
}\r
\r
@Override\r
}\r
}\r
\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (obj.getClass() != getClass())\r
+ return false;\r
+ CommandWrapper other= (CommandWrapper)obj;\r
+ return other.getCommandId().equals(getCommandId());\r
+ }\r
+ }\r
+ \r
+ private class ParameterizedCommandWrapper implements ICommandWrapper{\r
+ private ParameterizedCommand command;\r
+ \r
+ public ParameterizedCommandWrapper(ParameterizedCommand command) {\r
+ this.command = command;\r
+ }\r
+ \r
+ @Override\r
public Command getCommand() {\r
- return command;\r
+ return command.getCommand();\r
}\r
\r
+ @Override\r
public String getCommandId() {\r
return command.getId();\r
}\r
\r
+ @Override\r
+ public void run() {\r
+ try {\r
+ handlerService.executeCommand(command, null);\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (obj.getClass() != getClass())\r
+ return false;\r
+ ParameterizedCommandWrapper other= (ParameterizedCommandWrapper)obj;\r
+ return other.command.equals(command);\r
+ }\r
+ }\r
+ \r
+ private abstract class CommandAction extends Action {\r
+ private ICommandWrapper command;\r
+ \r
+ public CommandAction(ICommandWrapper 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
+ command.run();\r
+ }\r
+ \r
+ public Command getCommand() {\r
+ return command.getCommand();\r
+ }\r
+ \r
+ public String getCommandId() {\r
+ return command.getCommandId();\r
+ }\r
+ \r
\r
@Override\r
public boolean equals(Object obj) {\r
if (obj.getClass() != getClass())\r
return false;\r
CommandAction other= (CommandAction)obj;\r
- if (!other.getCommandId().equals(getCommandId()))\r
- return false;\r
- return true;\r
+ return command.equals(other.command);\r
}\r
\r
\r
@Override\r
public int hashCode() {\r
- return command.getId().hashCode();\r
+ return command.getCommandId().hashCode();\r
}\r
}\r
\r
+ \r
private class CommandCheckboxAction extends CommandAction {\r
\r
- public CommandCheckboxAction(Command command, String name, ImageDescriptor image) {\r
+ public CommandCheckboxAction(ICommandWrapper command, String name, ImageDescriptor image) {\r
super(command,name,image,Action.AS_CHECK_BOX);\r
\r
}\r
\r
private String value;\r
\r
- public CommandRadioAction(Command command, String name, String value, ImageDescriptor image) {\r
+ public CommandRadioAction(ICommandWrapper command, String name, String value, ImageDescriptor image) {\r
super(command,name,image,Action.AS_RADIO_BUTTON);\r
this.value = value;\r
}\r
\r
private class CommandPushAction extends CommandAction {\r
\r
- public CommandPushAction(Command command, String name, ImageDescriptor image) {\r
+ public CommandPushAction(ICommandWrapper command, String name, ImageDescriptor image) {\r
super(command,name,image,Action.AS_PUSH_BUTTON);\r
}\r
\r
if (combo != null)\r
combo.setEnabled(enabled);\r
}\r
+ \r
+ @Override\r
+ public void dispose() {\r
+ combo.dispose();\r
+ super.dispose();\r
+ }\r
}\r
\r
}\r