简体   繁体   中英

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?

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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM