/** may return a single object, or a collection */
 public static Object getNavigationSelectionObject() {
   if (!PlatformUI.getWorkbench().hasService(IHandlerService.class)) return null;
   IHandlerService hs =
       (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
   IEvaluationContext ctx = hs.getCurrentState();
   if (ctx == null) return null;
   return ctx.getDefaultVariable();
 }
  @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 */
    }
  }
  @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 */
    }
  }
  @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);
      }
    }
  }
Пример #5
0
 private void mockSelection(ISelection selection) {
   EvaluationContext context = new EvaluationContext(null, new Object());
   context.addVariable(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
   when(handlerService.getCurrentState()).thenReturn(context);
 }