KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > dialoglayout > www > ImageButtonControlTag


1 /*
2  * Copyright (c) 2005 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ImageButtonControlTag.java,v 1.14 2007/02/01 07:23:08 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.dialoglayout.www;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
27
28 import org.opensubsystems.core.www.PageElementCacheTag;
29 import org.opensubsystems.core.www.TagUtils;
30 import org.opensubsystems.patterns.tabbeddialog.www.TabbedDialogTag;
31
32 /**
33  * Custom tag to generate all HTML code necessary to display image button
34  * control, that is button with image and text, displayed int the dialog row.
35  *
36  * @version $Id: ImageButtonControlTag.java,v 1.14 2007/02/01 07:23:08 bastafidli Exp $
37  * @author Julo Legeny
38  * @code.reviewer Miro Halas
39  * @code.reviewed 1.7 2006/05/24 15:18:30 bastafidli
40  */

41 public class ImageButtonControlTag extends PageElementCacheTag
42 {
43    // Attributes ///////////////////////////////////////////////////////////////
44

45    /**
46     * Generated serial version id for this class.
47     */

48    private static final long serialVersionUID = 2954359974052918557L;
49
50    /**
51     * Css class or combination of classes for this image button that should
52     * contain background image that will be displayed within the button created
53     * by this tag. Common classes are defined in control.css style sheet.
54     */

55    protected String JavaDoc m_strCssclass;
56
57    /**
58     * Css class or combination of classes for this fake image button. This class
59     * will be assigned to the hyperlink that represents the fake button and that
60     * will be source of the events whenever user clicks on it.
61     */

62    protected String JavaDoc m_strFakecssclass;
63    
64    /**
65     * Tooltip that will be displayed for particular button control when user
66     * hovers with mouse over it.
67     */

68    protected String JavaDoc m_strTitle;
69
70    /**
71     * Type of the button control (button/reset/submit)
72     */

73    protected String JavaDoc m_strType;
74
75    /**
76     * Accesskey that will be used for mnemonics (using ALT+letter)
77     */

78    protected String JavaDoc m_strAccessKey;
79
80    /**
81     * Javascript to execute when user clicks on the button.
82     */

83    protected String JavaDoc m_strOnclick;
84
85    /**
86     * Flag which will tells us if the button is disabled. Default is false.
87     */

88    protected String JavaDoc m_strDisabled;
89
90    /**
91     * Flag which will tells us if to create a fake button using a DIV tag or
92     * a real button using a button tag. Default is false.
93     */

94    protected String JavaDoc m_strFake;
95
96    /**
97     * Does this control have focus when it is displayed on the page or not.
98     * Only one control on a page or tab can be marked as focus at a time.
99     * If multiple controls are marked this way, then the first one on the page
100     * or tab will get the focus.
101     */

102    protected String JavaDoc m_strFocus;
103
104    // Constructors /////////////////////////////////////////////////////////////
105

106    /**
107     * Constructor for custom tag.
108     */

109    public ImageButtonControlTag()
110    {
111       super();
112       
113       // The default class for buttons with images.
114
m_strCssclass = "clsImageTextButton";
115       m_strFakecssclass = null;
116       m_strDisabled = Boolean.FALSE.toString();
117       m_strFake = Boolean.FALSE.toString();
118    }
119    
120    // Business logic ///////////////////////////////////////////////////////////
121

122    /**
123     * {@inheritDoc}
124     */

125    public int doStartTag(
126    ) throws JspException JavaDoc
127    {
128       // Since we are buffering the body, we do not have to call
129
// doStartTag(StringBuffer)
130
return (EVAL_BODY_BUFFERED);
131    }
132    
133    /**
134     * {@inheritDoc}
135     */

136    public int doEndTag(
137    ) throws JspException JavaDoc
138    {
139       StringBuffer JavaDoc sbHtml = new StringBuffer JavaDoc();
140       BodyContent JavaDoc content = getBodyContent();
141       String JavaDoc strContent = "";
142       if (content != null)
143       {
144          strContent = content.getString();
145       }
146       
147       doEndTag(sbHtml, strContent);
148       
149       TagUtils.write(pageContext, sbHtml.toString());
150
151       if (isFocusedControl())
152       {
153          // Cache the content since it should be inserted at a row
154
// boundary and not at a field boundary
155
cache(TabbedDialogTag.FOCUSED_CONTROL_ID, m_strId);
156       }
157
158       return (EVAL_PAGE);
159    }
160    
161    /**
162     * Generate content of the buffer within the end tag. Since we are buffering
163     * the content we do not provide any doStartTag(StringBuffer) and the caller
164     * is responsible for buffering the body and passing it as parameter
165     *
166     * @param sbHtml - generated string buffer
167     * @param strContent - content which was specified between start and the end
168     * tag, which would be the name of the button
169     */

170    public void doEndTag(
171       StringBuffer JavaDoc sbHtml,
172       String JavaDoc strContent
173    )
174    {
175       if (isFakeButton())
176       {
177          if (strContent == null)
178          {
179             strContent = "";
180          }
181          
182          String JavaDoc contextpath;
183
184          contextpath = ((HttpServletRequest JavaDoc)pageContext.getRequest()).getContextPath();
185          /*
186          <div id="windowclosebutton"
187               class="clsImageOnlyFakeButton clsWindowHeaderButton clsImageCloseButton"><a
188               href="#"
189               onClick="restoreWindowSize();closeWindow('<%=contextpath%>');return false;"><img
190               id="windowclosebuttonimg"
191               src="<%=contextpath%>/patterns/images/transparentbutton.gif"
192               alt="Close window" title="Close window"></a></div>
193          */

194          sbHtml.append("<div");
195          // There are buttons which do not have id, for example context help
196
// close button
197
if ((m_strId != null) && (m_strId.length() > 0))
198          {
199             sbHtml.append(" id=\"");
200             sbHtml.append(m_strId);
201             sbHtml.append("\"");
202          }
203          if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
204          {
205             sbHtml.append(" class=\"");
206             sbHtml.append(m_strCssclass);
207             sbHtml.append("\"");
208          }
209          sbHtml.append("><a HREF=\"#\"");
210          if ((m_strFakecssclass != null) && (m_strFakecssclass.length() > 0))
211          {
212             sbHtml.append(" class=\"");
213             sbHtml.append(m_strFakecssclass);
214             sbHtml.append("\"");
215          }
216          if ((m_strOnclick != null) && (m_strOnclick.length() > 0))
217          {
218             sbHtml.append(" onClick=\"");
219             sbHtml.append(m_strOnclick);
220             sbHtml.append(";return false;\"");
221          }
222          sbHtml.append("><img");
223          if ((m_strId != null) && (m_strId.length() > 0))
224          {
225             sbHtml.append(" id=\"");
226             sbHtml.append(m_strId);
227             sbHtml.append("img\"");
228          }
229          sbHtml.append(" SRC=\"");
230          sbHtml.append(contextpath);
231          sbHtml.append("/patterns/images/transparentbutton.gif\"");
232          if ((m_strTitle != null) && (m_strTitle.length() > 0))
233          {
234             sbHtml.append(" alt=\"");
235             sbHtml.append(m_strTitle);
236             sbHtml.append("\"");
237             sbHtml.append(" title=\"");
238             sbHtml.append(m_strTitle);
239             sbHtml.append("\"");
240          }
241          sbHtml.append(">");
242          // Insert the button text passed in buffered body
243
formatButtonText(sbHtml, strContent);
244          sbHtml.append("</a></div>");
245       }
246       else
247       {
248          /*
249          <button id="resetchangebutton" class="clsImageTextButton"
250                  title="Reset all entered information"
251                  type="reset" accesskey="R">Reset</button>
252         */

253         
254          if ((strContent == null) || (strContent.trim().length() == 0))
255          {
256             // This is fix for ug #1606415. By including single characted but
257
// making it invisible but not nondisplayable all browsers will display
258
// the button correctly and not too short.
259
strContent = "<span style=\"visibility: hidden;\">.</span>";
260          }
261             
262          // Generate the start of the buton tag
263
sbHtml.append("<button id=\"");
264          sbHtml.append(m_strId);
265          sbHtml.append("\"");
266          if ((m_strCssclass != null) && (m_strCssclass.length() > 0))
267          {
268             sbHtml.append(" class=\"");
269             sbHtml.append(m_strCssclass);
270             sbHtml.append("\"");
271          }
272          if ((m_strTitle != null) && (m_strTitle.length() > 0))
273          {
274             sbHtml.append(" title=\"");
275             sbHtml.append(m_strTitle);
276             sbHtml.append("\"");
277          }
278          if ((m_strType != null) && (m_strType.length() > 0))
279          {
280             sbHtml.append(" type=\"");
281             sbHtml.append(m_strType);
282             sbHtml.append("\"");
283          }
284          else
285          {
286             sbHtml.append(" type=\"button\"");
287          }
288          if (isDisabledButton())
289          {
290             sbHtml.append(" disabled");
291          }
292          if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
293          {
294             sbHtml.append(" accesskey=\"");
295             sbHtml.append(m_strAccessKey);
296             sbHtml.append("\"");
297          }
298          if ((m_strOnclick != null) && (m_strOnclick.length() > 0))
299          {
300             sbHtml.append(" onClick=\"");
301             sbHtml.append(m_strOnclick);
302             sbHtml.append("\"");
303          }
304          sbHtml.append(">");
305    
306          // Insert the button text passed in buffered body
307
formatButtonText(sbHtml, strContent);
308    
309          sbHtml.append("</button>");
310       }
311    }
312    
313    /**
314     * @return String - Class for the button control
315     */

316    public String JavaDoc getCssclass(
317    )
318    {
319       return m_strCssclass;
320    }
321    
322    /**
323     * @param strCssclass - Class for the button control
324     */

325    public void setCssclass(
326       String JavaDoc strCssclass
327    )
328    {
329       m_strCssclass = strCssclass;
330    }
331    
332    /**
333     * @return String - Class for the hyperlink within the fake button control
334     */

335    public String JavaDoc getFakecssclass(
336    )
337    {
338       return m_strFakecssclass;
339    }
340    
341    /**
342     * @param strFakecssclass - Class for the hyperlink within the fake button
343     * control
344     */

345    public void setFakecssclass(
346       String JavaDoc strFakecssclass
347    )
348    {
349       m_strFakecssclass = strFakecssclass;
350    }
351    
352    /**
353     * @return String - Type of the button tag used to specify button
354     * control type (button/reset/submit).
355     */

356    public String JavaDoc getType(
357    )
358    {
359       return m_strType;
360    }
361
362    /**
363     * @param strType - Type of the button tag used to specify button
364     * control type (button/reset/submit).
365     */

366    public void setType(
367       String JavaDoc strType
368    )
369    {
370       m_strType = strType;
371    }
372
373    /**
374     * @return String - strTitle - Ttitle (tooltip) that will be shown for
375     * particular button..
376     */

377    public String JavaDoc getTitle(
378    )
379    {
380       return m_strTitle;
381    }
382
383    /**
384     * @param strTitle - Ttitle (tooltip) that will be shown for particular button.
385     */

386    public void setTitle(
387       String JavaDoc strTitle
388    )
389    {
390       m_strTitle = strTitle;
391    }
392
393    /**
394     * @return - Accesskey used for mnemonics (using ALT+letter)
395     */

396    public String JavaDoc getAccesskey(
397    )
398    {
399       return m_strAccessKey;
400    }
401
402    /**
403     * @param strAccessKey - AccessKey that will be used for mnemonics
404     * (using ALT+letter)
405     */

406    public void setAccesskey(
407       String JavaDoc strAccessKey
408    )
409    {
410       m_strAccessKey = strAccessKey;
411    }
412
413    /**
414     * @return String - javascript to execute when user clicks on the button
415     */

416    public String JavaDoc getOnclick()
417    {
418       return m_strOnclick;
419    }
420    
421    /**
422     * @param strOnclick - javascript to execute when user clicks on the button
423     */

424    public void setOnclick(
425       String JavaDoc strOnclick
426    )
427    {
428       if ((strOnclick != null) && (strOnclick.length() > 0))
429       {
430          if (strOnclick.startsWith(CACHE_INDICATOR))
431          {
432             // The attribute is telling me to retrieve it from cache.
433
String JavaDoc strTemp;
434             
435             strTemp = getCachedContent(strOnclick.substring(CACHE_INDICATOR.length()));
436             if ((strTemp != null) && (strTemp.length() > 0))
437             {
438                strOnclick = strTemp;
439             }
440          }
441       }
442       m_strOnclick = strOnclick;
443    }
444
445    /**
446     * @return String - If this button should be disabled then this attribute
447     * should say true or 1.
448     */

449    public String JavaDoc getDisabled(
450    )
451    {
452       return m_strDisabled;
453    }
454
455    /**
456     * @param strDisabled - If this button should be disabled then this attribute
457     * should say true or 1.
458     */

459    public void setDisabled(
460       String JavaDoc strDisabled
461    )
462    {
463       m_strDisabled = strDisabled;
464    }
465    
466    /**
467     * @param bDisabled - If this button should be disabled then this attribute
468     * should say true or 1.
469     */

470    public void setDisabled(
471       boolean bDisabled
472    )
473    {
474       m_strDisabled = Boolean.toString(bDisabled);
475    }
476
477    /**
478     * @return boolean - true if this button should be disabled
479     */

480    public boolean isDisabledButton(
481    )
482    {
483       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strDisabled))
484              || ("1".equals(m_strDisabled)));
485    }
486
487    /**
488     * @return String - If this should be fake button constructed using a DIV tag
489     * rather than a real button created using a button tag then
490     * this attribute should say true or 1.
491     */

