Search in sources :

Example 6 with Command

use of org.eclipse.core.commands.Command in project gfm_viewer by satyagraha.

the class Online method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    // update toggled state
    final Command command = event.getCommand();
    final boolean state = !HandlerUtil.toggleCommandState(command);
    LOGGER.fine("state: " + state);
    return null;
}
Also used : Command(org.eclipse.core.commands.Command)

Example 7 with Command

use of org.eclipse.core.commands.Command in project dbeaver by serge-rider.

the class ActionUtils method findCommandDescription.

@Nullable
public static String findCommandDescription(String commandId, IServiceLocator serviceLocator, boolean shortcutOnly) {
    String commandName = null;
    String shortcut = null;
    ICommandService commandService = serviceLocator.getService(ICommandService.class);
    if (commandService != null) {
        Command command = commandService.getCommand(commandId);
        if (command != null && command.isDefined()) {
            try {
                commandName = command.getName();
            } catch (NotDefinedException e) {
                log.debug(e);
            }
        }
    }
    IBindingService bindingService = serviceLocator.getService(IBindingService.class);
    if (bindingService != null) {
        TriggerSequence sequence = null;
        for (Binding b : bindingService.getBindings()) {
            ParameterizedCommand parameterizedCommand = b.getParameterizedCommand();
            if (parameterizedCommand != null && commandId.equals(parameterizedCommand.getId())) {
                sequence = b.getTriggerSequence();
            }
        }
        if (sequence == null) {
            sequence = bindingService.getBestActiveBindingFor(commandId);
        }
        if (sequence != null) {
            shortcut = sequence.format();
        }
    }
    if (shortcutOnly) {
        return shortcut == null ? "?" : shortcut;
    }
    if (shortcut == null) {
        return commandName;
    }
    if (commandName == null) {
        return shortcut;
    }
    return commandName + " (" + shortcut + ")";
}
Also used : Binding(org.eclipse.jface.bindings.Binding) TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) IBindingService(org.eclipse.ui.keys.IBindingService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService) Nullable(org.jkiss.code.Nullable)

Example 8 with Command

use of org.eclipse.core.commands.Command 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 9 with Command

use of org.eclipse.core.commands.Command in project eclipse.platform.text by eclipse.

the class EditorTest method testGenericEditorHasWordWrap.

@Test
public void testGenericEditorHasWordWrap() throws Exception {
    this.editor.setFocus();
    final StyledText editorTextWidget = (StyledText) this.editor.getAdapter(Control.class);
    Assert.assertFalse(editorTextWidget.getWordWrap());
    Assert.assertFalse(this.editor.isWordWrapEnabled());
    // Toggle word wrap
    ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
    Command wordWrapCommand = commandService.getCommand(ITextEditorActionDefinitionIds.WORD_WRAP);
    Assert.assertTrue(wordWrapCommand.isDefined());
    Assert.assertTrue(wordWrapCommand.isEnabled());
    Assert.assertTrue(wordWrapCommand.isHandled());
    PlatformUI.getWorkbench().getService(IHandlerService.class).executeCommand(wordWrapCommand.getId(), null);
    // 
    Assert.assertTrue(editorTextWidget.getWordWrap());
    Assert.assertTrue(this.editor.isWordWrapEnabled());
}
Also used : Control(org.eclipse.swt.widgets.Control) StyledText(org.eclipse.swt.custom.StyledText) IHandlerService(org.eclipse.ui.handlers.IHandlerService) Command(org.eclipse.core.commands.Command) ICommandService(org.eclipse.ui.commands.ICommandService) Test(org.junit.Test)

Example 10 with Command

use of org.eclipse.core.commands.Command in project eclipse.platform.text by eclipse.

the class EditorTest method testGenericEditorCanUseBlockSelection.

@Test
public void testGenericEditorCanUseBlockSelection() throws Exception {
    this.editor.setFocus();
    Assert.assertFalse(this.editor.isBlockSelectionModeEnabled());
    // Toggle word wrap
    ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
    Command wordWrapCommand = commandService.getCommand(ITextEditorActionDefinitionIds.BLOCK_SELECTION_MODE);
    Assert.assertTrue(wordWrapCommand.isDefined());
    Assert.assertTrue(wordWrapCommand.isEnabled());
    Assert.assertTrue(wordWrapCommand.isHandled());
    PlatformUI.getWorkbench().getService(IHandlerService.class).executeCommand(wordWrapCommand.getId(), null);
    // 
    Assert.assertTrue(this.editor.isBlockSelectionModeEnabled());
}
Also used : IHandlerService(org.eclipse.ui.handlers.IHandlerService) Command(org.eclipse.core.commands.Command) ICommandService(org.eclipse.ui.commands.ICommandService) Test(org.junit.Test)

Aggregations

Command (org.eclipse.core.commands.Command)31 ICommandService (org.eclipse.ui.commands.ICommandService)24 IHandlerService (org.eclipse.ui.handlers.IHandlerService)8 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)7 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)7 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)7 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)7 State (org.eclipse.core.commands.State)5 HashMap (java.util.HashMap)4 ExecutionException (org.eclipse.core.commands.ExecutionException)4 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)4 IEvaluationService (org.eclipse.ui.services.IEvaluationService)4 NotEnabledException (org.eclipse.core.commands.NotEnabledException)3 NotHandledException (org.eclipse.core.commands.NotHandledException)3 Test (org.junit.Test)3 Binding (org.eclipse.jface.bindings.Binding)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 IWorkbench (org.eclipse.ui.IWorkbench)2 RegistryToggleState (org.eclipse.ui.handlers.RegistryToggleState)2