KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: SidebarTag.java,v $
3  * $Revision: 1.9 $
4  * $Date: 2005/03/25 01:36:32 $
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.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * <p>A simple JSP tag for displaying sidebar information in the admin console. The
28  * {@link TabsTag} is similiar to this one.</p>
29  *
30  * <p>Attributes: <ul>
31  * <li><tt>bean</tt> (required) - the id of the request attribute which is a
32  * {@link AdminPageBean} instance. This class holds information
33  * needed to properly render the admin console sidebar.</li>
34  * <li><tt>css</tt> (optional) - the CSS class name used to decorate the LI of the sidebar items.</li>
35  * <li><tt>currentcss</tt> (optional) - the CSS class name used to decorate the LI of the
36  * currently selected sidebar item.</li>
37  * <li><tt>heaadercss</tt> (optional) - the CSS class name used to decorate the LI of the header
38  * section.</li></ul></p>
39  *
40  * <p>This class assumes there is a request attribute with the name specified by the bean attribute.</p>
41  *
42  * <p>This tag prints out minimal HTML. It basically prints an unordered list (UL element) with each
43  * LI containing an "A" tag specfied by the body content of this tag. For example, the body should contain
44  * a template A tag which will have its values replaced at runtime: <ul><tt>
45  *
46  * &lt;jive:sidebar bean="jivepageinfo"&gt; <br>
47  * &nbsp;&nbsp;&nbsp;&lt;a HREF="[url]" title="[description]"&gt;[name]&lt;/a&gt; <br>
48  * &nbsp;&nbsp;&nbsp;&lt;jive:subsidebar&gt; ... &lt;/jive:subsidebar&gt; <br>
49  * &lt;/jive:sidebar&gt;</tt></ul>
50  *
51  * There is a subsidebar tag for rendering the sub-sidebars. For more info, see the
52  * {@link SubSidebarTag} class.</p>
53  *
54  * <p>Available tokens for the "A" tag are: <ul>
55  * <li><tt>[id]</tt> - the ID of the sidebar item, usually not needed.</li>
56  * <li><tt>[name]</tt> - the name of the sidebar item, should be thought of as the display name.</li>
57  * <li><tt>[url]</tt> - the URL of the sidebar item.</li>
58  * <li><tt>[description]</tt> - the description of the sidebar item, good for mouse rollovers.</li></ul></p>
59  */

