Search in sources :

Example 16 with ICommandService

use of org.eclipse.ui.commands.ICommandService in project linuxtools by eclipse.

the class CommandUtils method execute.

/**
 * Executes the command identified by the given {@code id} on a context
 * based on the given selection
 *
 * @param id
 *            the id of the command to execute
 * @param selection
 *            the selection to use as a context to run the command
 */
public static void execute(final String id, final IStructuredSelection selection) {
    final ICommandService service = PlatformUI.getWorkbench().getService(ICommandService.class);
    final Command command = service != null ? service.getCommand(id) : null;
    if (command != null && command.isDefined()) {
        try {
            ParameterizedCommand pCmd = ParameterizedCommand.generateCommand(command, null);
            IHandlerService handlerSvc = PlatformUI.getWorkbench().getService(IHandlerService.class);
            IEvaluationContext ctx = handlerSvc.getCurrentState();
            ctx = new EvaluationContext(ctx, selection);
            ctx.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
            handlerSvc.executeCommandInContext(pCmd, null, ctx);
        } catch (Exception e) {
            Activator.log(e);
        }
    }
}
Also used : 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) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 17 with ICommandService

use of org.eclipse.ui.commands.ICommandService in project yamcs-studio by yamcs.

the class CommandStackView method createPartControl.

