X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Ftoolbar%2FToolbarContributor.java;h=0f7d3e26d26f90b2aef60cfcbecb396d4b2ec557;hp=84413bb4684f572fe2f20c53570c96be2fa33be0;hb=d11239c402eec37ba28edcfa7ea6ca7c1f01147f;hpb=2bcb7e8598e769cf21fa6525fc244e1da3eceb31 diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/toolbar/ToolbarContributor.java b/bundles/org.simantics.ui/src/org/simantics/ui/toolbar/ToolbarContributor.java index 84413bb46..0f7d3e26d 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/toolbar/ToolbarContributor.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/toolbar/ToolbarContributor.java @@ -11,6 +11,9 @@ import org.eclipse.core.commands.Command; import org.eclipse.core.commands.CommandEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.commands.ICommandListener; +import org.eclipse.core.commands.IParameter; +import org.eclipse.core.commands.Parameterization; +import org.eclipse.core.commands.ParameterizedCommand; import org.eclipse.core.commands.State; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; @@ -42,11 +45,12 @@ import org.eclipse.ui.handlers.RadioState; import org.eclipse.ui.handlers.RegistryToggleState; import org.eclipse.ui.menus.WorkbenchWindowControlContribution; import org.eclipse.ui.part.EditorActionBarContributor; - import org.simantics.db.common.utils.Logger; import org.simantics.ui.internal.Activator; +import org.simantics.ui.toolbar.ToolBarCommandRegistry.Parameter; import org.simantics.ui.toolbar.ToolBarCommandRegistry.ToolbarCommandExtension; import org.simantics.utils.datastructures.MapList; +import org.simantics.utils.ui.ExceptionUtils; /** @@ -64,7 +68,8 @@ import org.simantics.utils.datastructures.MapList; public class ToolbarContributor extends EditorActionBarContributor implements ICommandListener, IPartListener, CommandStateListener, IExecutableExtension { private static boolean DEBUG = false; // Print debug messages to console - private static boolean REUSE = true; // true: Reuse contribution items / false: delete items on dispose + private boolean REUSE = true; // true: Reuse contribution items (leave toolbar in disabled state when editor closes) + // false: delete items on dispose (remove toolbar editor closes) private static final String PLATFORM = "platform:/plugin/"; @@ -81,6 +86,8 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC MapList actions = new MapList(); CommandStateRegistry stateRegistry; + + IPartListener partListener; private Map menus = new HashMap(); @@ -88,6 +95,28 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class); stateRegistry = CommandStateRegistry.getInstance(); + // we need part listener to be notified for other editor activations (i.e editors that do not use this contributor). + partListener = new IPartListener() { + + @Override + public void partOpened(IWorkbenchPart part) {} + + @Override + public void partDeactivated(IWorkbenchPart part) {} + + @Override + public void partClosed(IWorkbenchPart part) {} + + @Override + public void partBroughtToTop(IWorkbenchPart part) {} + + @Override + public void partActivated(IWorkbenchPart part) { + if (part instanceof IEditorPart) + setContext2((IEditorPart)part); + } + }; + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(partListener); } @@ -106,7 +135,9 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC if ("toolbar".equals(key)) { toolbarId = value; - } + } else if ("hide".equals(key)) { + REUSE = !Boolean.parseBoolean(value); + } } } @@ -144,6 +175,26 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC if (DEBUG) System.out.println("Adding command to toolbar " +getToolbarId() + " " + ext); String commandId = ext.commandId; Command command = service.getCommand(commandId); + ICommandWrapper wrapper = new CommandWrapper(command); + + ParameterizedCommand parameterizedCommand = null; + + if (ext.parameters.size() > 0) { + try { + Parameterization parameterizations[] = new Parameterization[ext.parameters.size()]; + for (int i = 0; i < ext.parameters.size(); i++) { + Parameter param = ext.parameters.get(i); + IParameter parameter = command.getParameter(param.name); + parameterizations[i] = new Parameterization(parameter, param.value); + } + parameterizedCommand = new ParameterizedCommand(command, parameterizations); + wrapper = new ParameterizedCommandWrapper(parameterizedCommand); + } catch (org.eclipse.core.commands.common.NotDefinedException e) { + e.printStackTrace(); + ExceptionUtils.logError(e); + return; + } + } String type = ext.type; @@ -157,15 +208,15 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC CommandAction a = null; if (type.equals("toggle") && toggleState != null) { - a = new CommandCheckboxAction(command,name,image); + a = new CommandCheckboxAction(wrapper,name,image); stateRegistry.storeDefaultState(commandId); } else if (radioState != null && ext.value != null) { stateRegistry.storeDefaultState(commandId); if (type.equals("radio")) { - a = new CommandRadioAction(command,name,ext.value,image); + a = new CommandRadioAction(wrapper,name,ext.value,image); } else if (type.equals("combo")) { - a = new CommandRadioAction(command,name,ext.value,image); + a = new CommandRadioAction(wrapper,name,ext.value,image); ComboContribution combo = menus.get(commandId); if (REUSE && combo == null) { combo = (ComboContribution)mgr.find(commandId); @@ -190,7 +241,7 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC return; } } else if (type.equals("push")) { - a = new CommandPushAction(command,name,image); + a = new CommandPushAction(wrapper,name,image); } else { if (DEBUG) System.out.println(ext + " is not valid."); Logger.defaultLogError(ext + " is not valid."); @@ -267,6 +318,14 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC setContext((IEditorPart)part); } + private void setContext2(IEditorPart part) { + if (REUSE) + return; + if (this.activePart == part) + return; + setContext(null); + } + private void setContext(IEditorPart part) { this.activePart = part; if (activePart != null && !parts.contains(activePart)) { @@ -289,8 +348,9 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC a.setEnabled(false); } ComboContribution menu = menus.get(commandId); - if (menu != null) + if (menu != null) { menu.setEnabled(false); + } } } @@ -323,13 +383,16 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC actions.clear(); // without this the contributed toolbar widgets would continue reserve the space even when they are destroyed. - // TODO : how to make the toolbar fix its layout? - // Note: Using REUSE flag alleviates the problem, since the widgets are not removed. coolBarManager.update(true); + mgr.update(true); } CommandStateRegistry.getInstance().removeListener(this); super.dispose(); activePart = null; + if (partListener != null) { + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(partListener); + partListener = null; + } } boolean settingState = false; @@ -354,10 +417,12 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC private void restoreActionStates() { if (activePart == null) return; + if (DEBUG)System.out.println("Restore " + activePart); // toggles Map defaultToggleStates = stateRegistry.getDefaultToggleStates(); for (String commandId : defaultToggleStates.keySet()) { for (CommandAction a : actions.getValues(commandId)) { + if (DEBUG)System.out.println(commandId + " def " + defaultToggleStates.get(commandId)); a.setChecked(defaultToggleStates.get(commandId)); } } @@ -365,6 +430,7 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC if (editorStates != null) { for (String commandId : editorStates.keySet()) { for (CommandAction a : actions.getValues(commandId)) { + if (DEBUG)System.out.println(commandId + " " + editorStates.get(commandId)); a.setChecked(editorStates.get(commandId)); } } @@ -375,6 +441,7 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC String defaultValue = defaultRadioStates.get(commandId); for (CommandAction a : actions.getValues(commandId)) { CommandRadioAction r = (CommandRadioAction)a; + if (DEBUG)System.out.println(commandId + " def " + r.getValue().equals(defaultValue) +" " + r.getValue()); r.setChecked(r.getValue().equals(defaultValue)); } } @@ -385,6 +452,7 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC String defaultValue = editorRadioStates.get(commandId); for (CommandAction a : actions.getValues(commandId)) { CommandRadioAction r = (CommandRadioAction)a; + if (DEBUG)System.out.println(commandId + " " + r.getValue().equals(defaultValue) +" " + r.getValue()); r.setChecked(r.getValue().equals(defaultValue)); } } @@ -434,14 +502,28 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC return (Boolean)toggleState.getValue(); } - private abstract class CommandAction extends Action { + private interface ICommandWrapper { + + public Command getCommand(); + public String getCommandId(); + public void run(); + } + + private class CommandWrapper implements ICommandWrapper{ private Command command; - public CommandAction(Command command, String name, ImageDescriptor image, int style) { - super(name,style); + public CommandWrapper(Command command) { this.command = command; - if (image != null) - setImageDescriptor(image); + } + + @Override + public Command getCommand() { + return command; + } + + @Override + public String getCommandId() { + return command.getId(); } @Override @@ -453,35 +535,93 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC } } + @Override + public boolean equals(Object obj) { + if (obj.getClass() != getClass()) + return false; + CommandWrapper other= (CommandWrapper)obj; + return other.getCommandId().equals(getCommandId()); + } + } + + private class ParameterizedCommandWrapper implements ICommandWrapper{ + private ParameterizedCommand command; + + public ParameterizedCommandWrapper(ParameterizedCommand command) { + this.command = command; + } + + @Override public Command getCommand() { - return command; + return command.getCommand(); } + @Override public String getCommandId() { return command.getId(); } + @Override + public void run() { + try { + handlerService.executeCommand(command, null); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public boolean equals(Object obj) { + if (obj.getClass() != getClass()) + return false; + ParameterizedCommandWrapper other= (ParameterizedCommandWrapper)obj; + return other.command.equals(command); + } + } + + private abstract class CommandAction extends Action { + private ICommandWrapper command; + + public CommandAction(ICommandWrapper command, String name, ImageDescriptor image, int style) { + super(name,style); + this.command = command; + if (image != null) + setImageDescriptor(image); + } + + @Override + public void run() { + command.run(); + } + + public Command getCommand() { + return command.getCommand(); + } + + public String getCommandId() { + return command.getCommandId(); + } + @Override public boolean equals(Object obj) { if (obj.getClass() != getClass()) return false; CommandAction other= (CommandAction)obj; - if (!other.getCommandId().equals(getCommandId())) - return false; - return true; + return command.equals(other.command); } @Override public int hashCode() { - return command.getId().hashCode(); + return command.getCommandId().hashCode(); } } + private class CommandCheckboxAction extends CommandAction { - public CommandCheckboxAction(Command command, String name, ImageDescriptor image) { + public CommandCheckboxAction(ICommandWrapper command, String name, ImageDescriptor image) { super(command,name,image,Action.AS_CHECK_BOX); } @@ -505,7 +645,7 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC private String value; - public CommandRadioAction(Command command, String name, String value, ImageDescriptor image) { + public CommandRadioAction(ICommandWrapper command, String name, String value, ImageDescriptor image) { super(command,name,image,Action.AS_RADIO_BUTTON); this.value = value; } @@ -543,7 +683,7 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC private class CommandPushAction extends CommandAction { - public CommandPushAction(Command command, String name, ImageDescriptor image) { + public CommandPushAction(ICommandWrapper command, String name, ImageDescriptor image) { super(command,name,image,Action.AS_PUSH_BUTTON); } @@ -615,6 +755,12 @@ public class ToolbarContributor extends EditorActionBarContributor implements IC if (combo != null) combo.setEnabled(enabled); } + + @Override + public void dispose() { + combo.dispose(); + super.dispose(); + } } }