Search in sources :

Example 26 with IEvaluationContext

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

the class ViewProviderPaletteViewer method makeAndAssignActions.

/**
 * Creates the actions and assigns them to the viewers double click listener.
 */
private void makeAndAssignActions() {
    doubleClickHandler = new AbstractHandler() {

        public Object execute(ExecutionEvent event) {
            final ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
            Object obj = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (obj instanceof PaletteEditPart) {
                PaletteEditPart part = (PaletteEditPart) obj;
                IConfigurationElement[] elements = Platform.getExtensionRegistry().getExtensionPoint(extensionPointId).getConfigurationElements();
                for (IConfigurationElement element : elements) {
                    if (element.getAttribute("name").equals((// $NON-NLS-1$
                    (PaletteEntry) part.getModel()).getLabel())) {
                        // $NON-NLS-1$
                        Command command = commandService.getCommand(element.getAttribute("viewId"));
                        try {
                            return command.executeWithChecks(event);
                        } catch (Exception ex) {
                            LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                            return (null);
                        }
                    }
                }
            }
            return (null);
        }
    };
    viewer.getControl().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDoubleClick(final MouseEvent e) {
            if (e.button == 1) {
                // only left button double clicks
                final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
                IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
                ExecutionEvent event = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
                try {
                    // run assigned action
                    doubleClickHandler.execute(event);
                } catch (ExecutionException ex) {
                    LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                }
            }
        }

        @Override
        public void mouseDown(final MouseEvent e) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            Object obj = selection.getFirstElement();
            if (obj instanceof PaletteEditPart) {
                AlgorithmView.showContextHelp(extensionPointId, ((PaletteEntry) ((PaletteEditPart) obj).getModel()).getLabel());
                viewer.getControl().setFocus();
                viewer.setSelection(selection);
            }
        }
    });
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) MouseAdapter(org.eclipse.swt.events.MouseAdapter) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) PaletteEntry(org.eclipse.gef.palette.PaletteEntry) PaletteEditPart(org.eclipse.gef.ui.palette.editparts.PaletteEditPart) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) AbstractHandler(org.eclipse.core.commands.AbstractHandler) ICommandService(org.eclipse.ui.commands.ICommandService) ExecutionException(org.eclipse.core.commands.ExecutionException) IHandlerService(org.eclipse.ui.handlers.IHandlerService) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 27 with IEvaluationContext

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

the class ViewProviderTreeViewer method makeAndAssignActions.

/**
 * creates the actions and assigns them to the viewers double click listener
 */
private void makeAndAssignActions() {
    doubleClickHandler = new AbstractHandler() {

        public Object execute(ExecutionEvent event) {
            final ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
            TreeObject treeObject = (TreeObject) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            IConfigurationElement[] elements = Platform.getExtensionRegistry().getExtensionPoint(extensionPointId).getConfigurationElements();
            for (IConfigurationElement element : elements) {
                if (element.getAttribute("name").equals(treeObject.getName())) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    Command command = commandService.getCommand(element.getAttribute("viewId"));
                    try {
                        return command.executeWithChecks(event);
                    } catch (Exception ex) {
                        LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                    }
                }
            }
            return (null);
        }
    };
    addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(final DoubleClickEvent event) {
            Object obj = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (obj instanceof TreeParent) {
                if (viewer.getTree().getSelection()[0].getExpanded()) {
                    viewer.collapseToLevel(obj, 1);
                } else {
                    viewer.expandToLevel(obj, 1);
                }
            } else if (obj instanceof TreeObject) {
                try {
                    final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
                    IEvaluationContext evaluationContext = handlerService.createContextSnapshot(true);
                    ExecutionEvent executionEvent = new ExecutionEvent(null, Collections.EMPTY_MAP, null, evaluationContext);
                    // run assigned action
                    doubleClickHandler.execute(executionEvent);
                } catch (ExecutionException ex) {
                    LogUtil.logError(ViewsPlugin.PLUGIN_ID, ex);
                }
            }
        }
    });
    addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(final SelectionChangedEvent event) {
            Object treeObject = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (treeObject instanceof TreeParent) {
                // $NON-NLS-1$
                PlatformUI.getWorkbench().getHelpSystem().displayHelp(ViewsPlugin.PLUGIN_ID + ".algorithmsView");
                getControl().setFocus();
            } else if (treeObject instanceof TreeObject) {
                AlgorithmView.showContextHelp(extensionPointId, ((TreeObject) treeObject).getName());
                getControl().setFocus();
            }
        }
    });
}
Also used : TreeParent(org.jcryptool.core.views.content.structure.TreeParent) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) AbstractHandler(org.eclipse.core.commands.AbstractHandler) ICommandService(org.eclipse.ui.commands.ICommandService) ExecutionException(org.eclipse.core.commands.ExecutionException) IHandlerService(org.eclipse.ui.handlers.IHandlerService) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) TreeObject(org.jcryptool.core.views.content.structure.TreeObject) TreeObject(org.jcryptool.core.views.content.structure.TreeObject) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 28 with IEvaluationContext

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

