View and editor selections in Bioclipse2

From Bioclipse
Jump to: navigation, search
Development tutorial
Responsible author:Ola
Bioclipse version:N/A
"N/A" is not a number.
Last updated:2009-10-10
Tags:


Motivation

An editor should support both the outline view and properties view, this is done via Adapters.

Adapters

This is an example to return an adapter for the ContentOutlinePage (example from net.bioclipse.jmol):

public Object getAdapter(Class required) {
  if (IContentOutlinePage.class.equals(required)) {
    if (fOutlinePage == null) {
      fOutlinePage= new JmolContentOutlinePage(getEditorInput(), this);
    }
    return fOutlinePage;
  }
  return super.getAdapter(required);
}

And the OUltinePage must extend/implement:

  JmolContentOutlinePage extends ContentOutlinePage implements ISelectionListener


Synchronizing selection from an editor to a view

To synchronize selection from an editor to a view, the view should register itself as a listener of the ISelectionService when it is initialized. It will then automatically receive notification about selection changes and the workbench part that originated them.

The Editor must register a provider, for example a viewer, by:

    getSite().setSelectionProvider( myTreeViewer );

An alternative is that the editor implements ISelectionProvider, but that is much more work.

An editor can only have one registered selectionProvider on the Site, and changing them after creation is not supported. If an editor has multiple viewers on, e.g. different tabs, the solution is to have an intermediate SelectionProvider that is updated for example onPageChange(). See the net.bioclipse.pcore plugin, the class PcoreEditorSelectionProvider. More information available on: http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html in section 'Multiple Selection Providers Within a Part'.


Synchronizing selection from a view to an editor

Listening to selections in the Outline View is one example. Do the following thing in the editor:

Implement IAdaptable and ISelectionListener.

In createPartControl (to register the listener), add:

  getSite().getPage().addSelectionListener(this);

Implement selectionChanged(...):

public void selectionChanged(IWorkbenchPart part, ISelection selection) {
  if (selection instanceof IStructuredSelection) {
    IStructuredSelection selection2 = (IStructuredSelection) selection;
    System.out.println("Editor caught selection: " + selection2.getFirstElement());
  }
}



Links