Search in sources :

Example 1 with Command

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

the class ViewerSupportDefault method isLinked.

@Override
public boolean isLinked() {
    LOGGER.fine("");
    Command command = commandService.getCommand(GFM_VIEWER_PLUGIN_LINKED);
    State state = command.getState(RegistryToggleState.STATE_ID);
    return (Boolean) state.getValue();
}
Also used : Command(org.eclipse.core.commands.Command) RegistryToggleState(org.eclipse.ui.handlers.RegistryToggleState) State(org.eclipse.core.commands.State)

Example 2 with Command

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

the class Linked 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 3 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 4 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 5 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)

Aggregations

Command (org.eclipse.core.commands.Command)13 ICommandService (org.eclipse.ui.commands.ICommandService)8 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)6 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)4 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)3 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)3 State (org.eclipse.core.commands.State)3 RegistryToggleState (org.eclipse.ui.handlers.RegistryToggleState)3 HashMap (java.util.HashMap)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 NotEnabledException (org.eclipse.core.commands.NotEnabledException)2 NotHandledException (org.eclipse.core.commands.NotHandledException)2 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)2 Binding (org.eclipse.jface.bindings.Binding)2 IWorkbench (org.eclipse.ui.IWorkbench)2 IEvaluationService (org.eclipse.ui.services.IEvaluationService)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1