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 08:43, 26 October 2009 by Remysuen.ca.ibm.com (Talk | contribs) (Part consuming the 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.

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.addSelection(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 currently propagated by means of an MContext's "variable" in its XMI file and passed around using the IEclipseContext.

Application.xmi definition

?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>
    <!-- ... -->
  </children>
</application:Application>

Part providing the selection

// attach a selection listener to our jface viewer
contactsViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  public void selectionChanged(SelectionChangedEvent event) {
    IStructuredSelection selection = (IStructuredSelection) event.getSelection();
    // based on the selection, modify our local eclipse context
    eclipseContext.modify(IServiceConstants.SELECTION,
        selection.size() == 1 ? selection.getFirstElement() : selection.toArray());
  }
});

Part consuming the selection

/**
 * Public selection setter method for the injector to invoke.
 */
public void setSelection(Object selection) {
  if (selection instanceof Contact) {
    // render the details of the selected contact
    detailComposite.update((Contact) selection);
  }
}

Back to the top