• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic TreeViewer: Add nodes in the tree expand event.  RSS feed

Richard Hart
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,

I am new to using SWT and stumbled onto a solution to my problem. I had searched the web for the solution with no success so I am sharing my solution in this forum in hopes that it will help others.

All the example dynamic trees used a button Action to insert the node. I could find no example of inserting nodes in events like expand or collapse.

I am building an Eclipse plugin that uses a TreeViewer. The data for the tree comes from a web service and is too large to load everything upon initialization because it takes way too long. The thought is to dynamically load the nodes as needed. Initially only the first two levels are read. The reason for two levels is so that where a parent has children, the “+” symbol appears so that the branch can be expanded by the user. As the user expands branches, the needed nodes are loaded.

The natural place to read the new nodes into the tree structure is in the TreeExpansionEvent. The only problem with this is that SWT disables the Display.refresh() methods in this event because the view is being updated. Loading up the new nodes in the event will change the domain model but it is not reflected in the view because the refresh is never actually executed.

My first thought was to perform the loading of the new nodes in a new Thread but that too is prevented by SWT with an exception. The thread calling the refresh must be the same thread that created the Display.

The trick is to use the Display.asyncExec() method.

Here is my implementation of the treeListener listener:

Here is the UpdateTreeThread class:

The objectManager is not important for the example. It only provides access to the data to be loaded into the tree.

You just need to implement Runnable for whatever class will be executed by the Display.asyncExec() method. In my case is has class instance variables of TreeExpansionEvent and DctmObjectManager that are initialized in the TreeExpansionEvent listener before calling the asyncExec method.

I do not show the TreeBranchDisposeThread code because it is similar to the UpdateTreeThread. The only difference is disposing in stead of inserting.

I hope this helps others who are looking to make a dynamic TreeViewer.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!