KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > admin > TabsTag


1 /**
2  * $RCSfile: TabsTag.java,v $
3  * $Revision: 1.11 $
4  * $Date: 2005/03/15 09:14:13 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.admin;
13
14 import org.jivesoftware.util.StringUtils;
15 import org.jivesoftware.util.LocaleUtils;
16 import org.dom4j.Element;
17
18 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.JspWriter JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import java.util.List JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * <p>A simple JSP tag for displaying tab information in the admin console. The
27  * {@link SidebarTag} is similiar to this one.</p>
28  *
29  * <p>Attributes: <ul>
30  * <li><tt>bean</tt> (required) - the id of the request attribute which is a
31  * {@link AdminPageBean} instance. This class holds information
32  * needed to properly render the admin console tabs.</li>
33  * <li><tt>css</tt> (optional) - the CSS class name used to decorate the LI of the tab.</li>
34  * <li><tt>currentcss</tt> (optional) - the CSS class name used to decorate the LI of the
35  * currently selected tab.</li></ul></p>
36  *
37  * <p>This class assumes there is a request attribute with the name specified by the bean attribute.</p>
38  *
39  * <p>This tag prints out minimal HTML. It basically prints an unordered list (UL element) with each
40  * LI containing an "A" tag specfied by the body content of this tag. For example, the body should contain
41  * a template A tag which will have its values replaced at runtime: <ul><tt>
42  *
43  * &lt;jive:tabs bean="jivepageinfo"&gt; <br>
44  * &nbsp;&nbsp;&nbsp;&lt;a HREF="[url]" title="[description]"&gt;[name]&lt;/a&gt; <br>
45  * &lt;/jive:tabs&gt;</tt></ul>
46  *
47  * Available token are: <ul>
48  * <li><tt>[id]</tt> - the ID of the tab, usually not needed.</li>
49  * <li><tt>[name]</tt> - the name of the tab, should be thought of as the display name.</li>
50  * <li><tt>[url]</tt> - the URL of the tab.</li>
51  * <li><tt>[description]</tt> - the description of the tab, good for mouse rollovers.</li></ul></p>
52  */

53 public class TabsTag extends BodyTagSupport JavaDoc {
54
55     private String JavaDoc bean;
56     private String JavaDoc css;
57     private String JavaDoc currentcss;
58
59     /**
60      * The name of the request attribute which holds a {@link AdminPageBean} instance.
61      */

62     public String JavaDoc getBean() {
63         return bean;
64     }
65
66     /**
67      * Sets the name of the request attribute to hold a {@link AdminPageBean} instance.
68      */

69     public void setBean(String JavaDoc bean) {
70         this.bean = bean;
71     }
72
73     /**
74      * Returns the value of the CSS class to be used for tab decoration. If not set will return a blank string.
75      */

76     public String JavaDoc getCss() {
77         return clean(css);
78     }
79
80     /**
81      * Sets the CSS used for tab decoration.
82      */

83     public void setCss(String JavaDoc css) {
84         this.css = css;
85     }
86
87     /**
88      * Returns the value of the CSS class to be used for the currently selected LI (tab). If not set will
89      * return a blank string.
90      */

91     public String JavaDoc getCurrentcss() {
92         return clean(currentcss);
93     }
94
95     /**
96      * Sets the CSS class value for the currently selected tab.
97      */

98     public void setCurrentcss(String JavaDoc currentcss) {
99         this.currentcss = currentcss;
100     }
101
102     /**
103      * Does nothing, returns {@link #EVAL_BODY_BUFFERED} always.
104      */

105     public int doStartTag() throws JspException JavaDoc {
106         return EVAL_BODY_BUFFERED;
107     }
108
109     /**
110      * Gets the {@link AdminPageBean} instance from the request. If it doesn't exist then execution is stopped
111      * and nothing is printed. If it exists, retrieve values from it and render the tabs. The body content
112      * of the tag is assumed to have an A tag in it with tokens to replace (see class description).
113      *
114      * @return {@link #EVAL_PAGE} after rendering the tabs.
115      * @throws JspException if an exception occurs while rendering the tabs.
116      */

117     public int doEndTag() throws JspException JavaDoc {
118         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
119         String JavaDoc beanName = getBean();
120         // Get the page data bean from the request:
121
AdminPageBean pageInfo = (AdminPageBean)request.getAttribute(beanName);
122         // If the page info bean is not in the request then no tab will be selected - so, it'll fail gracefully
123
String JavaDoc pageID = null;
124         if (pageInfo != null) {
125             pageID = pageInfo.getPageID();
126         }
127         // Get tabs from the model:
128
List JavaDoc tabs = AdminConsole.getModel().selectNodes("//tab");
129         if (tabs.size() > 0) {
130             JspWriter JavaDoc out = pageContext.getOut();
131             // Build up the output in a buffer (is probably faster than a bunch of out.write's)
132
StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
133             buf.append("<ul>");
134             String JavaDoc body = getBodyContent().getString();
135             // For each tab, print out an <LI>.
136
Element currentTab = null;
137             if (pageID != null) {
138                 currentTab = (Element)AdminConsole.getModel().selectSingleNode(
139                             "//*[@id='" + pageID + "']/ancestor::tab");
140             }
141             for (int i=0; i<tabs.size(); i++) {
142                 Element tab = (Element)tabs.get(i);
143                 String JavaDoc value = body;
144                 if (value != null) {
145                     // The URL for the tab should be the URL of the first item in the tab.
146
value = StringUtils.replace(value, "[id]", clean(tab.attributeValue("id")));
147                     value = StringUtils.replace(value, "[url]",
148                             request.getContextPath() + "/" + clean(tab.attributeValue("url")));
149                     value = StringUtils.replace(value, "[name]", clean(i18n(tab.attributeValue("name"))));
150                     value = StringUtils.replace(value, "[description]", clean(i18n(tab.attributeValue("description"))));
151                 }
152                 String JavaDoc css = getCss();
153                 if (tab.equals(currentTab)) {
154                     css = getCurrentcss();
155                 }
156                 buf.append("<li class=\"").append(css).append("\">");
157                 buf.append(value);
158                 buf.append("</li>");
159             }
160
161             buf.append("</ul>");
162             try {
163                 out.write(buf.toString());
164             }
165             catch (IOException JavaDoc ioe) {
166                 throw new JspException JavaDoc(ioe.getMessage());
167             }
168         }
169         return EVAL_PAGE;
170     }
171
172     /**
173      * Cleans the given string - if it's null, it's converted to a blank string. If it has ' then those are
174      * converted to double ' so HTML isn't screwed up.
175      *
176      * @param in the string to clean
177      * @return a cleaned version - not null and with escaped characters.
178      */

179     private String JavaDoc clean(String JavaDoc in) {
180         return (in == null ? "" : StringUtils.replace(in, "'", "\\'"));
181     }
182
183     private String JavaDoc i18n(String JavaDoc in) {
184         if (in == null) {
185             return null;
186         }
187         // Look for the key symbol:
188
if (in.indexOf("${") == 0 && in.indexOf("}") == in.length()-1) {
189             return LocaleUtils.getLocalizedString(in.substring(2, in.length()-1));
190         }
191         return in;
192     }
193 }
Popular Tags