Source Viewer : WEB-INF/classes/org/apache/click/examples/page/tree/PageLinkTreePage.java
package org.apache.click.examples.page.tree;
import org.apache.click.Page;
import org.apache.click.control.PageLink;
import org.apache.click.examples.page.introduction.AdvancedForm;
import org.apache.click.examples.page.introduction.AdvancedTable;
import org.apache.click.examples.page.introduction.ControlListenerType1Page;
import org.apache.click.examples.page.introduction.ControlListenerType2Page;
import org.apache.click.examples.page.introduction.HelloWorld;
import org.apache.click.examples.page.introduction.SimpleForm;
import org.apache.click.examples.page.velocity.SimpleTable;
import org.apache.click.extras.tree.Tree;
import org.apache.click.extras.tree.TreeNode;
import org.apache.click.util.HtmlStringBuffer;
import org.apache.commons.lang.ClassUtils;
/**
* Demonstrates how to customize the rendering of tree nodes.
* <p/>
* In this example tree nodes render links to Pages.
*/
public class PageLinkTreePage extends TreePage {
private static final long serialVersionUID = 1L;
public static final String LINK_TREE_NODES_SESSION_KEY = "pageLinkTreeNodes";
// Protected Methods ------------------------------------------------------
/**
* Creates and return a new tree instance.
*/
@Override
protected Tree createTree() {
return new Tree("tree") {
private static final long serialVersionUID = 1L;
@Override
protected void renderValue(HtmlStringBuffer buffer, TreeNode treeNode) {
Object nodeValue = treeNode.getValue();
Class cls = null;
if(nodeValue instanceof Class) {
cls = (Class) nodeValue;
}
// If node value is a Page class, render a PageLink, otherwise
// render the node value
if (cls != null && Page.class.isAssignableFrom(cls)) {
String shortName = ClassUtils.getShortClassName(cls);
PageLink link = new PageLink(shortName, cls);
buffer.append(link);
} else {
buffer.append(nodeValue);
}
}
};
}
/**
* Build the tree model and stores it in the session. This model represents
* nodes which link to other example Pages.
*/
@Override
protected TreeNode createNodes() {
//Create a node representing the root directory with the specified
//parameter as the value. Because an id is not specified, a random
//one will be generated by the node. By default the root node is
//not rendered by the tree. This can be changed by calling
//tree.setRootNodeDisplayed(true).
TreeNode root = new TreeNode("Pages");
//Create a new directory, setting the root directory as its parent. Here
//we do specify a id as the 2nd argument, so no id is generated.
TreeNode general = new TreeNode("Intro", "1");
root.add(general);
boolean supportsChildNodes = false;
general.add(new TreeNode(HelloWorld.class, "1.1", supportsChildNodes));
general.add(new TreeNode(ControlListenerType1Page.class, "1.2", supportsChildNodes));
general.add(new TreeNode(ControlListenerType2Page.class, "1.3", supportsChildNodes));
TreeNode forms = new TreeNode("Forms", "2");
root.add(forms);
forms.add(new TreeNode(SimpleForm.class, "2.1", supportsChildNodes));
forms.add(new TreeNode(AdvancedForm.class, "2.2", supportsChildNodes));
TreeNode tables = new TreeNode("Tables", "3");
root.add(tables);
tables.add(new TreeNode(SimpleTable.class, "3.1", supportsChildNodes));
tables.add(new TreeNode(AdvancedTable.class, "3.2", supportsChildNodes));
return root;
}
/**
* Return the string under which the nodes are stored in the session.
*
* @return the string under which the nodes are stored in the session
*/
@Override
protected String getSessionKey() {
return LINK_TREE_NODES_SESSION_KEY;
}
}