net.sf.click.extras.tree
Class CheckboxTree

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.extras.tree.Tree
          extended bynet.sf.click.extras.tree.CheckboxTree
All Implemented Interfaces:
Control, Serializable

public class CheckboxTree
extends Tree

Implementation of a tree control that provides checkboxes to enable selection of nodes. This implementation assumes the tree is wrapped inside a html form. Each time the form is submitted, all checkbox values are processed by this control.

Below is screenshot of how the tree will render in a browser.

Tree Example

An example tree usage is provided below (this code was used to produce the screenshot):
 public class PlainTreePage extends BorderPage {

     protected Submit submit;
     protected Submit cancel;
     protected Form form;

     public PlainTreePage() {
         Tree tree = createTree();
         form = createForm();
         form.add(tree);
         addControl(form);
         submit = new Submit("submit", this, "onSubmitClick");
         cancel = new Submit("cancel", this, "onCancelClick");
         form.add(submit);
         form.add(cancel);
     }

     public Tree createTree() {
         Tree tree = new CheckboxTree("tree");

         // Build the tree model, by default the root node is not rendered.
         // This can be changed by calling setRootNodeDisplayed(true);
         TreeNode root = new TreeNode("c:");
         TreeNode dev = new TreeNode("dev", "1", root);
         new TreeNode("java.pdf", "2", dev);
         new TreeNode("ruby.pdf", "3", dev);

         TreeNode programFiles = new TreeNode("program files", "4", root);
         TreeNode adobe = new TreeNode("Adobe", "5", programFiles);
         // This node is a directory not a file, so setChildrenSupported to true.
         adobe.setChildrenSupported(true);

         TreeNode download = new TreeNode("downloads", "6", root);
         TreeNode web = new TreeNode("web", "7", download);
         new TreeNode("html.pdf", "8", web);
         new TreeNode("css.html", "9", web);

         TreeNode databases = new TreeNode("databases", "10", download);
         new TreeNode("mysql.html", "11", databases);
         new TreeNode("oracle.pdf", "12", databases);
         new TreeNode("postgres", "13", databases);

         tree.setRootNode(root);
         return tree;
     }

     public Form createForm() {
         Form form = new Form("form") {
             // PLEASE NOTE: CheckboxTree will only be processed by form if the
             // Form is submitted. Thus expanding and collapsing Tree nodes
             // won't work by default because the Tree won't be processed.

             // Here we override the default behavior and explicitly process
             // CheckboxTree so that expanding and collapsing nodes will still work,
             // even if the Form was not submitted.
             public boolean onProcess() {
                 if (form.isFormSubmission()) {
                     return super.onProcess();
                 } else {
                     return tree.onProcess();
                 }
             }
         };

         return form;
     }
 } 

Author:
Bob Schellink
See Also:
Tree, Serialized Form

Nested Class Summary
protected  class CheckboxTree.BaseCheckboxJavascriptRenderer
          Please note this class is only meant for developers of this control, not users.
protected  class CheckboxTree.CheckboxCookieHandler
          Please note this class is only meant for developers of this control, not users.
protected  class CheckboxTree.CheckboxCookieJavascriptRenderer
          Please note this class is only meant for developers of this control, not users.
protected static interface CheckboxTree.CheckboxJavascriptRenderer
          Please note this interface is only meant for developers of this control, not users.
protected  class CheckboxTree.CheckboxSessionHandler
          Please note this class is only meant for developers of this control, not users.
protected  class CheckboxTree.CheckboxSessionJavascriptRenderer
          Please note this class is only meant for developers of this control, not users.
 
Nested classes inherited from class net.sf.click.extras.tree.Tree
Tree.AbstractJavascriptRenderer, Tree.Callback, Tree.CookieHandler, Tree.CookieRenderer, Tree.JavascriptHandler, Tree.JavascriptRenderer, Tree.SessionHandler, Tree.SessionRenderer
 
Field Summary
static String HTML_IMPORTS
          Client side javascript import.
protected static String[] TREE_RESOURCES
          The Tree resource file names.
 