@Override
public void createPartControl(Composite parent) {
    GridLayout gl = new GridLayout();
    gl.marginHeight = 0;
    gl.marginWidth = 0;
    gl.verticalSpacing = 1;
    parent.setLayout(gl);
    ResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
    errorBackgroundColor = resourceManager.createColor(new RGB(255, 221, 221));
    bracketStyler = new Styler() {

        @Override
        public void applyStyles(TextStyle textStyle) {
            textStyle.font = JFaceResources.getTextFont();
        }
    };
    argNameStyler = new Styler() {

        @Override
        public void applyStyles(TextStyle textStyle) {
            textStyle.font = JFaceResources.getTextFont();
            textStyle.foreground = Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
        }
    };
    numberStyler = new Styler() {

        @Override
        public void applyStyles(TextStyle textStyle) {
            textStyle.font = JFaceResources.getTextFont();
            textStyle.foreground = Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
        }
    };
    errorStyler = new Styler() {

        @Override
        public void applyStyles(TextStyle textStyle) {
            textStyle.font = JFaceResources.getTextFont();
            textStyle.foreground = Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
            textStyle.underline = true;
            textStyle.underlineColor = Display.getDefault().getSystemColor(SWT.COLOR_RED);
            textStyle.underlineStyle = SWT.UNDERLINE_ERROR;
        }
    };
    issuedStyler = new Styler() {

        @Override
        public void applyStyles(TextStyle textStyle) {
            textStyle.font = JFaceResources.getFontRegistry().getItalic(JFaceResources.TEXT_FONT);
            textStyle.foreground = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
        }
    };
    skippedStyler = new Styler() {

        @Override
        public void applyStyles(TextStyle textStyle) {
            textStyle.font = JFaceResources.getFontRegistry().getItalic(JFaceResources.TEXT_FONT);
            textStyle.foreground = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
            textStyle.strikeout = true;
            textStyle.strikeoutColor = textStyle.foreground;
        }
    };
    Composite tableWrapper = new Composite(parent, SWT.NONE);
    tableWrapper.setLayoutData(new GridData(GridData.FILL_BOTH));
    TableColumnLayout tcl = new TableColumnLayout();
    tableWrapper.setLayout(tcl);
    commandTableViewer = new CommandStackTableViewer(tableWrapper, tcl, this);
    commandTableViewer.addDoubleClickListener(evt -> {
        IStructuredSelection sel = (IStructuredSelection) evt.getSelection();
        if (sel.getFirstElement() != null) {
            StackedCommand cmd = (StackedCommand) sel.getFirstElement();
            if (cmd.getStackedState() != StackedState.ISSUED && cmd.getStackedState() != StackedState.SKIPPED) {
                EditStackedCommandDialog dialog = new EditStackedCommandDialog(parent.getShell(), cmd);
                if (dialog.open() == Window.OK) {
                    refreshState();
                }
            }
        }
    });
    Composite controls = new Composite(parent, SWT.NONE);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.heightHint = 30;
    controls.setLayoutData(gd);
    gl = new GridLayout(2, false);
    gl.marginHeight = 0;
    gl.marginWidth = 0;
    controls.setLayout(gl);
    controls.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
    Composite bottomLeft = new Composite(controls, SWT.NONE);
    gd = new GridData(GridData.FILL_BOTH);
    gd.verticalAlignment = SWT.CENTER;
    bottomLeft.setLayoutData(gd);
    gl = new GridLayout(2, false);
    gl.marginHeight = 0;
    gl.marginWidth = 0;
    gl.horizontalSpacing = 0;
    gl.verticalSpacing = 0;
    bottomLeft.setLayout(gl);
    messageLabel = new Label(bottomLeft, SWT.NONE);
    messageLabel.setText("");
    messageLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Composite bottomRight = new Composite(controls, SWT.NONE);
    gd = new GridData(GridData.FILL_BOTH);
    gd.verticalAlignment = SWT.CENTER;
    gd.horizontalAlignment = SWT.RIGHT;
    bottomRight.setLayoutData(gd);
    gl = new GridLayout(2, false);
    gl.marginHeight = 0;
    gl.marginWidth = 0;
    gl.horizontalSpacing = 0;
    gl.verticalSpacing = 0;
    bottomRight.setLayout(gl);
    armButton = new Button(bottomRight, SWT.TOGGLE);
    armButton.setText("1. Arm");
    armButton.setToolTipText("Arm the selected command");
    armButton.setEnabled(false);
    armButton.addListener(SWT.Selection, evt -> {
        if (armButton.getSelection()) {
            ICommandService commandService = (ICommandService) getViewSite().getService(ICommandService.class);
            IEvaluationService evaluationService = (IEvaluationService) getViewSite().getService(IEvaluationService.class);
            Command cmd = commandService.getCommand("org.yamcs.studio.commanding.stack.arm");
            try {
                cmd.executeWithChecks(new ExecutionEvent(cmd, new HashMap<String, String>(), null, evaluationService.getCurrentState()));
            } catch (Exception e) {
                log.log(Level.SEVERE, "Could not execute command", e);
            }
        } else {
            CommandStack stack = CommandStack.getInstance();
            if (stack.getActiveCommand() != null && stack.getActiveCommand().isArmed()) {
                stack.getActiveCommand().setStackedState(StackedState.DISARMED);
            }
        }
    });
    issueButton = new Button(bottomRight, SWT.NONE);
    issueButton.setText("2. Issue");
    issueButton.setToolTipText("Issue the selected command");
    issueButton.setEnabled(false);
    issueButton.addListener(SWT.Selection, evt -> {
        ICommandService commandService = (ICommandService) getViewSite().getService(ICommandService.class);
        IEvaluationService evaluationService = (IEvaluationService) getViewSite().getService(IEvaluationService.class);
        Command cmd = commandService.getCommand("org.yamcs.studio.commanding.stack.issue");
        try {
            cmd.executeWithChecks(new ExecutionEvent(cmd, new HashMap<String, String>(), null, evaluationService.getCurrentState()));
        } catch (Exception e) {
            log.log(Level.SEVERE, "Could not execute command", e);
        }
    });
    commandTableViewer.addSelectionChangedListener(evt -> {
        IStructuredSelection sel = (IStructuredSelection) evt.getSelection();
        updateMessagePanel(sel);
        CommandStack stack = CommandStack.getInstance();
        armButton.setSelection(false);
        stack.disarmArmed();
        if (sel.isEmpty() || !stack.isValid() || !sel.getFirstElement().equals(stack.getActiveCommand())) {
            armButton.setEnabled(false);
            issueButton.setEnabled(false);
        } else if (stack.hasRemaining()) {
            setButtonEnable(armButton, true);
        }
        refreshState();
    });
    getViewSite().setSelectionProvider(commandTableViewer);
    // Set up connection state, and listen to changes
    connectionStateProvider = RCPUtils.findSourceProvider(getViewSite(), ConnectionStateProvider.STATE_KEY_CONNECTED, ConnectionStateProvider.class);
    connectionStateProvider.addSourceProviderListener(new ISourceProviderListener() {

        @Override
        public void sourceChanged(int sourcePriority, String sourceName, Object sourceValue) {
            refreshState();
        }

        @Override
        @SuppressWarnings("rawtypes")
        public void sourceChanged(int sourcePriority, Map sourceValuesByName) {
            refreshState();
        }
    });
    // Add the popup menu for pasting commands
    addPopupMenu();
    // Set initial state
    refreshState();
    CommandingCatalogue.getInstance().addCommandHistoryListener(cmdhistEntry -> {
        Display.getDefault().asyncExec(() -> processCommandHistoryEntry(cmdhistEntry));
    });
}
Also used : LocalResourceManager(org.eclipse.jface.resource.LocalResourceManager) HashMap(java.util.HashMap) ConnectionStateProvider(org.yamcs.studio.core.ui.connections.ConnectionStateProvider) ISourceProviderListener(org.eclipse.ui.ISourceProviderListener) Label(org.eclipse.swt.widgets.Label) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ICommandService(org.eclipse.ui.commands.ICommandService) GridLayout(org.eclipse.swt.layout.GridLayout) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) TextStyle(org.eclipse.swt.graphics.TextStyle) Button(org.eclipse.swt.widgets.Button) Composite(org.eclipse.swt.widgets.Composite) ResourceManager(org.eclipse.jface.resource.ResourceManager) LocalResourceManager(org.eclipse.jface.resource.LocalResourceManager) RGB(org.eclipse.swt.graphics.RGB) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) GridData(org.eclipse.swt.layout.GridData) Styler(org.eclipse.jface.viewers.StyledString.Styler) HashMap(java.util.HashMap) Map(java.util.Map) IEvaluationService(org.eclipse.ui.services.IEvaluationService)

