JFace TreeViewer

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

JFace TreeViewer

Postby cochand » Wed Jul 06, 2005 1:52 pm

How does WindowBuilder Pro help set the nodes of a JFace TreeViewer?

I see a data Property, which accepts Keys and Values. Entering data there doesn't change anything - the TreeViewer still contains the default Item 1 and Item 2 settings.

The following code is generated, where treeViewer.setInput has the parameter new Object(). I'm not sure how to set treeViewer values in WBP.

protected void createContents() {
shell = new Shell();
shell.setLayout(new RowLayout());
shell.setSize(467, 309);
shell.setText("SWT Application");

final TreeViewer treeViewer = new TreeViewer(shell, SWT.BORDER);
treeViewer.setLabelProvider(new TreeLabelProvider());
tree = treeViewer.getTree();
final RowData rowData = new RowData();
rowData.height = 243;
rowData.width = 168;
tree.setLayoutData(rowData);
tree.setLinesVisible(true);
tree.setData("k2", "k2v");
tree.setData("k1", "k1v");
treeViewer.setInput(new Object());

final Button button = new Button(shell, SWT.NONE);
final RowData rowData_1 = new RowData();
rowData_1.width = 247;
button.setLayoutData(rowData_1);
button.setText("button");
}
cochand
 
Posts: 17
Joined: Wed Jul 06, 2005 1:20 pm

Re: JFace TreeViewer

Postby Eric Clayberg » Thu Jul 07, 2005 3:56 am

cochand wrote:How does WindowBuilder Pro help set the nodes of a JFace TreeViewer?

In the property pane, you need to set the TreeViewer's contentProvider. You can either create a new content provider or you can select an existing class.

Alternatively, you can drop individual TreeItems on the inner Tree component of the TreeViewer. If you are going to do that, you should just use a simple Tree rather than a TreeViewer. A TreeViewer is a more object-oriented component that expects to be provided with a model.

cochand wrote:I see a data Property, which accepts Keys and Values. Entering data there doesn't change anything - the TreeViewer still contains the default Item 1 and Item 2 settings.

The data property contains arbitrary key/value pairs defined by your application (i.e, meta data). The Tree widget has no special knowledge of it.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Postby cochand » Thu Jul 07, 2005 11:56 am

My app needs to respond to tree node events, so it looks like I should use the JFace TreeViewer.

I see the class TreeContentProvider that was automatically generated when using WBP to double click on the contentProvider event of a JFace TreeViewer. I see the TreeContentProvider.getChildren method with item_0, item_1, and item_2.

I do not understand why this results in a tree with no leaf nodes (item_0, 1, and 2 repeat endlessly). How do I specify the tree hierarchy - ie some nodes with their specific children, and some leaf nodes?
cochand
 
Posts: 17
Joined: Wed Jul 06, 2005 1:20 pm

Postby cochand » Thu Jul 07, 2005 12:32 pm

The simple question is, How do I specify leaf nodes?
cochand
 
Posts: 17
Joined: Wed Jul 06, 2005 1:20 pm

Postby cochand » Thu Jul 07, 2005 1:47 pm

My solution so far has been to hard code the creation of the top level nodes, the leaves, and those inbetween, as shown below. Seems pretty hokey:

public Object[] getChildren(Object parentElement) {
Object objArrayNodes[];

// Create the nodes with no children (return an empty Object array)
if( parentElement.equals( "B" ) ||
parentElement.equals( "C" ) ||
parentElement.equals( "1" ) ||
parentElement.equals( "2" ) ||
parentElement.equals( "3" )
)
return new Object[] {};


// Create the nodes with children
if( parentElement.equals( "A" ) )
{
objArrayNodes = new Object[] { "1", "2", "3" };
return objArrayNodes;
}


// Create the top level nodes
objArrayNodes = new Object[] { "A", "B", "C" };
return objArrayNodes;
}
cochand
 
Posts: 17
Joined: Wed Jul 06, 2005 1:20 pm

Postby Eric Clayberg » Sun Jul 10, 2005 2:18 pm

cochand wrote:My app needs to respond to tree node events, so it looks like I should use the JFace TreeViewer. I see the class TreeContentProvider that was automatically generated when using WBP to double click on the contentProvider event of a JFace TreeViewer. I see the TreeContentProvider.getChildren method with item_0, item_1, and item_2. I do not understand why this results in a tree with no leaf nodes (item_0, 1, and 2 repeat endlessly). How do I specify the tree hierarchy - ie some nodes with their specific children, and some leaf nodes?

You are responsible for creating a content provider that corresponds to your model. WindowBuilder generates an initial class for you, but you need to modify it as needed by your application. The getChildren() method is responsible for returning the children for the input node. The default implementation will return three simple objects for every node in the tree. This results in a tree that goes on forever because every node always has three children.

cochand wrote:How do I specify leaf nodes?

A leaf node should return any empty collection of childen.

cochand wrote:My solution so far has been to hard code the creation of the top level nodes, the leaves, and those inbetween, as shown below. Seems pretty hokey

If you are going to use a TreeViewer, you need to create a content provider that reflects your domain model which presumably is already represented in a tree structure. Setting up the getChildren() method to map to your domain model should then be quite easy.

If you don't have a domain model and are simply intent on hard-coding a set of tree nodes, then you should not be using a TreeViewer. You should be using a simple Tree widget with simple TreeItems (all of which you can configure via drag/drop within Designer).
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest