KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > tabbeddialog > www > TabbedDialogTabTag


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: TabbedDialogTabTag.java,v 1.29 2007/01/07 06:14:27 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.patterns.tabbeddialog.www;
23
24 import java.util.List JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
28
29 import org.opensubsystems.core.www.BlockElementTag;
30 import org.opensubsystems.core.www.TagUtils;
31
32 /**
33  * Custom tag to generate all HTML code necessary to display one tab of the
34  * tabbed dialog. The tab of the dialog contains will be created from the content
35  * of the body of this tag. This tag has to be placed inside of dialog tag.
36  *
37  * @version $Id: TabbedDialogTabTag.java,v 1.29 2007/01/07 06:14:27 bastafidli Exp $
38  * @author Miro Halas
39  * @code.reviewer Miro Halas
40  * @code.reviewed 1.25 2006/05/31 01:22:21 jlegeny
41  */

42 public class TabbedDialogTabTag extends BlockElementTag
43 {
44    // Constants ////////////////////////////////////////////////////////////////
45

46    /**
47     * Name of the form tag
48     */

49    public static final String JavaDoc TABBED_DIALOG_FORM_TAG = "</form>";
50
51    // Attributes ///////////////////////////////////////////////////////////////
52

53    /**
54     * Generated serial version id for this class.
55     */

56    private static final long serialVersionUID = 1918742461661215567L;
57    
58    /**
59     * User visible text of the header for this tab.
60     */

61    protected String JavaDoc m_strHeader;
62    
63    /**
64     * If this tab is the active tab then this attribute should say true or 1.
65     * Only one tab of the tab dialog can be made active.
66     */

67    protected String JavaDoc m_strActive;
68
69    /**
70     * URL of an image to display in tab header.
71     */

72    protected String JavaDoc m_strImg;
73
74    /**
75     * Access key that will be used for mnemonics to allow user access the
76     * content of the tab using keyboard.
77     */

78    protected String JavaDoc m_strAccessKey;
79
80    /**
81     * JavaScript function that will be called when the tab becomes active.
82     */

83    protected String JavaDoc m_strOnFocus;
84
85    // Constructors /////////////////////////////////////////////////////////////
86

87    /**
88     * Constructor for custom tag.
89     */

90    public TabbedDialogTabTag()
91    {
92       super("clsTab", BlockElementTag.DIV_BLOCK_ELEMENT);
93       
94       m_strHeader = null;
95       m_strActive = Boolean.FALSE.toString();
96       m_strImg = null;
97       m_strAccessKey = "";
98       m_strOnFocus = "";
99    }
100    
101    // Business logic ///////////////////////////////////////////////////////////
102

103    /**
104     * {@inheritDoc}
105     */

106    public int doStartTag(
107    ) throws JspException JavaDoc
108    {
109       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
110       StringBuffer JavaDoc sbHtmlTemp = new StringBuffer JavaDoc();
111
112       Object JavaDoc objTemp;
113       String JavaDoc strTabbedDialogId;
114       String JavaDoc strHeaderUnderlined = m_strHeader;
115       String JavaDoc strTabbedDialogJSFunction;
116       String JavaDoc strActiveTab = null;
117       
118       objTemp = pageContext.getAttribute(TabbedDialogTag.ACTIVE_TABBED_DIALOG_ID);
119       if (objTemp == null)
120       {
121          throw new JspException JavaDoc("Cannot find tabbed dialog id on the page. Probable" +
122                                 " cause is that the tabbed dialog open tag is missing.");
123       }
124       strTabbedDialogId = (String JavaDoc)objTemp;
125
126       objTemp = getCachedContent(TabbedDialogTag.TABBED_DIALOG_ONSWITCH_JSFUNCTION,
127                                  false).trim();
128       if (objTemp == null)
129       {
130          throw new JspException JavaDoc("Cannot find javascript function called onswitch. Probable" +
131                                 " cause is that the scrollable table open tag is missing.");
132       }
133       strTabbedDialogJSFunction = (String JavaDoc)objTemp;
134
135       // Get parameter storing active tab name. If this parameter is
136
// specified and equals to m_strId, set up this tab as active
137
strActiveTab = pageContext.getRequest().getParameter("activetab");
138       if (strActiveTab == null)
139       {
140          strActiveTab = (String JavaDoc)pageContext.getRequest().getAttribute("activetab");
141       }
142       
143
144       // Check if there is sent tab header text. If yes, underline particular char.
145
if ((m_strHeader != null) && (m_strHeader.length() > 0))
146       {
147          int iCharIndex = -1;
148          // Look for first occurence of accesskey. First try looking for
149
// case sensitive and if not found then look for case insensitive.
150
// After char was found, surround it by underline tag <u></u>
151
if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
152          {
153             iCharIndex = m_strHeader.indexOf(m_strAccessKey);
154             if (iCharIndex == -1)
155             {
156                iCharIndex = m_strHeader.toLowerCase().indexOf(m_strAccessKey.toLowerCase());
157             }
158          }
159          if (iCharIndex == -1)
160          {
161             if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
162             {
163                // There was not found accesskey char within the label content.
164
// Create comment about it and add it into the html code.
165
sbHtmlTemp.append("<!-- Accesskey '");
166                sbHtmlTemp.append(m_strAccessKey);
167                sbHtmlTemp.append("' was not found within the label content for ");
168                sbHtmlTemp.append(strTabbedDialogId);
169                sbHtmlTemp.append(m_strId);
170                sbHtmlTemp.append(" -->");
171             }
172          }
173          else
174          {
175             // There was found accesskey char within the label content.
176
// Replace its position with the same but underlined char.
177
sbHtmlTemp.append(m_strHeader.substring(0, iCharIndex));
178             sbHtmlTemp.append("<u>");
179             sbHtmlTemp.append(m_strHeader.charAt(iCharIndex));
180             sbHtmlTemp.append("</u>");
181             sbHtmlTemp.append(m_strHeader.substring(iCharIndex + 1, m_strHeader.length()));
182             strHeaderUnderlined = sbHtmlTemp.toString();
183             sbHtmlTemp.delete(0, sbHtmlTemp.length());
184          }
185       }
186
187       /*
188       This is how a start of one tab of a tabbed dialog looks like
189       <div id="sessionbasicheader" class="clsTabHeaderActive">
190          <a HREF="#basic" rel="Subsection" rev="Subsection" accesskey="X"
191             onfocus="switchTabTo(event, 'test', null);dialogHelp(event);">Basic information</a>
192       </div>
193       <div id="sessionbasic" class="clsTabBodyActive">
194       */

195
196       // Generate the start of the tabbed dialog tab header
197
sbHtml.append("<div id=\"");
198       sbHtml.append(strTabbedDialogId);
199       sbHtml.append(m_strId);
200       sbHtml.append("header\"");
201       if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
202       {
203          sbHtml.append(" class=\"");
204          sbHtml.append(m_strCssclass);
205          sbHtml.append("Header");
206          if (((isActiveTab()) && ((strActiveTab == null)
207              || ((strActiveTab != null) && (strActiveTab.length() == 0))))
208              || ((strActiveTab != null) && (strActiveTab.equals(m_strId))))
209          {
210             sbHtml.append("Active");
211          }
212          sbHtml.append("\"");
213       }
214       sbHtml.append(">\n");
215       
216       // Generate the text on the tab as a hyperlink so that user can get there
217
// using Tab
218
sbHtml.append("<a HREF=\"#");
219       sbHtml.append(m_strId);
220       sbHtml.append("\" rel=\"Subsection\" rev=\"Subsection\"");
221       // Define accesskey attribute for href tag to be possible to switch tab
222
// using ALT+letter
223
if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
224       {
225          sbHtml.append(" accesskey=\"");
226          sbHtml.append(m_strAccessKey);
227          sbHtml.append("\"");
228       }
229       // Add onfocus method that will automatically switch to particular focused tab
230
sbHtml.append(" onfocus=\"switchTabTo('");
231       sbHtml.append(strTabbedDialogId);
232       sbHtml.append(m_strId);
233       sbHtml.append("header', '");
234       sbHtml.append(strTabbedDialogId);
235       sbHtml.append("', ");
236       sbHtml.append(strTabbedDialogJSFunction);
237       sbHtml.append(");dialogHelp(event);");
238       // add externaly defined javascript that has to be processed onfocus
239
if ((m_strOnFocus != null) && (m_strOnFocus.length() > 0))
240       {
241          sbHtml.append(m_strOnFocus);
242       }
243       sbHtml.append("\">");
244       // Generate the image in the tab header
245
if ((m_strImg != null) && (m_strImg.length() > 0))
246       {
247          sbHtml.append("<img SRC=\"");
248          sbHtml.append(m_strImg);
249          sbHtml.append("\" border=\"0\" alt=\"\"");
250          if ((m_strHeader != null) && (m_strHeader.length() > 0))
251          {
252             sbHtml.append(" title=\"");
253             sbHtml.append(m_strHeader);
254             sbHtml.append("\"");
255          }
256          sbHtml.append(" align=\"middle\">");
257          if ((m_strHeader != null) && (m_strHeader.length() > 0))
258          {
259             sbHtml.append(" ");
260          }
261       }
262
263       // Now add header text to the tab
264
if ((m_strHeader != null) && (m_strHeader.length() > 0))
265       {
266          sbHtml.append(strHeaderUnderlined);
267       }
268
269       sbHtml.append("</a>\n");
270       // Add comment if there was not found accesskey char within the label content
271
sbHtml.append(sbHtmlTemp);
272       // Generate the end of the tabbed dialog tab header
273
sbHtml.append("</div>\n");
274       
275       // Generate the start of the tabbed dialog tab body
276
sbHtml.append("<div id=\"");
277       sbHtml.append(strTabbedDialogId);
278       sbHtml.append(m_strId);
279       sbHtml.append("\"");
280       if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
281       {
282          sbHtml.append(" class=\"");
283          sbHtml.append(m_strCssclass);
284          sbHtml.append("Body");
285          if (((isActiveTab()) && ((strActiveTab == null)
286              || ((strActiveTab != null) && (strActiveTab.length() == 0))))
287              || ((strActiveTab != null) && (strActiveTab.equals(m_strId))))
288          {
289             sbHtml.append("Active");
290          }
291          sbHtml.append("\"");
292       }
293       sbHtml.append(">");
294       
295       TagUtils.write(pageContext, sbHtml.toString());
296       
297       return (EVAL_BODY_BUFFERED);
298    }
299
300    /**
301     * {@inheritDoc}
302     */

303    public int doEndTag(
304    ) throws JspException JavaDoc
305    {
306       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
307       Object JavaDoc objTemp;
308       String JavaDoc strTabbedDialogId;
309
310       // Get the buffered body and try to find inner <form>. If inner form exists
311
// then generate hidden variable that stores activetab name
312
BodyContent JavaDoc content = getBodyContent();
313       StringBuffer JavaDoc sbContent = new StringBuffer JavaDoc();
314       
315       objTemp = pageContext.getAttribute(TabbedDialogTag.ACTIVE_TABBED_DIALOG_ID);
316       if (objTemp == null)
317       {
318          throw new JspException JavaDoc("Cannot find tabbed dialog id on the page. Probable" +
319                                 " cause is that the tabbed dialog open tag is missing.");
320       }
321       strTabbedDialogId = (String JavaDoc)objTemp;
322
323       // Find out if there is any control which should have focus when this tab
324
// is focused
325
List JavaDoc items = getCachedContentElements(TabbedDialogTag.FOCUSED_CONTROL_ID,
326                                             true);
327       
328       if (!items.isEmpty())
329       {
330          String JavaDoc strControlId;
331          
332          if (items.size() > 1)
333          {
334             sbHtml.append("<!-- Multiple controls were set as focus in" +
335                           " this tab, using only the first one-->\n");
336          }
337          strControlId = items.get(0).toString();
338          
339          sbHtml.append("<input id=\"");
340          sbHtml.append(strTabbedDialogId);
341          sbHtml.append(m_strId);
342          sbHtml.append("focus\" type=\"hidden\" value=\"");
343          sbHtml.append(strControlId);
344          sbHtml.append("\">\n");
345       }
346       
347       if (content != null)
348       {
349          sbContent.append(content.getString());
350          
351          int iIndex;
352          int iFormIndex = 0;
353          int iNextIndex;
354          boolean bHasMoreForms = false;
355          
356          // TODO: Performance: We are repeatedly doin here
357
// sbContent.toString().toLowerCase()
358
// We should optimize the code to avoid this constant lowercasing
359

360          // check if there exist an inner form
361
iIndex = sbContent.toString().toLowerCase().indexOf(TABBED_DIALOG_FORM_TAG);
362          if (iIndex != -1)
363          {
364             do
365             {
366                // find out if there are more forms within 1 tab
367
// if not -> don't generate numeric form index (e.g. activetab_name_index)
368
// if yes -> generate numeric form index (e.g. activetab_name_index1,
369
// activetab_name_index2, ...)
370
iNextIndex = sbContent.toString().toLowerCase().indexOf(
371                                TABBED_DIALOG_FORM_TAG, iIndex + 1);
372                // Only if we haven't detected yet if there is another form
373
// try to detect if there is more than one form
374
if (!bHasMoreForms)
375                {
376                   bHasMoreForms = (iNextIndex != -1);
377                }
378                
379                // inner form exists, add input parameter for inner activetab
380
// and insert hidden parameters in front of this tag
381
sbHtml.append("<input id=\"activetab_");
382                sbHtml.append(m_strId);
383                if (bHasMoreForms)
384                {
385                   sbHtml.append("_");
386                   sbHtml.append(iFormIndex);
387                }
388                sbHtml.append("\" type=\"hidden\"");
389                sbHtml.append(" name=\"activetab_");
390                sbHtml.append(m_strId);
391                if (bHasMoreForms)
392                {
393                   sbHtml.append("_");
394                   sbHtml.append(iFormIndex++);
395                }
396                sbHtml.append("\" value=\"");
397                sbHtml.append(m_strId);
398                sbHtml.append("\">\n");
399                
400                // insert hidden parameter in front of the </form> tag
401
sbContent.insert(iIndex, sbHtml.toString());
402                // Try to process next form if any
403
iIndex = iNextIndex + sbHtml.length();
404                // clear buffer
405
sbHtml.delete(0, sbHtml.length());
406             }
407             while (iNextIndex != -1);
408          }
409          else
410          {
411             // inner form doesn't exist, add hidden parameter for focus
412
//sbContent.append(strContent);
413
sbContent.append(sbHtml);
414          }
415          
416          sbContent.append("</div>");
417       }
418
419       // Finish the tabbed dialog tab body
420
TagUtils.write(pageContext, sbContent.toString());
421       
422       return (EVAL_PAGE);
423    }
424
425    /**
426     * @return String - Text of the header for this tab
427     */

428    public String JavaDoc getHeader(
429    )
430    {
431       return m_strHeader;
432    }
433
434    /**
435     * @return String - If this tab is the active tab then this attribute should
436     * say true or 1.
437     */

438    public String JavaDoc getActive(
439    )
440    {
441       return m_strActive;
442    }
443
444    /**
445     * @return String - image to display in header if any
446     */

447    public String JavaDoc getImg()
448    {
449       return m_strImg;
450    }
451
452    /**
453     * @param strHeader - Text of the header for this tab
454     */

455    public void setHeader(
456       String JavaDoc strHeader
457    )
458    {
459       m_strHeader = strHeader;
460    }
461
462    /**
463     * @param strActive - If this tab is the active tab then this attribute should
464     * say true or 1.
465     */

466    public void setActive(
467       String JavaDoc strActive
468    )
469    {
470       m_strActive = strActive;
471    }
472    
473    /**
474     * @param bActive - If this tab is the active tab then this attribute should
475     * say true or 1.
476     */

477    public void setActive(
478       boolean bActive
479    )
480    {
481       m_strActive = Boolean.toString(bActive);
482    }
483
484    /**
485     * @param strImg - image to display in the header if any
486     */

487    public void setImg(
488       String JavaDoc strImg
489    )
490    {
491       m_strImg = strImg;
492    }
493
494    /**
495     * @return boolean - true if this tab is the active tab
496     */

497    public boolean isActiveTab(
498    )
499    {
500       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strActive))
501              || ("1".equals(m_strActive)));
502    }
503
504    /**
505     * @return - Accesskey used for mnemonics
506     */

507    public String JavaDoc getAccesskey(
508    )
509    {
510       return m_strAccessKey;
511    }
512
513    /**
514     * @param strAccessKey - AccessKey that will be used in label for mnemonics
515     */

516    public void setAccesskey(
517       String JavaDoc strAccessKey
518    )
519    {
520       m_strAccessKey = strAccessKey;
521    }
522
523    /**
524     * @return - Javascript called onfocus
525     */

526    public String JavaDoc getOnfocus(
527    )
528    {
529       return m_strOnFocus;
530    }
531
532    /**
533     * @param strOnFocus - Javascript called onfocus
534     */

535    public void setOnfocus(
536       String JavaDoc strOnFocus
537    )
538    {
539       m_strOnFocus = strOnFocus;
540    }
541 }
542
Popular Tags