View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/AbstractScriptHandlerTag.java,v 1.2 2006/01/13 13:38:50 hkollmann Exp $
3    * $Revision: 1.2 $
4    * $Date: 2006/01/13 13:38:50 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.taglib;
25  
26  import java.io.PrintWriter;
27  import java.io.StringWriter;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import org.dbforms.config.DbFormsConfig;
33  import org.dbforms.config.DbFormsConfigRegistry;
34  
35  import org.dbforms.util.Util;
36  
37  import javax.servlet.jsp.tagext.BodyTagSupport;
38  
39  
40  
41  /***
42   * <p>
43   * Base class for TagSupport that render form data-elements capable of
44   * including JavaScript event handlers and/or CSS Style attributes.
45   * </p>
46   *
47   * <p>
48   * the html/css releated properties and methods where originally done by Don
49   * Clasen for Apache Groups's Jakarta-Struts project.
50   * </p>
51   */
52  public abstract class AbstractScriptHandlerTag extends BodyTagSupport {
53     private static Log logCat = LogFactory.getLog(AbstractDbBaseHandlerTag.class.getName());
54  
55     /*** DOCUMENT ME! */
56     private String accessKey = null;
57  
58     // Focus Events
59  
60     /*** Component lost focus event. */
61     private String onBlur = null;
62  
63     /*** Content changed after component lost focus event. */
64     private String onChange = null;
65  
66     //  Mouse Events
67  
68     /*** Mouse click event. */
69     private String onClick = null;
70  
71     /*** Mouse double click event. */
72     private String onDblClick = null;
73  
74     /*** Component has received focus event. */
75     private String onFocus = null;
76  
77     //  Keyboard Events
78  
79     /*** Key down in component event. */
80     private String onKeyDown = null;
81  
82     /*** Key down and up together in component event. */
83     private String onKeyPress = null;
84  
85     /*** Key released in component event. */
86     private String onKeyUp = null;
87  
88     /*** Mouse pressed on component event. */
89     private String onMouseDown = null;
90  
91     /*** Mouse moved over component event. */
92     private String onMouseMove = null;
93  
94     /*** Mouse exit component event. */
95     private String onMouseOut = null;
96  
97     /*** Mouse over component event. */
98     private String onMouseOver = null;
99  
100    /*** Mouse released on component event. */
101    private String onMouseUp = null;
102 
103    // Text Events
104 
105    /*** Text selected in component event. */
106    private String onSelect = null;
107 
108    // CSS Style Support
109 
110    /*** Style attribute associated with component. */
111    private String style = null;
112 
113    /*** Named Style class associated with component. */
114    private String styleClass = null;
115 
116    /*** Tab index value. */
117    private String tabIndex = null;
118 
119    /*** Title (Tool Tip/Hint) attribute */
120    private String title = null;
121 
122    //  Navigation Management
123 
124    /***
125     * Sets the accessKey character.
126     *
127     * @param accessKey DOCUMENT ME!
128     */
129    public void setAccessKey(String accessKey) {
130       this.accessKey = accessKey;
131    }
132 
133 
134    /***
135     * Returns the accessKey character.
136     *
137     * @return DOCUMENT ME!
138     */
139    public String getAccessKey() {
140       return (this.accessKey);
141    }
142 
143 
144    /***
145     * DOCUMENT ME!
146     *
147     * @return DOCUMENT ME!
148     */
149    public DbFormsConfig getConfig() {
150       try {
151          return DbFormsConfigRegistry.instance()
152                                      .lookup();
153       } catch (Exception e) {
154          logCat.error("getConfig", e);
155 
156          return null;
157       }
158    }
159 
160 
161    // Focus Events
162 
163    /***
164     * Sets the onBlur event handler.
165     *
166     * @param onBlur DOCUMENT ME!
167     */
168    public void setOnBlur(String onBlur) {
169       this.onBlur = onBlur;
170    }
171 
172 
173    /***
174     * Returns the onBlur event handler.
175     *
176     * @return DOCUMENT ME!
177     */
178    public String getOnBlur() {
179       return onBlur;
180    }
181 
182 
183    // Text Events
184 
185    /***
186     * Sets the onChange event handler.
187     *
188     * @param onChange DOCUMENT ME!
189     */
190    public void setOnChange(String onChange) {
191       this.onChange = onChange;
192    }
193 
194 
195    /***
196     * Returns the onChange event handler.
197     *
198     * @return DOCUMENT ME!
199     */
200    public String getOnChange() {
201       return onChange;
202    }
203 
204 
205    // Mouse Events
206 
207    /***
208     * Sets the onClick event handler.
209     *
210     * @param onClick DOCUMENT ME!
211     */
212    public void setOnClick(String onClick) {
213       this.onClick = onClick;
214    }
215 
216 
217    /***
218     * Returns the onClick event handler.
219     *
220     * @return DOCUMENT ME!
221     */
222    public String getOnClick() {
223       return onClick;
224    }
225 
226 
227    /***
228     * Sets the onDblClick event handler.
229     *
230     * @param onDblClick DOCUMENT ME!
231     */
232    public void setOnDblClick(String onDblClick) {
233       this.onDblClick = onDblClick;
234    }
235 
236 
237    /***
238     * Returns the onDblClick event handler.
239     *
240     * @return DOCUMENT ME!
241     */
242    public String getOnDblClick() {
243       return onDblClick;
244    }
245 
246 
247    /***
248     * Sets the onFocus event handler.
249     *
250     * @param onFocus DOCUMENT ME!
251     */
252    public void setOnFocus(String onFocus) {
253       this.onFocus = onFocus;
254    }
255 
256 
257    /***
258     * Returns the onFocus event handler.
259     *
260     * @return DOCUMENT ME!
261     */
262    public String getOnFocus() {
263       return onFocus;
264    }
265 
266 
267    // Keyboard Events
268 
269    /***
270     * Sets the onKeyDown event handler.
271     *
272     * @param onKeyDown DOCUMENT ME!
273     */
274    public void setOnKeyDown(String onKeyDown) {
275       this.onKeyDown = onKeyDown;
276    }
277 
278 
279    /***
280     * Returns the onKeyDown event handler.
281     *
282     * @return DOCUMENT ME!
283     */
284    public String getOnKeyDown() {
285       return onKeyDown;
286    }
287 
288 
289    /***
290     * Sets the onKeyPress event handler.
291     *
292     * @param onKeyPress DOCUMENT ME!
293     */
294    public void setOnKeyPress(String onKeyPress) {
295       this.onKeyPress = onKeyPress;
296    }
297 
298 
299    /***
300     * Returns the onKeyPress event handler.
301     *
302     * @return DOCUMENT ME!
303     */
304    public String getOnKeyPress() {
305       return onKeyPress;
306    }
307 
308 
309    /***
310     * Sets the onKeyUp event handler.
311     *
312     * @param onKeyUp DOCUMENT ME!
313     */
314    public void setOnKeyUp(String onKeyUp) {
315       this.onKeyUp = onKeyUp;
316    }
317 
318 
319    /***
320     * Returns the onKeyUp event handler.
321     *
322     * @return DOCUMENT ME!
323     */
324    public String getOnKeyUp() {
325       return onKeyUp;
326    }
327 
328 
329    /***
330     * Sets the onMouseDown event handler.
331     *
332     * @param onMouseDown DOCUMENT ME!
333     */
334    public void setOnMouseDown(String onMouseDown) {
335       this.onMouseDown = onMouseDown;
336    }
337 
338 
339    /***
340     * Returns the onMouseDown event handler.
341     *
342     * @return DOCUMENT ME!
343     */
344    public String getOnMouseDown() {
345       return onMouseDown;
346    }
347 
348 
349    /***
350     * Sets the onMouseMove event handler.
351     *
352     * @param onMouseMove DOCUMENT ME!
353     */
354    public void setOnMouseMove(String onMouseMove) {
355       this.onMouseMove = onMouseMove;
356    }
357 
358 
359    /***
360     * Returns the onMouseMove event handler.
361     *
362     * @return DOCUMENT ME!
363     */
364    public String getOnMouseMove() {
365       return onMouseMove;
366    }
367 
368 
369    /***
370     * Sets the onMouseOut event handler.
371     *
372     * @param onMouseOut DOCUMENT ME!
373     */
374    public void setOnMouseOut(String onMouseOut) {
375       this.onMouseOut = onMouseOut;
376    }
377 
378 
379    /***
380     * Returns the onMouseOut event handler.
381     *
382     * @return DOCUMENT ME!
383     */
384    public String getOnMouseOut() {
385       return onMouseOut;
386    }
387 
388 
389    /***
390     * Sets the onMouseOver event handler.
391     *
392     * @param onMouseOver DOCUMENT ME!
393     */
394    public void setOnMouseOver(String onMouseOver) {
395       this.onMouseOver = onMouseOver;
396    }
397 
398 
399    /***
400     * Returns the onMouseOver event handler.
401     *
402     * @return DOCUMENT ME!
403     */
404    public String getOnMouseOver() {
405       return onMouseOver;
406    }
407 
408 
409    /***
410     * Sets the onMouseUp event handler.
411     *
412     * @param onMouseUp DOCUMENT ME!
413     */
414    public void setOnMouseUp(String onMouseUp) {
415       this.onMouseUp = onMouseUp;
416    }
417 
418 
419    /***
420     * Returns the onMouseUp event handler.
421     *
422     * @return DOCUMENT ME!
423     */
424    public String getOnMouseUp() {
425       return onMouseUp;
426    }
427 
428 
429    /***
430     * Sets the onSelect event handler.
431     *
432     * @param onSelect DOCUMENT ME!
433     */
434    public void setOnSelect(String onSelect) {
435       this.onSelect = onSelect;
436    }
437 
438 
439    /***
440     * Returns the onSelect event handler.
441     *
442     * @return DOCUMENT ME!
443     */
444    public String getOnSelect() {
445       return onSelect;
446    }
447 
448 
449    // CSS Style Support
450 
451    /***
452     * Sets the style attribute.
453     *
454     * @param style DOCUMENT ME!
455     */
456    public void setStyle(String style) {
457       this.style = style;
458    }
459 
460 
461    /***
462     * Returns the style attribute.
463     *
464     * @return DOCUMENT ME!
465     */
466    public String getStyle() {
467       return style;
468    }
469 
470 
471    /***
472     * Sets the style class attribute.
473     *
474     * @param styleClass DOCUMENT ME!
475     */
476    public void setStyleClass(String styleClass) {
477       this.styleClass = styleClass;
478    }
479 
480 
481    /***
482     * Returns the style class attribute.
483     *
484     * @return DOCUMENT ME!
485     */
486    public String getStyleClass() {
487       return styleClass;
488    }
489 
490 
491    /***
492     * Sets the tabIndex value.
493     *
494     * @param tabIndex DOCUMENT ME!
495     */
496    public void setTabIndex(String tabIndex) {
497       this.tabIndex = tabIndex;
498    }
499 
500 
501    /***
502     * Returns the tabIndex value.
503     *
504     * @return DOCUMENT ME!
505     */
506    public String getTabIndex() {
507       return (this.tabIndex);
508    }
509 
510 
511    /***
512     * Sets the title attribute.
513     *
514     * @param title DOCUMENT ME!
515     */
516    public void setTitle(String title) {
517       this.title = title;
518    }
519 
520 
521    /***
522     * Returns the title attribute.
523     *
524     * @return DOCUMENT ME!
525     */
526    public String getTitle() {
527       return title;
528    }
529 
530 
531    /***
532     * reset tag state
533     *
534     * @see javax.servlet.jsp.tagext.TryCatchFinally#doFinally()
535     */
536    public void doFinally() {
537       accessKey   = null;
538       tabIndex    = null;
539       onClick     = null;
540       onDblClick  = null;
541       onMouseOver = null;
542       onMouseOut  = null;
543       onMouseMove = null;
544       onMouseDown = null;
545       onMouseUp   = null;
546       onKeyDown   = null;
547       onKeyUp     = null;
548       onKeyPress  = null;
549       onSelect    = null;
550       onChange    = null;
551       onBlur      = null;
552       onFocus     = null;
553       style       = null;
554       styleClass  = null;
555       title       = null;
556    }
557 
558 	/***
559 	 * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
560 	 */
561 	public void doCatch(Throwable t) throws Throwable {
562 		StringWriter sw = new StringWriter();
563 		PrintWriter pw = new PrintWriter(sw);
564 		t.printStackTrace(pw);
565 		logCat.error("DbFormTag.doCatch called - " + t.toString() + "\n"
566 				+ sw.toString());
567 		throw t;
568 	}
569 
570    /***
571     * Prepares the event handlers for inclusion in the component's HTML tag.
572     *
573     * @return The prepared String for inclusion in the HTML tag.
574     */
575    protected String prepareEventHandlers() {
576       StringBuffer handlers = new StringBuffer();
577       prepareIdEvents(handlers); // Fossato, 20011008		
578       prepareMouseEvents(handlers);
579       prepareKeyEvents(handlers);
580       prepareTextEvents(handlers);
581       prepareFocusEvents(handlers);
582 
583       return handlers.toString();
584    }
585 
586 
587    /***
588     * Prepares the style attributes for inclusion in the component's HTML tag.
589     *
590     * @return The prepared String for inclusion in the HTML tag.
591     */
592    protected String prepareStyles() {
593       StringBuffer styles = new StringBuffer();
594 
595       if (!Util.isNull(getStyleClass())) {
596          styles.append(" class=\"");
597          styles.append(getStyleClass());
598          styles.append("\"");
599       }
600 
601       if (!Util.isNull(getStyle())) {
602          styles.append(" style=\"");
603          styles.append(getStyle());
604          styles.append("\"");
605       }
606 
607       if (!Util.isNull(getTitle())) {
608          styles.append(" title=\"");
609          styles.append(getTitle());
610          styles.append("\"");
611       }
612 
613       return styles.toString();
614    }
615 
616 
617    /***
618     * Prepares the focus event handlers, appending them to the the given
619     * StringBuffer.
620     *
621     * @param handlers The StringBuffer that output will be appended to.
622     */
623    private void prepareFocusEvents(StringBuffer handlers) {
624       if (onBlur != null) {
625          handlers.append(" onBlur=\"");
626          handlers.append(onBlur);
627          handlers.append("\"");
628       }
629 
630       if (onFocus != null) {
631          handlers.append(" onFocus=\"");
632          handlers.append(onFocus);
633          handlers.append("\"");
634       }
635    }
636 
637 
638    // -------------------------------------------------------- Private Methods
639 
640    /***
641     * Prepares the id handlers, appending them to the the given StringBuffer.
642     *
643     * @param handlers The StringBuffer that output will be appended to.
644     */
645    private void prepareIdEvents(StringBuffer handlers) { // Fossato, 20011008
646 
647       if (id != null) {
648          handlers.append(" id=\"")
649                  .append(id)
650                  .append("\"");
651       }
652    }
653 
654 
655    /***
656     * Prepares the keyboard event handlers, appending them to the the given
657     * StringBuffer.
658     *
659     * @param handlers The StringBuffer that output will be appended to.
660     */
661    private void prepareKeyEvents(StringBuffer handlers) {
662       if (onKeyDown != null) {
663          handlers.append(" onKeyDown=\"");
664          handlers.append(onKeyDown);
665          handlers.append("\"");
666       }
667 
668       if (onKeyUp != null) {
669          handlers.append(" onKeyUp=\"");
670          handlers.append(onKeyUp);
671          handlers.append("\"");
672       }
673 
674       if (onKeyPress != null) {
675          handlers.append(" onKeyPress=\"");
676          handlers.append(onKeyPress);
677          handlers.append("\"");
678       }
679    }
680 
681 
682    /***
683     * Prepares the mouse event handlers, appending them to the the given
684     * StringBuffer.
685     *
686     * @param handlers The StringBuffer that output will be appended to.
687     */
688    private void prepareMouseEvents(StringBuffer handlers) {
689       if (onClick != null) {
690          handlers.append(" onClick=\"");
691          handlers.append(onClick);
692          handlers.append("\"");
693       }
694 
695       if (onDblClick != null) {
696          handlers.append(" onDblClick=\"");
697          handlers.append(onDblClick);
698          handlers.append("\"");
699       }
700 
701       if (onMouseOver != null) {
702          handlers.append(" onMouseOver=\"");
703          handlers.append(onMouseOver);
704          handlers.append("\"");
705       }
706 
707       if (onMouseOut != null) {
708          handlers.append(" onMouseOut=\"");
709          handlers.append(onMouseOut);
710          handlers.append("\"");
711       }
712 
713       if (onMouseMove != null) {
714          handlers.append(" onMouseMove=\"");
715          handlers.append(onMouseMove);
716          handlers.append("\"");
717       }
718 
719       if (onMouseDown != null) {
720          handlers.append(" onMouseDown=\"");
721          handlers.append(onMouseDown);
722          handlers.append("\"");
723       }
724 
725       if (onMouseUp != null) {
726          handlers.append(" onMouseUp=\"");
727          handlers.append(onMouseUp);
728          handlers.append("\"");
729       }
730    }
731 
732 
733    /***
734     * Prepares the text event handlers, appending them to the the given
735     * StringBuffer.
736     *
737     * @param handlers The StringBuffer that output will be appended to.
738     */
739    private void prepareTextEvents(StringBuffer handlers) {
740       if (onSelect != null) {
741          handlers.append(" onSelect=\"");
742          handlers.append(onSelect);
743          handlers.append("\"");
744       }
745 
746       if (onChange != null) {
747          handlers.append(" onChange=\"");
748          handlers.append(onChange);
749          handlers.append("\"");
750       }
751    }
752 }