Search in sources :

Example 36 with IEvaluationContext

use of org.eclipse.core.expressions.IEvaluationContext in project liferay-ide by liferay.

the class UIUtil method executeCommand.

public static void executeCommand(String commandId, ISelection selection, Map<String, Object> parameters) throws ExecutionException, NotDefinedException, NotEnabledException, NotHandledException {
    IEvaluationContext evaluationContext = new EvaluationContext(null, Collections.emptyList());
    evaluationContext.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
    IWorkbench workbench = PlatformUI.getWorkbench();
    ICommandService commandService = (ICommandService) workbench.getService(ICommandService.class);
    Command migrate = commandService.getCommand(commandId);
    IHandlerService handlerService = (IHandlerService) workbench.getService(IHandlerService.class);
    if (parameters != null) {
        parameters.keySet().stream().forEach(parma -> evaluationContext.addVariable(parma, parameters.get(parma)));
    }
    handlerService.executeCommandInContext(ParameterizedCommand.generateCommand(migrate, null), null, evaluationContext);
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 37 with IEvaluationContext

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

the class ShowEditorsPulldownMenuAction method createEditorsMenu.

/**
 * Creates the menu.
 *
 * @param parent
 * @param menu
 * @return
 */
private static Menu createEditorsMenu(Control parent, Menu menu) {
    if (menu == null) {
        menu = new Menu(parent);
        IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(// $NON-NLS-1$
        "org.jcryptool.core.operations.editorServices");
        Comparator<IConfigurationElement> comp = new Comparator<IConfigurationElement>() {

            public int compare(IConfigurationElement o1, IConfigurationElement o2) {
                // $NON-NLS-1$
                String l1 = o1.getAttribute("label");
                // $NON-NLS-1$
                String l2 = o2.getAttribute("label");
                // $NON-NLS-1$
                String c1 = o1.getAttribute("category");
                // $NON-NLS-1$
                String c2 = o2.getAttribute("category");
                int cat = c1.compareTo(c2);
                int label = l1.compareTo(l2);
                if (cat != 0) {
                    return cat;
                } else {
                    if (// $NON-NLS-1$ //$NON-NLS-2$
                    o1.getAttribute("id").contains("org.jcryptool.editor.text") && !o2.getAttribute("id").contains("org.jcryptool.editor.text")) {
                        return -1;
                    }
                    if (// $NON-NLS-1$ //$NON-NLS-2$
                    !o1.getAttribute("id").contains("org.jcryptool.editor.text") && o2.getAttribute("id").contains("org.jcryptool.editor.text")) {
                        return 1;
                    }
                    if (label != 0) {
                        return label;
                    } else {
                        return o2.hashCode() - o1.hashCode();
                    }
                }
            }
        };
        Set<IConfigurationElement> entries = new TreeSet<IConfigurationElement>(comp);
        for (IExtension extension : point.getExtensions()) {
            for (IConfigurationElement element : extension.getConfigurationElements()) {
                entries.add(element);
            }
        }
        // $NON-NLS-1$
        String currentCat = entries.size() > 0 ? entries.iterator().next().getAttribute("category") : null;
        for (IConfigurationElement element : entries) {
            if (!currentCat.equals(element.getAttribute("category"))) {
                // $NON-NLS-1$
                new MenuItem(menu, SWT.SEPARATOR);
                // $NON-NLS-1$
                currentCat = element.getAttribute("category");
            }
            final MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
            // Set the Labels to the entries
            // $NON-NLS-1$
            menuItem.setText(element.getAttribute("label"));
            // set the actions to the entries
            try {
                // $NON-NLS-1$
                Object o = element.createExecutableExtension("pushAction");
                menuItem.setData(o);
            } catch (CoreException e) {
                LogUtil.logError(CorePlugin.PLUGIN_ID, e);
            }
            // subpath of the icon within the plugin //$NON-NLS-1$
            String iconPath = element.getAttribute("icon");
            if ((iconPath != null) && (!iconPath.equals(""))) {
                // $NON-NLS-1$
                try {
                    // id of the plugin for path resolution //$NON-NLS-1$
                    Object o = element.getAttribute("id");
                    if (o != null) {
                        Bundle bundle = Platform.getBundle(o.toString());
                        // $NON-NLS-1$
                        URL fileUrl = FileLocator.find(bundle, new Path("/"), null);
                        fileUrl = FileLocator.toFileURL(fileUrl);
                        iconPath = fileUrl.getFile() + iconPath;
                        menuItem.setImage(new Image(null, iconPath));
                    }
                } catch (IOException ex) {
                    LogUtil.logError(CorePlugin.PLUGIN_ID, ex);
                }
            }
            // Handle selection
            menuItem.addSelectionListener(new SelectionAdapter() {

                public void widgetSelected(SelectionEvent e) {
                    final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
                    IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
                    ExecutionEvent event = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
                    // execute the actions
                    Object o = ((MenuItem) e.getSource()).getData();
                    try {
                        IHandler handler = (IHandler) o;
                        handler.execute(event);
                    } catch (Exception ex) {
                        LogUtil.logError(CorePlugin.PLUGIN_ID, ex);
                    }
                }
            });
        }
    }
    return menu;
}
Also used : Image(org.eclipse.swt.graphics.Image) URL(java.net.URL) Comparator(java.util.Comparator) TreeSet(java.util.TreeSet) IExtension(org.eclipse.core.runtime.IExtension) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Path(org.eclipse.core.runtime.Path) Bundle(org.osgi.framework.Bundle) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) MenuItem(org.eclipse.swt.widgets.MenuItem) IOException(java.io.IOException) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) CoreException(org.eclipse.core.runtime.CoreException) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IHandler(org.eclipse.core.commands.IHandler)

