I have a question regarding "best practice" on the e4 selectionservice
1) Handling single and multiple selection
E.g on a tableviewer I can select a single or multiple elements. Depending on that, my active selection is either a object or a collection of objects.
What is the best practice to handle that in my listener?
....selectionService.setSelection(structuredSelection.getFirstElement())
OR
....selectionService.setSelection(structuredSelection.asList())
public void xy (@Optional @Named(IServiceConstants.ACTIVE_SELECTION) List selection){} --> selection is null if a single element is selected
OR
public void xyz (@Optional @Named(IServiceConstants.ACTIVE_SELECTION) MyObject selection){} --> selection is null if multiple elements are selected
Do I need to implement both methods to handle both scenarios? Why is a single element not packed in a list or vice versa?
2) How to handle active selections that can be adapted to a target Object? Do I need to have ISelection parameter and check for the adaptation manually or is there any way the framework can adapt and inject if possible?
Thanks a lot in advance
The normal selection is the actual IStructuredSelection
object, not its contents:
selectionService.setSelection(structuredSelection);
public void xx(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection)
So you only have one method and it receives all types of selection.
For adaptable objects there is nothing that will do this automatically. Use the org.eclipse.core.runtime.Adapters
class to adapt an object:
IFile file = Adapters.adapt(object, IFile.class);