示例#1
0
 @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);
   }
 }
  /**
   * 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));
  }
 private void setImages(IServiceLocator locator) {
   if (icon == null) {
     ICommandImageService service =
         (ICommandImageService) locator.getService(ICommandImageService.class);
     icon = service.getImageDescriptor(command.getId(), ICommandImageService.TYPE_DEFAULT);
     disabledIcon =
         service.getImageDescriptor(command.getId(), ICommandImageService.TYPE_DISABLED);
     hoverIcon = service.getImageDescriptor(command.getId(), ICommandImageService.TYPE_HOVER);
   }
 }
 /**
  * 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);
 }
  public final void testBindingTransform() throws Exception {
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    ParameterizedCommand addWS =
        new ParameterizedCommand(
            commandService.getCommand("org.eclipse.ui.navigate.addToWorkingSet"), null);
    KeySequence m18w = KeySequence.getInstance("M1+8 W");
    KeySequence m28w = KeySequence.getInstance("M2+8 W");
    boolean foundDeleteMarker = false;
    int numOfMarkers = 0;
    Binding[] bindings = bindingService.getBindings();
    for (int i = 0; i < bindings.length; i++) {
      final Binding binding = bindings[i];
      if (binding.getType() == Binding.SYSTEM) {
        String platform = binding.getPlatform();
        int idx = (platform == null ? -1 : platform.indexOf(','));
        assertEquals(binding.toString(), -1, idx);
        if (addWS.equals(binding.getParameterizedCommand())) {
          if (m18w.equals(binding.getTriggerSequence())) {
            numOfMarkers++;
            assertNull(m18w.format(), binding.getPlatform());
          } else if (m28w.equals(binding.getTriggerSequence())) {
            numOfMarkers++;
            assertTrue(
                platform,
                Util.WS_CARBON.equals(platform)
                    || Util.WS_COCOA.equals(platform)
                    || Util.WS_GTK.equals(platform)
                    || Util.WS_WIN32.equals(platform));
          }
        } else if (binding.getParameterizedCommand() == null
            && m18w.equals(binding.getTriggerSequence())) {
          assertTrue(
              platform,
              Util.WS_CARBON.equals(platform)
                  || Util.WS_COCOA.equals(platform)
                  || Util.WS_GTK.equals(platform)
                  || Util.WS_WIN32.equals(platform));
          numOfMarkers++;
          foundDeleteMarker = true;
        }
      }
    }
    assertEquals(3, numOfMarkers);
    assertTrue("Unable to find delete marker", foundDeleteMarker);
    TriggerSequence[] activeBindingsFor = bindingService.getActiveBindingsFor(addWS);
    assertEquals(1, activeBindingsFor.length);
  }
 public final String getText() {
   try {
     return command.getName();
   } catch (final NotDefinedException e) {
     return null;
   }
 }
 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 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 void testModifierNotApplied() throws Exception {

    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);
    ParameterizedCommand exportCmd =
        new ParameterizedCommand(commandService.getCommand("org.eclipse.ui.file.export"), null);
    Binding[] bindings = bindingService.getBindings();
    for (int i = 0; i < bindings.length; i++) {
      final Binding binding = bindings[i];
      if (binding.getType() != Binding.SYSTEM) continue;

      if (exportCmd.equals(binding.getParameterizedCommand())) {
        // make sure the modifier is NOT applied
        assertEquals(KeySequence.getInstance("M1+8 E"), binding.getTriggerSequence());
        break;
      }
    }
  }
示例#11
0
  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;
  }
示例#12
0
  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 testSinglePlatform() throws Exception {
    // Get the services.
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    ParameterizedCommand about =
        new ParameterizedCommand(
            commandService.getCommand("org.eclipse.ui.help.aboutAction"), null);
    KeySequence m18A = KeySequence.getInstance("M1+8 A");
    KeySequence m18B = KeySequence.getInstance("M1+8 B");
    int numAboutBindings = 0;

    Binding[] bindings = bindingService.getBindings();
    for (int i = 0; i < bindings.length; i++) {
      final Binding binding = bindings[i];
      if (binding.getType() == Binding.SYSTEM) {
        String platform = binding.getPlatform();
        int idx = (platform == null ? -1 : platform.indexOf(','));
        assertEquals(binding.toString(), -1, idx);
        if (about.equals(binding.getParameterizedCommand())) {
          if (m18A.equals(binding.getTriggerSequence())) {
            numAboutBindings++;
            assertNull("M+8 A", binding.getPlatform());
          } else if (m18B.equals(binding.getTriggerSequence())) {
            numAboutBindings++;
            // assertEquals(Util.WS_CARBON, binding.getPlatform());
            // temp work around for honouring carbon bindings
            assertTrue(
                "failure for platform: " + binding.getPlatform(),
                Util.WS_CARBON.equals(binding.getPlatform())
                    || Util.WS_COCOA.equals(binding.getPlatform()));
          }
        }
      }
    }
    if (Util.WS_CARBON.equals(SWT.getPlatform()) || Util.WS_COCOA.equals(SWT.getPlatform())) {
      assertEquals(2, numAboutBindings);
    } else {
      assertEquals(1, numAboutBindings);
    }
  }
  public void testModifierWithPlatform() throws Exception {

    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);
    ParameterizedCommand importCmd =
        new ParameterizedCommand(commandService.getCommand("org.eclipse.ui.file.import"), null);
    Binding[] bindings = bindingService.getBindings();
    int numOfMarkers = 0;
    for (int i = 0; i < bindings.length; i++) {
      final Binding binding = bindings[i];
      if (binding.getType() != Binding.SYSTEM) continue;

      if (importCmd.equals(binding.getParameterizedCommand())) {
        // make sure the modifier is applied
        assertEquals(KeySequence.getInstance("M2+8 I"), binding.getTriggerSequence());
        numOfMarkers++;
      }
    }

    // only one binding, if the platform matches
    assertEquals(numOfMarkers, 1);
  }
  public void testDifferentPlatform() throws Exception {

    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);
    ParameterizedCommand backCmd =
        new ParameterizedCommand(commandService.getCommand("org.eclipse.ui.navigate.back"), null);
    Binding[] bindings = bindingService.getBindings();
    for (int i = 0; i < bindings.length; i++) {
      final Binding binding = bindings[i];
      if (binding.getType() != Binding.SYSTEM) continue;

      if (backCmd.equals(binding.getParameterizedCommand())) {
        // make sure the modifier is NOT applied
        // this will fail on Photon (but Paul thinks we'll never run the
        // test suite on that platform :-)
        assertEquals(KeySequence.getInstance("M1+8 Q"), binding.getTriggerSequence());
        // and the platform should be null
        assertNull(binding.getPlatform());
        break;
      }
    }
  }
示例#16
0
 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);
       }
     }
   }
 }
 /*
  * (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);
   }
 }
 protected void setItemText(MMenuItem model, MenuItem item) {
   String text = model.getLocalizedLabel();
   if (model instanceof MHandledItem) {
     MHandledItem handledItem = (MHandledItem) model;
     IEclipseContext context = getContext(model);
     EBindingService bs = (EBindingService) context.get(EBindingService.class.getName());
     ParameterizedCommand cmd = handledItem.getWbCommand();
     if (cmd != null && (text == null || text.length() == 0)) {
       try {
         text = cmd.getName();
       } catch (NotDefinedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
     TriggerSequence sequence = bs.getBestSequenceFor(handledItem.getWbCommand());
     if (sequence != null) {
       text = text + '\t' + sequence.format();
     }
     item.setText(text == null ? handledItem.getCommand().getElementId() : text);
   } else {
     super.setItemText(model, item);
   }
 }
示例#20
0
  private void openSample(String file) throws CommandException {
    IHandlerService handlerService =
        (IHandlerService) getIntroSite().getService(IHandlerService.class);
    ICommandService commandService =
        (ICommandService) getIntroSite().getService(ICommandService.class);

    Command command =
        commandService.getCommand(
            "name.abuchen.portfolio.ui.commands.openSampleCommand"); //$NON-NLS-1$

    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("name.abuchen.portfolio.ui.param.sample", file); // $NON-NLS-1$
    ParameterizedCommand parameterized = ParameterizedCommand.generateCommand(command, parameters);
    handlerService.executeCommand(parameterized, null);
    PlatformUI.getWorkbench().getIntroManager().closeIntro(this);
  }
示例#21
0
    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);
    }
 public Object executeHandler(ParameterizedCommand command, IEclipseContext staticContext) {
   final IEclipseContext executionContext = getExecutionContext();
   addParms(command, staticContext);
   // executionContext.set(STATIC_CONTEXT, staticContext);
   push(executionContext, staticContext);
   try {
     // Command cmd = command.getCommand();
     return command.executeWithChecks(null, new ExpressionContext(peek().context));
   } catch (ExecutionException e) {
     staticContext.set(HANDLER_EXCEPTION, e);
   } catch (NotDefinedException e) {
     staticContext.set(HANDLER_EXCEPTION, e);
   } catch (NotEnabledException e) {
     staticContext.set(HANDLER_EXCEPTION, e);
   } catch (NotHandledException e) {
     staticContext.set(HANDLER_EXCEPTION, e);
   } finally {
     pop();
     // executionContext.remove(STATIC_CONTEXT);
   }
   return null;
 }
示例#23
0
  /**
   * 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);
  }
  /*
   * (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);
        }
      }
    }
  }
示例#25
0
 private void dispose() {
   myCommand.getCommand().removeCommandListener(this);
 }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.jface.action.ContributionItem#isEnabled()
  */
 public boolean isEnabled() {
   if (command != null) {
     return command.getCommand().isEnabled();
   }
   return false;
 }
示例#27
0
 public final String getActionDefinitionId() {
   return command.getId();
 }
示例#28
0
 public final boolean isEnabled() {
   final Command baseCommand = command.getCommand();
   return baseCommand.isEnabled() && enabled;
 }
示例#29
0
 public final boolean isHandled() {
   final Command baseCommand = command.getCommand();
   return baseCommand.isHandled();
 }
示例#30
0
  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);
    }
  }