1 20 21 package com.methodhead.tree; 22 23 import java.util.Map ; 24 25 import javax.swing.tree.TreeNode ; 26 27 public class ClientTreeRenderer 28 extends TreeRenderer { 29 30 32 34 36 38 41 protected void renderHeader( 42 StringBuffer buf ) 43 throws 44 TreeException { 45 46 buf.append( "<table>\n" ); 47 } 48 49 52 protected void renderFooter( 53 StringBuffer buf ) 54 throws 55 TreeException { 56 57 buf.append( "</table>\n" ); 58 } 59 60 61 protected void renderScript( 62 StringBuffer buf ) { 63 64 String openedHandleImage = getOpenedHandleImage(); 65 String closedHandleImage = getClosedHandleImage(); 66 67 buf.append( 68 "<script> " + 69 " " + 70 " function getNextTr( el ) { " + 71 " for ( var s = el.nextSibling; s != null; s = s.nextSibling ) " + 72 " if ( s.nodeName.toLowerCase() == \"tr\" ) " + 73 " return s; " + 74 " return null; " + 75 " } " + 76 " " + 77 " function getFirstNode( el ) { " + 78 " for ( var i = 0; i < el.childNodes.length; i++ ) " + 79 " if ( el.childNodes[ i ] && " + 80 " ( el.childNodes[ i ].nodeType == 1 ) ) " + 81 " return el.childNodes[ i ]; " + 82 " return null; " + 83 " } " + 84 " " + 85 " function getFirstText( el ) { " + 86 " for ( var i = 0; i < el.childNodes.length; i++ ) " + 87 " if ( el.childNodes[ i ] && " + 88 " ( el.childNodes[ i ].nodeType == 3 ) ) " + 89 " return el.childNodes[ i ]; " + 90 " return null; " + 91 " } " + 92 " " + 93 " function toggleNode( el ) { " + 94 " var r = getNextTr( el.parentNode.parentNode ); " + 95 " var n = getFirstNode( el ); " + 96 " var t = getFirstText( el ); " + 97 " " + 98 " if ( r.style.display != \"none\" ) { " + 99 " r.style.display = \"none\"; " + 100 " " + 101 " if ( ( n == null ) && ( t != null ) ) " + 102 " t.nodeValue = \"+\"; " + 103 " else " + 104 " n.src = \"" + closedHandleImage + "\"; " + 105 " } " + 106 " else { " + 107 " r.style.display = \"\"; " + 108 " " + 109 " if ( ( n == null ) && ( t != null ) ) " + 110 " t.nodeValue=\"-\"; " + 111 " else " + 112 " n.src = \"" + openedHandleImage + "\"; " + 113 " } " + 114 " } " + 115 "</script>\n" ); 116 } 117 118 protected void renderNode( 119 StringBuffer buf, 120 FoldingTreeNode node ) { 121 122 String s = null; 123 Map iconImages = getIconImages(); 124 125 buf.append( "<tr>" ); 129 130 if ( node.isLeaf() ) { 131 if ( iconImages == null ) { 132 buf.append( "<td> </td>" ); 133 } 134 else { 135 s = ( String )iconImages.get( SPACER_ICON ); 136 137 if ( s == null ) 138 buf.append( "<td> </td>" ); 139 else 140 buf.append( 141 "<td><img SRC=\"" + s + "\"></img></td>" ); 142 } 143 } 144 else { 145 146 buf.append( 150 "<td><a onclick=\"toggleNode(this)\" HREF=\"javascript:;\">" ); 151 152 if ( node.getOpened() ) { 153 if ( getOpenedHandleImage() == null ) 154 buf.append( "-" ); 155 else 156 buf.append( 157 "<img border=\"0\" SRC=\"" + getOpenedHandleImage() + "\"></img>" ); 158 } 159 else { 160 if ( getClosedHandleImage() == null ) 161 buf.append( "+" ); 162 else 163 buf.append( 164 "<img border=\"0\" SRC=\"" + getClosedHandleImage() + "\"></img>" ); 165 } 166 167 buf.append( "</a></td>" ); 168 } 169 170 if ( iconImages != null ) { 174 175 s = ( String )iconImages.get( node.getIconHint() ); 176 177 if ( s == null ) 178 s = ( String )iconImages.get( DEFAULT_ICON ); 179 180 if ( s == null ) { 181 buf.append( "<td> </td>" ); 182 } 183 else { 184 if ( node.getUrl() == null ) 185 buf.append( "<td><img SRC=\"" + s + "\"></td>" ); 186 else 187 buf.append( 188 "<td><a HREF=\"" + node.getUrl() + "\"><img border=\"0\" SRC=\"" + 189 s + "\"></a></td>" ); 190 } 191 } 192 193 if ( node.getUrl() == null ) 197 buf.append( "<td width=\"100%\">" + node.getLabel() + "</td>" ); 198 else 199 buf.append( 200 "<td width=\"100%\"><a HREF=\"" + node.getUrl() + "\">" + 201 node.getLabel() + "</a></td>" ); 202 203 buf.append( "</tr>\n" ); 204 205 if ( !node.isLeaf() ) { 206 207 buf.append( "<tr style=\"display:" ); 211 212 if ( node.getOpened() ) 213 buf.append( "normal" ); 214 else 215 buf.append( "none" ); 216 217 buf.append( "\"><td> </td>" ); 218 219 buf.append( "<td colspan=\"2\"><table>" ); 220 221 for ( int i = 0; i < node.getChildCount(); i++ ) 222 renderNode( buf, ( FoldingTreeNode )node.getChildAt( i ) ); 223 224 buf.append( "</table></td></tr>\n" ); 225 } 226 } 227 228 public String renderTree( 229 FoldingTreeNode root ) 230 throws 231 TreeException { 232 233 StringBuffer buf = new StringBuffer (); 234 235 if ( !scriptRendered_ ) { 236 scriptRendered_ = true; 237 renderScript( buf ); 238 } 239 240 renderHeader( buf ); 241 242 if ( isRootHidden() ) { 243 for ( int i = 0; i < root.getChildCount(); i++ ) { 244 renderNode( buf, ( FoldingTreeNode )root.getChildAt( i ) ); 245 } 246 } 247 else { 248 renderNode( buf, root ); 249 } 250 251 renderFooter( buf ); 252 253 return buf.toString(); 254 } 255 256 258 260 private boolean scriptRendered_ = false; 261 } 262 | Popular Tags |