Search in sources :

Example 1 with EvaluationContext

use of org.eclipse.core.expressions.EvaluationContext in project che by eclipse.

the class ParticipantExtensionPoint method createEvaluationContext.

//---- Helper methods ------------------------------------------------------------------
private static EvaluationContext createEvaluationContext(RefactoringProcessor processor, Object element, String[] affectedNatures) {
    EvaluationContext result = new EvaluationContext(null, element);
    result.setAllowPluginActivation(true);
    //$NON-NLS-1$
    result.addVariable("element", element);
    //$NON-NLS-1$
    result.addVariable("affectedNatures", Arrays.asList(affectedNatures));
    //$NON-NLS-1$
    result.addVariable("processorIdentifier", processor.getIdentifier());
    return result;
}
Also used : EvaluationContext(org.eclipse.core.expressions.EvaluationContext)

Example 2 with EvaluationContext

use of org.eclipse.core.expressions.EvaluationContext in project che by eclipse.

the class ParticipantExtensionPoint method getParticipants.

/**
	 * Returns all participants for a given element.
	 *
	 * @param status a refactoring status to report status if problems occurred while
	 *  loading the participants
	 * @param processor the processor that will own the participants
	 * @param element the element to be copied or a corresponding descriptor
	 * @param arguments the arguments for the participants
	 * @param filter a participant filter to exclude certain participants, or <code>null</code>
	 *  if no filtering is desired
	 * @param affectedNatures an array of project natures affected by the refactoring
	 * @param shared a list of shared participants
	 *
	 * @return an array of participants
	 */
public RefactoringParticipant[] getParticipants(RefactoringStatus status, RefactoringProcessor processor, Object element, RefactoringArguments arguments, IParticipantDescriptorFilter filter, String[] affectedNatures, SharableParticipants shared) {
    if (fParticipants == null)
        init();
    EvaluationContext evalContext = createEvaluationContext(processor, element, affectedNatures);
    List result = new ArrayList();
    for (Iterator iter = fParticipants.iterator(); iter.hasNext(); ) {
        ParticipantDescriptor descriptor = (ParticipantDescriptor) iter.next();
        if (!descriptor.isEnabled()) {
            iter.remove();
        } else {
            try {
                RefactoringStatus filterStatus = new RefactoringStatus();
                if (descriptor.matches(evalContext, filter, filterStatus)) {
                    RefactoringParticipant participant = shared.get(descriptor);
                    if (participant != null) {
                        ((ISharableParticipant) participant).addElement(element, arguments);
                    } else {
                        participant = descriptor.createParticipant();
                        if (fParticipantClass.isInstance(participant)) {
                            if (participant.initialize(processor, element, arguments)) {
                                participant.setDescriptor(descriptor);
                                result.add(participant);
                                if (participant instanceof ISharableParticipant)
                                    shared.put(descriptor, participant);
                            }
                        } else {
                            status.addError(Messages.format(RefactoringCoreMessages.ParticipantExtensionPoint_participant_removed, descriptor.getName()));
                            RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.ParticipantExtensionPoint_wrong_type, new String[] { descriptor.getName(), fParticipantClass.getName() }));
                            iter.remove();
                        }
                    }
                } else {
                    status.merge(filterStatus);
                }
            } catch (CoreException e) {
                logMalfunctioningParticipant(status, descriptor, e);
                iter.remove();
            } catch (RuntimeException e) {
                logMalfunctioningParticipant(status, descriptor, e);
                iter.remove();
            }
        }
    }
    return (RefactoringParticipant[]) result.toArray(new RefactoringParticipant[result.size()]);
}
Also used : ParticipantDescriptor(org.eclipse.ltk.internal.core.refactoring.ParticipantDescriptor) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.eclipse.core.expressions.EvaluationContext)

Example 3 with EvaluationContext

use of org.eclipse.core.expressions.EvaluationContext in project translationstudio8 by heartsome.

the class ConverterCommandTrigger method openDialog.

private static void openDialog(IWorkbenchWindow window, IFile file, String commandId) {
    IWorkbench workbench = window.getWorkbench();
    ICommandService commandService = (ICommandService) workbench.getService(ICommandService.class);
    Command command = commandService.getCommand(commandId);
    try {
        EvaluationContext context = new EvaluationContext(null, IEvaluationContext.UNDEFINED_VARIABLE);
        context.addVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, window);
        if (file != null) {
            StructuredSelection selection = new StructuredSelection(file);
            context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
        }
        command.executeWithChecks(new ExecutionEvent(command, Collections.EMPTY_MAP, null, context));
    } catch (ExecutionException e) {
        LOGGER.error("", e);
    } catch (NotDefinedException e) {
        LOGGER.error("", e);
    } catch (NotEnabledException e) {
        LOGGER.error("", e);
    } catch (NotHandledException e) {
        LOGGER.error("", e);
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) NotHandledException(org.eclipse.core.commands.NotHandledException) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) ExecutionException(org.eclipse.core.commands.ExecutionException) NotEnabledException(org.eclipse.core.commands.NotEnabledException) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 4 with EvaluationContext

use of org.eclipse.core.expressions.EvaluationContext in project dbeaver by serge-rider.

the class ActionUtils method runCommand.

public static void runCommand(String commandId, ISelection selection, IServiceLocator serviceLocator) {
    if (commandId != null) {
        try {
            ICommandService commandService = serviceLocator.getService(ICommandService.class);
            IHandlerService handlerService = serviceLocator.getService(IHandlerService.class);
            if (commandService != null) {
                Command command = commandService.getCommand(commandId);
                boolean needContextPatch = false;
                if (selection != null) {
                    needContextPatch = true;
                    if (serviceLocator instanceof IWorkbenchSite) {
                        final ISelection curSelection = ((IWorkbenchSite) serviceLocator).getSelectionProvider().getSelection();
                        if (curSelection instanceof IStructuredSelection && selection instanceof IStructuredSelection) {
                            if (((IStructuredSelection) curSelection).size() == ((IStructuredSelection) selection).size() && ((IStructuredSelection) curSelection).getFirstElement() == ((IStructuredSelection) selection).getFirstElement()) {
                                // The same selection
                                needContextPatch = false;
                            }
                        }
                    }
                }
                if (selection != null && needContextPatch) {
                    // Create new eval context
                    IEvaluationContext context = new EvaluationContext(handlerService.createContextSnapshot(false), selection);
                    if (serviceLocator instanceof IWorkbenchPartSite) {
                        context.addVariable(ISources.ACTIVE_PART_NAME, ((IWorkbenchPartSite) serviceLocator).getPart());
                    }
                    context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
                    ParameterizedCommand pc = new ParameterizedCommand(command, null);
                    handlerService.executeCommandInContext(pc, null, context);
                } else if (command != null && command.isEnabled()) {
                    handlerService.executeCommand(commandId, null);
                }
            }
        } catch (Exception e) {
            log.error("Can't execute command '" + commandId + "'", e);
        }
    }
}
Also used : IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) ISelection(org.eclipse.jface.viewers.ISelection) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException)

Example 5 with EvaluationContext

use of org.eclipse.core.expressions.EvaluationContext in project eclipse.platform.text by eclipse.

the class CodeMiningProviderDescriptor method matches.

/**
 * Returns true if the given viewer, editor matches the enabledWhen expression
 * and false otherwise.
 *
 * @param viewer
 *            the viewer
 * @param editor
 *            the editor
 * @return true if the given viewer, editor matches the enabledWhen expression
 *         and false otherwise.
 */
public boolean matches(ISourceViewer viewer, ITextEditor editor) {
    EvaluationContext context = new EvaluationContext(null, editor);
    context.setAllowPluginActivation(true);
    // $NON-NLS-1$
    context.addVariable("viewer", viewer);
    // $NON-NLS-1$
    context.addVariable("editor", editor);
    // $NON-NLS-1$
    context.addVariable("editorInput", editor.getEditorInput());
    try {
        return fEnabledWhen.evaluate(context) == EvaluationResult.TRUE;
    } catch (CoreException e) {
        TextEditorPlugin.getDefault().getLog().log(// $NON-NLS-1$
        new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, "Error while 'enabledWhen' evaluation", e));
        return false;
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) EvaluationContext(org.eclipse.core.expressions.EvaluationContext)

Aggregations

EvaluationContext (org.eclipse.core.expressions.EvaluationContext)71 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)56 ArrayList (java.util.ArrayList)26 IterateExpression (org.eclipse.core.internal.expressions.IterateExpression)22 CountExpression (org.eclipse.core.internal.expressions.CountExpression)14 InstanceofExpression (org.eclipse.core.internal.expressions.InstanceofExpression)13 AdaptExpression (org.eclipse.core.internal.expressions.AdaptExpression)12 Command (org.eclipse.core.commands.Command)11 WithExpression (org.eclipse.core.internal.expressions.WithExpression)11 ICommandService (org.eclipse.ui.commands.ICommandService)11 EvaluationResult (org.eclipse.core.expressions.EvaluationResult)10 EqualsExpression (org.eclipse.core.internal.expressions.EqualsExpression)10 CoreException (org.eclipse.core.runtime.CoreException)10 EnablementExpression (org.eclipse.core.internal.expressions.EnablementExpression)9 TestExpression (org.eclipse.core.internal.expressions.TestExpression)9 IHandlerService (org.eclipse.ui.handlers.IHandlerService)9 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)8 Expression (org.eclipse.core.expressions.Expression)8 SystemTestExpression (org.eclipse.core.internal.expressions.SystemTestExpression)8 AndExpression (org.eclipse.core.internal.expressions.AndExpression)7