Search in sources :

Example 41 with IEvaluationContext

use of org.eclipse.core.expressions.IEvaluationContext in project eclipse.platform.runtime by eclipse.

the class ExpressionTests method testIterateExpressionAndTrue.

public void testIterateExpressionAndTrue() throws Exception {
    final List<Object> result = new ArrayList<>();
    Expression myExpression = new Expression() {

        @Override
        public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
            result.add(context.getDefaultVariable());
            return EvaluationResult.TRUE;
        }
    };
    // $NON-NLS-1$
    IterateExpression exp = new IterateExpression("and");
    exp.add(myExpression);
    List<String> input = new ArrayList<>();
    // $NON-NLS-1$
    input.add("one");
    // $NON-NLS-1$
    input.add("two");
    EvaluationContext context = new EvaluationContext(null, input);
    assertTrue(EvaluationResult.TRUE == exp.evaluate(context));
    assertTrue(result.equals(input));
}
Also used : AndExpression(org.eclipse.core.internal.expressions.AndExpression) SystemTestExpression(org.eclipse.core.internal.expressions.SystemTestExpression) ResolveExpression(org.eclipse.core.internal.expressions.ResolveExpression) OrExpression(org.eclipse.core.internal.expressions.OrExpression) Expression(org.eclipse.core.expressions.Expression) IterateExpression(org.eclipse.core.internal.expressions.IterateExpression) EnablementExpression(org.eclipse.core.internal.expressions.EnablementExpression) InstanceofExpression(org.eclipse.core.internal.expressions.InstanceofExpression) CountExpression(org.eclipse.core.internal.expressions.CountExpression) WithExpression(org.eclipse.core.internal.expressions.WithExpression) NotExpression(org.eclipse.core.internal.expressions.NotExpression) AdaptExpression(org.eclipse.core.internal.expressions.AdaptExpression) EqualsExpression(org.eclipse.core.internal.expressions.EqualsExpression) TestExpression(org.eclipse.core.internal.expressions.TestExpression) ArrayList(java.util.ArrayList) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IterateExpression(org.eclipse.core.internal.expressions.IterateExpression) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext)

Example 42 with IEvaluationContext

use of org.eclipse.core.expressions.IEvaluationContext in project dbeaver by dbeaver.

the class DebugUIInternals method createShortcutActions.

public static Map<IAction, String> createShortcutActions(Object[] selected, String mode, int accelerator) {
    Map<IAction, String> result = new LinkedHashMap<IAction, String>();
    if (selected == null) {
        return result;
    }
    List<Object> selection = Arrays.asList(selected);
    IEvaluationContext context = DebugUIPlugin.createEvaluationContext(selection);
    context.setAllowPluginActivation(true);
    // $NON-NLS-1$
    context.addVariable("selection", selection);
    List<LaunchShortcutExtension> allShortCuts = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getLaunchShortcuts();
    List<LaunchShortcutExtension> filteredShortCuts = new ArrayList<LaunchShortcutExtension>();
    Iterator<LaunchShortcutExtension> iter = allShortCuts.iterator();
    while (iter.hasNext()) {
        LaunchShortcutExtension ext = iter.next();
        if (WorkbenchActivityHelper.filterItem(ext)) {
            continue;
        }
        try {
            Expression expr = ext.getContextualLaunchEnablementExpression();
            if (ext.evalEnablementExpression(context, expr)) {
                filteredShortCuts.add(ext);
            }
        } catch (CoreException e) {
            IStatus status = new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), // $NON-NLS-1$
            "Launch shortcut '" + ext.getId() + "' enablement expression caused exception. Shortcut was removed.", // $NON-NLS-1$
            e);
            DebugUIPlugin.log(status);
            iter.remove();
        }
    }
    for (LaunchShortcutExtension ext : filteredShortCuts) {
        for (String supported : ext.getModes()) {
            if (supported.equals(mode)) {
                LaunchShortcutAction action = new LaunchShortcutAction(supported, ext);
                // $NON-NLS-1$
                action.setActionDefinitionId(ext.getId() + "." + supported);
                String helpContextId = ext.getHelpContextId();
                if (helpContextId != null) {
                    PlatformUI.getWorkbench().getHelpSystem().setHelp(action, helpContextId);
                }
                StringBuffer label = new StringBuffer();
                if (accelerator >= 0 && accelerator < 10) {
                    // add the numerical accelerator
                    label.append('&');
                    label.append(accelerator);
                    label.append(' ');
                }
                String contextLabel = ext.getContextLabel(supported);
                // replace default action label with context label if
                // specified.
                label.append((contextLabel != null) ? contextLabel : action.getText());
                action.setText(label.toString());
                String category = ext.getCategory();
                result.put(action, category);
                accelerator++;
            }
        }
    }
    return result;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) IAction(org.eclipse.jface.action.IAction) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) LaunchShortcutExtension(org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension) LaunchShortcutAction(org.eclipse.debug.internal.ui.actions.LaunchShortcutAction) CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.core.expressions.Expression)

Example 43 with IEvaluationContext

use of org.eclipse.core.expressions.IEvaluationContext in project archi by archimatetool.

the class ShowToolbarAction method isVisible.

private boolean isVisible() {
    boolean isVisible = false;
    IEvaluationService service = PlatformUI.getWorkbench().getService(IEvaluationService.class);
    IEvaluationContext appState = service.getCurrentState();
    Object coolbar = appState.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME);
    if (coolbar instanceof Boolean) {
        isVisible = ((Boolean) coolbar).booleanValue();
    }
    return isVisible;
}
Also used : IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IEvaluationService(org.eclipse.ui.services.IEvaluationService)

