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

Riena/Detached Views

< Riena
Revision as of 21:52, 13 May 2009 by Elias.eclipsesource.com (Talk | contribs) (Riena Detached Views)

Riena Detached Views

This document explains how to easily open 'auxilary' views when a certain navigation node is selected in the main window.

Riena detached views.png

Riena has a mechanism that allows developers to show a ViewPart in a separate 'detached' shell/window. This can be used to show one or more views in separate shells. These shells are draggable, moveable, but not closeable.

Only one view can be shown in each 'detached' shell. This view can be a regular ViewPart or an implementation of Riena's SubModuleView.

Hooking into the Navigation Tree

To show / hide / dispose the 'detached' shells, we want to react to events in the navigation tree on the left of the main window. Typically when a node is selected, the corresponding SubModuleView is shown in the workarea (i.e. main area on the right). When this happens we would like to show additional 'detached' views. When the navigation node becomes unselected, we would like to hide these views.

To accomplish that we need to attach a custom listener to the view's navigation node. This is typically done in the #basicCreatePartControl(...) method of your sub module:

protected void basicCreatePartControl(Composite parent) {
  // ...
  getNavigationNode().addSimpleListener(new NodeListener());
}

The class NodeListener extends SimpleNavigationNodeAdapter. This adapter has several methods, that are invoked at different points during the lifecycle of a navigation node. When the node becomes selected (=activated) the method #activated((INavigationNode source) is invoked -- in that case we show the detached view. When the node becomes de-selected (=deactivated) the method #deactivated(INavigationNode source) is invoked -- in that case we hide the detached view. When the node is destroyed (=disposed) the method #disposed(INavigationNode source) is invoked -- in that case perform any necessary clean-up.

private final class NodeListener extends SimpleNavigationNodeAdapter {
  public void activated(INavigationNode<?> source) {
    // show detached views
  }
  public void deactivated(INavigationNode<?> source) {
    // hide detached views
  } 		
  public void disposed(INavigationNode<?> source) {
    // clean-up
}

Showing / Hiding detached views

To show / hide / dipose the detached views we need to manage a Shell for each view. Riena provides the class DetachedViewsManager, which makes this easy.

  1. We need to create a new instance of DetachedViewsManager in our listener as a class variable. The manager needs to know the main window, so we pass it a reference to Shell or to IWorkbenchSite via the constructor.
  2. To show a view, we invoke dvManager.showView(String id, Class<? extends ViewPart> viewClazz, int position). The first time this method is invoked for a given id, it will create a new shell and show a new view instance of the type clazz in it. It will then open the shell at the given position. The second time it is invoked, the shell with the specified id will already exist, so it will just make it visible. More information on the parameters:
    • id: is an arbitrary String, used to identify the shell hosting this view
    • viewClazz: a class specifying a ViewPart which we want to show in the shell. This class must have a parameterless constructor.
    • position: one of SWT.{TOP,BOTTOM,LEFT,RIGHT}. Will open the detached view on the appropriate side of the main window. There is also an alternative method #showView(..., Rectangle bounds) which will open the shell with a position and size you specify</tt>
  1. To hide a view, we invoke dvManager.hideView(String id), where id is the name of the shell we wish to hide.
  2. To clean-up, we invoke dvManager.dispose(). This will dispose all shells that have been created but are currently hidden. Typically this is done in the #disposed(...) method of the node listener. Afterwards it is also a good idea to remove the node listener itself from the navigation node.

Here is the full code for the NodeListener class:

private final class NodeListener extends SimpleNavigationNodeAdapter {
  private final DetachedViewsManager dvManager = new DetachedViewsManager(getSite());
 
  public void activated(INavigationNode<?> source) {
    dvManager.showView("viewRight", TreeSubModuleView.class, SWT.RIGHT);
  }
 
  public void deactivated(INavigationNode<?> source) {
    dvManager.hideView("viewRight"); //$NON-NLS-1$
  }
 
  public void disposed(INavigationNode<?> source) {
    dvManager.dispose();
    getNavigationNode().removeSimpleListener(this);
  }
}

References

See:

  • class DetachedSubModuleView in the org.eclipse.riena.example.client bundle
  • class DetachedViewsManager in the org.eclipse.riena.ui.swt bundle

Back to the top