492    public String JavaDoc getFake(
493    )
494    {
495       return m_strFake;
496    }
497
498    /**
499     * @param strFake - If this should be fake button constructed using a DIV tag
500     * rather than a real button created using a button tag then
501     * this attribute should say true or 1.
502     */

503    public void setFake(
504       String JavaDoc strFake
505    )
506    {
507       m_strFake = strFake;
508    }
509    
510    /**
511     * @param bFake - If this should be fake button constructed using a DIV tag
512     * rather than a real button created using a button tag then
513     * this attribute should say true or 1.
514     */

515    public void setFake(
516       boolean bFake
517    )
518    {
519       m_strFake = Boolean.toString(bFake);
520    }
521
522    /**
523     * @return boolean - true if this should be fake button constructed using a
524     * DIV tag rather than a real button created using a button
525     * tag
526     */

527    public boolean isFakeButton(
528    )
529    {
530       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strFake))
531              || ("1".equals(m_strFake)));
532    }
533
534    /**
535     * @return String - Does this control have focus when it si displayed on the
536     * page or not.
537     */

538    public String JavaDoc getFocus()
539    {
540       return m_strFocus;
541    }
542
543    /**
544     * @param strFocus - If this control have focus when it si displayed on the
545     * page say true or 1.
546     */

547    public void setFocus(
548       String JavaDoc strFocus
549    )
550    {
551       m_strFocus = strFocus;
552    }
553    
554    /**
555     * @param bFocus - Does this control have focus when it si displayed on the
556     * page or tab.
557     */