Example 18 with ICommandService

use of org.eclipse.ui.commands.ICommandService in project yamcs-studio by yamcs.

the class ScrollLockHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
    CommandHistoryView view = (CommandHistoryView) part;
    ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
    Command command = service.getCommand("org.yamcs.studio.commanding.cmdhist.scrollLockCommand");
    boolean oldState = HandlerUtil.toggleCommandState(command);
    view.enableScrollLock(!oldState);
    return null;
}
Also used : IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) Command(org.eclipse.core.commands.Command) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 19 with ICommandService

use of org.eclipse.ui.commands.ICommandService in project yamcs-studio by yamcs.

the class OPIShell method sendUpdateCommand.

/**
 * Alert whoever is listening that a new OPIShell has been created.
 */
private static void sendUpdateCommand() {
    IServiceLocator serviceLocator = PlatformUI.getWorkbench();
    ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
    try {
        Command command = commandService.getCommand(OPI_SHELLS_CHANGED_ID);
        command.executeWithChecks(new ExecutionEvent());
    } catch (ExecutionException | NotHandledException | NotEnabledException | NotDefinedException e) {
        log.log(Level.WARNING, "Failed to send OPI shells changed command", e);
    }
}
Also used : NotHandledException(org.eclipse.core.commands.NotHandledException) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IServiceLocator(org.eclipse.ui.services.IServiceLocator) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) ExecutionException(org.eclipse.core.commands.ExecutionException) NotEnabledException(org.eclipse.core.commands.NotEnabledException) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 20 with ICommandService

use of org.eclipse.ui.commands.ICommandService in project yamcs-studio by yamcs.

the class ScriptUtil method executeEclipseCommand.

/**
 * Execute an Eclipse command with optional parameters. Any parameters must be defined along with the command in
 * plugin.xml.
 *
 * @param commandId
 *            the Eclipse command id
 * @param parameters
 *            a list of further String arguments alternating key, value: * executeEclipseCommand("id", ["pkey",
 *            "pvalue"])
 */
public static final void executeEclipseCommand(String commandId, String[] parameters) {
    IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService.class);
    try {
        if (parameters.length % 2 != 0) {
            throw new IllegalArgumentException("Parameterized commands must have " + "an equal number of keys and values");
        }
        if (parameters.length == 0) {
            handlerService.executeCommand(commandId, null);
        } else {
            ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);
            Parameterization[] params = new Parameterization[parameters.length / 2];
            Command c = commandService.getCommand(commandId);
            for (int i = 0; i < parameters.length / 2; i++) {
                String key = parameters[2 * i];
                String value = parameters[2 * i + 1];
                IParameter p = c.getParameter(key);
                Parameterization pp = new Parameterization(p, value);
                params[i] = pp;
            }
            ParameterizedCommand pc = new ParameterizedCommand(c, params);
            handlerService.executeCommand(pc, null);
        }
    } catch (Exception e) {
        ErrorHandlerUtil.handleError("Failed to execute eclipse command: " + commandId, e);
    }
}
Also used : IParameter(org.eclipse.core.commands.IParameter) IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) Parameterization(org.eclipse.core.commands.Parameterization) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService)

Aggregations

ICommandService (org.eclipse.ui.commands.ICommandService)38 Command (org.eclipse.core.commands.Command)24 IHandlerService (org.eclipse.ui.handlers.IHandlerService)8 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)7 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)7 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)6 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)6 ExecutionException (org.eclipse.core.commands.ExecutionException)4 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)4 IEvaluationService (org.eclipse.ui.services.IEvaluationService)4 HashMap (java.util.HashMap)3 NotEnabledException (org.eclipse.core.commands.NotEnabledException)3 NotHandledException (org.eclipse.core.commands.NotHandledException)3 State (org.eclipse.core.commands.State)3 ISelection (org.eclipse.jface.viewers.ISelection)3 StyledText (org.eclipse.swt.custom.StyledText)3 Composite (org.eclipse.swt.widgets.Composite)3 Test (org.junit.Test)3 IXliffEditor (net.heartsome.cat.ts.ui.editors.IXliffEditor)2 IHandler (org.eclipse.core.commands.IHandler)2