Fields inherited from class net.sf.click.extras.tree.Tree
COLLAPSE_ICON, EXPAND_ICON, EXPAND_TREE_NODE_PARAM, expandOrCollapseNodeIds, JAVASCRIPT_COOKIE_IMPORTS, JAVASCRIPT_COOKIE_POLICY, JAVASCRIPT_IMPORTS, JAVASCRIPT_SESSION_POLICY, javascriptHandler, LEAF_ICON, rootNode, SELECT_TREE_NODE_PARAM, selectOrDeselectNodeIds, TREE_IMAGES, TREE_IMPORTS
 
Fields inherited from class net.sf.click.control.AbstractControl
actionListener, attributes, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface net.sf.click.Control
CONTROL_MESSAGES
 
Constructor Summary
CheckboxTree()
          Create a Tree with no name defined.
CheckboxTree(String name)
          Create an Tree control for the given name.
 
Method Summary
 void bindRequestValue()
          This method binds any expand/collapse changes from the request parameters.
 void bindSelectOrDeselectValues()
          Binds the users request of selected nodes to the tree's nodes.
protected  Decorator createDecorator()
          Creates and returns a custom Decorator that will render a Checkbox for each tree node.
protected  Tree.JavascriptHandler createJavascriptHandler(int javascriptPolicy)
          Creates and return a new JavascriptHandler for the specified tree node.
 String getHtmlImports()
          Return the HTML head import statements for the CSS stylesheet file: click/tree/checkbox-tree.js.
protected  String getInputType()
          Return the input type of the CheckboxTree, default value is "checkbox".
 boolean isSelectChildNodes()
          Returns true if child nodes will also be selected/deselected.
 void onDeploy(ServletContext servletContext)
          Deploy all files defined in the constant TREE_RESOURCES to the click/tree web directory when the application is initialized.
protected  void onFormSubmission()
          This method is invoked when the CheckboxTree parent Form is submitted.
 void onInit()
          Create and set the Tree's decorator that will render a Checkbox for each tree node.
protected  void renderCheckbox(HtmlStringBuffer buffer, TreeNode treeNode)
          Renders a Checkbox for the specified treeNode to the buffer.
 void setSelectChildNodes(boolean selectChildNodes)
          Sets whether child nodes will also be selected/deselected.
 
Methods inherited from class net.sf.click.extras.tree.Tree
addListener, bindExpandOrCollapseValues, cleanupSession, collapse, collapse, collapseAll, deselect, deselect, deselectAll, expand, expand, expandAll, expandOrCollapse, find, find, fireNodeCollapsed, fireNodeDeselected, fireNodeExpanded, fireNodeSelected, getControlSizeEst, getDecorator, getExpandClass, getExpandedNodes, getHref, getIconClass, getRequestValue, getRequestValues, getRootNode, getSelectedNodes, hasRootNode, isExpandedParent, isJavascriptEnabled, isNotifyListeners, isRootNodeDisplayed, iterator, iterator, onProcess, processNodes, processNodes, removeListener, render, renderExpandAndCollapseAction, renderIcon, renderTree, renderTreeNode, renderTreeNodeEnd, renderTreeNodeStart, renderValue, select, select, selectAll, selectOrDeselect, setActionListener, setDecorator, setExpandState, setExpandState, setExpandState, setJavascriptEnabled, setJavascriptEnabled, setListener, setNotifyListeners, setRootNode, setRootNodeDisplayed, setSelectState, setSelectState, setSelectState, shouldRenderChildren, toString
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getId, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, getTag, hasAttribute, hasAttributes, hasStyles, onDestroy, onRender, registerActionEvent, removeStyleClass, renderTagBegin, renderTagEnd, setAttribute, setId, setName, setParent, setStyle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HTML_IMPORTS

public static final String HTML_IMPORTS
Client side javascript import. This extends on the functions available in Tree.

See Also:
Constant Field Values

TREE_RESOURCES

protected static final String[] TREE_RESOURCES
The Tree resource file names.

Constructor Detail

CheckboxTree

public CheckboxTree(String name)
Create an Tree control for the given name.

Parameters:
name - the tree name
Throws:
IllegalArgumentException - if the name is null

