// the 'paste' key binding overrides the 'redo' key binding on Windows
  // platforms
  public void testPasteAndRedoBindingEmacs() throws Exception {
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    final Scheme emacsScheme = bindingService.getScheme(EMACS_SCHEME_ID);
    assertNotNull(emacsScheme);
    final Scheme defaultScheme =
        bindingService.getScheme(IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID);
    assertNotNull(defaultScheme);

    final Binding[] originalBindings = bindingService.getBindings();
    bindingService.savePreferences(emacsScheme, originalBindings);

    ParameterizedCommand pasteCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.EDIT_PASTE), null);
    ParameterizedCommand redoCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.EDIT_REDO), null);

    final KeySequence keyCtrlY = KeySequence.getInstance("CTRL+Y");

    final Binding pasteBinding = bindingService.getPerfectMatch(keyCtrlY);
    assertNotNull(pasteBinding);
    assertEquals(pasteCmd, pasteBinding.getParameterizedCommand());
    assertEquals(EMACS_SCHEME_ID, pasteBinding.getSchemeId());

    // reset the scheme
    bindingService.savePreferences(defaultScheme, originalBindings);
    final Binding redoBinding = bindingService.getPerfectMatch(keyCtrlY);
    assertNotNull(redoBinding);
    assertEquals(redoCmd, redoBinding.getParameterizedCommand());
    assertEquals(IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID, redoBinding.getSchemeId());
  }
