KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > BaseHandlerTag


1 /*
2  * $Id: BaseHandlerTag.java 168243 2005-05-05 02:35:30Z niallp $
3  *
4  * Copyright 1999-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.html;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
28
29 import org.apache.commons.beanutils.BeanUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts.Globals;
33 import org.apache.struts.action.ActionMessages;
34 import org.apache.struts.taglib.TagUtils;
35 import org.apache.struts.taglib.logic.IterateTag;
36 import org.apache.struts.util.MessageResources;
37 import org.apache.struts.util.RequestUtils;
38
39 /**
40  * Base class for tags that render form elements capable of including JavaScript
41  * event handlers and/or CSS Style attributes. This class does not implement
42  * the doStartTag() or doEndTag() methods. Subclasses should provide
43  * appropriate implementations of these.
44  *
45  * @version $Rev: 168243 $ $Date: 2005-05-05 03:35:30 +0100 (Thu, 05 May 2005) $
46  */

47 public abstract class BaseHandlerTag extends BodyTagSupport JavaDoc {
48
49     /**
50      * Commons Logging instance.
51      */

52     private static Log log = LogFactory.getLog(BaseHandlerTag.class);
53
54     // ----------------------------------------------------- Instance Variables
55

56     /**
57      * The default Locale for our server.
58      * @deprecated Use Locale.getDefault() directly.
59      */

60     protected static final Locale JavaDoc defaultLocale = Locale.getDefault();
61
62     /**
63      * The message resources for this package.
64      */

65     protected static MessageResources messages =
66         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
67
68     // Navigation Management
69

70     /** Access key character. */
71     protected String JavaDoc accesskey = null;
72
73     /** Tab index value. */
74     protected String JavaDoc tabindex = null;
75
76     // Indexing ability for Iterate
77

78     /** Whether to created indexed names for fields
79      * @since Struts 1.1
80      */

81     protected boolean indexed = false;
82
83     // Mouse Events
84

85     /** Mouse click event. */
86     private String JavaDoc onclick = null;
87
88     /** Mouse double click event. */
89     private String JavaDoc ondblclick = null;
90
91     /** Mouse over component event. */
92     private String JavaDoc onmouseover = null;
93
94     /** Mouse exit component event. */
95     private String JavaDoc onmouseout = null;
96
97     /** Mouse moved over component event. */
98     private String JavaDoc onmousemove = null;
99
100     /** Mouse pressed on component event. */
101     private String JavaDoc onmousedown = null;
102
103     /** Mouse released on component event. */
104     private String JavaDoc onmouseup = null;
105
106     // Keyboard Events
107

108     /** Key down in component event. */
109     private String JavaDoc onkeydown = null;
110
111     /** Key released in component event. */
112     private String JavaDoc onkeyup = null;
113
114     /** Key down and up together in component event. */
115     private String JavaDoc onkeypress = null;
116
117     // Text Events
118

119     /** Text selected in component event. */
120     private String JavaDoc onselect = null;
121
122     /** Content changed after component lost focus event. */
123     private String JavaDoc onchange = null;
124
125     // Focus Events and States
126

127     /** Component lost focus event. */
128     private String JavaDoc onblur = null;
129
130     /** Component has received focus event. */
131     private String JavaDoc onfocus = null;
132
133     /** Component is disabled. */
134     private boolean disabled = false;
135
136     /** Indicates whether 'disabled' is a valid attribute */
137     protected boolean doDisabled = true;
138
139     /** Component is readonly. */
140     private boolean readonly = false;
141
142     /**
143      * <p>Indicates whether 'readonly' is a valid attribute.</p>
144      *
145      * <p>According to the HTML 4.0 Specification &lt;readonly&gt;
146      * is valid for &lt;input type="text"&gt;, &lt;input type="password"&gt;
147      * and &lt;textarea"&gt; elements. Therefore, except for those tags this
148      * value is set to <code>false</code>.</p>
149      */

150     protected boolean doReadonly = false;
151
152     // CSS Style Support
153

154     /** Style attribute associated with component. */
155     private String JavaDoc style = null;
156
157     /** Named Style class associated with component. */
158     private String JavaDoc styleClass = null;
159
160     /** Identifier associated with component. */
161     private String JavaDoc styleId = null;
162
163     /** The request attribute key for our error messages (if any). */
164     private String JavaDoc errorKey = Globals.ERROR_KEY;
165
166     /** Style attribute associated with component when errors exist. */
167     private String JavaDoc errorStyle = null;
168
169     /** Named Style class associated with component when errors exist. */
170     private String JavaDoc errorStyleClass = null;
171
172     /** Identifier associated with component when errors exist. */
173     private String JavaDoc errorStyleId = null;
174
175     // Other Common Attributes
176

177     /** The alternate text of this element. */
178     private String JavaDoc alt = null;
179
180     /** The message resources key of the alternate text. */
181     private String JavaDoc altKey = null;
182
183     /** The name of the message resources bundle for message lookups. */
184     private String JavaDoc bundle = null;
185
186     /** The name of the session attribute key for our locale. */
187     private String JavaDoc locale = Globals.LOCALE_KEY;
188
189     /** The advisory title of this element. */
190     private String JavaDoc title = null;
191
192     /** The message resources key of the advisory title. */
193     private String JavaDoc titleKey = null;
194
195     // ------------------------------------------------------------- Properties
196

197     // Navigation Management
198

199     /** Sets the accessKey character. */
200     public void setAccesskey(String JavaDoc accessKey) {
201         this.accesskey = accessKey;
202     }
203
204     /** Returns the accessKey character. */
205     public String JavaDoc getAccesskey() {
206         return (this.accesskey);
207     }
208
209     /** Sets the tabIndex value. */
210     public void setTabindex(String JavaDoc tabIndex) {
211         this.tabindex = tabIndex;
212     }
213
214     /** Returns the tabIndex value. */
215     public String JavaDoc getTabindex() {
216         return (this.tabindex);
217     }
218
219     // Indexing ability for Iterate [since Struts 1.1]
220

221     /** Sets the indexed value.
222      * @since Struts 1.1
223      */

224     public void setIndexed(boolean indexed) {
225         this.indexed = indexed;
226     }
227
228     /** Returns the indexed value.
229      * @since Struts 1.1
230      */

231     public boolean getIndexed() {
232         return (this.indexed);
233     }
234
235     // Mouse Events
236

237     /** Sets the onClick event handler. */
238     public void setOnclick(String JavaDoc onClick) {
239         this.onclick = onClick;
240     }
241
242     /** Returns the onClick event handler. */
243     public String JavaDoc getOnclick() {
244         return onclick;
245     }
246
247     /** Sets the onDblClick event handler. */
248     public void setOndblclick(String JavaDoc onDblClick) {
249         this.ondblclick = onDblClick;
250     }
251
252     /** Returns the onDblClick event handler. */
253     public String JavaDoc getOndblclick() {
254         return ondblclick;
255     }
256
257     /** Sets the onMouseDown event handler. */
258     public void setOnmousedown(String JavaDoc onMouseDown) {
259         this.onmousedown = onMouseDown;
260     }
261
262     /** Returns the onMouseDown event handler. */
263     public String JavaDoc getOnmousedown() {
264         return onmousedown;
265     }
266
267     /** Sets the onMouseUp event handler. */
268     public void setOnmouseup(String JavaDoc onMouseUp) {
269         this.onmouseup = onMouseUp;
270     }
271
272     /** Returns the onMouseUp event handler. */
273     public String JavaDoc getOnmouseup() {
274         return onmouseup;
275     }
276
277     /** Sets the onMouseMove event handler. */
278     public void setOnmousemove(String JavaDoc onMouseMove) {
279         this.onmousemove = onMouseMove;
280     }
281
282     /** Returns the onMouseMove event handler. */
283     public String JavaDoc getOnmousemove() {
284         return onmousemove;
285     }
286
287     /** Sets the onMouseOver event handler. */
288     public void setOnmouseover(String JavaDoc onMouseOver) {
289         this.onmouseover = onMouseOver;
290     }
291
292     /** Returns the onMouseOver event handler. */
293     public String JavaDoc getOnmouseover() {
294         return onmouseover;
295     }
296
297     /** Sets the onMouseOut event handler. */
298     public void setOnmouseout(String JavaDoc onMouseOut) {
299         this.onmouseout = onMouseOut;
300     }
301
302     /** Returns the onMouseOut event handler. */
303     public String JavaDoc getOnmouseout() {
304         return onmouseout;
305     }
306
307     // Keyboard Events
308

309     /** Sets the onKeyDown event handler. */
310     public void setOnkeydown(String JavaDoc onKeyDown) {
311         this.onkeydown = onKeyDown;
312     }
313
314     /** Returns the onKeyDown event handler. */
315     public String JavaDoc getOnkeydown() {
316         return onkeydown;
317     }
318
319     /** Sets the onKeyUp event handler. */
320     public void setOnkeyup(String JavaDoc onKeyUp) {
321         this.onkeyup = onKeyUp;
322     }
323
324     /** Returns the onKeyUp event handler. */
325     public String JavaDoc getOnkeyup() {
326         return onkeyup;
327     }
328
329     /** Sets the onKeyPress event handler. */
330     public void setOnkeypress(String JavaDoc onKeyPress) {
331         this.onkeypress = onKeyPress;
332     }
333
334     /** Returns the onKeyPress event handler. */
335     public String JavaDoc getOnkeypress() {
336         return onkeypress;
337     }
338
339     // Text Events
340

341     /** Sets the onChange event handler. */
342     public void setOnchange(String JavaDoc onChange) {
343         this.onchange = onChange;
344     }
345
346     /** Returns the onChange event handler. */
347     public String JavaDoc getOnchange() {
348         return onchange;
349     }
350
351     /** Sets the onSelect event handler. */
352     public void setOnselect(String JavaDoc onSelect) {
353         this.onselect = onSelect;
354     }
355
356     /** Returns the onSelect event handler. */
357     public String JavaDoc getOnselect() {
358         return onselect;
359     }
360
361     // Focus Events and States
362

363     /** Sets the onBlur event handler. */
364     public void setOnblur(String JavaDoc onBlur) {
365         this.onblur = onBlur;
366     }
367
368     /** Returns the onBlur event handler. */
369     public String JavaDoc getOnblur() {
370         return onblur;
371     }
372
373     /** Sets the onFocus event handler. */
374     public void setOnfocus(String JavaDoc onFocus) {
375         this.onfocus = onFocus;
376     }
377
378     /** Returns the onFocus event handler. */
379     public String JavaDoc getOnfocus() {
380         return onfocus;
381     }
382
383     /** Sets the disabled event handler. */
384     public void setDisabled(boolean disabled) {
385         this.disabled = disabled;
386     }
387
388     /** Returns the disabled event handler. */
389     public boolean getDisabled() {
390         return disabled;
391     }
392
393     /** Sets the readonly event handler. */
394     public void setReadonly(boolean readonly) {
395         this.readonly = readonly;
396     }
397
398     /** Returns the readonly event handler. */
399     public boolean getReadonly() {
400         return readonly;
401     }
402
403     // CSS Style Support
404

405     /** Sets the style attribute. */
406     public void setStyle(String JavaDoc style) {
407         this.style = style;
408     }
409
410     /** Returns the style attribute. */
411     public String JavaDoc getStyle() {
412         return style;
413     }
414
415     /** Sets the style class attribute. */
416     public void setStyleClass(String JavaDoc styleClass) {
417         this.styleClass = styleClass;
418     }
419
420     /** Returns the style class attribute. */
421     public String JavaDoc getStyleClass() {
422         return styleClass;
423     }
424
425     /** Sets the style id attribute. */
426     public void setStyleId(String JavaDoc styleId) {
427         this.styleId = styleId;
428     }
429
430     /** Returns the style id attribute. */
431     public String JavaDoc getStyleId() {
432         return styleId;
433     }
434
435     /** Returns the error key attribute. */
436     public String JavaDoc getErrorKey() {
437         return errorKey;
438     }
439
440     /** Sets the error key attribute. */
441     public void setErrorKey(String JavaDoc errorKey) {
442         this.errorKey = errorKey;
443     }
444
445     /** Returns the error style attribute. */
446     public String JavaDoc getErrorStyle() {
447         return errorStyle;
448     }
449
450     /** Sets the error style attribute. */
451     public void setErrorStyle(String JavaDoc errorStyle) {
452         this.errorStyle = errorStyle;
453     }
454
455     /** Returns the error style class attribute. */
456     public String JavaDoc getErrorStyleClass() {
457         return errorStyleClass;
458     }
459
460     /** Sets the error style class attribute. */
461     public void setErrorStyleClass(String JavaDoc errorStyleClass) {
462         this.errorStyleClass = errorStyleClass;
463     }
464
465     /** Returns the error style id attribute. */
466     public String JavaDoc getErrorStyleId() {
467         return errorStyleId;
468     }
469
470     /** Sets the error style id attribute. */
471     public void setErrorStyleId(String JavaDoc errorStyleId) {
472         this.errorStyleId = errorStyleId;
473     }
474
475     // Other Common Elements
476

477     /** Returns the alternate text attribute. */
478     public String JavaDoc getAlt() {
479         return alt;
480     }
481
482     /** Sets the alternate text attribute. */
483     public void setAlt(String JavaDoc alt) {
484         this.alt = alt;
485     }
486
487     /** Returns the message resources key of the alternate text. */
488     public String JavaDoc getAltKey() {
489         return altKey;
490     }
491
492     /** Sets the message resources key of the alternate text. */
493     public void setAltKey(String JavaDoc altKey) {
494         this.altKey = altKey;
495     }
496
497     /** Returns the name of the message resources bundle to use. */
498     public String JavaDoc getBundle() {
499         return bundle;
500     }
501
502     /** Sets the name of the message resources bundle to use. */
503     public void setBundle(String JavaDoc bundle) {
504         this.bundle = bundle;
505     }
506
507     /** Returns the name of the session attribute for our locale. */
508     public String JavaDoc getLocale() {
509         return locale;
510     }
511
512     /** Sets the name of the session attribute for our locale. */
513     public void setLocale(String JavaDoc locale) {
514         this.locale = locale;
515     }
516
517     /** Returns the advisory title attribute. */
518     public String JavaDoc getTitle() {
519         return title;
520     }
521
522     /** Sets the advisory title attribute. */
523     public void setTitle(String JavaDoc title) {
524         this.title = title;
525     }
526
527     /** Returns the message resources key of the advisory title. */
528     public String JavaDoc getTitleKey() {
529         return titleKey;
530     }
531
532     /** Sets the message resources key of the advisory title. */
533     public void setTitleKey(String JavaDoc titleKey) {
534         this.titleKey = titleKey;
535     }
536
537     // --------------------------------------------------------- Public Methods
538

539     /**
540      * Release any acquired resources.
541      */

542     public void release() {
543
544         super.release();
545         accesskey = null;
546         alt = null;
547         altKey = null;
548         bundle = null;
549         errorKey = Globals.ERROR_KEY;
550         errorStyle = null;
551         errorStyleClass = null;
552         errorStyleId = null;
553         indexed = false;
554         locale = Globals.LOCALE_KEY;
555         onclick = null;
556         ondblclick = null;
557         onmouseover = null;
558         onmouseout = null;
559         onmousemove = null;
560         onmousedown = null;
561         onmouseup = null;
562         onkeydown = null;
563         onkeyup = null;
564         onkeypress = null;
565         onselect = null;
566         onchange = null;
567         onblur = null;
568         onfocus = null;
569         disabled = false;
570         readonly = false;
571         style = null;
572         styleClass = null;
573         styleId = null;
574         tabindex = null;
575         title = null;
576         titleKey = null;
577
578     }
579
580     // ------------------------------------------------------ Protected Methods
581

582     /**
583      * Return the text specified by the literal value or the message resources
584      * key, if any; otherwise return <code>null</code>.
585      *
586      * @param literal Literal text value or <code>null</code>
587      * @param key Message resources key or <code>null</code>
588      *
589      * @exception JspException if both arguments are non-null
590      */

591     protected String JavaDoc message(String JavaDoc literal, String JavaDoc key) throws JspException JavaDoc {
592
593         if (literal != null) {
594             if (key != null) {
595                 JspException JavaDoc e =
596                     new JspException JavaDoc(messages.getMessage("common.both"));
597                 TagUtils.getInstance().saveException(pageContext, e);
598                 throw e;
599             } else {
600                 return (literal);
601             }
602         } else {
603             if (key != null) {
604                 return TagUtils.getInstance().message(
605                     pageContext,
606                     getBundle(),
607                     getLocale(),
608                     key);
609             } else {
610                 return null;
611             }
612         }
613
614     }
615
616     private Class JavaDoc loopTagSupportClass = null;
617     private Method JavaDoc loopTagSupportGetStatus = null;
618     private Class JavaDoc loopTagStatusClass = null;
619     private Method JavaDoc loopTagStatusGetIndex = null;
620     private boolean triedJstlInit = false;
621     private boolean triedJstlSuccess = false;
622
623     private Integer JavaDoc getJstlLoopIndex() {
624         if (!triedJstlInit) {
625             triedJstlInit = true;
626             try {
627                 loopTagSupportClass =
628                     RequestUtils.applicationClass(
629                         "javax.servlet.jsp.jstl.core.LoopTagSupport");
630
631                 loopTagSupportGetStatus =
632                     loopTagSupportClass.getDeclaredMethod("getLoopStatus", null);
633
634                 loopTagStatusClass =
635                     RequestUtils.applicationClass(
636                         "javax.servlet.jsp.jstl.core.LoopTagStatus");
637
638                 loopTagStatusGetIndex =
639                     loopTagStatusClass.getDeclaredMethod("getIndex", null);
640
641                 triedJstlSuccess = true;
642
643             } catch (ClassNotFoundException JavaDoc ex) {
644                 // These just mean that JSTL isn't loaded, so ignore
645
} catch (NoSuchMethodException JavaDoc ex) {
646             }
647         }
648
649         if (triedJstlSuccess) {
650             try {
651                 Object JavaDoc loopTag = findAncestorWithClass(this, loopTagSupportClass);
652                 if (loopTag == null) {
653                     return null;
654                 }
655
656                 Object JavaDoc status = loopTagSupportGetStatus.invoke(loopTag, null);
657                 return (Integer JavaDoc) loopTagStatusGetIndex.invoke(status, null);
658
659             } catch (IllegalAccessException JavaDoc ex) {
660                 log.error(ex.getMessage(), ex);
661
662             } catch (IllegalArgumentException JavaDoc ex) {
663                 log.error(ex.getMessage(), ex);
664
665             } catch (InvocationTargetException JavaDoc ex) {
666                 log.error(ex.getMessage(), ex);
667
668             } catch (NullPointerException JavaDoc ex) {
669                 log.error(ex.getMessage(), ex);
670
671             } catch (ExceptionInInitializerError JavaDoc ex) {
672                 log.error(ex.getMessage(), ex);
673             }
674         }
675         return null;
676     }
677
678     /**
679      * Appends bean name with index in brackets for tags with
680      * 'true' value in 'indexed' attribute.
681      * @param handlers The StringBuffer that output will be appended to.
682      * @exception JspException if 'indexed' tag used outside of iterate tag.
683      */

684     protected void prepareIndex(StringBuffer JavaDoc handlers, String JavaDoc name)
685         throws JspException JavaDoc {
686
687         if (name != null) {
688             handlers.append(name);
689         }
690
691         handlers.append("[");
692         handlers.append(getIndexValue());
693         handlers.append("]");
694
695         if (name != null) {
696             handlers.append(".");
697         }
698     }
699
700     /**
701      * Returns the index value for tags with 'true' value in 'indexed' attribute.
702      * @return the index value.
703      * @exception JspException if 'indexed' tag used outside of iterate tag.
704      */

705     protected int getIndexValue() throws JspException JavaDoc {
706
707         // look for outer iterate tag
708
IterateTag iterateTag =
709             (IterateTag) findAncestorWithClass(this, IterateTag.class);
710         if (iterateTag != null) {
711             return iterateTag.getIndex();
712         }
713
714         // Look for JSTL loops
715
Integer JavaDoc i = getJstlLoopIndex();
716         if (i != null) {
717             return i.intValue();
718         }
719
720         // this tag should be nested in an IterateTag or JSTL loop tag, if it's not, throw exception
721
JspException JavaDoc e =
722              new JspException JavaDoc(messages.getMessage("indexed.noEnclosingIterate"));
723         TagUtils.getInstance().saveException(pageContext, e);
724         throw e;
725
726     }
727
728     /**
729      * Prepares the style attributes for inclusion in the component's HTML tag.
730      * @return The prepared String for inclusion in the HTML tag.
731      * @exception JspException if invalid attributes are specified
732      */

733     protected String JavaDoc prepareStyles() throws JspException JavaDoc {
734
735         StringBuffer JavaDoc styles = new StringBuffer JavaDoc();
736
737         boolean errorsExist = doErrorsExist();
738
739         if (errorsExist && getErrorStyleId() != null) {
740             prepareAttribute(styles , "id", getErrorStyleId());
741         } else {
742             prepareAttribute(styles , "id", getStyleId());
743         }
744
745         if (errorsExist && getErrorStyle() != null) {
746             prepareAttribute(styles , "style", getErrorStyle());
747         } else {
748             prepareAttribute(styles , "style", getStyle());
749         }
750
751         if (errorsExist && getErrorStyleClass() != null) {
752             prepareAttribute(styles , "class", getErrorStyleClass());
753         } else {
754             prepareAttribute(styles , "class", getStyleClass());
755         }
756
757         prepareAttribute(styles , "title", message(getTitle(), getTitleKey()));
758         prepareAttribute(styles , "alt", message(getAlt(), getAltKey()));
759
760         return styles.toString();
761
762     }
763
764     /**
765      * Determine if there are errors for the component.
766      * @return Whether errors exist.
767      */

768     protected boolean doErrorsExist() throws JspException JavaDoc {
769
770         boolean errorsExist = false;
771
772         if (getErrorStyleId() != null ||
773             getErrorStyle() != null ||
774             getErrorStyleClass() != null) {
775             String JavaDoc actualName = prepareName();
776             if (actualName != null) {
777                 ActionMessages errors = TagUtils.getInstance()
778                                                 .getActionMessages(pageContext,
779                                                                    errorKey);
780                 errorsExist = (errors != null && errors.size(actualName) > 0);
781             }
782         }
783         return errorsExist;
784
785     }
786
787     /**
788      * Prepares the actual name of the component.
789      * @return The actual component name.
790      */

791     protected String JavaDoc prepareName() throws JspException JavaDoc {
792         return null;
793     }
794
795     /**
796      * Prepares the event handlers for inclusion in the component's HTML tag.
797      * @return The prepared String for inclusion in the HTML tag.
798      */

799     protected String JavaDoc prepareEventHandlers() {
800         StringBuffer JavaDoc handlers = new StringBuffer JavaDoc();
801         prepareMouseEvents(handlers);
802         prepareKeyEvents(handlers);
803         prepareTextEvents(handlers);
804         prepareFocusEvents(handlers);
805         return handlers.toString();
806     }
807
808     /**
809      * Prepares the mouse event handlers, appending them to the the given
810      * StringBuffer.
811      * @param handlers The StringBuffer that output will be appended to.
812      */

813     protected void prepareMouseEvents(StringBuffer JavaDoc handlers) {
814
815         prepareAttribute(handlers, "onclick", getOnclick());
816         prepareAttribute(handlers, "ondblclick", getOndblclick());
817         prepareAttribute(handlers, "onmouseover", getOnmouseover());
818         prepareAttribute(handlers, "onmouseout", getOnmouseout());
819         prepareAttribute(handlers, "onmousemove", getOnmousemove());
820         prepareAttribute(handlers, "onmousedown", getOnmousedown());
821         prepareAttribute(handlers, "onmouseup", getOnmouseup());
822
823     }
824
825     /**
826      * Prepares the keyboard event handlers, appending them to the the given
827      * StringBuffer.
828      * @param handlers The StringBuffer that output will be appended to.
829      */

830     protected void prepareKeyEvents(StringBuffer JavaDoc handlers) {
831
832         prepareAttribute(handlers, "onkeydown", getOnkeydown());
833         prepareAttribute(handlers, "onkeyup", getOnkeyup());
834         prepareAttribute(handlers, "onkeypress", getOnkeypress());
835
836     }
837
838     /**
839      * Prepares the text event handlers, appending them to the the given
840      * StringBuffer.
841      * @param handlers The StringBuffer that output will be appended to.
842      */

843     protected void prepareTextEvents(StringBuffer JavaDoc handlers) {
844
845         prepareAttribute(handlers, "onselect", getOnselect());
846         prepareAttribute(handlers, "onchange", getOnchange());
847
848     }
849
850     /**
851      * Prepares the focus event handlers, appending them to the the given
852      * StringBuffer.
853      * @param handlers The StringBuffer that output will be appended to.
854      */

855     protected void prepareFocusEvents(StringBuffer JavaDoc handlers) {
856
857         prepareAttribute(handlers, "onblur", getOnblur());
858         prepareAttribute(handlers, "onfocus", getOnfocus());
859
860         // Get the parent FormTag (if necessary)
861
FormTag formTag = null;
862         if ((doDisabled && !getDisabled()) ||
863             (doReadonly && !getReadonly())) {
864             formTag = (FormTag)pageContext.getAttribute(Constants.FORM_KEY,
865                                                         PageContext.REQUEST_SCOPE);
866         }
867
868         // Format Disabled
869
if (doDisabled) {
870             boolean formDisabled = formTag == null ? false : formTag.isDisabled();
871             if (formDisabled || getDisabled()) {
872                 handlers.append(" disabled=\"disabled\"");
873             }
874         }
875
876         // Format Read Only
877
if (doReadonly) {
878             boolean formReadOnly = formTag == null ? false : formTag.isReadonly();
879             if (formReadOnly || getReadonly()) {
880                 handlers.append(" readonly=\"readonly\"");
881             }
882         }
883
884     }
885
886     /**
887      * 'Hook' to enable tags to be extended and
888      * additional attributes added.
889      * @param handlers The StringBuffer that output will be appended to.
890      */

891     protected void prepareOtherAttributes(StringBuffer JavaDoc handlers) {
892     }
893
894     /**
895      * Prepares an attribute if the value is not null, appending it to the the given
896      * StringBuffer.
897      * @param handlers The StringBuffer that output will be appended to.
898      */

899     protected void prepareAttribute(StringBuffer JavaDoc handlers, String JavaDoc name, Object JavaDoc value) {
900         if (value != null) {
901             handlers.append(" ");
902             handlers.append(name);
903             handlers.append("=\"");
904             handlers.append(value);
905             handlers.append("\"");
906         }
907     }
908
909     /**
910      * Allows HTML tags to find out if they're nested within an %lt;html:html&gt; tag that
911      * has xhtml set to true.
912      * @return true if the tag is nested within an html tag with xhtml set to true, false
913      * otherwise.
914      * @since Struts 1.1
915      */

916     protected boolean isXhtml() {
917         return TagUtils.getInstance().isXhtml(this.pageContext);
918     }
919
920     /**
921      * Returns the closing brace for an input element depending on xhtml status. The tag
922      * must be nested within an %lt;html:html&gt; tag that has xhtml set to true.
923      * @return String - &gt; if xhtml is false, /&gt; if xhtml is true
924      * @since Struts 1.1
925      */

926     protected String JavaDoc getElementClose() {
927         return this.isXhtml() ? " />" : ">";
928     }
929
930     /**
931      * Searches all scopes for the bean and calls BeanUtils.getProperty() with the
932      * given arguments and converts any exceptions into JspException.
933      *
934      * @param beanName The name of the object to get the property from.
935      * @param property The name of the property to get.
936      * @return The value of the property.
937      * @throws JspException
938      * @since Struts 1.1
939      */

940     protected String JavaDoc lookupProperty(String JavaDoc beanName, String JavaDoc property)
941         throws JspException JavaDoc {
942
943         Object JavaDoc bean = TagUtils.getInstance().lookup(this.pageContext, beanName, null);
944         if (bean == null) {
945             throw new JspException JavaDoc(messages.getMessage("getter.bean", beanName));
946         }
947
948         try {
949             return BeanUtils.getProperty(bean, property);
950
951         } catch (IllegalAccessException JavaDoc e) {
952             throw new JspException JavaDoc(
953                 messages.getMessage("getter.access", property, beanName));
954
955         } catch (InvocationTargetException JavaDoc e) {
956             Throwable JavaDoc t = e.getTargetException();
957             throw new JspException JavaDoc(
958                 messages.getMessage("getter.result", property, t.toString()));
959
960         } catch (NoSuchMethodException JavaDoc e) {
961             throw new JspException JavaDoc(
962                 messages.getMessage("getter.method", property, beanName));
963         }
964     }
965
966 }
967
Popular Tags