1 28 29 30 package com.caucho.portal.alpharenderer; 31 32 import com.caucho.util.L10N; 33 34 import java.io.IOException ; 35 import java.io.PrintWriter ; 36 import java.util.logging.Logger ; 37 38 abstract public class Menu 39 { 40 private static L10N L = new L10N( Menu.class ); 41 42 static protected final Logger log = 43 Logger.getLogger( Menu.class.getName() ); 44 45 private Location _location = Location.HEADER; 46 private int _showMin = 1; 47 48 public Menu() 49 { 50 } 51 52 56 public void setShowMin( int showMin ) 57 { 58 _showMin = showMin; 59 } 60 61 64 public void setLocation( String location ) 65 { 66 _location = Location.getLocation( location ); 67 } 68 69 public Location getLocation() 70 { 71 return _location; 72 } 73 74 public void init() 75 { 76 } 77 78 public MenuRenderer createRenderer() 79 { 80 return new MenuRenderer( this ); 81 } 82 83 abstract protected void menuStart( StringBuffer buf ); 84 85 abstract protected void menuItem( StringBuffer buf, 86 int count, 87 String name, 88 String shortDescription, 89 String url, 90 boolean isSelected ); 91 92 abstract protected void menuEnd( StringBuffer buf, int count ); 93 94 static public class MenuRenderer 95 { 96 private Menu _menu; 97 private int _count; 98 99 private StringBuffer _buf = new StringBuffer (); 100 101 MenuRenderer( Menu menu ) 102 { 103 _menu = menu; 104 } 105 106 public void add( String title, 107 String shortDescription, 108 String url, 109 boolean isSelected ) 110 { 111 if ( _count == 0 ) { 112 _menu.menuStart( _buf ); 113 } 114 115 _count++; 116 117 _menu.menuItem( _buf, _count, title, shortDescription, url, isSelected ); 118 } 119 120 public void print( PrintWriter out ) 121 throws IOException 122 { 123 if ( _count >= _menu._showMin ) { 124 int len = _buf.length(); 125 _menu.menuEnd( _buf, _count ); 126 127 out.print( _buf ); 128 129 _buf.setLength( len ); 130 } 131 } 132 133 public String toString() 134 { 135 String toString = ""; 136 137 if ( _count >= _menu._showMin ) { 138 int len = _buf.length(); 139 _menu.menuEnd( _buf, _count ); 140 141 toString = _buf.toString(); 142 143 _buf.setLength( len ); 144 } 145 146 return toString; 147 } 148 149 } 150 151 protected static void appendEscaped( StringBuffer buf, String string ) 152 { 153 if ( string == null ) { 154 buf.append( string ); 155 return; 156 } 157 158 for ( int i = 0; i < string.length(); i++ ) { 159 char ch = string.charAt( i ); 160 161 switch ( ch ) { 162 case '<': buf.append( "<" ); break; 163 case '>': buf.append( ">" ); break; 164 case '&': buf.append( "&" ); break; 165 case '\"': buf.append( """ ); break; 166 case '\'': buf.append( "’" ); break; 167 default: buf.append( ch ); 168 } 169 } 170 } 171 } 172 173 | Popular Tags |