KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > taglib > TabsTag


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Michel-Ange ANTON
22  * --------------------------------------------------------------------------
23  * $Id: TabsTag.java,v 1.5 2003/06/20 17:15:42 antonma Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.webapp.taglib;
28
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 import javax.servlet.jsp.JspException JavaDoc;
33 import javax.servlet.jsp.JspWriter JavaDoc;
34
35 public class TabsTag extends WhereAreYouTag {
36
37 // ----------------------------------------------------- Constants
38

39     private static final String JavaDoc s_ImageSeparator = "dot.gif";
40
41 // ----------------------------------------------------- Instance Variables
42

43     private ArrayList JavaDoc m_LabelTabs = new ArrayList JavaDoc();
44     private ArrayList JavaDoc m_HrefTabs = new ArrayList JavaDoc();
45     private ArrayList JavaDoc m_SelectedTabs = new ArrayList JavaDoc();
46     private String JavaDoc m_Body = null;
47
48 // ------------------------------------------------------------- Properties
49

50     private int widthTab = 0;
51     private int heightTab = 0;
52     private String JavaDoc width = "100%";
53     private String JavaDoc height = "";
54     private int widthSeparator = 3;
55     private int heightSeparator = 1;
56     private int marginPanel = 5;
57     private String JavaDoc imagesDir = null;
58
59     /**
60      * Accessor WidthTab property.
61      */

62     public int getWidthTab() {
63         return widthTab;
64     }
65
66     public void setWidthTab(int widthTab) {
67         this.widthTab = widthTab;
68     }
69
70     /**
71      * Accessor HeightTab property.
72      */

73     public int getHeightTab() {
74         return heightTab;
75     }
76
77     public void setHeightTab(int heightTab) {
78         this.heightTab = heightTab;
79     }
80
81     /**
82      * Accessor Width property.
83      */

84     public String JavaDoc getWidth() {
85         return width;
86     }
87
88     public void setWidth(String JavaDoc width) {
89         this.width = width;
90     }
91
92     /**
93      * Accessor Height property.
94      */

95     public String JavaDoc getHeight() {
96         return height;
97     }
98
99     /**
100      * Accessor Height property.
101      */

102     public void setHeight(String JavaDoc height) {
103         this.height = height;
104     }
105
106     /**
107      * Accessor WidthSeparator property.
108      */

109     public int getWidthSeparator() {
110         return widthSeparator;
111     }
112
113     public void setWidthSeparator(int widthSeparator) {
114         this.widthSeparator = widthSeparator;
115     }
116
117     /**
118      * Accessor HeightSeparator property.
119      */

120     public int getHeightSeparator() {
121         return heightSeparator;
122     }
123
124     public void setHeightSeparator(int heightSeparator) {
125         this.heightSeparator = heightSeparator;
126     }
127
128     /**
129      * Accessor MarginPanel property.
130      */

131     public int getMarginPanel() {
132         return marginPanel;
133     }
134
135     public void setMarginPanel(int marginPanel) {
136         this.marginPanel = marginPanel;
137     }
138
139     /**
140      * Accessor ImagesDir property.
141      */

142     public String JavaDoc getImagesDir() {
143         return imagesDir;
144     }
145
146     public void setImagesDir(String JavaDoc imagesDir) {
147         this.imagesDir = imagesDir;
148     }
149
150 // --------------------------------------------------------- Public Methods
151

152     public int doStartTag()
153         throws JspException JavaDoc {
154
155         this.m_LabelTabs.clear();
156         this.m_HrefTabs.clear();
157         this.m_SelectedTabs.clear();
158         this.m_Body = null;
159
160         return (EVAL_BODY_BUFFERED);
161     }
162
163     /**
164      * Render this instant actions control.
165      *
166      * @exception JspException if a processing error occurs
167      */

168     public int doEndTag()
169         throws JspException JavaDoc {
170         JspWriter JavaDoc out = pageContext.getOut();
171         try {
172             verifySelected();
173             render(out);
174         }
175         catch (IOException JavaDoc e) {
176             throw new JspException JavaDoc(e);
177         }
178         return (EVAL_PAGE);
179     }
180
181     /**
182      * Release all state information set by this tag.
183      */

184     public void release() {
185         this.m_LabelTabs.clear();
186         this.m_HrefTabs.clear();
187         this.m_SelectedTabs.clear();
188         this.m_Body = null;
189         this.width = null;
190         this.height = null;
191         this.imagesDir = null;
192     }
193
194 // -------------------------------------------------------- Package Methods
195

196     /**
197      * Add a new Action to the set that will be rendered by this control.
198      *
199      * @param label Localized label visible to the user
200      * @param selected Initial selected state of this option
201      * @param url URL to which control should be transferred if selected
202      */

203
204     void addTab(String JavaDoc ps_Label, String JavaDoc ps_Href, boolean ps_Selected) {
205         m_LabelTabs.add(ps_Label);
206         m_HrefTabs.add(ps_Href);
207         m_SelectedTabs.add(new Boolean JavaDoc(ps_Selected));
208     }
209
210     void setBody(String JavaDoc ps_Body) {
211         m_Body = ps_Body;
212     }
213
214 // ------------------------------------------------------ Private Methods
215

216     private void render(JspWriter JavaDoc out)
217         throws IOException JavaDoc, JspException JavaDoc {
218         out.println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");
219         out.println("<tr>");
220         out.println("<td>");
221         renderTabs(out);
222         out.println("</td>");
223         out.println("</tr>");
224         out.println("<tr>");
225         out.println("<td>");
226         renderPanel(out);
227         out.println("</td>");
228         out.println("</tr>");
229         out.println("</table>");
230     }
231
232     private void renderPanel(JspWriter JavaDoc out)
233         throws IOException JavaDoc, JspException JavaDoc {
234         // Render the panel of this element
235
out.print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"");
236         out.print(marginPanel);
237         out.print("\"");
238         out.print(" width=\"");
239         out.print(width);
240         out.print("\"");
241         out.println(">");
242         out.println("<tr valign=\"top\">");
243         out.println("<td class=\"panel\">");
244         out.print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"");
245         out.print(width);
246         out.print("\"");
247         out.println(">");
248         out.println("<tr valign=\"top\">");
249         out.print("<td height=\"");
250         out.print(height);
251         out.println("\" class=\"panel\">");
252         if (m_Body != null) {
253             out.println(m_Body);
254         }
255         out.println("</td>");
256         out.println("</tr>");
257         out.println("</table>");
258         out.println("</td>");
259         out.println("</tr>");
260         out.println("</table>");
261     }
262
263     private void renderTabs(JspWriter JavaDoc out)
264         throws IOException JavaDoc, JspException JavaDoc {
265         int i = 0;
266         boolean bSelected = false;
267         StringBuffer JavaDoc sbLabel = null;
268         String JavaDoc sImageSep = null;
269
270         // Prepare image separator
271
if (imagesDir != null) {
272             sImageSep = imagesDir + "/" + s_ImageSeparator;
273         }
274         else if (isUsingWhere()) {
275             sImageSep = getImagesRoot() + "/" + s_ImageSeparator;
276         }
277         else {
278             sImageSep = s_ImageSeparator;
279         }
280         // Render the beginning of this element
281
try {
282             out.println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >");
283         }
284         catch (IOException JavaDoc ex) {
285         }
286
287         // Render each defined label tab
288
int n = m_LabelTabs.size();
289         out.println("<tr>");
290         for (i = 0; i < n; i++) {
291             // Init Label display
292
sbLabel = new StringBuffer JavaDoc();
293             // Detect selected tab
294
bSelected = ((Boolean JavaDoc) m_SelectedTabs.get(i)).booleanValue();
295             // Display begin tag
296
out.print("<td align=\"center\"");
297             // Display width tab and prepare label
298
if (widthTab > 0) {
299                 out.print(" width=\"");
300                 out.print(widthTab);
301                 out.print("\"");
302                 sbLabel.append(m_LabelTabs.get(i));
303             }
304             else {
305                 sbLabel.append("&nbsp;");
306                 sbLabel.append(m_LabelTabs.get(i));
307                 sbLabel.append("&nbsp;");
308             }
309             out.print(" class=\"");
310             if (bSelected == true) {
311                 out.print("tabSelect");
312             }
313             else {
314                 out.print("tab");
315             }
316             out.print("\"");
317             // Height tab
318
if (heightTab > 0) {
319                 out.print(" height=\"");
320                 out.print(heightTab);
321                 out.print("\"");
322             }
323             out.print(">");
324
325             // Display label
326
if ((bSelected == true) || (m_HrefTabs.get(i).toString().length() == 0)) {
327                 // Simple label
328
out.print(sbLabel.toString());
329             }
330             else {
331                 // Link label
332
out.print("<a HREF=\"");
333                 out.print(m_HrefTabs.get(i).toString());
334                 out.print("\"");
335                 out.print(" class=\"");
336                 out.print("tab");
337                 out.print("\"");
338                 out.print(">");
339                 out.print(sbLabel.toString());
340                 out.print("</a>");
341             }
342             out.println("</td>");
343
344             if (i < (n - 1)) {
345                 // Separator Part
346
out.print("<td");
347                 out.print(" class=\"tabSeparatorVertical\"");
348                 out.print(">");
349                 // Image
350
out.print("<img SRC=\"");
351                 out.print(sImageSep);
352                 out.print("\" width=\"");
353                 out.print(widthSeparator);
354                 out.print("\" height=\"");
355                 out.print(heightSeparator);
356                 out.print("\" border=\"0\">");
357                 out.println("</td>");
358             }
359         }
360         out.println("</tr>");
361
362         // Render underline
363
out.println("<tr>");
364         for (i = 0; i < n; i++) {
365             bSelected = ((Boolean JavaDoc) m_SelectedTabs.get(i)).booleanValue();
366             // Label Line Part
367
out.print("<td");
368             out.print(" class=\"");
369             if (bSelected == true) {
370                 out.print("tabSelect");
371             }
372             else {
373                 out.print("tabSeparatorHorizontal");
374             }
375             out.print("\"");
376             out.print(">");
377             out.print("<img SRC=\"");
378             out.print(sImageSep);
379             out.print("\" width=\"");
380             out.print(widthSeparator);
381             out.print("\" height=\"");
382             out.print(heightSeparator);
383             out.print("\" border=\"0\">");
384             out.println("</td>");
385
386             // Last tab ?
387
if (i < (n - 1)) {
388                 // Separator Line Part
389
out.print("<td");
390                 out.print(" class=\"tabSeparatorHorizontal\"");
391                 out.print(">");
392                 out.print("<img SRC=\"");
393                 out.print(sImageSep);
394                 out.print("\" width=\"");
395                 out.print(widthSeparator);
396                 out.print("\" height=\"");
397                 out.print(heightSeparator);
398                 out.print("\" border=\"0\">");
399                 out.println("</td>");
400             }
401         }
402         // Display end tag
403
out.println("</tr>");
404         out.println("</table>");
405     }
406
407     /**
408      * Verify if one tab is selected else select the first.
409      */

410     private void verifySelected() {
411         boolean bFound = false;
412         for (int i = 0; i < m_SelectedTabs.size(); i++) {
413             if (bFound == true) {
414                 // unselect all next if one is found
415
m_SelectedTabs.set(i, new Boolean JavaDoc(false));
416             }
417             else {
418                 // detect the first selected
419
bFound = ((Boolean JavaDoc) m_SelectedTabs.get(i)).booleanValue();
420             }
421         }
422         if ((bFound == false) && (m_SelectedTabs.size() > 0)) {
423             m_SelectedTabs.set(0, new Boolean JavaDoc(true));
424         }
425     }
426 }
427
Popular Tags