/**
  * Since these actions are re-created each time the run/debug as menu is filled, the enablement of
  * this action is static.
  */
 private void updateEnablement() {
   IWorkbenchWindow wb = DebugUIPlugin.getActiveWorkbenchWindow();
   boolean enabled = false;
   if (wb != null) {
     IWorkbenchPage page = wb.getActivePage();
     if (page != null) {
       ISelection selection = page.getSelection();
       if (selection instanceof IStructuredSelection) {
         IStructuredSelection structuredSelection = (IStructuredSelection) selection;
         try {
           // check enablement logic, if any
           Expression expression = fShortcut.getShortcutEnablementExpression();
           if (expression == null) {
             enabled = !structuredSelection.isEmpty();
           } else {
             List list = structuredSelection.toList();
             IEvaluationContext context = new EvaluationContext(null, list);
             context.addVariable("selection", list); // $NON-NLS-1$
             enabled = fShortcut.evalEnablementExpression(context, expression);
           }
         } catch (CoreException e) {
         }
       } else {
         IEditorPart editor = page.getActiveEditor();
         if (editor != null) {
           enabled = true;
         }
       }
     }
   }
   setEnabled(enabled);
 }
  public static IResource getSelectedResource(IEvaluationContext evaluationContext) {
    if (evaluationContext == null) {
      return null;
    }

    Object variable = evaluationContext.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
    if (variable instanceof IStructuredSelection) {
      Object selectedObject = ((IStructuredSelection) variable).getFirstElement();
      if (selectedObject instanceof IAdaptable) {
        IResource resource = (IResource) ((IAdaptable) selectedObject).getAdapter(IResource.class);
        if (resource != null) {
          return resource;
        }
      }
    } else {
      // checks the active editor
      variable = evaluationContext.getVariable(ISources.ACTIVE_EDITOR_NAME);
      if (variable instanceof IEditorPart) {
        IEditorInput editorInput = ((IEditorPart) variable).getEditorInput();
        if (editorInput instanceof IFileEditorInput) {
          return ((IFileEditorInput) editorInput).getFile();
        }
      }
    }
    return null;
  }
 /** 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 */
    }
  }
  /** {@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;
  }
 private static IStructuredSelection getActiveSelection() {
   IStructuredSelection selection = null;
   IEvaluationService evaluationService =
       (IEvaluationService) PlatformUI.getWorkbench().getService(IEvaluationService.class);
   if (evaluationService != null) {
     IEvaluationContext currentState = evaluationService.getCurrentState();
     Object variable = currentState.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
     if (variable instanceof IStructuredSelection) {
       selection = (IStructuredSelection) variable;
     }
   }
   return (selection == null) ? StructuredSelection.EMPTY : selection;
 }
  public static ExecutionEvent createExecutionEvent(
      final IGraphicalEditPart editPart, MouseEvent mouseEvent) {
    final IEvaluationContext context = new EvaluationContext(null, editPart);
    context.addVariable(
        ISources.ACTIVE_CURRENT_SELECTION_NAME,
        new StructuredSelection(Collections.singletonList(editPart)));

    final Map<String, String> parameters = new HashMap<String, String>();
    if (mouseEvent != null) {
      parameters.put("x", String.valueOf(mouseEvent.x));
      parameters.put("y", String.valueOf(mouseEvent.y));
    }

    return new ExecutionEvent(null, parameters, null, context);
  }
  @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 Object execute(final ExecutionEvent event) throws ExecutionException {
    final IEvaluationContext context = (IEvaluationContext) event.getApplicationContext();

    /* Get the map */
    final IWorkbenchWindow window =
        (IWorkbenchWindow) context.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
    final IWorkbenchPage activePage = window.getActivePage();

    /* set input to gtt tables */
    final IScenarioDataProvider dataProvider = KalypsoAFGUIFrameworkPlugin.getDataProvider();
    final IFolder scenarioFolder = (IFolder) dataProvider.getScenarioFolder();

    KalypsoCorePlugin.getDefault().getSelectionManager().clear();

    WorkflowHandlerUtils.setGttInput(
        activePage,
        "NaNodes",
        "urn:org.kalypso.model.rrm.resultOutputManagement:workflow:NaNodes:gtt",
        Messages.getString("ResultOutputManagementTaskHandler_0"),
        scenarioFolder); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    WorkflowHandlerUtils.setGttInput(
        activePage,
        "Catchments",
        "urn:org.kalypso.model.rrm.resultOutputManagement:workflow:Catchments:gtt",
        Messages.getString("ResultOutputManagementTaskHandler_1"),
        scenarioFolder); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    WorkflowHandlerUtils.setGttInput(
        activePage,
        "StorageChannels",
        "urn:org.kalypso.model.rrm.resultOutputManagement:workflow:StorageChannels:gtt",
        Messages.getString("ResultOutputManagementTaskHandler_2"),
        scenarioFolder); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    WorkflowHandlerUtils.setGftInput(
        activePage,
        "Outputs",
        "urn:org.kalypso.model.rrm.resultOutputManagement:workflow:Outputs:gft",
        Messages.getString("ResultOutputManagementTaskHandler_3"),
        scenarioFolder); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    return null;
  }
  @Override
  public void setEnabled(Object evaluationContext) {
    if (evaluationContext instanceof IEvaluationContext) {
      IEvaluationContext evalContext = (IEvaluationContext) evaluationContext;
      Object selection = evalContext.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
      if (selection instanceof ISelection) {
        IProject[] selectedProjects = getSelectedProjects((ISelection) selection);

        Repository repository = org.eclipse.flux.core.Activator.getDefault().getRepository();
        for (IProject project : selectedProjects) {
          if (repository.isConnected(project)) {
            setBaseEnabled(true);
            return;
          }
        }
      }
    }

    setBaseEnabled(false);
  }
  @Override
  public final EvaluationResult evaluate(final IEvaluationContext context) throws CoreException {
    final EvaluationResult result = super.evaluate(context);
    if (result == EvaluationResult.FALSE) {
      return result;
    }

    final Object defaultVariable = context.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
    if (defaultVariable instanceof ISelection) {
      final ISelection selection = (ISelection) defaultVariable;
      if (enabler.isEnabledForSelection(selection)) {
        return EvaluationResult.TRUE;
      }
    }

    return EvaluationResult.FALSE;
  }
 /* (non-Javadoc)
  * @see Expression#evaluate(IVariablePool)
  */
 public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
   if (fTypeName == null) return EvaluationResult.FALSE;
   Object var = context.getDefaultVariable();
   Object adapted = null;
   IAdapterManager manager = Platform.getAdapterManager();
   if (Expressions.isInstanceOf(var, fTypeName)) {
     adapted = var;
   } else {
     if (manager.hasAdapter(var, fTypeName)) {
       adapted = manager.getAdapter(var, fTypeName);
     } else {
       // if the adapter manager doesn't have an adapter contributed,
       // try to see if the variable itself implements IAdaptable
       if (var instanceof IAdaptable) {
         try {
           Class typeClazz = Class.forName(fTypeName, false, var.getClass().getClassLoader());
           adapted = ((IAdaptable) var).getAdapter(typeClazz);
         } catch (ClassNotFoundException e) {
         }
       }
       if (adapted == null) {
         // all attempts failed, return false
         return EvaluationResult.FALSE;
       }
     }
   }
   // the adapted result is null but hasAdapter returned true check
   // if the adapter is loaded.
   if (adapted == null) {
     if (manager.queryAdapter(var, fTypeName) == IAdapterManager.NOT_LOADED) {
       return EvaluationResult.NOT_LOADED;
     } else {
       return EvaluationResult.FALSE;
     }
   }
   return evaluateAnd(new DefaultVariable(context, adapted));
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.core.expressions.Expression#evaluate(org.eclipse.core
  * .expressions.IEvaluationContext)
  */
 public EvaluationResult evaluate(IEvaluationContext context) {
   Object v = context.getVariable(ISources.ACTIVE_PART_ID_NAME);
   return EvaluationResult.valueOf(partId.equals(v));
 }
 public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
   String variable = (String) context.getVariable("username");
   return lookFor.equals(variable) ? EvaluationResult.TRUE : EvaluationResult.FALSE;
 }