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

Difference between revisions of "E4/EAS/Selection"

< E4‎ | EAS
(Part consuming the selection)
(Part consuming the selection)
Line 74: Line 74:
 
</source>
 
</source>
  
===[http://dev.eclipse.org/viewcvs/index.cgi/e4/org.eclipse.e4.ui/examples/org.eclipse.e4.demo.contacts/src/org/eclipse/e4/demo/contacts/views/DetailsView.java?revision=1.4&view=markup Part] consuming the selection===
+
===[http://dev.eclipse.org/viewcvs/index.cgi/e4/org.eclipse.e4.ui/examples/org.eclipse.e4.demo.contacts/src/org/eclipse/e4/demo/contacts/views/DetailsView.java?revision=1.11&view=markup Part] consuming the selection===
 
<source lang="java">
 
<source lang="java">
 
/**
 
/**
  * Public selection setter method for the injector to invoke.
+
  * Setter method for the injector to invoke.
 
  */
 
  */
public void setSelection(Object selection) {
+
@Inject
   if (selection instanceof Contact) {
+
@Optional
     // render the details of the selected contact
+
void setSelection(@Named(IServiceConstants.SELECTION) Contact contact) {
     detailComposite.update((Contact) selection);
+
   if (contact == null) {
 +
     /* implementation not shown */
 +
  } else {
 +
     /* implementation not shown */
 
   }
 
   }
 
}
 
}
 
</source>
 
</source>

Revision as of 09:45, 10 November 2009

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

/**
 * Setter method for the injector to invoke.
 */
@Inject
@Optional
void setSelection(@Named(IServiceConstants.SELECTION) Contact contact) {
  if (contact == null) {
    /* implementation not shown */
  } else {
    /* implementation not shown */
  }
}

Back to the top