CheckboxTree

public CheckboxTree()
Create a Tree with no name defined.

Please note the control's name must be defined before it is valid.

Method Detail

onInit

public void onInit()
Create and set the Tree's decorator that will render a Checkbox for each tree node.

See Also:
createDecorator(), Control.onInit()

isSelectChildNodes

public boolean isSelectChildNodes()
Returns true if child nodes will also be selected/deselected.

Returns:
true if child nodes will be selected, false otherwise

setSelectChildNodes

public void setSelectChildNodes(boolean selectChildNodes)
Sets whether child nodes will also be selected/deselected.

Please note: this feature only works if JavaScript support is enabled.

Parameters:
selectChildNodes - determines if child nodes will be selected/deselected

getHtmlImports

public String getHtmlImports()
Return the HTML head import statements for the CSS stylesheet file: click/tree/checkbox-tree.js.

This method calls super.getHtmlImports() to retrieve any imports defined in the super class.

Specified by:
getHtmlImports in interface Control
Overrides:
getHtmlImports in class Tree
Returns:
the HTML head import statements for the control stylesheet

onDeploy

public void onDeploy(ServletContext servletContext)
Deploy all files defined in the constant TREE_RESOURCES to the click/tree web directory when the application is initialized.

This method calls super.onDeploy() to copy any files defined in the super class.

Specified by:
onDeploy in interface Control
Overrides:
onDeploy in class Tree
Parameters:
servletContext - the servlet context
See Also:
Control.onDeploy(ServletContext)

bindSelectOrDeselectValues

public void bindSelectOrDeselectValues()
Binds the users request of selected nodes to the tree's nodes.

This method is automatically invoked when the CheckboxTree's parent form is submitted.

See onFormSubmission() for more details.

If you do not want CheckboxTree to automatically invoke this method, you can override onFormSubmission() to do nothing by default.

Then you must manually invoke this method when the form is submitted. For example:

 public void onInit() {
     CheckboxTree tree = new CheckboxTree("tree") {
         public void onFormSubmission() {
             // Do nothing
         }
     }
     Form form = createForm();
     form.add(tree);
     Submit submit = new Submit("submit");
     form.add(submit);
     submit.setActionListener(new ActionListener() {
         public boolean onAction(Control source) {
             tree.bindSelectOrDeselectValues();
             return true;
         }
     });
     addControl(form);
 } 

Overrides:
bindSelectOrDeselectValues in class Tree

bindRequestValue

public void bindRequestValue()
This method binds any expand/collapse changes from the request parameters.

In other words the node id's of expanded and collapsed nodes are retrieved from the request.

Overrides:
bindRequestValue in class Tree
See Also:
Tree.bindExpandOrCollapseValues()

onFormSubmission

protected void onFormSubmission()
This method is invoked when the CheckboxTree parent Form is submitted.

This method delegates to bindSelectOrDeselectValues() in order to update the selected and deselected nodes.


createDecorator

protected Decorator createDecorator()
Creates and returns a custom Decorator that will render a Checkbox for each tree node.

Returns:
a decorator that renders a Checkbox for each tree node

renderCheckbox

protected void renderCheckbox(HtmlStringBuffer buffer,
                              TreeNode treeNode)
Renders a Checkbox for the specified treeNode to the buffer.

This method invokes getInputType() which returns "checkbox" by default, but allows subclasses to change the input type if necessary.

Parameters:
buffer - string buffer containing the markup
treeNode - treeNode to render

getInputType

protected String getInputType()
Return the input type of the CheckboxTree, default value is "checkbox".

This method allows subclasses to change the input type if necessary. For example in order to render Radio buttons instead of Checkboxes, override this method and return the input type "radio".

Returns:
the input type of the CheckboxTree

createJavascriptHandler

protected Tree.JavascriptHandler createJavascriptHandler(int javascriptPolicy)
Creates and return a new JavascriptHandler for the specified tree node. This implementation overrides the super class, to return its own custom JavascriptHandlers.

Overrides:
createJavascriptHandler in class Tree
Parameters:
javascriptPolicy - the current javascript policy
Returns:
newly created JavascriptHandler