1 20 21 package com.methodhead.tree; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import javax.swing.tree.TreeNode ; 29 30 39 public class ServerTreeRenderer 40 extends TreeRenderer { 41 42 44 46 48 50 53 public String renderTree( 54 FoldingTreeNode root ) 55 throws 56 TreeException { 57 58 if ( openUrl_ == null ) 59 throw new TreeException( "openUrl has not been set." ); 60 61 if ( closeUrl_ == null ) 62 throw new TreeException( "closeUrl has not been set." ); 63 64 if ( getClosedHandleImage() == null ) 68 closedHandleImageHtml_ = "+"; 69 else 70 closedHandleImageHtml_ = 71 "<img border=\"0\" SRC=\"" + getClosedHandleImage() + "\">"; 72 73 if ( getOpenedHandleImage() == null ) 74 openedHandleImageHtml_ = "-"; 75 else 76 openedHandleImageHtml_ = 77 "<img border=\"0\" SRC=\"" + getOpenedHandleImage() + "\">"; 78 79 List nodes = new ArrayList (); 83 getVisibleNodes( nodes, root ); 84 85 int maxVisibleLevel = getMaxLevel( nodes ); 89 90 StringBuffer buf = new StringBuffer (); 94 renderHeader( buf ); 95 96 for ( Iterator iter = nodes.iterator(); iter.hasNext(); ) { 97 FoldingTreeNode node = ( FoldingTreeNode )iter.next(); 98 renderNode( buf, maxVisibleLevel, node ); 99 } 100 101 renderFooter( buf ); 102 103 return buf.toString(); 107 } 108 109 112 protected void getVisibleNodes( 113 List rows, 114 FoldingTreeNode node ) { 115 116 if ( !node.isRoot() || !isRootHidden() ) 117 rows.add( node ); 118 119 if ( !node.isLeaf() && node.getOpened() ) 120 for ( int i = 0; i < node.getChildCount(); i++ ) 121 getVisibleNodes( rows, ( FoldingTreeNode )node.getChildAt( i ) ); 122 } 123 124 127 protected int getMaxLevel( 128 List nodes ) { 129 130 int level = 0; 131 for ( Iterator iter = nodes.iterator(); iter.hasNext(); ) { 132 FoldingTreeNode node = ( FoldingTreeNode )iter.next(); 133 134 int tmp = node.getLevel(); 135 136 if ( tmp > level ) 137 level = tmp; 138 } 139 140 return level; 141 } 142 143 146 protected void renderHeader( 147 StringBuffer buf ) 148 throws 149 TreeException { 150 151 buf.append( "<table>\n" ); 152 } 153 154 157 protected void renderFooter( 158 StringBuffer buf ) 159 throws 160 TreeException { 161 162 buf.append( "</table>\n" ); 163 } 164 165 168 protected void renderNode( 169 StringBuffer buf, 170 int maxVisibleLevel, 171 FoldingTreeNode node ) { 172 173 int hiddenRootOffset = 0; 178 if ( isRootHidden() ) 179 hiddenRootOffset = 1; 180 181 String s = null; 185 String t = null; 186 187 buf.append( "<tr>" ); 188 189 for ( int i = hiddenRootOffset; i < node.getLevel(); i++ ) 193 buf.append( "<td> </td>" ); 194 195 if ( node.isLeaf() ) { 199 buf.append( 200 "<td> </td>" ); 201 } 202 else { 203 if ( !node.getOpened() ) 207 buf.append( 208 "<td><a HREF=\"" + openUrl_ + 209 node.hashCode() + "\">" + closedHandleImageHtml_ + "</a></td>" ); 210 211 else 215 buf.append( 216 "<td><a HREF=\"" + closeUrl_ + 217 node.hashCode() + "\">" + openedHandleImageHtml_ + "</a></td>" ); 218 } 219 220 Map iconImages = getIconImages(); 224 225 if ( iconImages != null ) { 226 227 s = ( String )iconImages.get( node.getIconHint() ); 228 229 if ( s == null ) 230 s = ( String )iconImages.get( DEFAULT_ICON ); 231 232 if ( s == null ) { 233 buf.append( "<td> </td>" ); 234 } 235 else { 236 if ( node.getUrl() == null ) 237 buf.append( "<td><img SRC=\"" + s + "\"></td>" ); 238 else 239 buf.append( 240 "<td><a HREF=\"" + node.getUrl() + "\"><img border=\"0\" SRC=\"" + 241 s + "\"></a></td>" ); 242 } 243 } 244 245 if ( node.getUrl() == null ) 249 buf.append( 250 "<td width=\"100%\" colspan=\"" + 251 ( maxVisibleLevel - node.getLevel() + 1 ) + "\">" + 252 node.getLabel() + "</td>" ); 253 else 254 buf.append( 255 "<td width=\"100%\" colspan=\"" + 256 ( maxVisibleLevel - node.getLevel() + 1 ) + 257 "\"><a HREF=\"" + node.getUrl() + "\">" + node.getLabel() + 258 "</a></td>" ); 259 260 buf.append( "</tr>\n" ); 261 } 262 263 265 269 public void setOpenUrl( String openUrl ) { 270 openUrl_ = openUrl; 271 } 272 273 277 public void setCloseUrl( String closeUrl ) { 278 closeUrl_ = closeUrl; 279 } 280 281 283 protected String openUrl_ = null; 284 protected String closeUrl_ = null; 285 286 private String openedHandleImageHtml_ = null; 287 private String closedHandleImageHtml_ = null; 288 } 289 | Popular Tags |