KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > tabs > tags > TabHeadingsTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.tabs.tags;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
24
25 import org.apache.struts.taglib.TagUtils;
26
27 import com.sslexplorer.tabs.TabModel;
28
29 /**
30  * Custom tag to render HTML that displays a row of tabs retrieved from
31  * a {@link com.sslexplorer.tabs.TabModel}.
32  * <p>
33  * This tag takes no attributes as it derives all of its attributes from
34  * the &lt;tabSet&gt; tag that is must be insed.
35  *
36  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
37  * @see com.sslexplorer.tabs.tags.TabSetTag
38  */

39 public class TabHeadingsTag extends BodyTagSupport JavaDoc {
40     
41     // Protected instance variables
42

43     protected String JavaDoc text;
44
45     /* (non-Javadoc)
46      * @see org.apache.struts.taglib.html.BaseFieldTag#doStartTag()
47      */

48     public int doStartTag() throws JspException JavaDoc {
49         Object JavaDoc value = findAncestorWithClass(this, TabSetTag.class);
50         if (value == null) {
51             throw new JspException JavaDoc("TabHeadingsTag must be contained in a TabSetTag");
52         }
53         TabModel model = ((TabSetTag)value).getModel();
54         String JavaDoc bundle = ((TabSetTag)value).getBundle();
55         String JavaDoc locale = ((TabSetTag)value).getLocale();
56         String JavaDoc selectedTab = model.getSelectedTab();
57         String JavaDoc resourcePrefix = ((TabSetTag)value).getResourcePrefix();
58         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
59         buf.append("<div class=\"tabHeadings\">");
60         buf.append("<ul>");
61         for(int i = 0 ; i < model.getTabCount(); i++) {
62             String JavaDoc tabName = model.getTabName(i);
63             String JavaDoc tabTitle = model.getTabTitle(i);
64             String JavaDoc tabBundle = model.getTabBundle(i);
65             
66             // List item
67
buf.append("<li id=\"tab_item_");
68             buf.append(tabName);
69             buf.append("\" class=\"");
70             if(selectedTab == null) {
71                 buf.append(i == 0 ? "selectedTab" : "hiddenTab");
72             }
73             else {
74                 buf.append(selectedTab.equals(tabName) ? "selectedTab" : "hiddenTab");
75             }
76             buf.append("\">");
77             
78             // Link
79
buf.append("<a id=\"tab_link_");
80             buf.append(tabName);
81             buf.append("\" ");
82             if(selectedTab == null) {
83                 buf.append(i == 0 ? "class=\"currentTab\" " : "");
84             }
85             else {
86                 buf.append(selectedTab.equals(tabName) ? "class=\"currentTab\" " : "");
87             }
88             int idx = 0;
89             buf.append("onclick=\"javascript: var deselect = new Array();");
90             for(int j = 0 ; j < model.getTabCount(); j++) {
91                 String JavaDoc tn = model.getTabName(j);
92                 boolean s = tabName.equals(tn);
93                 if(!s) {
94                     buf.append("deselect[");
95                     buf.append(idx++);
96                     buf.append("]='");
97                     buf.append(tn);
98                     buf.append("';");
99                 }
100             }
101             buf.append("setSelectedTab('");
102             buf.append(tabName);
103             buf.append("',deselect);\" HREF=\"#\">");
104             if(tabTitle == null) {
105                 tabTitle =
106                     TagUtils.getInstance().message(
107                         pageContext,
108                         tabBundle == null ? bundle : tabBundle,
109                         locale,
110                         resourcePrefix + "." + tabName + ".title",
111                         new String JavaDoc[] { });
112             }
113             buf.append(tabTitle == null? tabName : tabTitle);
114             buf.append("</a>");
115             buf.append("</li>");
116         }
117         buf.append("</ul>");
118         buf.append("</div>");
119         text = buf.toString();
120         return (SKIP_BODY);
121     }
122
123     /* (non-Javadoc)
124      * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
125      */

126     public int doEndTag() throws JspException JavaDoc {
127         TagUtils.getInstance().write(this.pageContext, text);
128         return EVAL_PAGE;
129     }
130     
131
132 }
133
Popular Tags