Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

E4/EAS/Selection

< E4‎ | EAS
Revision as of 23:14, 23 October 2009 by Remysuen.ca.ibm.com (Talk | contribs) (Eclipse 3.x API)

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.

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:

private void attachSelectionListener() {
  ISelectionService selectionService = (ISelectionService) getSite().getService(ISelectionService.class);
  selectionService.addSelection(new ISelectionListener() {
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
      IStructuredSelection structuredSelection = (IStructuredSelection) selection;
      detailComposite.update((Contact) structuredSelection.getFirstElement());
    }
  });
}

e4 (Java)

In the e4 world, selection is currently propagated by means of an MContext's "variable" in its XMI file and passed around using the IEclipseContext.

Application.xmi:

?xml version="1.0" encoding="ASCII"?>
<application:Application xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:application="http://www.eclipse.org/ui/2008/UIModel"
    xsi:schemaLocation="http://www.eclipse.org/ui/2008/UIModel ../org.eclipse.e4.ui.model.workbench/model/UIElements.ecore">
  <children xsi:type="application:Window">
    <!-- ... -->
    <variables>selection</variables>
    <variables>input</variables>
    <!-- ... -->
  </children>
</application:Application>

View providing the selection:

contactsViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  public void selectionChanged(SelectionChangedEvent event) {
    IStructuredSelection selection = (IStructuredSelection) event.getSelection();
    eclipseContext.modify(IServiceConstants.SELECTION,
        selection.size() == 1 ? selection.getFirstElement() : selection.toArray());
  }
});

View consuming the selection:

public void setSelection(Object selection) {
  if (selection instanceof Contact) {
    detailComposite.update((Contact) selection);
  }
}

Back to the top