Example 38 with IEvaluationContext

use of org.eclipse.core.expressions.IEvaluationContext in project polymap4-core by Polymap4.

the class NavigatorPlugin method getEvalContext.

/**
 * @param selection
 * @return an evaluation context
 */
public static IEvaluationContext getEvalContext(Object selection) {
    IEvaluationContext c = new EvaluationContext(getApplicationContext(), selection);
    c.setAllowPluginActivation(true);
    return c;
}
Also used : IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext)

Example 39 with IEvaluationContext

use of org.eclipse.core.expressions.IEvaluationContext in project polymap4-core by Polymap4.

the class CommonActionProviderDescriptor method isEnabledFor.

/**
 * Determine if this action provider descriptor is enabled for the given selection.
 * The action provider descriptor is enabled for the selection if and only if it is
 * enabled for each element in the selection.
 *
 * @param aStructuredSelection
 *            The selection from the viewer
 * @return True if and only if the extension is enabled for each element in
 *         the selection.
 */
public boolean isEnabledFor(IStructuredSelection aStructuredSelection) {
    if (enablement == null) {
        return false;
    }
    if (aStructuredSelection.isEmpty()) {
        IEvaluationContext context = null;
        context = NavigatorPlugin.getEmptyEvalContext();
        if (NavigatorPlugin.safeEvaluate(enablement, context) != EvaluationResult.TRUE) {
            return false;
        }
    } else {
        IEvaluationContext context = null;
        IEvaluationContext parentContext = NavigatorPlugin.getApplicationContext();
        Iterator elements = aStructuredSelection.iterator();
        while (elements.hasNext()) {
            context = new EvaluationContext(parentContext, elements.next());
            context.setAllowPluginActivation(true);
            if (NavigatorPlugin.safeEvaluate(enablement, context) != EvaluationResult.TRUE) {
                return false;
            }
        }
    }
    return true;
}
Also used : IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) Iterator(java.util.Iterator) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext)

Example 40 with IEvaluationContext

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

the class ExpressionTests method testIterateExpressionAndFalse.

public void testIterateExpressionAndFalse() 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.FALSE;
        }
    };
    // $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.FALSE == exp.evaluate(context));
    // $NON-NLS-1$
    assertTrue(result.size() == 1 && result.get(0).equals("one"));
}
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)

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