Example 44 with IEvaluationContext

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

the class DebugUIInternals method createShortcutActions.

public static Map<IAction, String> createShortcutActions(Object[] selected, String mode, int accelerator) {
    Map<IAction, String> result = new LinkedHashMap<IAction, String>();
    if (selected == null) {
        return result;
    }
    List<Object> selection = Arrays.asList(selected);
    IEvaluationContext context = DebugUIPlugin.createEvaluationContext(selection);
    context.setAllowPluginActivation(true);
    // $NON-NLS-1$
    context.addVariable("selection", selection);
    List<LaunchShortcutExtension> allShortCuts = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getLaunchShortcuts();
    List<LaunchShortcutExtension> filteredShortCuts = new ArrayList<LaunchShortcutExtension>();
    Iterator<LaunchShortcutExtension> iter = allShortCuts.iterator();
    while (iter.hasNext()) {
        LaunchShortcutExtension ext = iter.next();
        if (WorkbenchActivityHelper.filterItem(ext)) {
            continue;
        }
        try {
            Expression expr = ext.getContextualLaunchEnablementExpression();
            if (ext.evalEnablementExpression(context, expr)) {
                filteredShortCuts.add(ext);
            }
        } catch (CoreException e) {
            IStatus status = new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), // $NON-NLS-1$
            "Launch shortcut '" + ext.getId() + "' enablement expression caused exception. Shortcut was removed.", // $NON-NLS-1$
            e);
            DebugUIPlugin.log(status);
            iter.remove();
        }
    }
    for (LaunchShortcutExtension ext : filteredShortCuts) {
        for (String supported : ext.getModes()) {
            if (supported.equals(mode)) {
                LaunchShortcutAction action = new LaunchShortcutAction(supported, ext);
                // $NON-NLS-1$
                action.setActionDefinitionId(ext.getId() + "." + supported);
                String helpContextId = ext.getHelpContextId();
                if (helpContextId != null) {
                    PlatformUI.getWorkbench().getHelpSystem().setHelp(action, helpContextId);
                }
                StringBuffer label = new StringBuffer();
                if (accelerator >= 0 && accelerator < 10) {
                    // add the numerical accelerator
                    label.append('&');
                    label.append(accelerator);
                    label.append(' ');
                }
                String contextLabel = ext.getContextLabel(supported);
                // replace default action label with context label if
                // specified.
                label.append((contextLabel != null) ? contextLabel : action.getText());
                action.setText(label.toString());
                String category = ext.getCategory();
                result.put(action, category);
                accelerator++;
            }
        }
    }
    return result;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) IAction(org.eclipse.jface.action.IAction) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) LaunchShortcutExtension(org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension) LaunchShortcutAction(org.eclipse.debug.internal.ui.actions.LaunchShortcutAction) CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.core.expressions.Expression)

Example 45 with IEvaluationContext

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

the class ActionUtils method runCommand.

public static void runCommand(String commandId, ISelection selection, Map<String, Object> parameters, 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;
                            }
                        }
                    }
                }
                Parameterization[] parametrization = null;
                if (!CommonUtils.isEmpty(parameters)) {
                    parametrization = new Parameterization[parameters.size()];
                    int paramIndex = 0;
                    for (Map.Entry<String, Object> param : parameters.entrySet()) {
                        IParameter parameter = command.getParameter(param.getKey());
                        if (parameter != null) {
                            parametrization[paramIndex] = new Parameterization(parameter, CommonUtils.toString(param.getValue()));
                        } else {
                            log.debug("Parameter '" + param.getKey() + "' not found in command '" + commandId + "'");
                            parametrization[paramIndex] = null;
                        }
                        paramIndex++;
                    }
                }
                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, parametrization);
                    handlerService.executeCommandInContext(pc, null, context);
                } else if (command != null) {
                    if (command.isEnabled()) {
                        ParameterizedCommand pc = new ParameterizedCommand(command, parametrization);
                        handlerService.executeCommand(pc, null);
                    } else {
                        log.warn("Command '" + commandId + "' is disabled");
                    }
                } else {
                    log.warn("Command '" + commandId + "' not found");
                }
            }
        } catch (Exception e) {
            DBWorkbench.getPlatformUI().showError("Error running command", "Can't execute command '" + commandId + "'", e);
        }
    }
}
Also used : IParameter(org.eclipse.core.commands.IParameter) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Parameterization(org.eclipse.core.commands.Parameterization) ICommandService(org.eclipse.ui.commands.ICommandService) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) 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) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) Map(java.util.Map) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Aggregations

IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)49 ICommandService (org.eclipse.ui.commands.ICommandService)22 Command (org.eclipse.core.commands.Command)21 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)19 IHandlerService (org.eclipse.ui.handlers.IHandlerService)19 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)11 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)11 ISelection (org.eclipse.jface.viewers.ISelection)10 ExecutionException (org.eclipse.core.commands.ExecutionException)9 Expression (org.eclipse.core.expressions.Expression)9 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)7 CoreException (org.eclipse.core.runtime.CoreException)7 ArrayList (java.util.ArrayList)6 AdaptExpression (org.eclipse.core.internal.expressions.AdaptExpression)6 IterateExpression (org.eclipse.core.internal.expressions.IterateExpression)6 WithExpression (org.eclipse.core.internal.expressions.WithExpression)6 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)5 AndExpression (org.eclipse.core.internal.expressions.AndExpression)5 CountExpression (org.eclipse.core.internal.expressions.CountExpression)5 EnablementExpression (org.eclipse.core.internal.expressions.EnablementExpression)5