558    public void setFocus(
559       boolean bFocus
560    )
561    {
562       m_strFocus = Boolean.toString(bFocus);
563    }
564
565    /**
566     * @return boolean - true if this control has focus when it is displayed
567     * on a page or tab
568     */

569    public boolean isFocusedControl(
570    )
571    {
572       return ((Boolean.TRUE.toString().equalsIgnoreCase(m_strFocus))
573              || ("1".equals(m_strFocus)));
574    }
575    
576    /**
577     * Format the text of the button underlining the access key if one was defined.
578     *
579     * @param sbHtml - buffer where to insert the formated text
580     * @param strContent - the text to display in the button
581     */

582    protected void formatButtonText(
583       StringBuffer JavaDoc sbHtml,
584       String JavaDoc strContent
585    )
586    {
587       StringBuffer JavaDoc sbHtmlTemp = new StringBuffer JavaDoc();
588       int iCharIndex = -1;
589       
590       // Look for first occurence of accesskey. First try looking for
591
// case sensitive and if not found then look for case insensitive.
592
// After char was found, surround it by underline tag <u></u>
593
if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
594       {
595          iCharIndex = strContent.indexOf(m_strAccessKey);
596          if (iCharIndex == -1)
597          {
598             iCharIndex = strContent.toLowerCase().indexOf(m_strAccessKey.toLowerCase());
599          }
600       }
601       if (iCharIndex == -1)
602       {
603          if ((m_strAccessKey != null) && (m_strAccessKey.length() > 0))
604          {
605             // There was not found accesskey char within the label content.
606
// Create comment about it and add it into the html code.
607
sbHtmlTemp.append("\n<!-- Accesskey '");
608             sbHtmlTemp.append(m_strAccessKey);
609             sbHtmlTemp.append("' was not found within the image button content for ");
610             sbHtmlTemp.append(m_strId);
611             sbHtmlTemp.append(" -->\n");
612          }
613       }
614       else
615       {
616          // There was found accesskey char within the image button content.
617
// Replace its position with the same but underlined char.
618
sbHtmlTemp.append(strContent.substring(0, iCharIndex));
619          sbHtmlTemp.append("<u>");
620          sbHtmlTemp.append(strContent.charAt(iCharIndex));
621          sbHtmlTemp.append("</u>");
622          sbHtmlTemp.append(strContent.substring(iCharIndex + 1, strContent.length()));
623          strContent = sbHtmlTemp.toString();
624       }
625       sbHtml.append(strContent);
626    }
627 }
628
Popular Tags