Eclipse 4.0 RCP: Selection
Join the DZone community and get the full member experience.
Join For FreeOne of the objectives of Eclipse 4.0 was to make rich client development easier. One good example is the following use case: One view provides a selection (e.g. using a JFace TableViewer) and another view wants to get notified about a new selection of a domain object to get the chance to react on it.
First the selection provider part. The selection services can be easily injected using dependency injection.
@Inject private ESelectionService selectionService;
When using a JFace viewer, the viewer selection has to be forwarded to the selction service:
tableViewer.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); selectionService.setSelection(selection.getFirstElement()); } });
On the consumer side, the domain object is injected using an @Optional and @Named active selection. The following code snipped is part of a details view POJO that used es WritableValue (Eclipse data binding) to as holder for the actual domain object of type IContact.
@Inject public void setSelection( @Optional @Named(IServiceConstants.ACTIVE_SELECTION) IContact contact) { if (contact != null) { contactValue.setValue(contact); } }
That’s pretty convenient and concise compared with the code needed in Eclipse 3.x.
Have Fun!
Kai
Opinions expressed by DZone contributors are their own.
Comments