Thursday, July 13, 2006

Displaying Non-Resource Content Using the Common Navigator Framework

Introduction
This post discusses the procedure for displaying non-resource nodes in a CNF Viewer. Examples of non-resource nodes are database tables or servers.

Prerequisites
The article assumes that you have some familiarity with the Common Navigator Framework. If not there are some great articles by Michael Elder.

Define a View and a Viewer
Define a view and a viewer using the common navigator framework.


This definition also adds the content binding for displaying standard resource nodes in the project structure.

Our Content
Our content will consist of the class called Child which represents a non-resource child and a class called NRParent, which will form the parent of all Child classes in the project tree. Note that in a real world the Child class could be something like a database table.





Next a Content provider and a Label provider need to be defined for this content. The content provider is simple - if a parent is of type IProject, it contributes a NRParent instance, if a parent is of type NRParent, it provides some children of type Child and if it is of type Child, it just provides more children.

Here are the relevant code sections from the content provider and the label provider -






The point to note is that when the parent is of type IProject, is when you need to provide first level non-resource nodes to the project structure. Ofcourse you can provide non-resource nodes to a specific folder within a project too or even a file. In that case, check for the parent being of type IFolder or IFile respectively. I can't think of a specific use-case for it right now, but it is something that is possible.


Now add a navigatorContent extension that defines the content provider, the label provider and trigger point. Note the trigger point and the possible children elements.


Finally, add a viewerContentBinding to bind the above navigatorContent to the viewer.



That is it. Execute a Run configuration for the plugin and open the view to see the new non-resource nodes.

As suggested by Michael, the source code has been added as an attachment to bug 143127: [CommonNavigator] Examples

5 comments:

Michael D. Elder said...

Hi Aashish!

Great work! If you need a place to host the example, you can always attach it as a zip file for bug 143127: [CommonNavigator] Examples. If you'd like to check out the org.eclipse.ui.examples.navigator plugin from the Eclipse CVS repo and create a patch, I can review it for release so that your example will be part of the official Eclipse build.

One small note, for your getChildren() (and getElements()) methods, you'll want to cache the node you create to return as a child of IProject (or whatever the parent is). If you recreate the objects you return in the array, then the nodes will continually collapse whenever a structural refresh event is generated.

Aashish Patil said...

Hi Michael,

Thanks for the pointer.
I've posted the source as an attachment to the bug you mentioned.

Regarding the caching -
The tree widget already caches child nodes. Thus, the getChildren(...) in the content provider is called only the first time that the children are created unless an update is invoked on the parent node.

Anonymous said...

Hi Aashish,
Your article is very helpful. Thanks! I have a couple of questions:
1. I am interested in displaying Only non-resource content, i.e. I don't want to display the folders and files that may exist under the IProject. How can I accomplish this?
2. This is related to your comment that the tree viewer caches child nodes. In my case lets say that the the child elements of NRParent has changed. How do I force the viewer to refresh so that a new set of child elements are shown for NRParent?

I am very new to eclipse, my apologies if my questions are very basic.

-Atish

Евгений Флоров said...

Hi,
"override" option is not understandable.
Im use this option for override resource content manager inside one of project subtree. But this is not worked :(
In Eclipse sources only JDT use this.
Do you have any links to worked examples for this option use?

Aashish Patil said...

I'd recommend trying the Eclipse newsgroups.

Thanks,
Aashish

  DB Reading BASE: An Acid Alternative: In partitioned databases, trading some consistency for availability can lead to dramatic improvement...