001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Michel-Ange ANTON
022: * --------------------------------------------------------------------------
023: * $Id: TabsTag.java 2637 2003-06-20 17:15:42Z antonma $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.webapp.taglib;
028:
029: import java.io.IOException;
030: import java.util.ArrayList;
031:
032: import javax.servlet.jsp.JspException;
033: import javax.servlet.jsp.JspWriter;
034:
035: public class TabsTag extends WhereAreYouTag {
036:
037: // ----------------------------------------------------- Constants
038:
039: private static final String s_ImageSeparator = "dot.gif";
040:
041: // ----------------------------------------------------- Instance Variables
042:
043: private ArrayList m_LabelTabs = new ArrayList();
044: private ArrayList m_HrefTabs = new ArrayList();
045: private ArrayList m_SelectedTabs = new ArrayList();
046: private String m_Body = null;
047:
048: // ------------------------------------------------------------- Properties
049:
050: private int widthTab = 0;
051: private int heightTab = 0;
052: private String width = "100%";
053: private String height = "";
054: private int widthSeparator = 3;
055: private int heightSeparator = 1;
056: private int marginPanel = 5;
057: private String imagesDir = null;
058:
059: /**
060: * Accessor WidthTab property.
061: */
062: public int getWidthTab() {
063: return widthTab;
064: }
065:
066: public void setWidthTab(int widthTab) {
067: this .widthTab = widthTab;
068: }
069:
070: /**
071: * Accessor HeightTab property.
072: */
073: public int getHeightTab() {
074: return heightTab;
075: }
076:
077: public void setHeightTab(int heightTab) {
078: this .heightTab = heightTab;
079: }
080:
081: /**
082: * Accessor Width property.
083: */
084: public String getWidth() {
085: return width;
086: }
087:
088: public void setWidth(String width) {
089: this .width = width;
090: }
091:
092: /**
093: * Accessor Height property.
094: */
095: public String getHeight() {
096: return height;
097: }
098:
099: /**
100: * Accessor Height property.
101: */
102: public void setHeight(String 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 getImagesDir() {
143: return imagesDir;
144: }
145:
146: public void setImagesDir(String imagesDir) {
147: this .imagesDir = imagesDir;
148: }
149:
150: // --------------------------------------------------------- Public Methods
151:
152: public int doStartTag() throws JspException {
153:
154: this .m_LabelTabs.clear();
155: this .m_HrefTabs.clear();
156: this .m_SelectedTabs.clear();
157: this .m_Body = null;
158:
159: return (EVAL_BODY_BUFFERED);
160: }
161:
162: /**
163: * Render this instant actions control.
164: *
165: * @exception JspException if a processing error occurs
166: */
167: public int doEndTag() throws JspException {
168: JspWriter out = pageContext.getOut();
169: try {
170: verifySelected();
171: render(out);
172: } catch (IOException e) {
173: throw new JspException(e);
174: }
175: return (EVAL_PAGE);
176: }
177:
178: /**
179: * Release all state information set by this tag.
180: */
181: public void release() {
182: this .m_LabelTabs.clear();
183: this .m_HrefTabs.clear();
184: this .m_SelectedTabs.clear();
185: this .m_Body = null;
186: this .width = null;
187: this .height = null;
188: this .imagesDir = null;
189: }
190:
191: // -------------------------------------------------------- Package Methods
192:
193: /**
194: * Add a new Action to the set that will be rendered by this control.
195: *
196: * @param label Localized label visible to the user
197: * @param selected Initial selected state of this option
198: * @param url URL to which control should be transferred if selected
199: */
200:
201: void addTab(String ps_Label, String ps_Href, boolean ps_Selected) {
202: m_LabelTabs.add(ps_Label);
203: m_HrefTabs.add(ps_Href);
204: m_SelectedTabs.add(new Boolean(ps_Selected));
205: }
206:
207: void setBody(String ps_Body) {
208: m_Body = ps_Body;
209: }
210:
211: // ------------------------------------------------------ Private Methods
212:
213: private void render(JspWriter out) throws IOException, JspException {
214: out
215: .println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");
216: out.println("<tr>");
217: out.println("<td>");
218: renderTabs(out);
219: out.println("</td>");
220: out.println("</tr>");
221: out.println("<tr>");
222: out.println("<td>");
223: renderPanel(out);
224: out.println("</td>");
225: out.println("</tr>");
226: out.println("</table>");
227: }
228:
229: private void renderPanel(JspWriter out) throws IOException,
230: JspException {
231: // Render the panel of this element
232: out
233: .print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"");
234: out.print(marginPanel);
235: out.print("\"");
236: out.print(" width=\"");
237: out.print(width);
238: out.print("\"");
239: out.println(">");
240: out.println("<tr valign=\"top\">");
241: out.println("<td class=\"panel\">");
242: out
243: .print("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"");
244: out.print(width);
245: out.print("\"");
246: out.println(">");
247: out.println("<tr valign=\"top\">");
248: out.print("<td height=\"");
249: out.print(height);
250: out.println("\" class=\"panel\">");
251: if (m_Body != null) {
252: out.println(m_Body);
253: }
254: out.println("</td>");
255: out.println("</tr>");
256: out.println("</table>");
257: out.println("</td>");
258: out.println("</tr>");
259: out.println("</table>");
260: }
261:
262: private void renderTabs(JspWriter out) throws IOException,
263: JspException {
264: int i = 0;
265: boolean bSelected = false;
266: StringBuffer sbLabel = null;
267: String sImageSep = null;
268:
269: // Prepare image separator
270: if (imagesDir != null) {
271: sImageSep = imagesDir + "/" + s_ImageSeparator;
272: } else if (isUsingWhere()) {
273: sImageSep = getImagesRoot() + "/" + s_ImageSeparator;
274: } else {
275: sImageSep = s_ImageSeparator;
276: }
277: // Render the beginning of this element
278: try {
279: out
280: .println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >");
281: } catch (IOException ex) {
282: }
283:
284: // Render each defined label tab
285: int n = m_LabelTabs.size();
286: out.println("<tr>");
287: for (i = 0; i < n; i++) {
288: // Init Label display
289: sbLabel = new StringBuffer();
290: // Detect selected tab
291: bSelected = ((Boolean) m_SelectedTabs.get(i))
292: .booleanValue();
293: // Display begin tag
294: out.print("<td align=\"center\"");
295: // Display width tab and prepare label
296: if (widthTab > 0) {
297: out.print(" width=\"");
298: out.print(widthTab);
299: out.print("\"");
300: sbLabel.append(m_LabelTabs.get(i));
301: } else {
302: sbLabel.append(" ");
303: sbLabel.append(m_LabelTabs.get(i));
304: sbLabel.append(" ");
305: }
306: out.print(" class=\"");
307: if (bSelected == true) {
308: out.print("tabSelect");
309: } else {
310: out.print("tab");
311: }
312: out.print("\"");
313: // Height tab
314: if (heightTab > 0) {
315: out.print(" height=\"");
316: out.print(heightTab);
317: out.print("\"");
318: }
319: out.print(">");
320:
321: // Display label
322: if ((bSelected == true)
323: || (m_HrefTabs.get(i).toString().length() == 0)) {
324: // Simple label
325: out.print(sbLabel.toString());
326: } else {
327: // Link label
328: out.print("<a href=\"");
329: out.print(m_HrefTabs.get(i).toString());
330: out.print("\"");
331: out.print(" class=\"");
332: out.print("tab");
333: out.print("\"");
334: out.print(">");
335: out.print(sbLabel.toString());
336: out.print("</a>");
337: }
338: out.println("</td>");
339:
340: if (i < (n - 1)) {
341: // Separator Part
342: out.print("<td");
343: out.print(" class=\"tabSeparatorVertical\"");
344: out.print(">");
345: // Image
346: out.print("<img src=\"");
347: out.print(sImageSep);
348: out.print("\" width=\"");
349: out.print(widthSeparator);
350: out.print("\" height=\"");
351: out.print(heightSeparator);
352: out.print("\" border=\"0\">");
353: out.println("</td>");
354: }
355: }
356: out.println("</tr>");
357:
358: // Render underline
359: out.println("<tr>");
360: for (i = 0; i < n; i++) {
361: bSelected = ((Boolean) m_SelectedTabs.get(i))
362: .booleanValue();
363: // Label Line Part
364: out.print("<td");
365: out.print(" class=\"");
366: if (bSelected == true) {
367: out.print("tabSelect");
368: } else {
369: out.print("tabSeparatorHorizontal");
370: }
371: out.print("\"");
372: out.print(">");
373: out.print("<img src=\"");
374: out.print(sImageSep);
375: out.print("\" width=\"");
376: out.print(widthSeparator);
377: out.print("\" height=\"");
378: out.print(heightSeparator);
379: out.print("\" border=\"0\">");
380: out.println("</td>");
381:
382: // Last tab ?
383: if (i < (n - 1)) {
384: // Separator Line Part
385: out.print("<td");
386: out.print(" class=\"tabSeparatorHorizontal\"");
387: out.print(">");
388: out.print("<img src=\"");
389: out.print(sImageSep);
390: out.print("\" width=\"");
391: out.print(widthSeparator);
392: out.print("\" height=\"");
393: out.print(heightSeparator);
394: out.print("\" border=\"0\">");
395: out.println("</td>");
396: }
397: }
398: // Display end tag
399: out.println("</tr>");
400: out.println("</table>");
401: }
402:
403: /**
404: * Verify if one tab is selected else select the first.
405: */
406: private void verifySelected() {
407: boolean bFound = false;
408: for (int i = 0; i < m_SelectedTabs.size(); i++) {
409: if (bFound == true) {
410: // unselect all next if one is found
411: m_SelectedTabs.set(i, new Boolean(false));
412: } else {
413: // detect the first selected
414: bFound = ((Boolean) m_SelectedTabs.get(i))
415: .booleanValue();
416: }
417: }
418: if ((bFound == false) && (m_SelectedTabs.size() > 0)) {
419: m_SelectedTabs.set(0, new Boolean(true));
420: }
421: }
422: }
|