KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > taglib > TabSheetTag


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37
38 package net.sourceforge.cruisecontrol.taglib;
39
40 import java.io.IOException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import javax.servlet.jsp.JspTagException JavaDoc;
45 import javax.servlet.jsp.JspWriter JavaDoc;
46 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
47 import javax.servlet.jsp.tagext.Tag JavaDoc;
48
49 import net.sourceforge.cruisecontrol.util.CCTagException;
50
51 /**
52  * A sheet of navigation tabs.
53  *
54  * @author <a HREF="mailto:robertdw@users.sourceforge.net">Robert Watkins</a>
55  * @author <a HREF="mailto:hak@2mba.dk">Hack Kampbjorn</a>
56  */

57 public class TabSheetTag extends CruiseControlBodyTagSupport {
58     private List JavaDoc tabs = new ArrayList JavaDoc();
59     private Tab selectedTab;
60     private static final Tab NONE_SELECTED = null;
61     private static final String JavaDoc EOL = "\r\n";
62     private static final String JavaDoc START_SHEET = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"
63             + "<tbody>"
64             + EOL
65             + " <tr>"
66             + EOL
67             + " <td bgcolor=\"#FFFFFF\"><img border=\"0\" SRC=\"images/bluestripestop.gif\"></td>"
68             + EOL
69             + " </tr>"
70             + EOL;
71     private static final String JavaDoc END_SHEET = " <tr>"
72             + EOL
73             + " <td bgcolor=\"#FFFFFF\"><img border=\"0\" SRC=\"images/bluestripesbottom.gif\"></td>"
74             + EOL
75             + " </tr>"
76             + EOL
77             + "</tbody></table>"
78             + EOL;
79
80     /**
81      * On starting the tag, we clear out the per-instance state (should be clear already, but hey).
82      * @return EVAL_BODY_TAG, indicating that we should look in the body.
83      */

84     public int doStartTag() {
85         clearTabs();
86         return BodyTag.EVAL_BODY_TAG;
87     }
88
89     /**
90      * Add a Tab to the list of tabs.
91      * @param tab the tab to add.
92      */

93     public void addTab(Tab tab) {
94         if (tab.isSelected()) {
95             selectedTab = tab;
96         }
97         tabs.add(tab);
98     }
99
100     /** Clear up state when told to. */
101     public void release() {
102         super.release();
103         clearTabs();
104     }
105
106     /**
107      * Having finished the tag, we iterate over the tabs, printing them out correctly.
108      * @return EVAL_PAGE
109      * @throws JspTagException if there's an error, like an IO error.
110      */

111     public int doEndTag() throws JspTagException JavaDoc {
112         try {
113             final JspWriter JavaDoc out = getPageContext().getOut();
114             startTable(out);
115             printTabHeaders(out);
116             printBody(out);
117             endTable(out);
118             return Tag.EVAL_PAGE;
119         } catch (IOException JavaDoc e) {
120             err(e);
121             throw new CCTagException("IO Error: " + e.getMessage(), e);
122         }
123     }
124
125     private void endTable(final JspWriter JavaDoc out) throws IOException JavaDoc {
126         out.write(END_SHEET);
127     }
128
129     private void startTable(final JspWriter JavaDoc out) throws IOException JavaDoc {
130         out.write(START_SHEET);
131     }
132
133     private void clearTabs() {
134         selectedTab = NONE_SELECTED;
135         tabs.clear();
136     }
137
138     /**
139      * Print out the tab headers. The selected tab is rendered as a plain label, the other tabs are rendered as links.
140      * @throws IOException if there's an IO error.
141      */

142     private void printTabHeaders(JspWriter JavaDoc out) throws IOException JavaDoc {
143         out.write("<tr>");
144         out.write("<td bgcolor=\"#FFFFFF\">");
145         out.write("<div align=\"left\">");
146         out.write("<table class=\"tab-table\" align=\"center\" valign=\"middle\" cellspacing=\"0\"");
147         out.write(" cellpadding=\"0\" border=\"1\"><tbody><tr>");
148         for (Iterator JavaDoc iterator = tabs.iterator(); iterator.hasNext();) {
149             Tab tab = (Tab) iterator.next();
150             if (tab.isRow()) {
151                 out.write("</tr><tr>");
152             } else if (tab == selectedTab) {
153                 out.write("<td class=\"tabs-selected\">");
154                 out.write(tab.getLabel());
155                 out.write("</td>");
156             } else {
157                 out.write("<td class=\"tabs\">");
158                 out.write("<a class=\"tabs-link\" HREF=\"");
159                 out.write(tab.getUrl() != null ? tab.getUrl() : createUrl("tab", tab.getName()));
160                 out.write("\">");
161                 out.write(tab.getLabel());
162                 out.write("</a>");
163                 out.write("</td>");
164             }
165         }
166         out.write("</tr></tbody></table></div>");
167         out.write("</td>");
168         out.write("</tr>");
169     }
170
171     /**
172      * Print out the body of the selected tab (if any).
173      * @throws IOException if there's an IO error.
174      */

175     private void printBody(JspWriter JavaDoc out) throws IOException JavaDoc {
176         if (selectedTab != NONE_SELECTED) {
177             getBodyContent().writeOut(out);
178         }
179     }
180
181     public boolean hasTabs() {
182         return !tabs.isEmpty();
183     }
184 }
185
Popular Tags