package jaxcent.yui; import jaxcent.*; import java.util.*; /** * Class Tree encapsulates the YUI Tree component. * Objects of this class must be initialized with the id for the DIV where * the tree is to be placed. After that, nodes can be added/removed to the * tree, and other operations can be performed. */ public class Tree extends JaxcentPage { String treeVarName; // Internal JS name for tree Hashtable nodeList = new Hashtable(); // List of tree nodes boolean dateCreated = false; // Variables for date formatting char dateSep = '/'; boolean dateMonthFirst = true; /** * To create the tree on the page, init must be called with the * id for a DIV where the tree is to be drawn. */ public void init( String treeDivId ) throws Jaxception { treeVarName = getJSVar(); execJavaScriptCode( treeVarName + " = new YAHOO.widget.TreeView( '" + treeDivId + "');" ); execJavaScriptCode( treeVarName + ".subscribe('clickEvent', yuiTreeClickHandler );" ); draw(); } // Called in node constructor to add nodes to nodeList void nodeAdded( TreeNode node ) { nodeList.put( node.getVarName(), node ); } /** * The draw method updates the tree. */ public void draw() throws Jaxception { execJavaScriptCode( treeVarName + ".draw();" ); } // Quote JavaScript strings. String quote( String str ) { if ( str.indexOf( '\'' ) < 0 ) return str; StringBuffer sb = new StringBuffer(); for ( int i = 0; i < str.length(); i++ ) { char ch = str.charAt( i ); sb.append( ch ); if ( ch == '\'' ) sb.append( ch ); } return sb.toString(); } // Internal method for adding a text node to the tree TreeNode addTextNode( String text, boolean editable, String parent ) throws Jaxception { TreeNode node = new TreeNode( this, editable, false ); String varname = node.getVarName(); if ( parent == null ) parent = treeVarName + ".getRoot()"; String str = varname + " = new YAHOO.widget.TextNode('" + quote( text ) + "', " + parent + ", true)"; execJavaScriptCode( str ); execJavaScriptCode( varname + ".varname = '" + varname + "'" ); if ( editable ) execJavaScriptCode( varname + ".editable = true" ); draw(); return node; } /** Add a non-editable text node to the tree root */ public TreeNode addTextNode( String text ) throws Jaxception { return addTextNode( text, false, null ); } /** Add an editable text node to the tree root */ public TreeNode addEditableTextNode( String text ) throws Jaxception { return addTextNode( text, true, null ); } /** Sets the format for displaying the date in date nodes. * sep is the character to use to separate parts of the date. * monthFirst is false if month comes after date, true if month comes first. */ public void setDateFormat( char sep, boolean monthFirst ) throws Jaxception { if ( dateCreated ) throw new Jaxception( "Dates have already been created, cannot change separator" ); if ( sep == '\'' || sep == '"' ) throw new Jaxception( "Invalid date separator" ); dateSep = sep; dateMonthFirst = monthFirst; } // Internal method to add date node TreeNode addDateNode( java.util.Date date, boolean editable, String parent ) throws Jaxception { TreeNode node = new TreeNode( this, editable, true ); String varname = node.getVarName(); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.setTime( date ); String datestr = ""; dateCreated = true; if ( dateMonthFirst ) { datestr += 1 + cal.get( cal.MONTH ); datestr += dateSep; datestr += cal.get( cal.DAY_OF_MONTH ); } else { datestr += cal.get( cal.DAY_OF_MONTH ); datestr += dateSep; datestr += 1 + cal.get( cal.MONTH ); } datestr += dateSep; datestr += cal.get( cal.YEAR ); String str = varname + " = new YAHOO.widget.DateNode({type:'DateNode',label:'" + datestr + "',"; if ( editable ) { str += "editable:true,"; } else str += "editable:false,"; str += "calendarConfig: { DATE_FIELD_DELIMITER:'" + dateSep + "',"; if ( dateMonthFirst ) str += "MDY_MONTH_POSITION:1,MDY_DAY_POSITION:2,MDY_YEAR_POSITION:3"; else str += "MDY_DAY_POSITION:1,MDY_MONTH_POSITION:2,MDY_YEAR_POSITION:3"; str += "}}, "; if ( parent == null ) parent = treeVarName + ".getRoot()"; str += parent; str += ", true )"; execJavaScriptCode( str ); execJavaScriptCode( varname + ".varname = '" + varname + "'" ); draw(); return node; } /** Add a non-editable date node to the tree root */ public TreeNode addDateNode( java.util.Date date ) throws Jaxception { return addDateNode( date, false, null ); } /** Add an editable date node to the tree root */ public TreeNode addEditableDateNode( java.util.Date date ) throws Jaxception { return addDateNode( date, true, null ); } /** Expand the entire tree. */ public void expandAll() throws Jaxception { execJavaScriptCode( treeVarName + ".expandAll();" ); } /** Collapse the entire tree. */ public void collapseAll() throws Jaxception { execJavaScriptCode( treeVarName + ".collapseAll();" ); } // Delete a node void deleteNode( String nodeVarName ) throws Jaxception { execJavaScriptCode( treeVarName + ".removeNode( " + nodeVarName + ", true );" ); draw(); } /** * Over-ridable method for receiving click events on tree nodes. */ protected void onClick( TreeNode node ) { } protected void onJavaScriptRequest(java.lang.String cmd, java.lang.String[] args) { if ( cmd.equals( "YuiTreeOnClick" )) { onClick( (TreeNode) nodeList.get( args[0] )); } } // Internal method for generating JavaScript variable names. static int varNumber = 1; synchronized static String getJSVar() { return "Jaxcent__YUI_InternalVar__" + varNumber++; } }