001package bradleyross.j2ee.tags;
002// import javax.servlet.jsp.*;
003import javax.servlet.jsp.JspWriter;
004import javax.servlet.jsp.JspException;
005import javax.servlet.jsp.tagext.TagSupport;
006import javax.servlet.jsp.PageContext;
007/**
008 * This custom tag can be used to center an object on
009 * the web page horizontally.
010 * <p>By using optional attributes, it can also
011 *    position the element against the left or right
012 *    border and can provide the material with a class
013 *    that can be referenced by the CSS style sheet.</p>
014 * @author Bradley Ross
015 *
016 */
017@SuppressWarnings("serial")
018public class CenterTag extends TagSupport 
019{
020/**
021 * Value of the attribute <code>align</code> for the
022 * custom tag.
023 */
024protected String align = null;
025/**
026 * Used by the servlet generated by the application server to
027 * set the value of the attribute <code>align</code>
028 * for the custom tag.  
029 * <p>The name of the attribute is
030 *    passed to the application server in the TLD file
031 *    so that the application server can use the name
032 *    of the attribute to write function calls with
033 *    the correct class names.</p>
034 *    @see #align
035 * @param value Value to be used for attribute align
036 */
037public void setAlign(String value)
038{ align = value; }
039/**
040 * Returns the value of the attribute <code>align</code> for the
041 * custom tag.
042 * @see #align
043 * @return Value of attribute
044 */
045public String getAlign()
046{ return align; }
047/** 
048 * Value of the attribute <code>className</code> for the 
049 * custom tag.
050 * <p>When the tag is translated into a scriptlet, this
051 *    attribute becomes the value of the <code>class</code>
052 *    attribute for a <code>div</code> element.  However,
053 *    class is a reserved word in Java, requiring the use
054 *    of a different name for the attribute.</p>
055 */
056protected String className = null;
057/**
058 * Used by the application server to set the
059 * value of the variable className when it is
060 * encountered as an attribute in the custom tags.
061 * @see #className
062 * @param value Value to be used for attribute className
063 */
064public void setClassName(String value)
065{ className = value; }
066/**
067 * Returns the value of the attribute <code>className</code>
068 * for the custom tag.
069 * @see #className
070 * @return Value of attribute
071 */
072public String getClassName()
073{ return className; }
074/**
075 * Value of the attribute <code>style</code> for the
076 * custom tag.
077 */
078protected String style = null;
079/**
080 * Used by the application server to set the
081 * value of the variable <code>style</code> when it is
082 * encountered as an attribute in the
083 * custom tags.
084 * @see #style
085 * @param value Value to be used for attribute style
086 */
087protected void setStyle(String value)
088{ style = value; }
089/**
090 * Returns the value of the attribute <code>style</code>
091 * for the custom tag.
092 * @see #style
093 * @return Value of attribute
094 */
095protected String getStyle()
096{ return style; }
097public void setPageContext(PageContext context)
098{
099        super.setPageContext(context);
100}
101/**
102 * Writes text for the JSP (Java Server Page) when the opening tag
103 *    of the custom tag is encountered.
104 * <p>The block of text is surrounded by a table since a table's width
105 *    will automatically shrink if the sum of the column widths is
106 *    less that the available width.  Setting margin-left and
107 *    margin-right to auto causes the table to be centered in
108 *    this situation.</p>
109 * <p>The method returns {@link #EVAL_BODY_INCLUDE} to indicate that
110 *    the material in the body of the tag will be processed by the
111 *    JSP preprocessor.</p>
112 */
113public int doStartTag() throws JspException
114{       
115        try
116        {
117                JspWriter out = pageContext.getOut();
118                if (align == null) { align="center"; }
119                if (!align.equalsIgnoreCase("left") && !align.equalsIgnoreCase("center") &&
120                                !align.equalsIgnoreCase("right"))
121                {
122                        out.println("<!-- " + align + " is not a valid option.  Using center -->");
123                        align = "center";
124                }
125                if (align.equalsIgnoreCase("left"))
126                { out.print("<table style=\"margin-left:0px;margin-right:auto;clear:both;\">" +
127                                "<tr><td>"); }
128                else if (align.equalsIgnoreCase("center"))
129                { out.print("<table style=\"margin-left:auto;margin-right:auto;clear:both;\">" +
130                                "<tr><td>"); }
131                else if (align.equalsIgnoreCase("right"))
132                { out.print("<table style=\"margin-left:auto;margin-right:0px;clear:both;\">" +
133                                "<tr><td>"); }
134                if (className != null || style != null)
135                {
136                        out.print("<div ");
137                        if (className != null)
138                        { out.print(" class=\"" + className + "\""); }
139                        if (style != null)
140                        { out.print(" style=\"" + style + "\""); }
141                        out.print(">");
142                }
143                out.print("<!-- Identifier: " + id + " -->");
144                if (align != null)
145                { out.print("<!-- align: " + align + " -->"); }
146                if (className != null)
147                { out.print("<!-- class: " + className + " -->"); }
148                if (style != null)
149                { out.print("<!-- style: " + style + " -->"); }
150        }
151        catch (java.io.IOException e)
152        {
153                throw new JspException(e.getClass().getName() + " " + e.getMessage(), e);
154        }
155        return EVAL_BODY_INCLUDE;
156}
157/**
158 * Writes test for the JSP when the closing tag for the custom
159 *    tag is encountered.
160 * <p>The method returns {@link #EVAL_PAGE} to indicate
161 *    that processing of the Java Server Page should continue.</p>
162 */
163public int doEndTag() throws JspException
164{
165        try
166        {
167                JspWriter out = pageContext.getOut();
168                if (align != null || style != null)
169                { out.print("</div>"); }
170                out.print("</td></tr></table>");
171        }
172        catch (java.io.IOException e)
173        {
174        }
175        return EVAL_PAGE;
176}
177}