Ejemplo n.º 2
0
  public void testTwoHandlers() throws Exception {
    createHandlerActivation(C1_ID, H1, new String[] {ISources.ACTIVE_CONTEXT_NAME});
    createHandlerActivation(
        C2_ID, H2, new String[] {ISources.ACTIVE_CONTEXT_NAME, ISources.ACTIVE_ACTION_SETS_NAME});

    Command cmd = commandService.getCommand(CMD_ID);
    assertTrue("Command should be defined", cmd.isDefined());

    assertFalse("Should not be handled yet", cmd.isHandled());
    IContextActivation activationC1 = activateContext(C1_ID);
    assertTrue("Should definitely be handled", cmd.isHandled());

    ActTestHandler handler1 = (ActTestHandler) testHandlers.get(H1);
    int count1 = handler1.executionCount;
    cmd.executeWithChecks(new ExecutionEvent());
    assertEquals("The handler count should be incremented", count1 + 1, handler1.executionCount);

    activateContext(C2_ID);
    assertTrue("Should still be handled", cmd.isHandled());

    ActTestHandler handler2 = (ActTestHandler) testHandlers.get(H2);
    int count2 = handler2.executionCount;
    count1 = handler1.executionCount;
    cmd.executeWithChecks(new ExecutionEvent());
    assertEquals("The handler1 count should not be incremented", count1, handler1.executionCount);
    assertEquals("The handler2 count should be incremented", count2 + 1, handler2.executionCount);

    contextService.deactivateContext(activationC1);
    assertTrue("Will still be handled", cmd.isHandled());
  }
  void createCommand(String commandId, Map parameters) {
    if (commandId == null) {
      WorkbenchPlugin.log(
          "Unable to create menu item \""
              + getId() //$NON-NLS-1$
              + "\", no command id"); //$NON-NLS-1$
      return;
    }
    Command cmd = commandService.getCommand(commandId);
    if (!cmd.isDefined()) {
      WorkbenchPlugin.log(
          "Unable to create menu item \""
              + getId() //$NON-NLS-1$
              + "\", command \""
              + commandId
              + "\" not defined"); //$NON-NLS-1$ //$NON-NLS-2$
      return;
    }

    if (parameters == null || parameters.size() == 0) {
      command = new ParameterizedCommand(cmd, null);
      return;
    }

    try {
      ArrayList parmList = new ArrayList();
      Iterator i = parameters.entrySet().iterator();
      while (i.hasNext()) {
        Map.Entry entry = (Map.Entry) i.next();
        String parmName = (String) entry.getKey();
        IParameter parm;
        parm = cmd.getParameter(parmName);
        if (parm == null) {
          WorkbenchPlugin.log(
              "Unable to create menu item \""
                  + getId() //$NON-NLS-1$
                  + "\", parameter \""
                  + parmName
                  + "\" for command \"" //$NON-NLS-1$ //$NON-NLS-2$
                  + commandId
                  + "\" is not defined"); //$NON-NLS-1$
          return;
        }
        parmList.add(new Parameterization(parm, (String) entry.getValue()));
      }
      command =
          new ParameterizedCommand(
              cmd, (Parameterization[]) parmList.toArray(new Parameterization[parmList.size()]));
    } catch (NotDefinedException e) {
      // this shouldn't happen as we checked for !defined, but we
      // won't take the chance
      WorkbenchPlugin.log(
          "Failed to create menu item " //$NON-NLS-1$
              + getId(),
          e);
    }
  }
  @Override
  public void execute(String newText) throws CoreException {

    if (newText.isEmpty()) {
      if (getModel().getText() != null) {
        EnquiryTranslationRemoveCommand removeCmd = new EnquiryTranslationRemoveCommand(getModel());
        removeCmd.execute(newText);
      }
      return;
    }
    if (newText.equals(getModel().getText())) {
      // do nothing if text is the same.
      return;
    }

    IEditorPart editorPart = getActiveEditorPart();
    if (editorPart != null) {

      // Extract useful parameters needed for the command
      IWorkbenchPartSite site = editorPart.getSite();

      // Invoke the command
      IHandlerService handlerService = (IHandlerService) site.getService(IHandlerService.class);
      ICommandService commandService = (ICommandService) site.getService(ICommandService.class);

      CommandStack commandStack = (CommandStack) editorPart.getAdapter(CommandStack.class);

      try {

        // Get the command and assign values to parameters
        Command command = commandService.getCommand(EDIT_COMMAND_ID);
        Parameterization parameter = new Parameterization(command.getParameter("text"), newText);
        ParameterizedCommand parmCommand =
            new ParameterizedCommand(command, new Parameterization[] {parameter});

        // Prepare the evaluation context
        IEvaluationContext ctx = handlerService.getCurrentState();
        ctx.addVariable("model", getModel());
        ctx.addVariable("commandStack", commandStack);

        // Execute the command
        handlerService.executeCommandInContext(parmCommand, null, ctx);

      } catch (Exception ex) {
        IStatus status =
            new Status(
                IStatus.ERROR,
                Activator.PLUGIN_ID,
                "Error while executing command to update translation",
                ex);
        throw new CoreException(status);
      }
    } else {
      /** @TODO log warn no active editorPart, should never occur */
    }
  }
  /** {@inheritDoc} */
  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    StructuredSelection selection =
        (StructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
    AbstractRootEditor rootEditor = (AbstractRootEditor) HandlerUtil.getActiveEditor(event);
    RepositoryDefinition repositoryDefinition =
        rootEditor.getInputDefinition().getRepositoryDefinition();

    Object selectedObject = selection.getFirstElement();
    ExceptionSensorData dataToNavigateTo = null;
    if (selectedObject instanceof ExceptionSensorData) {
      dataToNavigateTo = (ExceptionSensorData) selectedObject;
    } else if (selectedObject instanceof InvocationSequenceData) {
      List<ExceptionSensorData> exceptions =
          ((InvocationSequenceData) selectedObject).getExceptionSensorDataObjects();
      if ((null != exceptions) && !exceptions.isEmpty()) {
        for (ExceptionSensorData exSensorData : exceptions) {
          if (0 != exSensorData.getMethodIdent()) {
            dataToNavigateTo = exSensorData;
            break;
          }
        }
      }
    }

    if (null != dataToNavigateTo) {
      ExceptionSensorData exceptionSensorData = dataToNavigateTo;

      // exit if the object does not carry the methodIdent
      if (null == exceptionSensorData.getThrowableType()) {
        return null;
      }

      InputDefinition inputDefinition =
          getInputDefinition(repositoryDefinition, exceptionSensorData);

      // open the view via command
      IHandlerService handlerService =
          (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
      ICommandService commandService =
          (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);

      Command command = commandService.getCommand(OpenViewHandler.COMMAND);
      ExecutionEvent executionEvent = handlerService.createExecutionEvent(command, new Event());
      IEvaluationContext context = (IEvaluationContext) executionEvent.getApplicationContext();
      context.addVariable(OpenViewHandler.INPUT, inputDefinition);

      try {
        command.executeWithChecks(executionEvent);
      } catch (NotDefinedException | NotEnabledException | NotHandledException e) {
        throw new ExecutionException("Error opening the exception type view.", e);
      }
    }
    return null;
  }
Ejemplo n.º 6
0
  public static boolean isShowPlanNames() {
    ICommandService commandService =
        (ICommandService)
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);

    Command command = commandService.getCommand("at.ac.tuwien.ieg.asbruedit.show-planname-toggle");

    State state = command.getState("org.eclipse.ui.commands.toggleState");

    boolean isToggled = (Boolean) state.getValue();
    return isToggled;
  }
Ejemplo n.º 7
0
 public void run() {
   ICommandService commandService =
       (ICommandService) this.cheatSheetCatalogView.getSite().getService(ICommandService.class);
   IStructuredSelection structuredSelection =
       (IStructuredSelection) this.cheatSheetCatalogView.getViewer().getSelection();
   CheatSheetReference cheatSheetReference =
       (CheatSheetReference) structuredSelection.getFirstElement();
   if (cheatSheetReference.getUrl().startsWith("platform:/")) {
     Command command = commandService.getCommand("org.eclipse.ui.cheatsheets.openCheatSheet");
     Map<String, String> map = new HashMap<String, String>();
     map.put("cheatSheetId", cheatSheetReference.getUrl().substring("platform:/".length()));
     try {
       command.executeWithChecks(new ExecutionEvent(command, map, null, null));
     } catch (Exception e) {
       ErrorDialog errorDialog =
           new ErrorDialog(
               "Impossible to open Cheat Sheet",
               "An error occured while trying to open cheat sheet.",
               e);
       errorDialog.open();
     }
   } else {
     Command command = commandService.getCommand("org.eclipse.ui.cheatsheets.openCheatSheetURL");
     Map<String, String> map = new HashMap<String, String>();
     map.put("cheatSheetId", cheatSheetReference.getId());
     map.put("name", cheatSheetReference.getName());
     map.put("url", cheatSheetReference.getUrl());
     try {
       command.executeWithChecks(new ExecutionEvent(command, map, null, null));
     } catch (Exception e) {
       ErrorDialog errorDialog =
           new ErrorDialog(
               "Impossible to open Cheat Sheet",
               "An error occured while trying to open cheat sheet.",
               e);
       errorDialog.open();
     }
   }
 }
Ejemplo n.º 8
0
 /** Alert whoever is listening that a new OPIShell has been created. */
 private static void sendUpdateCommand() {
   IServiceLocator serviceLocator = PlatformUI.getWorkbench();
   ICommandService commandService =
       (ICommandService) serviceLocator.getService(ICommandService.class);
   try {
     Command command = commandService.getCommand(OPI_SHELLS_CHANGED_ID);
     command.executeWithChecks(new ExecutionEvent());
   } catch (ExecutionException
       | NotHandledException
       | NotEnabledException
       | NotDefinedException e) {
     log.log(Level.WARNING, "Failed to send OPI shells changed command", e);
   }
 }
