Previous Page
Next Page

7.4. Linking the View

In many situations, the current selection in the active view can affect the selection in other views, cause an editor to open, change the selected editor, or change the selection within an already open editor. For example, in the Java browsing perspective (see Section 1.2.1.1, Java perspectives, on page 7), changing the selection in the Types view changes the selection in both the Projects and the Packages views, changes the content displayed in the Members view, and changes the active editor. For a view to both publish its own selection and to consume the selection of the active part, it must be both a selection provider and a selection listener.

7.4.1. Selection provider

For a view to be a selection provider, it must register itself as a selection provider with the view site. In addition, each of the objects contained in the view should be adaptable (see the next section) so that other objects can adapt the selected objects into objects they can understand. In the Favorites view, register the view as a selection provider by adding the following to the createTableViewer() method:

getSite().setSelectionProvider(viewer);

7.4.2. Adaptable objects

The org.eclipse.core.runtime.IAdaptable interface allows an object to convert one type of object that it may not understand to another type of object that it can interrogate and manipulate (more on adapters in Section 20.3, Adapters, on page 714). For the Favorites view, this means that the IFavoritesItem interface must extend the IAdaptable interface, and the following two getAdapter() methods must be added to FavoriteResource and FavoriteJavaElement, respectively.

public Object getAdapter(Class adapter) {
   if (adapter.isInstance(resource))
      return resource;
   return Platform.getAdapterManager().getAdapter(this, adapter);
}

public Object getAdapter(Class adapter) {
   if (adapter.isInstance(element))
      return element;
   IResource resource = element.getResource();
   if (adapter.isInstance(resource))
      return resource;
   return Platform.getAdapterManager().getAdapter(this, adapter);
}

7.4.3. Selection listener

For a view to consume the selection of another part, it must add a selection listener to the page so that when the active part changes or the selection in the active part changes, it can react by altering its own selection appropriately. For the Favorites view, if the selection contains objects that can be adapted to the objects in the view, then the view should adjust its selection. To accomplish this, add a call at the end of the createPartControl() method to the following new hookPageSelection() method.

private ISelectionListener pageSelectionListener;

private void hookPageSelection() {
   pageSelectionListener = new ISelectionListener() {
      public void selectionChanged(
         IWorkbenchPart part,
         ISelection selection) {
            pageSelectionChanged(part, selection);
      }
   };
   getSite().getPage().addPostSelectionListener(
      pageSelectionListener);
}
protected void pageSelectionChanged(
   IWorkbenchPart part,
   ISelection selection
) {
  if (part == this)
     return;
  if (!(selection instanceof IStructuredSelection))
     return;
  IStructuredSelection sel = (IStructuredSelection) selection;
  IFavoriteItem[] items = FavoritesManager.getManager()
     .existingFavoritesFor(sel.iterator());
  if (items.length > 0)
     viewer.setSelection(new StructuredSelection(items), true);
}

Then add the following to the dispose() method to clean up when the Favorites view is closed.

if (pageSelectionListener != null)
   getSite().getPage().removePostSelectionListener(
       pageSelectionListener);

7.4.4. Opening an editor

When a user double-clicks on a file in the Favorites view, a file editor should open. To accomplish this, add a new method to the MouseAdapter in the hookMouse method of the FavoritesView class.

public void mouseDoubleClick(MouseEvent e) {
   OpenEditorActionDelegate.openEditor(
      getSite().getPage(), viewer.getSelection());
}

This method references a new static method in a new OpenEditor-ActionDelegate class. The new method examines the first element in the current selection, and if that element is an instance of IFile, opens an editor on that file.

public static void openEditor(
   IWorkbenchPage page, ISelection selection)
{

   // Get the first element.

   if (!(selection instanceof IStructuredSelection))
       return;
   Iterator iter = ((IStructuredSelection) selection).iterator();
   if (!iter.hasNext())
       return;
   Object elem = iter.next();
   // Adapt the first element to a file.

   if (!(elem instanceof IAdaptable))
       return;

   IFile file = (IFile) ((IAdaptable) elem).getAdapter(IFile.class);
   if (file == null)
       return;

   // Open an editor on that file.

   try {
      IDE.openEditor(page, file);
    }
   catch (PartInitException e) {
      FavoritesLog.logError(
         "Open editor failed: " + file.toString(), e);
    }
}


Previous Page
Next Page