Search code examples
javatreevieweclipse-rcpjfacetreeviewer

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


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?


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.