Thursday Sep 07, 2006
Thursday Sep 07, 2006
Here is a mini tutorial that teaches you how to dynamically build a tree structure from data in a database. You populate the first-level nodes in the Tree with names from a database and the second-level nodes with the trip dates for that person.
Notes: Due to cookie problems, it is hard to tell which node the user selects. See http://blogs.sun.com/divas/entry/workaround_for_dynamic_trees for a workaround
Events related to tree node selection do not work correctly in portlets because the Tree component uses cookies to pass the selected node id back and forth, and cookies are not correctly handled by the portlet container.
You begin by building a home page that includes the Tree component and the TRIP database table. The following figure shows the page.
Figure 1: Page Design |
DatabaseTree.Travel Information, and press Enter. In the Properties window, set the id property to displayTree and the clientSide property to True.
clientSide is True, every child node (expanded or not) is sent to the client, but is not made visible unless the
parent node is expanded. When clientSide is False, only the child nodes of the expanded parent nodes are rendered.Next, you connect the page with a database table in the Travel data source. You then use the Query Editor to modify the SQL query used to retrieve the data so that the traveler names appear in alphabetical order and the travel dates appear in chronological order.
Figure 2 : Query Editor |
Now you add code to the prerender() method to dynamically build the Tree component from the TRIP and PERSON database tables.
prerender method. Replace the body of the prerender method with the following code shown in bold.| Code Sample 1: prerender method for Page1 |
public void prerender() {
try {
// Set up the variables we will need
Integer currentPersonId = new Integer(-1);
// If nbrChildren is not 0 then this is a
// postback and we have our tree already
int nbrChildren = displayTree.getChildCount();
if (nbrChildren == 0) {
// List of outer (person) nodes
List outerChildren = displayTree.getChildren();
// Erase previous contents
outerChildren.clear();
// List of inner (trip) nodes
List innerChildren = null;
// Execute the SQL query
tripDataProvider.refresh();
// Iterate over the rows of the result set.
// Every time we encounter a new person, add first level node.
// Add second level trip nodes to the parent person node.
boolean hasNext = tripDataProvider.cursorFirst();
while (hasNext) {
Integer newPersonId =
(Integer) tripDataProvider.getValue(
"TRIP.PERSONID");
if (!newPersonId.equals(currentPersonId)) {
currentPersonId = newPersonId;
TreeNode personNode = new TreeNode();
personNode.setId("person" + newPersonId.toString());
personNode.setText(
(String)tripDataProvider.getValue(
"PERSON.NAME"));
outerChildren.add(personNode);
innerChildren = personNode.getChildren();
}
// Create a new trip node
TreeNode tripNode = new TreeNode();
tripNode.setId("trip" +
tripDataProvider.getValue(
"TRIP.TRIPID").toString());
tripNode.setText(
tripDataProvider.getValue(
"TRIP.DEPDATE").toString());
// Set "action" property to use for navigation
tripNode.setAction(
getApplication().createMethodBinding
("#{Page1.tripNode_action}", null));
innerChildren.add(tripNode);
hasNext = tripDataProvider.cursorNext();
}
}
} catch (Exception ex) {
log("Exception gathering tree data", ex);
error("Exception gathering tree data: " + ex);
}
}
|
Figure 3: Dynamic Tree Node |
Posted by Todd on September 18, 2006 at 09:49 AM PDT #
Posted by diva#2 on September 18, 2006 at 09:54 AM PDT #
In prerender() Code Sample:
int nbrChildren = displayTree.getChildCount();
Where is displayTree declared?
I get a compilation error.
Posted by Alexandra on June 27, 2008 at 06:58 AM PDT #
Alexandra,
Step 2 says: From the Basic section of the Palette, drag a Tree component onto the Page, type Travel Information, and press Enter. In the Properties window, set the id property to displayTree and the clientSide property to True.
When you set the id property to displayTree, that changes the name of the tree object to displayTree.
Posted by diva #2 on June 30, 2008 at 09:48 AM PDT #
Thanks Alexandra!
Posted by Guillermo on September 07, 2008 at 07:51 PM PDT #