Ejemplo n.º 9
0
  public void testForNegativeNumber() throws Exception {
    String c5_id = C_PREFIX + ISources.ACTIVE_MENU_NAME;
    String h5 = C_PREFIX + "h5";
    createHandlerActivation(
        c5_id, h5, new String[] {ISources.ACTIVE_CONTEXT_NAME, ISources.ACTIVE_MENU_NAME});
    createHandlerActivation(C1_ID, H1, new String[] {ISources.ACTIVE_CONTEXT_NAME});

    Command cmd = commandService.getCommand(CMD_ID);

    activateContext(C1_ID);
    activateContext(c5_id);

    assertHandlerIsExecuted(cmd, h5);
  }
  public void testAboutBindingIn3x() throws Exception {
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    final Scheme activeScheme = bindingService.getActiveScheme();

    ParameterizedCommand aboutCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.HELP_ABOUT), null);
    ParameterizedCommand activateEditorCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR), null);

    final KeySequence keyF12 = KeySequence.getInstance("F12");
    final Binding editorBinding = bindingService.getPerfectMatch(keyF12);
    assertNotNull(editorBinding);
    assertEquals(activateEditorCmd, editorBinding.getParameterizedCommand());

    EBindingService ebs = (EBindingService) fWorkbench.getService(EBindingService.class);
    HashMap<String, String> attrs = new HashMap<String, String>();
    attrs.put(EBindingService.TYPE_ATTR_TAG, "user");
    final Binding localAboutBinding =
        ebs.createBinding(keyF12, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding.getType());

    final Binding[] bindings = bindingService.getBindings();
    Binding[] added = new Binding[bindings.length + 1];
    System.arraycopy(bindings, 0, added, 0, bindings.length);

    added[bindings.length] = localAboutBinding;
    bindingService.savePreferences(activeScheme, added);

    final Binding secondMatch = bindingService.getPerfectMatch(keyF12);
    // fails
    assertNotNull(secondMatch);
    assertEquals(aboutCmd, secondMatch.getParameterizedCommand());
  }
  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);
  }
Ejemplo n.º 12
0
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.ui.tests.harness.util.UITestCase#doSetUp()
   */
  protected void doSetUp() throws Exception {
    for (int i = 0; i < CREATE_CONTEXTS.length; i++) {
      final String[] contextInfo = CREATE_CONTEXTS[i];
      final Context context = contextService.getContext(contextInfo[0]);
      if (!context.isDefined()) {
        context.define(contextInfo[1], contextInfo[2], contextInfo[3]);
      }
    }

    Command cmd = commandService.getCommand(CMD_ID);
    if (!cmd.isDefined()) {
      Category cat = commandService.getCategory(CATEGORY_ID);
      cmd.define("Test Handler", "Test handler activation", cat);
    }
  }
  @Override
  public void execute(String newText) throws CoreException {

    IEditorPart editorPart = getActiveEditorPart();
    if (editorPart != null) {

      // Extract useful parameters needed for the command
      IWorkbenchPartSite site = editorPart.getSite();

      // Invoke the command
      IHandlerService handlerService = (IHandlerService) site.getService(IHandlerService.class);
      ICommandService commandService = (ICommandService) site.getService(ICommandService.class);

      if (editorPart instanceof VersionMultiPageEditor) {
        VersionMultiPageEditor vEditor = (VersionMultiPageEditor) editorPart;
        EditingDomain editingDomain = vEditor.getVersionFormEditor().getEditingDomain();
        CommandStack commandStack = editingDomain.getCommandStack();

        try {

          // Get the command and assign values to parameters
          Command command = commandService.getCommand(getCommandId());
          ParameterizedCommand parmCommand =
              new ParameterizedCommand(command, new Parameterization[] {});

          // Prepare the evaluation context
          IEvaluationContext ctx = handlerService.getCurrentState();
          ctx.addVariable("model", getModel());
          ctx.addVariable("commandStack", commandStack);
          ctx.addVariable("editingDomain", editingDomain);

          // Execute the command
          handlerService.executeCommandInContext(parmCommand, null, ctx);

        } catch (Exception ex) {
          IStatus status =
              new Status(
                  IStatus.ERROR,
                  VersionEditorActivator.PLUGIN_ID,
                  "Error while executing command to remove translation",
                  ex);
          throw new CoreException(status);
        }
      }
    } else {
      /** @TODO log warn no active editorPart, should never occur */
    }
  }
Ejemplo n.º 14
0
 public static void executeCommand(IViewSite site, String command) {
   ICommandService cmdService = (ICommandService) site.getService(ICommandService.class);
   IHandlerService hdlService = (IHandlerService) site.getService(IHandlerService.class);
   Command cmd = cmdService.getCommand(command);
   try {
     hdlService.executeCommand(cmd.getId(), null);
   } catch (ExecutionException e) {
     e.printStackTrace();
   } catch (NotDefinedException e) {
     e.printStackTrace();
   } catch (NotEnabledException e) {
     e.printStackTrace();
   } catch (NotHandledException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 15
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);
  }
  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;
      }
    }
  }
Ejemplo n.º 17
0
  /**
   * @throws ExecutionException
   * @throws NotDefinedException
   * @throws NotEnabledException
   * @throws NotHandledException
   */
  private void doTestForBreak()
      throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
    Command cmd = commandService.getCommand(CMD_ID);
    assertTrue("Command should be defined", cmd.isDefined());

    assertFalse("Should not be handled yet", cmd.isHandled());

    IContextActivation c1 = activateContext(C1_ID);
    IContextActivation c2 = activateContext(C2_ID);
    IContextActivation c3 = activateContext(C3_ID);

    assertTrue("Should still be handled", cmd.isHandled());

    assertHandlerIsExecuted(cmd, H3);

    contextService.deactivateContext(c3);
    contextService.deactivateContext(c2);
    contextService.deactivateContext(c1);
  }
  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);
    }
  }
  private void addSave(IMenuManager menu) {
    ICommandService commandService =
        (ICommandService) fContainer.getWorkbenchPart().getSite().getService(ICommandService.class);
    final Command command = commandService.getCommand(IWorkbenchCommandConstants.FILE_SAVE);

    final IHandler handler = command.getHandler();
    if (handler != null) {
      if (fSaveContributionItem == null) {
        fSaveContributionItem =
            new CommandContributionItem(
                new CommandContributionItemParameter(
                    fContainer.getWorkbenchPart().getSite(),
                    null,
                    command.getId(),
                    CommandContributionItem.STYLE_PUSH));
      }
      // save is an editable dependent action, ie add only when edit
      // is possible
      if (handler.isHandled() && getSourceViewer().isEditable()) menu.add(fSaveContributionItem);
    }
  }
Ejemplo n.º 20
0
  public void testBasicHandler() throws Exception {

    createHandlerActivation(C1_ID, H1, new String[] {ISources.ACTIVE_CONTEXT_NAME});

    Command cmd = commandService.getCommand(CMD_ID);
    assertTrue("Command should be defined", cmd.isDefined());

    assertFalse("Should not be handled yet", cmd.isHandled());

    IContextActivation activationC1 = activateContext(C1_ID);
    assertTrue("Should definitely be handled", cmd.isHandled());

    ActTestHandler handler1 = (ActTestHandler) testHandlers.get(H1);
    int count = handler1.executionCount;
    cmd.executeWithChecks(new ExecutionEvent());
    assertEquals("The handler count should be correct", count + 1, handler1.executionCount);

    contextService.deactivateContext(activationC1);

    assertFalse("Should not be handled", cmd.isHandled());
  }
  @Override
  public void launch(final IEditorPart editor, final String mode) {
    assert mode.equals("run"); // $NON-NLS-1$

    final IWorkbenchPart workbenchPart = UIAccess.getActiveWorkbenchPart(false);
    final IWorkbenchPartSite site = workbenchPart.getSite();
    if (site != null) {
      final IHandlerService handlerService =
          (IHandlerService) site.getService(IHandlerService.class);
      final ICommandService commandService =
          (ICommandService) site.getService(ICommandService.class);

      final Map<String, String> parameters = new HashMap<String, String>();
      parameters.put(RCodeLaunching.FILE_COMMAND_ID_PARAMTER_ID, fFileCommandId);
      final Command command =
          commandService.getCommand(
              !fGotoConsole
                  ? RCodeLaunching.RUN_FILEVIACOMMAND_COMMAND_ID
                  : RCodeLaunching.RUN_FILEVIACOMMAND_GOTOCONSOLE_COMMAND_ID);
      final ExecutionEvent executionEvent =
          new ExecutionEvent(command, parameters, null, handlerService.getCurrentState());
      if (!editor.equals(HandlerUtil.getActivePart(executionEvent))) {
        LaunchShortcutUtil.handleUnsupportedExecution(executionEvent);
        return;
      }
      try {
        command.executeWithChecks(executionEvent);
      } catch (final ExecutionException e) {
        LaunchShortcutUtil.handleRLaunchException(
            e, RLaunchingMessages.RScriptLaunch_error_message, executionEvent);
      } catch (final NotDefinedException e) {
        LaunchShortcutUtil.handleUnsupportedExecution(executionEvent);
      } catch (final NotEnabledException e) {
        LaunchShortcutUtil.handleUnsupportedExecution(executionEvent);
      } catch (final NotHandledException e) {
        LaunchShortcutUtil.handleUnsupportedExecution(executionEvent);
      }
    }
  }
  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;
      }
    }
  }