60 public class SidebarTag extends BodyTagSupport JavaDoc {
61
62     private String JavaDoc bean;
63     private String JavaDoc css;
64     private String JavaDoc currentcss;
65     private String JavaDoc headercss;
66     private SubSidebarTag subsidebarTag;
67
68     /**
69      * The name of the request attribute which holds a {@link AdminPageBean} instance.
70      */

71     public String JavaDoc getBean() {
72         return bean;
73     }
74
75     /**
76      * Sets the name of the request attribute to hold a {@link AdminPageBean} instance.
77      */

78     public void setBean(String JavaDoc bean) {
79         this.bean = bean;
80     }
81
82     /**
83      * Returns the value of the CSS class to be used for tab decoration. If not set will return a blank string.
84      */

85     public String JavaDoc getCss() {
86         return clean(css);
87     }
88
89     /**
90      * Sets the CSS used for tab decoration.
91      */

92     public void setCss(String JavaDoc css) {
93         this.css = css;
94     }
95
96     /**
97      * Returns the value of the CSS class to be used for the currently selected LI (tab). If not set will
98      * return a blank string.
99      */

100     public String JavaDoc getCurrentcss() {
101         return clean(currentcss);
102     }
103
104     /**
105      * Sets the CSS class value for the currently selected tab.
106      */

107     public void setCurrentcss(String JavaDoc currentcss) {
108         this.currentcss = currentcss;
109     }
110
111     /**
112      * Returns the value of the CSS class to be used for sidebar header sections.
113      */

114     public String JavaDoc getHeadercss() {
115         return headercss;
116     }
117
118     /**
119      * Sets the CSS value used for the sidebar header sections.
120      */

121     public void setHeadercss(String JavaDoc headercss) {
122         this.headercss = headercss;
123     }
124
125     /**
126      * Returns the subsidebar tag - should be declared in the body of this tag (see class description).
127      */

128     public SubSidebarTag getSubsidebarTag() {
129         return subsidebarTag;
130     }
131
132     /**
133      * Sets the subsidebar tag - used by the container.
134      */

135     public void setSubSidebar(SubSidebarTag subsidebarTag) {
136         this.subsidebarTag = subsidebarTag;
137     }
138
139     /**
140      * Does nothing, returns {@link #EVAL_BODY_BUFFERED} always.
141      */

142     public int doStartTag() throws JspException JavaDoc {
143         return EVAL_BODY_BUFFERED;
144     }
145
146     /**
147      * Gets the {@link AdminPageBean} instance from the request. If it doesn't exist then execution is stopped
148      * and nothing is printed. If it exists, retrieve values from it and render the sidebar items. The body content
149      * of the tag is assumed to have an A tag in it with tokens to replace (see class description) as well
150      * as having a subsidebar tag..
151      *
152      * @return {@link #EVAL_PAGE} after rendering the tabs.
153      * @throws JspException if an exception occurs while rendering the sidebar items.
154      */

155     public int doEndTag() throws JspException JavaDoc {
156         // Start by getting the request from the page context
157
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
158
159         // Check for body of this tag and the child tag
160
if (getBodyContent().getString() == null) {
161             throw new JspException JavaDoc("Error, no template (body value) set for the sidebar tag.");
162         }
163         if (subsidebarTag.getBody() == null) {
164             throw new JspException JavaDoc("Error, no template (body value) set for the subsidebar tag");
165         }
166
167         // Get the page data bean from the request:
168
AdminPageBean pageInfo = (AdminPageBean)request.getAttribute(getBean());
169
170         // If the page info bean is not in the request then no tab will be selected - so, it'll fail gracefully
171
if (pageInfo != null) {
172
173             // Get the initial subpage and page IDs
174
String JavaDoc subPageID = pageInfo.getSubPageID();
175             String JavaDoc pageID = pageInfo.getPageID();
176
177             // If the pageID is null, use the subPageID to set it. If both the pageID and subPageIDs are null,
178
// return because these are key to execution of the tag.
179
if (subPageID != null || pageID != null) {
180
181                 if (pageID == null) {
182                     Element subPage = AdminConsole.getElemnetByID(subPageID);
183                     pageID = subPage.getParent().getParent().attributeValue("id");
184                 }
185
186                 // Top level menu items
187
if (AdminConsole.getModel().elements().size() > 0) {
188                     JspWriter JavaDoc out = pageContext.getOut();
189                     StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
190
191                     Element current = null;
192                     Element subcurrent = null;
193                     if (subPageID != null) {
194                         subcurrent = AdminConsole.getElemnetByID(subPageID);
195                     }
196                     current = AdminConsole.getElemnetByID(pageID);
197
198                     Element currentTab = (Element)AdminConsole.getModel().selectSingleNode(
199                             "//*[@id='" + pageID + "']/ancestor::tab");
200
201                     boolean isSubmenu = false;
202                     if (subcurrent != null) {
203                         isSubmenu = subcurrent.getParent().getParent().getName().equals("item");
204                     }
205
206                     // Loop through all items in the root, print them out
207
if (currentTab != null) {
208                         Collection JavaDoc items = currentTab.elements();
209                         if (items.size() > 0) {
210                             buf.append("<ul>");
211                             for (Iterator JavaDoc iter=items.iterator(); iter.hasNext(); ) {
212                                 Element sidebar = (Element)iter.next();
213                                 String JavaDoc header = sidebar.attributeValue("name");
214                                 // Print the header:
215
String JavaDoc hcss = getHeadercss();
216                                 if (hcss == null) {
217                                     hcss = "";
218                                 }
219                                 buf.append("<li class=\"").append(hcss).append("\">").append(clean(i18n(header))).append("</li>");
220                                 // Now print all items:
221

222                                 for (Iterator JavaDoc subitems=sidebar.elementIterator(); subitems.hasNext(); ) {
223                                     Element item = (Element)subitems.next();
224                                     String JavaDoc subitemID = item.attributeValue("id");
225                                     String JavaDoc subitemName = item.attributeValue("name");
226                                     String JavaDoc subitemURL = item.attributeValue("url");
227                                     String JavaDoc subitemDescr = item.attributeValue("description");
228                                     String JavaDoc value = getBodyContent().getString();
229                                     if (value != null) {
230                                         value = StringUtils.replace(value, "[id]", clean(subitemID));
231                                         value = StringUtils.replace(value, "[name]", clean(i18n(subitemName)));
232                                         value = StringUtils.replace(value, "[description]", clean(i18n(subitemDescr)));
233                                         value = StringUtils.replace(value, "[url]",
234                                                 request.getContextPath() + "/" + clean(subitemURL));
235                                     }
236                                     String JavaDoc css = getCss();
237                                     boolean isCurrent = item.equals(current);
238                                     boolean showSubmenu = subPageID != null;
239                                     if (isCurrent && !showSubmenu) {
240                                         css = getCurrentcss();
241                                     }
242                                     buf.append("<li class=\"").append(css).append("\">").append(value).append("</li>");
243
244                                     // Print out a submenu if one exists:
245
if (isSubmenu && isCurrent) {
246                                         // Get the parent of the current item so we can get its
247
// items - those will be siblings of the current item:
248
Iterator JavaDoc siblings = subcurrent.getParent().elementIterator();
249                                         boolean hadNext = siblings.hasNext();
250                                         if (hadNext) {
251                                             // Print out beginning UL
252
buf.append("<ul class=\"subitems\">\n");
253                                             // Print the header LI
254
String JavaDoc subheader = subcurrent.getParent().attributeValue("name");
255                                             buf.append("<li class=\"").append(hcss).append("\">").append(clean(i18n(subheader))).append("</li>");
256                                         }
257                                         String JavaDoc extraParams = pageInfo.getExtraParams();
258                                         while (siblings.hasNext()) {
259                                             Element sibling = (Element)siblings.next();
260                                             String JavaDoc sibID = sibling.attributeValue("id");
261                                             String JavaDoc sibName = sibling.attributeValue("name");
262                                             String JavaDoc sibDescr = sibling.attributeValue("description");
263                                             String JavaDoc sibURL = sibling.attributeValue("url");
264                                             if (extraParams != null) {
265                                                 sibURL += ((sibURL.indexOf('?') > -1 ? "&" : "?") + extraParams);
266                                             }
267                                             boolean isSubCurrent = sibling.equals(subcurrent);
268                                             String JavaDoc subcss = getCss();
269                                             if (isSubCurrent) {
270                                                 subcss = getCurrentcss();
271                                             }
272                                             String JavaDoc svalue = getSubsidebarTag().getBody();
273                                             if (svalue != null) {
274                                                 svalue = StringUtils.replace(svalue, "[id]", clean(sibID));
275                                                 svalue = StringUtils.replace(svalue, "[name]", clean(i18n(sibName)));
276                                                 svalue = StringUtils.replace(svalue, "[description]", clean(i18n(sibDescr)));
277                                                 svalue = StringUtils.replace(svalue, "[url]",
278                                                         request.getContextPath() + "/" + clean(sibURL));
279                                             }
280                                             buf.append("<li class=\"").append(subcss).append("\">").append(svalue).append("</li>\n");
281                                         }
282                                         if (hadNext) {
283                                             // Print out ending UL
284
buf.append("<br></ul>\n");
285                                         }
286                                     }
287                                 }
288                             }
289                             buf.append("</ul>");
290                             try {
291                                 out.write(buf.toString());
292                             }
293                             catch (IOException JavaDoc e) {
294                                 throw new JspException JavaDoc(e);
295                             }
296                         }
297                     }
298                 }
299             }
300         }
301         return EVAL_PAGE;
302     }
303
304     /**
305      * Cleans the given string - if it's null, it's converted to a blank string. If it has ' then those are
306      * converted to double ' so HTML isn't screwed up.
307      *
308      * @param in the string to clean
309      * @return a cleaned version - not null and with escaped characters.
310      */

311     private String JavaDoc clean(String JavaDoc in) {
312         return (in == null ? "" : StringUtils.replace(in, "'", "\\'"));
313     }
314
315     private static String JavaDoc i18n(String JavaDoc in) {
316         if (in == null) {
317             return null;
318         }
319         // Look for the key symbol:
320
if (in.indexOf("${") == 0 && in.indexOf("}") == in.length()-1) {
321             return LocaleUtils.getLocalizedString(in.substring(2, in.length()-1));
322         }
323         return in;
324     }
325 }
Popular Tags