package com.elseforif.servlet.utility; import java.io.IOException; import com.elseforif.servlet.ElseForIfServlet; import com.elseforif.servlet.utility.constraint.ServletParameters; import com.elseforif.URLs; import java.io.PrintWriter; /** * This extends ParametricHTMLWriter (a facilitator for generating HTML * output) to encapsulate HTML structures common to elseforif.com. According * to HTMLWriter, it wraps a PrintWriter to present a single, unified * output interface to servlet code. *

* Most servlets will want to call 'printPreContent()' (or * 'printPreContentAfterBODY()') and 'printPostContent()', which produce * most of the page's structure, allowing the servlet to focus on its own, * specific output. 'startBubble()' and 'finishBubble()' are also used * frequently to format subsections within pages. * * @author Jason Van Cleve */ public class ElseForIfHTMLWriter extends ParametricHTMLWriter { public static final String BACKGROUND_COLOR_HEX = "#B0D0B0"; public static final String DEFAULT_STYLE_SHEET = "/css/default.css"; /** * Construct an ElseForIfHTMLWriter wrapper for PrintWriter 'output'. * * @param output the PrintWriter I will use for 'write...()' calls. * @param in my servlet parameters object */ public ElseForIfHTMLWriter(PrintWriter output, ServletParameters in) { super(output, null, null, in); m_xhtml = false; m_transpixelImageURL = "/image/transpixel.gif"; } /** * Output a HEAD construct with the given page title. * * @param servlet my servlet */ public void printHEAD(ElseForIfServlet servlet) throws IOException { StringBuffer tags = new StringBuffer(400); tags.append(METATag("description", servlet.getDescription())); tags.append(METATag("keywords", servlet.getKeywords())); tags.append(METATag("robots", "index,follow")); /* */ tags.append(styleSheetLINK(DEFAULT_STYLE_SHEET)); tags.append(SCRIPTFile("/js/Menu.js")); printHEAD(servlet.getTitle(), tags.toString()); } /** * Output a default BODY tag. */ public void printBeginBODY() throws IOException { print(""); } /** * Output a preliminary HTML page, including the HEAD and BODY tags and * other code, up to the main page content. * * @param servlet the servlet outputting the page in question */ public void printPreContent(ElseForIfServlet servlet) throws IOException { printHEAD(servlet); printBeginBODY(); printPreContentAfterBODY(servlet); } /** * Output the part of an HTML page after the opening BODY tag but before * the main content section. This is useful where a particular servlet * needs to produce a custom HEAD tag, for instance. * * @param servlet the servlet outputting the page in question */ public void printPreContentAfterBODY(ElseForIfServlet servlet) throws IOException { printBeginTABLE(0, 0, 0); print(TR + TD_NOWRAP); printIMG("/img/title-bar-N.gif", 894, 68); printSpacer(20, 1); print(END_TD + END_TR + END_TABLE + NL); printBeginTABLE(0, 0, 0); print(TR); printBeginTD("valign=\"top\" height=\"1%\" width=\"99%\" colspan=\"2\""); printIMG("/img/title-bar-S.gif", 500, 35); print(END_TD); boolean homeMode = servlet.getMenuMode() == ElseForIfServlet.MENU_MODE_HOME; printBeginTD("rowspan=\"2\" width=\"1%\" valign=\"top\" align=\"right\""); if (homeMode) printIcons(); else printMenu(servlet); print(END_TD + END_TR + TR); printBeginTD("valign=\"top\" width=\"1%\""); if (homeMode) printMenu(servlet); else printSpacer(24, 1); print(END_TD); printBeginTD("align=\"center\" valign=\"top\" width=\"98%\""); printSpacer(1, homeMode ? 12 : 28); print(BR + NL); } /** * Output the main menu, possibly disabling one of the links according to * the "menu mode" of 'servlet'. * * @param servlet the servlet outputting the page in question */ public void printMenu(ElseForIfServlet servlet) throws IOException { printBeginDIV("menu"); printIMG("/img/menu/menu.gif", 144, 34); print(BR + NL); printSpacer(1, 10); int menuMode = servlet.getMenuMode(); printMenuItem("home", URLs.HOME, menuMode == ElseForIfServlet.MENU_MODE_HOME); printMenuItem("products", URLs.PRODUCTS, menuMode == ElseForIfServlet.MENU_MODE_PRODUCTS); printMenuItem("services", URLs.SERVICES, menuMode == ElseForIfServlet.MENU_MODE_SERVICES); printMenuItem("development", URLs.DEVELOPMENT, menuMode == ElseForIfServlet.MENU_MODE_DEVELOPMENT); printMenuItem("contact", URLs.CONTACT, menuMode == ElseForIfServlet.MENU_MODE_CONTACT); print(END_DIV); } /** * Output a single item in the main menu. Mainly, this method is called by * the preceeding one, to avoid redundancy. * * @param name the name of the menu item * @param url the URL to be linked * @param disabled whether the menu item should be disabled */ public void printMenuItem(String name, String url, boolean disabled) throws IOException { print(BR + NL); printSpacer(1, 10); print(BR + NL); if (!disabled) printA(url, IMG("/img/menu/" + name + ".gif", "name=" + name + " alt=\"" + name + "\"", 144, 34), "onMouseOver='toggle(document.images." + name + ", true);'" + " onMouseOut='toggle(document.images." + name + ", false);'"); else printIMG("/img/menu/" + name + "-disable.gif", 144, 34); } /** * Output the list of icon links that appears on the home page. */ public void printIcons() throws IOException { printBeginDIV("icons"); printA("http://java.sun.com/getjava/", IMG("/img/icon/java.gif", "class=icon alt=\"icon\"", 88, 31)); String spacer = BR + NL + spacer(1, 10) + BR + NL; String attrs = "class=icon alt=\"icon\""; print(spacer); printA("http://www.anybrowser.org/campaign/", IMG("/img/icon/any-browser.gif", attrs, 88, 31)); print(spacer); printA("http://creativecommons.org/", IMG("/img/icon/creative-commons.gif", attrs, 88, 31)); print(spacer); printA("http://slashdot.org", IMG("/img/icon/slashdot.gif", attrs, 88, 31)); print(spacer); printA("http://sourceforge.net/", IMG("/img/icon/sourceforge.png", attrs, 88, 31)); print(spacer); printA("http://www.open-content.net/", IMG("/img/icon/open-content.gif", attrs, 85, 33)); print(spacer); printA("http://www.gimp.org/", IMG("/img/icon/gimp.gif", attrs, 90, 36)); print(spacer); printA("http://www.postgresql.org/", IMG("/img/icon/postgres.gif", attrs, 88, 31)); print(spacer); printA("http://www.vim.org/", IMG("/img/icon/vim.gif", attrs, 90, 33)); print(spacer); printA("http://www.apache.org/", IMG("/img/icon/apache.gif", attrs, 88, 31)); print(spacer); printA("/", IMG("/img/icon.gif", attrs, 88, 31)); print(END_DIV); } /** * Output the part of an HTML page after the main content section, including * the closing BODY and HTML tags. * * @param servlet the servlet outputting the page in question */ public void printPostContent(ElseForIfServlet servlet) throws IOException { int menuMode = servlet.getMenuMode(); print(END_TABLE_3); printSpacer(1, 10); print(CENTER); printIMG("/img/penu/head.gif", 122, 42); printPenuItem("home", 28, URLs.HOME, menuMode == ElseForIfServlet.MENU_MODE_HOME); printPenuItem("products", 39, URLs.PRODUCTS, menuMode == ElseForIfServlet.MENU_MODE_PRODUCTS); printPenuItem("services", 38, URLs.SERVICES, menuMode == ElseForIfServlet.MENU_MODE_SERVICES); printPenuItem("development", 56, URLs.DEVELOPMENT, menuMode == ElseForIfServlet.MENU_MODE_DEVELOPMENT); printPenuItem("contact", 35, URLs.CONTACT, menuMode == ElseForIfServlet.MENU_MODE_CONTACT); printIMG("/img/penu/tail.gif", 75, 42); print(NL + BR + NL); printSpacer(1, 14); print(END_CENTER); print(BOTH_END_TAGS); } /** * Output part of the "penu" at the bottom of each page. This is here to * save code in the previous method. * * @param name the name of the menu item * @param width the width of the image segment to be displayed * @param url the URL to which it should link * @param disabled whether the menu item should be disabled */ public void printPenuItem(String name, int width, String url, boolean disabled) throws IOException { if (!disabled) printA(url, IMG("/img/penu/" + name + ".gif", "name=penu_" + name + " alt=\"" + name + "\"", width, 42), "onMouseOver='toggle(document.images.penu_" + name + ", true);'" + " onMouseOut='toggle(document.images.penu_" + name + ", false);'"); else printIMG("/img/penu/" + name + "-disable.gif", width, 42); } /** * Start a box, or "bubble", delimiting a subsection within the page being * generated. Servlets may call this, then output the contents of the * subsection, then call 'finishBubble()' complete the HTML construct. * * @param title a heading for the bubble */ public void startBubble(String title) throws IOException { printBeginDIV("bubble"); printBeginDIV("bubbleHeading"); print(title); print(END_DIV); printBeginDIV("bubbleText"); } /** * Finish the box, or "bubble", delimiting a subsection within a page. * This should be called at some point after 'startBubble()' to ensure * that HTML tags opened in that method are properly closed. */ public void finishBubble() throws IOException { print(END_DIV + END_DIV); } }