Ejemplo n.º 24
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);
    }
  }
  public void testAboutBinding() throws Exception {
    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    final Scheme activeScheme = bindingService.getActiveScheme();
    final Binding[] originalBindings = bindingService.getBindings();

    ParameterizedCommand aboutCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.HELP_ABOUT), null);
    ParameterizedCommand activateEditorCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR), null);

    final KeySequence keyF12 = KeySequence.getInstance("F12");
    final KeySequence keyAltCtrlShiftI = KeySequence.getInstance("ALT+CTRL+SHIFT+I");
    final Binding editorBinding = bindingService.getPerfectMatch(keyF12);
    assertNotNull(editorBinding);
    assertEquals(activateEditorCmd, editorBinding.getParameterizedCommand());

    EBindingService ebs = (EBindingService) fWorkbench.getService(EBindingService.class);
    HashMap<String, String> attrs = new HashMap<String, String>();
    attrs.put(EBindingService.TYPE_ATTR_TAG, "user");
    final Binding localAboutBinding =
        ebs.createBinding(keyF12, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding.getType());

    // test unbinding a system binding and adding a user binding (same
    // triggers and context)
    final Binding[] bindings = originalBindings;
    Binding[] added = new Binding[bindings.length + 2];
    System.arraycopy(bindings, 0, added, 0, bindings.length);

    Binding del =
        new KeyBinding(
            keyF12,
            null,
            IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID,
            IContextService.CONTEXT_ID_WINDOW,
            null,
            null,
            null,
            Binding.USER);
    added[bindings.length] = del;
    added[bindings.length + 1] = localAboutBinding;
    bindingService.savePreferences(activeScheme, added);

    // the match should be the user binding that we just added
    final Binding secondMatch = bindingService.getPerfectMatch(keyF12);
    assertNotNull(secondMatch);
    assertEquals(aboutCmd, secondMatch.getParameterizedCommand());

    // go back to the defaults
    bindingService.savePreferences(activeScheme, originalBindings);
    final Binding thirdMatch = bindingService.getPerfectMatch(keyF12);
    assertNotNull(thirdMatch);
    assertEquals(activateEditorCmd, thirdMatch.getParameterizedCommand());

    // try assigning alt-ctrl-shift-i (no other binding uses this for this
    // context) to the 'about' command
    final Binding localAboutBinding1 =
        ebs.createBinding(keyAltCtrlShiftI, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertEquals(Binding.USER, localAboutBinding1.getType());
    Binding[] added1 = new Binding[bindings.length + 1];
    System.arraycopy(bindings, 0, added1, 0, bindings.length);
    added1[bindings.length] = localAboutBinding1;

    bindingService.savePreferences(activeScheme, added1);
    final Binding fourthMatch = bindingService.getPerfectMatch(keyAltCtrlShiftI);
    assertNotNull(fourthMatch);
    assertEquals(aboutCmd, fourthMatch.getParameterizedCommand());
  }
  public void testAboutBindingEmacs() throws Exception {

    ICommandService commandService = (ICommandService) fWorkbench.getAdapter(ICommandService.class);
    IBindingService bindingService = (IBindingService) fWorkbench.getAdapter(IBindingService.class);

    final Scheme emacsScheme = bindingService.getScheme(EMACS_SCHEME_ID);
    assertNotNull(emacsScheme);
    final Binding[] originalBindings = bindingService.getBindings();
    bindingService.savePreferences(emacsScheme, originalBindings);

    ParameterizedCommand findAndReplaceCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE), null);
    ParameterizedCommand aboutCmd =
        new ParameterizedCommand(
            commandService.getCommand(IWorkbenchCommandConstants.HELP_ABOUT), null);

    final KeySequence keyAltR = KeySequence.getInstance("ALT+R");
    final KeySequence keyAltCtrlShiftI = KeySequence.getInstance("ALT+CTRL+SHIFT+I");
    final Binding findAndReplaceBinding = bindingService.getPerfectMatch(keyAltR);

    assertNotNull(findAndReplaceBinding);
    assertEquals(findAndReplaceCmd, findAndReplaceBinding.getParameterizedCommand());
    assertEquals(EMACS_SCHEME_ID, findAndReplaceBinding.getSchemeId());

    EBindingService ebs = (EBindingService) fWorkbench.getService(EBindingService.class);
    HashMap<String, String> attrs = new HashMap<String, String>();
    attrs.put(EBindingService.TYPE_ATTR_TAG, "user");
    attrs.put(EBindingService.SCHEME_ID_ATTR_TAG, EMACS_SCHEME_ID);
    final Binding localAboutBinding =
        ebs.createBinding(keyAltR, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertNotNull(localAboutBinding);
    assertEquals(Binding.USER, localAboutBinding.getType());
    assertEquals(EMACS_SCHEME_ID, localAboutBinding.getSchemeId());

    final Binding[] bindings = originalBindings;
    Binding[] added = new Binding[bindings.length + 2];
    System.arraycopy(bindings, 0, added, 0, bindings.length);

    Binding del =
        new KeyBinding(
            keyAltR,
            null,
            EMACS_SCHEME_ID,
            IContextService.CONTEXT_ID_WINDOW,
            null,
            null,
            null,
            Binding.USER);
    added[bindings.length] = del;
    added[bindings.length + 1] = localAboutBinding;
    bindingService.savePreferences(emacsScheme, added);

    // the match should be the user binding that we just added
    final Binding secondMatch = bindingService.getPerfectMatch(keyAltR);
    assertNotNull(secondMatch);
    assertEquals(aboutCmd, secondMatch.getParameterizedCommand());

    // go back to the defaults
    bindingService.savePreferences(emacsScheme, originalBindings);
    final Binding thirdMatch = bindingService.getPerfectMatch(keyAltR);
    assertNotNull(thirdMatch);
    assertEquals(findAndReplaceCmd, thirdMatch.getParameterizedCommand());

    // try assigning alt-ctrl-shift-i (no other binding uses this for this
    // context) to the 'about' command
    final Binding localAboutBinding1 =
        ebs.createBinding(keyAltCtrlShiftI, aboutCmd, IContextService.CONTEXT_ID_WINDOW, attrs);
    assertNotNull(localAboutBinding1);
    assertEquals(Binding.USER, localAboutBinding1.getType());
    assertEquals(EMACS_SCHEME_ID, localAboutBinding.getSchemeId());

    Binding[] added1 = new Binding[bindings.length + 1];
    System.arraycopy(bindings, 0, added1, 0, bindings.length);
    added1[bindings.length] = localAboutBinding1;

    bindingService.savePreferences(emacsScheme, added1);
    final Binding fourthMatch = bindingService.getPerfectMatch(keyAltCtrlShiftI);
    assertNotNull(fourthMatch);
    assertEquals(aboutCmd, fourthMatch.getParameterizedCommand());
    assertEquals(EMACS_SCHEME_ID, fourthMatch.getSchemeId());
  }
Ejemplo n.º 27
0
 private void defineCommand(final String commandId, final String name, AbstractHandler handler) {
   Command command = commandService.getCommand(commandId);
   command.define(name, null, autogeneratedCategory);
   command.setHandler(handler);
 }