Architecture

UI Core

Plug-in "org.eclipse.gmt.modisco.infra.browser.uicore" contains the core parts of the browser, that can be integrated in any tree viewer. It provides a tree content provider, a label provider, and a customization manager.

CustomizableModelContentProvider and CustomizableModelLabelProvider are abstract classes that must be sub-classed, providing an instance of a CustomizationManager.

The CustomizationManager class is the entry point for customizing the way things look and behave in the browser, through:

If parameters are modified in CustomizationManager after the TreeViewer was displayed, the viewer must be refreshed explicitly to account for these changes.

You may also want to override method getRootElements in CustomizableModelContentProvider, to provide the EObjects you want to display at the root of the tree.

Pay attention to the fact that the elements in the JFace model are not directly EObjects, but objects internal to the browser. So, don't redefine the getElements method to return EObjects, since that wouldn't work.

EObject from selection

If you want to get an EObject from a selection, you must use an adapter like this:

EObject eObject = (EObject) Platform.getAdapterManager().getAdapter(selectedElement, EObject.class);

Selection from EObject

To select an EObject in a TreeViewer created using UiCore, you can't just create a StructuredSelection(eObject), since the TreeViewer is composed of elements that encapsulate the EObjects.

Instead, you can retrieve the wrapping element by doing something like this:

 public Object findElementForEObject(EObject eObjectToFind, TreeViewer treeViewer) {
   ITreeContentProvider contentProvider = (ITreeContentProvider) treeViewer
       .getContentProvider();
   Object[] elements = contentProvider.getElements(treeViewer.getInput());
   LinkedList<Object> elementsToHandle = new LinkedList<Object>();
   for (Object element : elements) {
     elementsToHandle.add(element);
   }
   while (!elementsToHandle.isEmpty()) {
     Object e = elementsToHandle.removeFirst();
     EObject eObject = (EObject) Platform.getAdapterManager().getAdapter(e, EObject.class);
     if (eObject != null && eObject.equals(eObjectToFind)) {
       return e;
     }
     if (contentProvider.hasChildren(e)) {
       Object[] children = contentProvider.getChildren(e);
       if (children != null) {
         for (Object child : children) {
           elementsToHandle.addLast(child);
         }
       }
     }
   }
   return null;
 }

Context menu / Selection provider

If you need to use a selection provider, for example if you want to add a context menu on your UiCore viewer, you can use UnwrappingSelectionProvider :

getSite().registerContextMenu(MENU_ID, contextMenu, new UnwrappingSelectionProvider(treeViewer));

UnwrappingSelectionProvider also offers static methods should you need them:

prototype to avoid browser freezing with virtual tree

Here is the description of a strategy that moves all computations previously done in the UI thread (in the content and label providers) into background jobs. It relies on the SWT.VIRTUAL style for the SWT Tree, that allows using a lazy content provider (ILazyTreeContentProvider). Together with a label provider that extends CellLabelProvider, that allows doing asynchronous updates (again, in order to not block the UI thread).

It works like this: JFace asks for an update (through the content or label provider), that is enqueued on one of the Jobs (round-robin with the number of Jobs equal to the number of available processors). Once the information is computed, it is applied on the UI thread (mandatory with SWT). These updates are batched in order to minimize the number of switches to the UI thread.

With this strategy implemented, the browser does not freeze anymore, but it is unfortunately a lot slower on Windows. This seems currently unavoidable on Windows (see Bug 129457 - Virtual Tree very slow).

This prototype is available on a branch: https://dev.eclipse.org/svnroot/modeling/org.eclipse.mdt.modisco/plugins/branches/browser_lazy

Category:MoDisco

Updating This Document

This document is maintained in a collaborative wiki. If you wish to update or modify this document please visit http://wiki.eclipse.org/MoDisco/Components/ModelBrowser/Documentation/0.9