Eclipse Search

Loading

Jan 11, 2009

Commands Part 4: Misc items ...

How do I know about execution of a command?

You can use the IExecutionListener:

ICommandService service = (ICommandService) serviceLocator.getService(ICommandService.class);
service.addExecutionListener(new IExecutionListener() {

    public void notHandled(String commandId, NotHandledException exception) {
    }

    public void postExecuteFailure(String commandId, ExecutionException exception) {
    }

    public void postExecuteSuccess(String commandId, Object returnValue) {
    }

    public void preExecute(String commandId, ExecutionEvent event) {
    }
});

The IExecutionListener is merely an observer of the command execution so it can neither veto on it nor make any changes to the event.

How do I get the active Window/Shell in my handler code?

Use HandlerUtil. Its a handy class which gives you access to most common variables like active window, active shell, context ids, active part, current selection,  etc.

Shell shell = HandlerUtil.getActiveShell(event);
ISelection selection = HandlerUtil.getCurrentSelection(event);

All of the get* methods will have a get*Checked variants. The first ones will return you null if the variable is not available. The checked ones will throw you an exception if the variable is null. If you have your own variable, you can use the getVariable() method to get it.

Can I programmatically execute a Command?

Yes. But don't simple call command.execute(), use the IHandlerService to execute a command:

IHandlerService handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class);
handlerService.executeCommand(commandId, null);

Can I programmatically create and manipulate a Command?

Yes! You can use ICommandService to create a new command and then associate a handler thru IHandlerService:

ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
Command command = commandService.getCommand("my.new.undefined.command");
command.define("New Command", "This is created Programatically!", 
    commandService.getCategory("org.eclipse.ui.category.window"));

IHandlerService handlerService = (IHandlerService) serviceLocator.getService(IHandlerService.class);
handlerService.activateHandler(command.getId(), new AbstractHandler() {

    public Object execute(ExecutionEvent event) throws ExecutionException {
        System.out.println("Command executed !");
        return null;
    }
});

 

See also:

Part 1: Actions Vs Commands
Part 2: Selection and Enablement of Handlers
Part 3: Parameters for Commands
Part 5: ISourceProvider & dynamically updating Commands
Part 6: 'toggle' & 'radio' style menu contribution

11 comments:

  1. Hi Prakash,
    your HOWTO for the commands is realy good. So now I have changed many actions in my own programm to commands.
    But I have one question, how can I activate / make visible an command in an special perspektive for example "open file" to that i can open an file editor.
    But in other perspectives you dont see this command?

    Greetings
    Heiko

    ReplyDelete
  2. There's also obtaining the command service from the site, right? Does this automatically scope any changes to the part? If, for example, I want an editor to set some handlers that are only live for that editor, what's the correct procedure?

    ReplyDelete
  3. Oh, one other thing. Key binding could use the same treatment. It seems that there's no programmatic way to change them, but it would be reassuring to here that from an expert.

    ReplyDelete
  4. I've enjoyed this tutorial but there is one element of the new command framework that you have not covered. I've also searched other resources and have not found my answer. I have also experimented with code and have not solved my problem. This makes me conclude that the answer is so obvious I can't see it or that there is no answer. Perhaps you can help distinguish.

    Using the command framework I have not been able to add, using only plugin.xml entries, an ability to add toolbar menu items to a specific page of of a pagebook view. The Eclipse PDE implements, in code, various contributing content outline pages for the outline view. When no editor is open the outline view has no toolbar items. Depending on the file opened for edit, the icons in the views toolbar change to suit the file. Can that be done for a new contributing page using only command framework entries? I also want to implement the same behavior in my own pagebook views.

    ReplyDelete
  5. Your tutorial is really good.
    I'm using more commands than before.

    But I think commands (or its handlers) cannot have a checked state. This is a limitation.

    ReplyDelete
  6. I've recently come across your blog and love it, finding it very well written and informative, thank you!

    Question about the above (possibly a dumb one, but whatever): where are you getting the "serviceLocator" from?

    ReplyDelete
  7. @Mike,
    Its an instance of IServiceLocator. Workbench, WorkbenchWindow, WorkbenchSite all are service locators. You can also create one, if you have to

    ReplyDelete
  8. Okay, thanks.

    So, if I'm in a handler for command X, and want to gain access to command Y (to disable it), how should I go about getting it?

    In case I'm on the wrong track altogether....what I'm ultimately trying to do is logically tie two toolbar buttons together so only one is active at a given time; one you press one, it disables itself and enables the other, like a switch. Am I on the right track for implementing this?

    Thanks much.

    ReplyDelete
  9. @Mike,
    You should be using the command parameter and toggle contributions. Check how the Align command at the 6th part in this Command series. You can go thru the complete tutorial here: http://eclipse-tips.com/index.php/tutorials/1-actions-vs-commands

    ReplyDelete
  10. How to call a command without using workbench
    I do not want to use below
    handlerService = (IHandlerService ) PlatformUI.getWorkbench().getService(IHandlerService.class);
    handlerService.executeCommand(command, null);

    Parvez Ahmad Hakim
    Srinagar Kashmir

    ReplyDelete
  11. You need an IServiceLocator to get the service. It could be Workbench/WorkbenchWindow/any other implementations of that interface

    ReplyDelete