Workaround for Dynamic Trees

There have been many posts on the Sun Java Studio Creator IDE forum from people having trouble with using getSelected() or getCookieSelectedTreeNode() when building dynamic trees. We have had the same problems while writing our database tree tutorial. I have filed some bugs on their behalf as well as ours, but we won't see fixes in the near future. In a nutshell, you can't tell what node was selected if the user has cookies turned off, and if there are stale cookies around, getCookieSelectedTreeNode might serve up that stale cookie instead of the actual node selected.

So, in the meantime, I came up with this workaournd. What we do in the database tree tutorial (which is still work in progress) is to pass the selected value in a URL.

Here is the modified code. Notice the setUrl()

part in bold.

 
    public void prerender() {
        // If the Request Bean's personId is set, then
        // we just came back from the Trip page
        // and had displayed a selected trip.
        // We use the personId later to determine whether
        // to expand a person's node
        Integer expandedPersonId = getRequestBean1().getPersonId();
        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"));
                        // If the request bean passed a person id,
                        // expand that person's node
                        personNode.setExpanded(newPersonId.equals
                                (expandedPersonId));
                        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());
                    tripNode.setUrl("/faces/Trip.jsp?tripId=" +
                            tripDataProvider.getValue("TRIP.TRIPID").toString());
                    innerChildren.add(tripNode);
                    hasNext = tripDataProvider.cursorNext();
                }
            }
            
        } catch (Exception ex) {
            log("Exception gathering tree data", ex);
            error("Exception gathering tree data: " + ex);
        } 
     }

Then, in the destination page, we add the following code to pick up the passed parameter.

 


    public void prerender() {

        // Get the person id from the request parameters
        String parmTripId = (String)
        getExternalContext().getRequestParameterMap().get("tripId");
        
        if (parmTripId != null) {
            Integer tripId = new Integer(parmTripId);
            try {
                getSessionBean1().getTripRowSet1().setObject(1, tripId);
                tripDataProvider1.refresh();
            } catch (Exception e) {
                error("Cannot display trip " + tripId);
                log("Cannot display trip " + tripId, e);
            }
        }else {
            error("No trip id specified.");
        }
     }
Comments:

Post a Comment:
Comments are closed for this entry.
About

divas

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today