001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.portal.alpharenderer;
030:
031: import com.caucho.util.L10N;
032:
033: import java.io.IOException;
034: import java.io.PrintWriter;
035: import java.util.logging.Logger;
036:
037: abstract public class Menu {
038: private static L10N L = new L10N(Menu.class);
039:
040: static protected final Logger log = Logger.getLogger(Menu.class
041: .getName());
042:
043: private Location _location = Location.HEADER;
044: private int _showMin = 1;
045:
046: public Menu() {
047: }
048:
049: /**
050: * The minimum number of items that must be in the menu for
051: * it to be shown, default is 2.
052: */
053: public void setShowMin(int showMin) {
054: _showMin = showMin;
055: }
056:
057: /**
058: * One of "hidden", "frame", "header", "footer"; default "header".
059: */
060: public void setLocation(String location) {
061: _location = Location.getLocation(location);
062: }
063:
064: public Location getLocation() {
065: return _location;
066: }
067:
068: public void init() {
069: }
070:
071: public MenuRenderer createRenderer() {
072: return new MenuRenderer(this );
073: }
074:
075: abstract protected void menuStart(StringBuffer buf);
076:
077: abstract protected void menuItem(StringBuffer buf, int count,
078: String name, String shortDescription, String url,
079: boolean isSelected);
080:
081: abstract protected void menuEnd(StringBuffer buf, int count);
082:
083: static public class MenuRenderer {
084: private Menu _menu;
085: private int _count;
086:
087: private StringBuffer _buf = new StringBuffer();
088:
089: MenuRenderer(Menu menu) {
090: _menu = menu;
091: }
092:
093: public void add(String title, String shortDescription,
094: String url, boolean isSelected) {
095: if (_count == 0) {
096: _menu.menuStart(_buf);
097: }
098:
099: _count++;
100:
101: _menu.menuItem(_buf, _count, title, shortDescription, url,
102: isSelected);
103: }
104:
105: public void print(PrintWriter out) throws IOException {
106: if (_count >= _menu._showMin) {
107: int len = _buf.length();
108: _menu.menuEnd(_buf, _count);
109:
110: out.print(_buf);
111:
112: _buf.setLength(len);
113: }
114: }
115:
116: public String toString() {
117: String toString = "";
118:
119: if (_count >= _menu._showMin) {
120: int len = _buf.length();
121: _menu.menuEnd(_buf, _count);
122:
123: toString = _buf.toString();
124:
125: _buf.setLength(len);
126: }
127:
128: return toString;
129: }
130:
131: }
132:
133: protected static void appendEscaped(StringBuffer buf, String string) {
134: if (string == null) {
135: buf.append(string);
136: return;
137: }
138:
139: for (int i = 0; i < string.length(); i++) {
140: char ch = string.charAt(i);
141:
142: switch (ch) {
143: case '<':
144: buf.append("<");
145: break;
146: case '>':
147: buf.append(">");
148: break;
149: case '&':
150: buf.append("&");
151: break;
152: case '\"':
153: buf.append(""");
154: break;
155: case '\'':
156: buf.append("’");
157: break;
158: default:
159: buf.append(ch);
160: }
161: }
162: }
163: }
|