KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > alpharenderer > Menu


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.portal.alpharenderer;
31
32 import com.caucho.util.L10N;
33
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 abstract public class Menu
39 {
40   private static L10N L = new L10N( Menu.class );
41
42   static protected final Logger JavaDoc 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   /**
53    * The minimum number of items that must be in the menu for
54    * it to be shown, default is 2.
55    */

56   public void setShowMin( int showMin )
57   {
58     _showMin = showMin;
59   }
60
61   /**
62    * One of "hidden", "frame", "header", "footer"; default "header".
63    */

64   public void setLocation( String JavaDoc 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 JavaDoc buf );
84
85   abstract protected void menuItem( StringBuffer JavaDoc buf,
86                                     int count,
87                                     String JavaDoc name,
88                                     String JavaDoc shortDescription,
89                                     String JavaDoc url,
90                                     boolean isSelected );
91
92   abstract protected void menuEnd( StringBuffer JavaDoc buf, int count );
93
94   static public class MenuRenderer
95   {
96     private Menu _menu;
97     private int _count;
98
99     private StringBuffer JavaDoc _buf = new StringBuffer JavaDoc();
100
101     MenuRenderer( Menu menu )
102     {
103       _menu = menu;
104     }
105
106     public void add( String JavaDoc title,
107                      String JavaDoc shortDescription,
108                      String JavaDoc 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 JavaDoc out )
121       throws IOException JavaDoc
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 JavaDoc toString()
134     {
135       String JavaDoc 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 JavaDoc buf, String JavaDoc 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( "&lt;" ); break;
163         case '>': buf.append( "&gt;" ); break;
164         case '&': buf.append( "&amp;" ); break;
165                   case '\"': buf.append( "&quot;" ); break;
166         case '\'': buf.append( "&rsquo;" ); break;
167         default: buf.append( ch );
168       }
169     }
170   }
171 }
172
173
Popular Tags