Search code examples
javaswteclipse-rcpjfacetreeviewer

Is getChildren() of ContentProvider called before treeExpanded() of ITreeViewerListener?


I want to fetch data if a specific element of a tree is expanded.

public void treeExpanded(TreeExpansionEvent event) {  
   Object element = event.getElement();
   if (element instanceof MyClass) {
      fetch.......
   }
} 

The problem is that the content provider seems to trigger getChildren() before the treeExpanded()-method so I get a NullPointerException everytime I expand the element because the content provider tries to use the data which is never fetched. How can i solve this properly?


Solution

  • Your code should not depend on the order in which getChildren() and treeExpanded() is called. This is an implementation detail of the TreeViewer and may change in the future.

    Fetch the elements to be shown in getChildren() or inputChanged() of the content provider.

    If your fetch operation takes too long to be run in the UI thread, run it in a background thread and return a placeholder element that is replaced, once the fetch operation finishes. For an example, see the DeferredTreeContentManager of org.eclipse.ui.

    Alternatively you can see if the JFace DeferredContentProvider fits your needs.