When adding a selection listener to the Eclipse RCP Workbench how are you supposed to know the ID to use?

StackOverflow https://stackoverflow.com/questions/9803036

Question

I have a view part and in my createPartControl function I have this:

viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

getSite().setSelectionProvider(viewer);
getSite().getWorkbenchWindow().getSelectionService()
            .addSelectionListener(this);

I'm registering the viewpart as a selection listener, but I only really want to listen to selections provided by the treeviewer in this viewpart. This is because i'm trying to make a context sensitive popup menu so I want to know which node is selected ready for when the menu popup happens (I'm hoping that selection listener code is executed before the menulistener stuff) and the documentation for this framework is unhelpful at best.

I know that the addSelectionListener(String ID, ISelectionListener listener) method, but I cannot work out how on earth I am supposed to know the ID of the viewer I want to listen to, can anyone please help me to work this out?

Était-ce utile?

La solution

I only really want to listen to selections provided by the treeviewer in this viewpart

Why not add selection listener directly to viewer?

how on earth I am supposed to know the ID of the viewer

It's the ID of your view (part) not (tree)viewer.

You can also call viewer.getSelection() directly without having any listeners.

Autres conseils

The SelectionService allows you to react to selections in other components. Components that were not written by you or that are written years after you have published your plugin-in.

If your intention is not to react on selections anywhere in Eclipse (e.g. react on the selection of a Java methode regardlessly in which view or editor), there is nothing wrong with registering the listener on you TreeViewer directly.

That way you do not have to filter out the wrong selection events, but can be sure to only get those of your viewer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top