Eclipse4/RCP/EAS/Workbench Services

From Eclipsepedia

< Eclipse4‎ | RCP‎ | EAS
Revision as of 12:48, 12 April 2011 by Remysuen.ca.ibm.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Part Service

In order to allow clients to easily work with the modeled workbench, there needs to be some convenience APIs present for them to perform simple operations like "show part X", "activate part Y", or "bring part Z to the top of its stack".

API Comparison

In 3.x, there is IPartService and IWorkbenchPage, with IWorkbenchPage extending IPartService. Together, these two interfaces defines the general operations that clients often want to do.

In Eclipse 4, the notion of a workbench page is not present. The part service API is basically a merge of the existing 3.x IPartService and IWorkbenchPage interfaces.

Show a view

Eclipse 3.x Eclipse 4.x
// show the 'Project Explorer' view
workbenchPage.showView(IPageLayout.PROJECT_EXPLORER);
// show the 'Project Explorer' view
partService.showView(IPageLayout.PROJECT_EXPLORER);

Activate a part

Eclipse 3.x Eclipse 4.x
// activate the 'Project Explorer' view
workbenchPage.activate(projectExplorerPart);
// activate the 'Project Explorer' part
partService.activate(projectExplorerPart);

Bring a part to the top of its stack

Eclipse 3.x Eclipse 4.x
// bring the 'Project Explorer' view to the top of its stack
workbenchPage.bringToTop(projectExplorerPart);
// bring the 'Project Explorer' part to the top of its stack
partService.bringToTop(projectExplorerPart);

Selection Service

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.

Use cases

Listening for selection changes

  • Parts that wishes to listen to global selection
  • Parts that listens to a specific "type" of part
    1. 'Outline' view -> active editor
    2. 'Templates' view -> active editor
    3. 'Javadoc' view -> active editor
  • Parts that listens to parts based on a condition
    1. 'Properties' view only cares about property sources / property page providers, ignores itself and others like the 'Cheat Sheets' (?) view
  • Parts that listen to a specific part
    1. 'Variables' -> 'Debug' (?), updating itself based on stack frame
    2. 'Projects' -> 'Packages' -> 'Types' -> 'Members', from the 'Java Browsing' perspective
    3. 'Preview' -> 'Library' in the photo demo
    4. 'Preview' -> 'Thumbnails' in the photo demo
    5. 'Thumbnails' -> 'Library' in the photo demo
    6. 'Exif' -> 'Library' in the photo demo
    7. 'Location' -> 'Exif' in the photo demo
    8. 'Details' -> 'Contacts' in the contacts demo
  • "Nested" parts listening to "itself"
    1. 'History' view could theoretically be broken up into two. The bottom details "renderer" listens to the table above.

Retrieving the "current" selection

  • When a handler asks the 'Properties' view for its selection, it should be null because the 'Properties' view doesn't post a selection.
  • Likewise, it should be possible to ask a specific part for its selection, in 3.x, this is done by ISelectionService's getSelection(String) method

API Comparison

In Eclipse 3.x, the 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 ISelectionProvider that is attached to an IWorkbenchSite.

The Eclipse 4.x ESelectionService performs functions similar to the ISelectionService although it also handles the propagation of selections.

Part providing a selection

Eclipse 3.x Eclipse 4.x
/**
 * 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);
}
@Inject
private ESelectionService selectionService;
 
@PostConstruct
void create() {
  // attach a selection listener to our jface viewer
  viewer.addSelectionChangedListener(new ISelectionChangedListener() {
    public void selectionChanged(SelectionChangedEvent event) {
      IStructuredSelection selection = (IStructuredSelection) event.getSelection();
      // set the selection to the service
      selectionService.setSelection(
          selection.size() == 1 ? selection.getFirstElement() : selection.toArray());
    }
  });
}

Part consuming the selection

Eclipse 3.x Eclipse 4.x
/**
 * 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) );
      }
    }
  });
}
@Inject
void setSelection(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) Contact contact) {
  if (contact == null) {
    /* implementation not shown */
  } else {
    /* implementation not shown */
  }
}

Handler Service

API Comparison

Execute a command

Eclipse 3.x Eclipse 4.x
IHandlerService service =
    serviceLocator.getService(IHandlerService.class);
service.executeCommand(
    IWorkbenchCommandConstants.CLOSE_ALL, null);
@Inject
private EHandlerService handlerService;
 
@Inject
private ECommandService commandService;
 
public void closeAll() {
  ParameterizedCommand command =
      handlerService.createCommand(IWorkbenchCommandConstants.CLOSE_ALL, 
          null);
  commandService.executeHandler(command);
}