the class NewHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    String parameter = event.getParameter("wizardParameter");
    Command command = null;
    ICommandService commandService = (ICommandService) HandlerUtil.getActiveSite(event).getService(ICommandService.class);
    if (parameter == null || parameter.equalsIgnoreCase("project")) {
        command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newProject");
    } else if (parameter.equalsIgnoreCase("folder")) {
        command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newFolder");
    } else if (parameter.equalsIgnoreCase("tm")) {
        command = commandService.getCommand("net.heartsome.cat.database.ui.tm.command.newTM");
    } else if (parameter.equalsIgnoreCase("tb")) {
        command = commandService.getCommand("net.heartsome.cat.database.ui.tb.command.newTB");
    }
    if (command == null) {
        return null;
    }
    IEvaluationService evalService = (IEvaluationService) HandlerUtil.getActiveSite(event).getService(IEvaluationService.class);
    IEvaluationContext currentState = evalService.getCurrentState();
    ExecutionEvent executionEvent = new ExecutionEvent(command, Collections.EMPTY_MAP, this, currentState);
    try {
        command.executeWithChecks(executionEvent);
    } catch (NotDefinedException e) {
        LOGGER.error("", e);
    } catch (NotEnabledException e) {
        LOGGER.error("", e);
    } catch (NotHandledException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : NotHandledException(org.eclipse.core.commands.NotHandledException) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) NotEnabledException(org.eclipse.core.commands.NotEnabledException) ICommandService(org.eclipse.ui.commands.ICommandService) IEvaluationService(org.eclipse.ui.services.IEvaluationService)

Example 29 with IEvaluationContext

use of org.eclipse.core.expressions.IEvaluationContext in project tdi-studio-se by Talend.

the class TalendLaunchShortcutAction method updateEnablement.

/**
     * 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();
    IWorkbenchWindow wb = PlatformUI.getWorkbench().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);
                        //$NON-NLS-1$
                        context.addVariable("selection", list);
                        enabled = fShortcut.evalEnablementExpression(context, expression);
                    }
                } catch (CoreException e) {
                }
            } else {
                IEditorPart editor = page.getActiveEditor();
                if (editor != null) {
                    enabled = true;
                }
            }
        }
    }
    setEnabled(enabled);
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.core.expressions.Expression) ISelection(org.eclipse.jface.viewers.ISelection) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) List(java.util.List) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) IEditorPart(org.eclipse.ui.IEditorPart)

Example 30 with IEvaluationContext

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

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) {
                    if (command.isEnabled()) {
                        handlerService.executeCommand(commandId, null);
                    } else {
                        log.warn("Command '" + commandId + "' is disabled");
                    }
                } else {
                    log.warn("Command '" + commandId + "' not found");
                }
            }
        } 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)

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