/** * Tests whether the preference store will be read automatically when a change to the preference * store is made. * * @throws ParseException If "ALT+SHIFT+Q A" cannot be parsed by KeySequence. */ public final void testAutoLoad() throws ParseException { // Get the services. ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class); IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class); bindingService.readRegistryAndPreferences(commandService); // Check the pre-conditions. final String emacsSchemeId = EMACS_SCHEME_ID; assertFalse( "The active scheme should be Emacs yet", emacsSchemeId.equals(bindingService.getActiveScheme().getId())); final KeySequence formalKeySequence = KeySequence.getInstance("ALT+SHIFT+Q A"); final String commandId = "org.eclipse.ui.views.showView"; Binding[] bindings = bindingService.getBindings(); int i; for (i = 0; i < bindings.length; i++) { final Binding binding = bindings[i]; if ((binding.getType() == Binding.USER) && (formalKeySequence.equals(binding.getTriggerSequence()))) { final ParameterizedCommand command = binding.getParameterizedCommand(); final String actualCommandId = (command == null) ? null : command.getCommand().getId(); assertFalse("The command should not yet be bound", commandId.equals(actualCommandId)); break; } } assertEquals("There shouldn't be a matching command yet", bindings.length, i); // Modify the preference store. final IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore(); store.setValue( "org.eclipse.ui.commands", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><org.eclipse.ui.commands><activeKeyConfiguration keyConfigurationId=\"" + emacsSchemeId + "\"/><keyBinding commandId=\"" + commandId + "\" contextId=\"org.eclipse.ui.contexts.window\" keyConfigurationId=\"org.eclipse.ui.defaultAcceleratorConfiguration\" keySequence=\"" + formalKeySequence + "\"/></org.eclipse.ui.commands>"); // Check that the values have changed. assertEquals( "The active scheme should now be Emacs", emacsSchemeId, bindingService.getActiveScheme().getId()); bindings = bindingService.getBindings(); for (i = 0; i < bindings.length; i++) { final Binding binding = bindings[i]; if ((binding.getType() == Binding.USER) && (formalKeySequence.equals(binding.getTriggerSequence()))) { final ParameterizedCommand command = binding.getParameterizedCommand(); final String actualCommandId = (command == null) ? null : command.getCommand().getId(); assertEquals("The command should be bound to 'ALT+SHIFT+Q A'", commandId, actualCommandId); break; } } assertFalse("There should be a matching command now", (bindings.length == i)); }
@Override public void commandChanged(CommandEvent commandEvent) { myItem.setEnabled(myCommand.getCommand().isEnabled()); try { myItem.setToolTipText(myCommand.getCommand().getDescription()); } catch (final NotDefinedException ex) { LogUtils.error(this, ex); } }
public final String getDescription() { try { return command.getCommand().getDescription(); } catch (final NotDefinedException e) { return null; } }
public final void runWithEvent(final Event event) { final Command baseCommand = command.getCommand(); final ExecutionEvent executionEvent = new ExecutionEvent(command.getCommand(), command.getParameterMap(), event, null); try { baseCommand.execute(executionEvent); firePropertyChange(IAction.RESULT, null, Boolean.TRUE); } catch (final NotHandledException e) { firePropertyChange(IAction.RESULT, null, Boolean.FALSE); } catch (final ExecutionException e) { firePropertyChange(IAction.RESULT, null, Boolean.FALSE); // TODO Should this be logged? } }
public final boolean isChecked() { final State state = command.getCommand().getState(IMenuStateIds.STYLE); if (state instanceof ToggleState) { final Boolean currentValue = (Boolean) state.getValue(); return currentValue.booleanValue(); } return false; }
public final void setText(final String text) { final State state = command.getCommand().getState(INamedHandleStateIds.NAME); if (state instanceof TextState) { final String currentValue = (String) state.getValue(); if (!Util.equals(text, currentValue)) { state.setValue(text); } } }
public final int getStyle() { // TODO Pulldown. This does not currently support the pulldown style. final State state = command.getCommand().getState(IMenuStateIds.STYLE); if (state instanceof RadioState) { return IAction.AS_RADIO_BUTTON; } else if (state instanceof ToggleState) { return IAction.AS_CHECK_BOX; } return IAction.AS_PUSH_BUTTON; }
public final void setChecked(final boolean checked) { final State state = command.getCommand().getState(IMenuStateIds.STYLE); if (state instanceof ToggleState) { final Boolean currentValue = (Boolean) state.getValue(); if (checked != currentValue.booleanValue()) { if (checked) { state.setValue(Boolean.TRUE); } else { state.setValue(Boolean.FALSE); } } } }
/** * Fill in a temporary static context for execution. * * @param command * @return a context not part of the normal hierarchy */ private void addParms(ParameterizedCommand command, IEclipseContext staticContext) { final Map parms = command.getParameterMap(); Iterator i = parms.entrySet().iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); String parameterId = (String) entry.getKey(); staticContext.set( parameterId, convertParameterValue(command.getCommand(), parameterId, (String) entry.getValue())); } staticContext.set(PARM_MAP, parms); staticContext.set(ParameterizedCommand.class, command); }
/* * (non-Javadoc) * * @see org.eclipse.jface.action.ContributionItem#dispose() */ public void dispose() { if (elementRef != null) { commandService.unregisterElement(elementRef); elementRef = null; } if (commandListener != null) { command.getCommand().removeCommandListener(commandListener); commandListener = null; } command = null; commandService = null; disposeOldImages(); super.dispose(); }
public boolean canExecute(ParameterizedCommand command, IEclipseContext staticContext) { final IEclipseContext executionContext = getExecutionContext(); addParms(command, staticContext); // executionContext.set(STATIC_CONTEXT, staticContext); push(executionContext, staticContext); try { Command cmd = command.getCommand(); cmd.setEnabled(new ExpressionContext(peek().context)); return cmd.isEnabled(); } finally { pop(); // executionContext.remove(STATIC_CONTEXT); } }
private CommandToolItemAdapter(String commandId) { myCommandId = commandId; myItem = new ToolItem(getToolBar(), SWT.PUSH); try { myCommand = myCommandService.deserialize(commandId); myCommand.getCommand().addCommandListener(this); myItem.setImage(getImage(ICommandImageService.TYPE_DEFAULT)); myItem.setDisabledImage(getImage(ICommandImageService.TYPE_DISABLED)); myItem.setHotImage(getImage(ICommandImageService.TYPE_HOVER)); commandChanged(null); } catch (final NotDefinedException ex) { LogUtils.error(this, ex); } catch (final SerializationException ex) { LogUtils.error(this, ex); } myItem.addListener(SWT.Dispose, this); myItem.addListener(SWT.Selection, this); }
/** * Constructs a new instance of <code>ActionProxy</code>. * * @param id The initial action identifier; may be <code>null</code>. * @param command The command with which this action is associated; must not be <code>null</code>. * @param style The image style to use for this action, may be <code>null</code>. * @param serviceLocator A service locator that can be used to find various command-based * services; must not be <code>null</code>. */ public CommandLegacyActionWrapper( final String id, final ParameterizedCommand command, final String style, final IServiceLocator serviceLocator) { if (command == null) { throw new NullPointerException( "An action proxy can't be created without a command"); //$NON-NLS-1$ } if (serviceLocator == null) { throw new NullPointerException( "An action proxy can't be created without a service locator"); //$NON-NLS-1$ } this.command = command; this.id = id; this.style = style; this.serviceLocator = serviceLocator; // TODO Needs to listen to command, state, binding and image changes. command.getCommand().addCommandListener(commandListener); }
public final boolean isEnabled() { final Command baseCommand = command.getCommand(); return baseCommand.isEnabled() && enabled; }
private void dispose() { myCommand.getCommand().removeCommandListener(this); }
/** * Create a CommandContributionItem to place in a ContributionManager. * * @param serviceLocator a service locator that is most appropriate for this contribution. * Typically the local {@link IWorkbenchWindow} or {@link IWorkbenchPartSite} will be * sufficient. * @param id The id for this item. May be <code>null</code>. Items without an id cannot be * referenced later. * @param commandId A command id for a defined command. Must not be <code>null</code>. * @param parameters A map of strings to strings which represent parameter names to values. The * parameter names must match those in the command definition. * @param icon An icon for this item. May be <code>null</code>. * @param disabledIcon A disabled icon for this item. May be <code>null</code>. * @param hoverIcon A hover icon for this item. May be <code>null</code>. * @param label A label for this item. May be <code>null</code>. * @param mnemonic A mnemonic for this item to be applied to the label. May be <code>null</code>. * @param tooltip A tooltip for this item. May be <code>null</code>. Tooltips are currently only * valid for toolbar contributions. * @param style The style of this menu contribution. See the STYLE_* contants. */ public CommandContributionItem( IServiceLocator serviceLocator, String id, String commandId, Map parameters, ImageDescriptor icon, ImageDescriptor disabledIcon, ImageDescriptor hoverIcon, String label, String mnemonic, String tooltip, int style) { super(id); this.icon = icon; this.disabledIcon = disabledIcon; this.hoverIcon = hoverIcon; this.label = label; this.mnemonic = mnemonic; this.tooltip = tooltip; this.style = style; menuService = (IMenuService) serviceLocator.getService(IMenuService.class); commandService = (ICommandService) serviceLocator.getService(ICommandService.class); handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class); // bindingService = (IBindingService) serviceLocator // .getService(IBindingService.class); createCommand(commandId, parameters); if (command != null) { try { UIElement callback = new UIElement(serviceLocator) { public void setChecked(boolean checked) { CommandContributionItem.this.setChecked(checked); } public void setDisabledIcon(ImageDescriptor desc) { CommandContributionItem.this.setDisabledIcon(desc); } public void setHoverIcon(ImageDescriptor desc) { CommandContributionItem.this.setHoverIcon(desc); } public void setIcon(ImageDescriptor desc) { CommandContributionItem.this.setIcon(desc); } public void setText(String text) { CommandContributionItem.this.setText(text); } public void setTooltip(String text) { CommandContributionItem.this.setTooltip(text); } public void setDropDownId(String id) { dropDownMenuOverride = id; } }; elementRef = commandService.registerElementForCommand(command, callback); command.getCommand().addCommandListener(getCommandListener()); setImages(serviceLocator); } catch (NotDefinedException e) { WorkbenchPlugin.log( "Unable to register menu item \"" + getId() //$NON-NLS-1$ + "\", command \"" + commandId + "\" not defined"); //$NON-NLS-1$ //$NON-NLS-2$ } } }
/* * (non-Javadoc) * * @see org.eclipse.jface.action.ContributionItem#update(java.lang.String) */ public void update(String id) { if (widget != null) { if (widget instanceof MenuItem) { MenuItem item = (MenuItem) widget; String text = label; if (text == null) { if (command != null) { try { text = command.getCommand().getName(); } catch (NotDefinedException e) { WorkbenchPlugin.log( "Update item failed " //$NON-NLS-1$ + getId(), e); } } } // TODO: [bm] key bindings // text = updateMnemonic(text); // // String keyBindingText = null; // if (command != null) { // TriggerSequence[] bindings = bindingService // .getActiveBindingsFor(command); // if (bindings.length > 0) { // keyBindingText = bindings[0].format(); // } // } // if (text != null) { // if (keyBindingText == null) { item.setText(text); // } else { // item.setText(text + '\t' + keyBindingText); // } // } updateIcons(); if (item.getSelection() != checkedState) { item.setSelection(checkedState); } boolean shouldBeEnabled = isEnabled(); if (item.getEnabled() != shouldBeEnabled) { item.setEnabled(shouldBeEnabled); } } else if (widget instanceof ToolItem) { ToolItem item = (ToolItem) widget; if (icon != null) { updateIcons(); } else if (label != null) { item.setText(label); } if (tooltip != null) item.setToolTipText(tooltip); else { String text = label; if (text == null) { if (command != null) { try { text = command.getCommand().getName(); } catch (NotDefinedException e) { WorkbenchPlugin.log( "Update item failed " //$NON-NLS-1$ + getId(), e); } } } if (text != null) { item.setToolTipText(text); } } if (item.getSelection() != checkedState) { item.setSelection(checkedState); } boolean shouldBeEnabled = isEnabled(); if (item.getEnabled() != shouldBeEnabled) { item.setEnabled(shouldBeEnabled); } } } }
/* * (non-Javadoc) * * @see org.eclipse.jface.action.ContributionItem#isEnabled() */ public boolean isEnabled() { if (command != null) { return command.getCommand().isEnabled(); } return false; }
public final void setActionDefinitionId(final String id) { // Get the old values. final boolean oldChecked = isChecked(); final String oldDescription = getDescription(); final boolean oldEnabled = isEnabled(); final boolean oldHandled = isHandled(); final ImageDescriptor oldDefaultImage = getImageDescriptor(); final ImageDescriptor oldDisabledImage = getDisabledImageDescriptor(); final ImageDescriptor oldHoverImage = getHoverImageDescriptor(); final String oldText = getText(); // Update the command. final Command oldBaseCommand = command.getCommand(); oldBaseCommand.removeCommandListener(commandListener); final ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class); final Command newBaseCommand = commandService.getCommand(id); command = new ParameterizedCommand(newBaseCommand, null); newBaseCommand.addCommandListener(commandListener); // Get the new values. final boolean newChecked = isChecked(); final String newDescription = getDescription(); final boolean newEnabled = isEnabled(); final boolean newHandled = isHandled(); final ImageDescriptor newDefaultImage = getImageDescriptor(); final ImageDescriptor newDisabledImage = getDisabledImageDescriptor(); final ImageDescriptor newHoverImage = getHoverImageDescriptor(); final String newText = getText(); // Fire property change events, as necessary. if (newChecked != oldChecked) { if (oldChecked) { firePropertyChange(IAction.CHECKED, Boolean.TRUE, Boolean.FALSE); } else { firePropertyChange(IAction.CHECKED, Boolean.FALSE, Boolean.TRUE); } } if (!Util.equals(oldDescription, newDescription)) { firePropertyChange(IAction.DESCRIPTION, oldDescription, newDescription); firePropertyChange(IAction.TOOL_TIP_TEXT, oldDescription, newDescription); } if (newEnabled != oldEnabled) { if (oldEnabled) { firePropertyChange(IAction.ENABLED, Boolean.TRUE, Boolean.FALSE); } else { firePropertyChange(IAction.ENABLED, Boolean.FALSE, Boolean.TRUE); } } if (newHandled != oldHandled) { if (oldHandled) { firePropertyChange(IAction.HANDLED, Boolean.TRUE, Boolean.FALSE); } else { firePropertyChange(IAction.HANDLED, Boolean.FALSE, Boolean.TRUE); } } if (!Util.equals(oldDefaultImage, newDefaultImage)) { firePropertyChange(IAction.IMAGE, oldDefaultImage, newDefaultImage); } if (!Util.equals(oldDisabledImage, newDisabledImage)) { firePropertyChange(IAction.IMAGE, oldDisabledImage, newDisabledImage); } if (!Util.equals(oldHoverImage, newHoverImage)) { firePropertyChange(IAction.IMAGE, oldHoverImage, newHoverImage); } if (!Util.equals(oldText, newText)) { firePropertyChange(IAction.TEXT, oldText, newText); } }
public final boolean isHandled() { final Command baseCommand = command.getCommand(); return baseCommand.isHandled(); }