Search code examples
commandeclipse-rcpeclipse-3.6

Current selection missing in RCP application context


To get the current selected object in a command handler I do

HandlerUtil.getCurrentSelection(event);

which finds the current selection from the application context stored in the event.

However I'm hunting a bug where this returns null, although I can clearly see a selection on the table.

I added a selection listener to the appropriate table viewer:

tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

    @Override
    public void selectionChanged(SelectionChangedEvent event) {
        LOGGER.trace(event);
    }
});

I can see the log output right before the call to

HandlerUtil.getCurrentSelection(event);

Any idea why the selection isn't available in the command hanlder?


Solution

  • You must register a 'selection provider' for your part to make the selection available to command handlers (and other things).

    You do this in a view or editor with:

    getSite().setSelectionProvider(selectionProvider);
    

    where selectionProvider is something that implements ISelectionProvider. The JFace viewers such as TableViewer, TreeViewer, ... implement this interface.