Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
E4/EAS/Selection
The selection service is often tied in with the adapter mechanism and leveraged by views to present different information about the items that the user currently has selected.
Contents
Use cases
Listening for selection changes
- Parts that wishes to listen to global selection
- Parts that listens to a specific "type" of part
- 'Outline' view -> active editor
- 'Templates' view -> active editor
- 'Javadoc' view -> active editor
- Parts that listens to parts based on a condition
- 'Properties' view only cares about property sources / property page providers, ignores itself and others like the 'Cheat Sheets' (?) view
- Parts that listen to a specific part
- 'Variables' -> 'Debug' (?), updating itself based on stack frame
- 'Projects' -> 'Packages' -> 'Types' -> 'Members', from the 'Java Browsing' perspective
- 'Preview' -> 'Library' in the photo demo
- 'Preview' -> 'Thumbnails' in the photo demo
- 'Thumbnails' -> 'Library' in the photo demo
- 'Exif' -> 'Library' in the photo demo
- 'Location' -> 'Exif' in the photo demo
- 'Details' -> 'Contacts' in the contacts demo
- "Nested" parts listening to "itself"
- 'History' view could theoretically be broken up into two. The bottom details "renderer" listens to the table above.
Retrieving the "current" selection
- When a handler asks the 'Properties' view for its selection, it should be null because the 'Properties' view doesn't post a selection.
- Likewise, it should be possible to ask a specific part for its selection, in 3.x, this is done by
ISelectionService
'sgetSelection(String)
method
Behaviour
- If you have the 'Project Explorer' (has something selected), 'Navigator' (has nothing selected), and 'Properties' view open. And you activate the 'Project Explorer' and then the 'Navigator', should the 'Properties' view go blank?
- Slightly off-topic and more relating to how the context perceives the "active editor" but still of relevance given the whole "I only care about X" case, but if you have the Java editor and the 'Outline' view and the 'Outline' view is maximized, it must not go blank. See bug 267425 comment 9.
- The action selection should not be hijacked by another part. That is the workbench window's active selection should not change when a non-active part alters its selection. Consider a slideshow of images when the active part is not the slideshow view.
- For parts that are embedded within dialogs, their selection should also not interfere with the workbench window's selection.
- If the target part is closed and then reopened later, the consuming part should be able to monitor the "regenerated" part with no special code.
- When a part that was posting selection is removed from the user interface, is it a requirement for it to post an empty/null selection? This behaviour needs to be defined and spec'd or defined and spec'd as "undefined".
Eclipse 3.x API
In Eclipse 3.x, the org.eclipse.ui.ISelectionService allows workbench parts to query the active selection in addition to attaching listeners for monitoring selection changes. This is usually tied in with a org.eclipse.jface.viewers.ISelectionProvider that is attached to an org.eclipse.ui.IWorkbenchSite.
Part providing a selection
/** * Attach the selection provider to the part site. */ private void attachSelectionProvider() { // first get the site for attaching the selection provider IWorkbenchPartSite partSite = getSite(); // now set the provider to our jface viewer so that any selection // made in the tree will be propagated exposed to consuming clients // of the workbench partSite.setSelectionProvider(contactsViewer); }
Part consuming the selection
/** * Attach a selection listener to the selection service so * that we can render the details of the contact that the * user has selected. */ private void attachSelectionListener() { // get the selection service from our local service locator ISelectionService selectionService = (ISelectionService) getSite().getService(ISelectionService.class); // attach the selection listener selectionService.addSelectionListener(new ISelectionListener() { public void selectionChanged(IWorkbenchPart part, ISelection selection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; Object element = structuredSelection.getFirstElement(); if (element instanceof Contact) { // render the details of the selected contact detailComposite.update((Contact) ); } } }); }
e4 (Java)
In the e4 world, selection is propagated with the ESelectionService
.
Part providing the selection
@Inject private ESelectionService selectionService; @PostConstruct void create() { // attach a selection listener to our jface viewer viewer.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); // set the selection to the service selectionService.setSelection( selection.size() == 1 ? selection.getFirstElement() : selection.toArray()); } }); }
Part consuming the selection
@Inject void setSelection(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) Contact contact) { if (contact == null) { /* implementation not shown */ } else { /* implementation not shown */ } }