1 20 21 package com.methodhead.menu; 22 23 import com.methodhead.tree.FoldingTreeNode; 24 25 57 public class MenuRenderer { 58 59 61 63 65 67 72 protected static String getVarName( 73 int[] path ) { 74 75 String html = "Menu"; 76 77 for ( int i = 0; i < path.length; i++ ) { 78 if ( i == 0 ) 79 html += path[ i ]; 80 else 81 html += "_" + path[ i ]; 82 } 83 84 return html; 85 } 86 87 91 protected static int getLongestLabelLength( 92 FoldingTreeNode menu ) { 93 94 int len = 0; 95 int tmp = 0; 96 97 for ( int i = 0; i < menu.getChildCount(); i++ ) { 98 99 String label = ( ( FoldingTreeNode )menu.getChildAt( i ) ).getLabel(); 100 if ( label == null ) 101 label = ""; 102 103 tmp = label.length(); 104 if ( tmp > len ) 105 len = tmp; 106 } 107 108 return len; 109 } 110 111 protected void getMenuHtml( 112 StringBuffer buf, 113 FoldingTreeNode menu, 114 int[] path, 115 int width, 116 int height ) { 117 118 String url = menu.getUrl(); 122 if ( url.length() > 0 ) 123 url = baseUrl_ + url; 124 125 String label = menu.getLabel(); 129 if ( label == null ) 130 label = "null label"; 131 132 buf.append( 133 getVarName( path ) + "=new Array(\"" + label + "\",\"" + 134 url + "\",\"\"," + menu.getChildCount() + 135 "," + height + "," + width + ");\n" ); 136 137 if ( menu.getChildCount() > 0 ) { 141 142 width = getLongestLabelLength( menu ) * fontWidthHint_; 146 147 int[] menupath = new int[ path.length + 1 ]; 151 System.arraycopy( path, 0, menupath, 0, path.length ); 152 int last = menupath.length - 1; 153 154 for ( int i = 0; i < menu.getChildCount(); i++ ) { 158 menupath[ last ]++; 159 getMenuHtml( 160 buf, 161 ( FoldingTreeNode )menu.getChildAt( i ), 162 menupath, 163 width, 164 height ); 165 } 166 } 167 } 168 169 173 public String renderMenu( 174 FoldingTreeNode menu ) { 175 176 StringBuffer buf = new StringBuffer (); 177 178 buf.append( "NoOffFirstLineMenus=" + menu.getChildCount() + ";\n" ); 179 180 int[] path = new int[ 1 ]; 181 for ( int i = 0; i < menu.getChildCount(); i++ ) { 182 path[ 0 ] = i + 1; 183 FoldingTreeNode submenu = ( FoldingTreeNode )menu.getChildAt( i ); 184 185 String label = submenu.getLabel(); 189 if ( label == null ) 190 label = "null label"; 191 192 getMenuHtml( 193 buf, 194 submenu, 195 path, 196 fontWidthHint_ * label.length(), 197 fontHeightHint_ ); 198 } 199 200 return buf.toString(); 201 } 202 203 205 209 public void setFontWidthHint( 210 int width ) { 211 212 fontWidthHint_ = width; 213 } 214 215 219 public void setFontHeightHint( 220 int height ) { 221 222 fontHeightHint_ = height; 223 } 224 225 228 public void setBaseUrl( 229 String baseUrl ) { 230 231 baseUrl_ = baseUrl; 232 } 233 234 236 protected int fontWidthHint_ = 10; 237 protected int fontHeightHint_ = 20; 238 protected String baseUrl_ = ""; 239 } 240 | Popular Tags |