KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > DbBaseButtonTag


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbBaseButtonTag.java,v 1.33 2004/10/24 13:47:30 hkollmann Exp $
3  * $Revision: 1.33 $
4  * $Date: 2004/10/24 13:47:30 $
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 org.dbforms.config.Table;
27
28 import org.dbforms.util.MessageResources;
29 import org.dbforms.util.Util;
30
31 import org.dbforms.validation.ValidatorConstants;
32
33 import java.util.Locale JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.jsp.JspException JavaDoc;
37
38 /**
39  * abstract base class for buttons supports 3 types of Buttons: #fixme - docu
40  *
41  * @author Joachim Peer
42  */

43 public abstract class DbBaseButtonTag extends DbBaseHandlerTag {
44
45     /** DOCUMENT ME! */
46     protected static final int FLAVOR_STANDARD = 0;
47
48     /** DOCUMENT ME! */
49     protected static final int FLAVOR_IMAGE = 1;
50
51     /** DOCUMENT ME! */
52     protected static final int FLAVOR_MODERN = 2;
53
54     // logging category for this class
55
private static int uniqueID = 0;
56
57     private String JavaDoc alt; // used if flavor is "image"
58

59     private String JavaDoc border; // used to set html border attribute"
60

61     private String JavaDoc caption; // used if flavor is "standard"
62

63     //20040225 JFM
64
private String JavaDoc disabledBehaviour;
65
66     private String JavaDoc disabledImageAlt;
67
68     private String JavaDoc disabledImageHeight;
69
70     private String JavaDoc disabledImageSrc;
71
72     private String JavaDoc disabledImageWidth;
73
74     private String JavaDoc followUp;
75
76     private String JavaDoc followUpOnError;
77
78     private String JavaDoc src; // used if flavor is "image"
79

80     private Table table;
81
82     private int choosenFlavor = 0; // default = standard
83

84     /**
85      * DOCUMENT ME!
86      *
87      * @param src
88      * DOCUMENT ME!
89      */

90     public void setAlt(String JavaDoc src) {
91         this.alt = src;
92     }
93
94     /**
95      * DOCUMENT ME!
96      *
97      * @return DOCUMENT ME!
98      */

99     public String JavaDoc getAlt() {
100         return alt;
101     }
102
103     /**
104      * Sets the border
105      *
106      * @param border
107      * The border to set
108      */

109     public void setBorder(String JavaDoc border) {
110         this.border = border;
111     }
112
113     /**
114      * Gets the border
115      *
116      * @return Returns a String
117      */

118     public String JavaDoc getBorder() {
119         return border;
120     }
121
122     /**
123      * DOCUMENT ME!
124      *
125      * @param caption
126      * DOCUMENT ME!
127      */

128     public void setCaption(String JavaDoc caption) {
129         this.caption = caption;
130     }
131
132     /**
133      * DOCUMENT ME!
134      *
135      * @return DOCUMENT ME!
136      */

137     public String JavaDoc getCaption() {
138         return caption;
139     }
140
141     /**
142      * DOCUMENT ME!
143      *
144      * @return
145      */

146     public int getChoosenFlavor() {
147         return choosenFlavor;
148     }
149
150     /**
151      * DOCUMENT ME!
152      *
153      * @param disabledBehaviour -
154      * possible values: "nohtml", "altimage", "disabled"
155      */

156     public void setDisabledBehaviour(String JavaDoc disabledBehaviour) {
157         this.disabledBehaviour = disabledBehaviour;
158     }
159
160     /**
161      * DOCUMENT ME!
162      *
163      * @return
164      */

165     public String JavaDoc getDisabledBehaviour() {
166         return disabledBehaviour;
167     }
168
169     /**
170      * DOCUMENT ME!
171      *
172      * @param disabledImageAlt -
173      * alternative image if button is disabled and flavor 'image'
174      */

175     public void setDisabledImageAlt(String JavaDoc disabledImageAlt) {
176         this.disabledImageAlt = disabledImageAlt;
177     }
178
179     /**
180      * DOCUMENT ME!
181      *
182      * @return alternative image alt text if button is disabled and flavor
183      * 'image'
184      */

185     public String JavaDoc getDisabledImageAlt() {
186         return disabledImageAlt;
187     }
188
189     /**
190      * DOCUMENT ME!
191      *
192      * @param disabledImageHeight -
193      * the height of disabledImageSrc
194      */

195     public void setDisabledImageHeight(String JavaDoc disabledImageHeight) {
196         this.disabledImageHeight = disabledImageHeight;
197     }
198
199     /**
200      * DOCUMENT ME!
201      *
202      * @return
203      */

204     public String JavaDoc getDisabledImageHeight() {
205         return disabledImageHeight;
206     }
207
208     /**
209      * DOCUMENT ME!
210      *
211      * @param disabledImageSrc -
212      * alternative image alt text if button is disabled and flavor
213      * 'image'
214      */

215     public void setDisabledImageSrc(String JavaDoc disabledImageSrc) {
216         this.disabledImageSrc = disabledImageSrc;
217     }
218
219     /**
220      * DOCUMENT ME!
221      *
222      * @return alternative image src if button is disabled and flavor 'image'
223      */

224     public String JavaDoc getDisabledImageSrc() {
225         return disabledImageSrc;
226     }
227
228     /**
229      * DOCUMENT ME!
230      *
231      * @param disabledImageWidth -
232      * the width of disabledImageSrc
233      */

234     public void setDisabledImageWidth(String JavaDoc disabledImageWidth) {
235         this.disabledImageWidth = disabledImageWidth;
236     }
237
238     /**
239      * DOCUMENT ME!
240      *
241      * @return
242      */

243     public String JavaDoc getDisabledImageWidth() {
244         return disabledImageWidth;
245     }
246
247     /**
248      * DOCUMENT ME!
249      *
250      * @param flavor
251      * DOCUMENT ME!
252      */

253     public void setFlavor(String JavaDoc flavor) {
254         if ("image".equals(flavor)) {
255             choosenFlavor = FLAVOR_IMAGE;
256         } else if ("modern".equals(flavor)) {
257             choosenFlavor = FLAVOR_MODERN;
258         } else {
259             choosenFlavor = FLAVOR_STANDARD;
260         }
261     }
262
263     /**
264      * DOCUMENT ME!
265      *
266      * @param followUp
267      * DOCUMENT ME!
268      */

269     public void setFollowUp(String JavaDoc followUp) {
270         this.followUp = followUp;
271     }
272
273     /**
274      * DOCUMENT ME!
275      *
276      * @return DOCUMENT ME!
277      */

278     public String JavaDoc getFollowUp() {
279         return followUp;
280     }
281
282     /**
283      * Sets the followUpOnError
284      *
285      * @param followUpOnError
286      * The followUpOnError to set
287      */

288     public void setFollowUpOnError(String JavaDoc followUpOnError) {
289         this.followUpOnError = followUpOnError;
290     }
291
292     /**
293      * Gets the followUpOnError
294      *
295      * @return Returns a String
296      */

297     public String JavaDoc getFollowUpOnError() {
298         return followUpOnError;
299     }
300
301     /**
302      * DOCUMENT ME!
303      *
304      * @param parent
305      * DOCUMENT ME!
306      */

307     public void setParent(final javax.servlet.jsp.tagext.Tag JavaDoc parent) {
308         super.setParent(parent);
309         table = getParentForm().getTable();
310     }
311
312     /**
313      * DOCUMENT ME!
314      *
315      * @param src
316      * DOCUMENT ME!
317      */

318     public void setSrc(String JavaDoc src) {
319         this.src = src;
320     }
321
322     /**
323      * DOCUMENT ME!
324      *
325      * @return DOCUMENT ME!
326      */

327     public String JavaDoc getSrc() {
328         return src;
329     }
330
331     /**
332      * DOCUMENT ME!
333      *
334      * @return
335      */

336     public Table getTable() {
337         return table;
338     }
339
340     /**
341      * DOCUMENT ME!
342      *
343      * @return DOCUMENT ME!
344      *
345      * @throws javax.servlet.jsp.JspException
346      * DOCUMENT ME!
347      * @throws JspException
348      * DOCUMENT ME!
349      */

350     public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
351         if (choosenFlavor == FLAVOR_MODERN) {
352             try {
353                 if (bodyContent != null) {
354                     bodyContent.writeOut(bodyContent.getEnclosingWriter());
355                 }
356
357                 pageContext.getOut().write("</button>");
358             } catch (java.io.IOException JavaDoc ioe) {
359                 throw new JspException JavaDoc("IO Error: " + ioe.getMessage());
360             }
361         }
362
363         return EVAL_PAGE;
364     }
365
366     /**
367      * DOCUMENT ME!
368      */

369     public void doFinally() {
370         followUp = null;
371         followUpOnError = null;
372         table = null;
373         choosenFlavor = 0;
374         caption = null;
375         src = null;
376         alt = null;
377         border = null;
378         super.doFinally();
379     }
380
381     /**
382      * DOCUMENT ME!
383      *
384      * @return DOCUMENT ME!
385      *
386      * @throws javax.servlet.jsp.JspException
387      * DOCUMENT ME!
388      */

389     public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
390         if ((getParentForm().getFormValidatorName() != null)
391                 && (getParentForm().getFormValidatorName().length() > 0)
392                 && getParentForm().hasJavascriptValidationSet()) {
393             String JavaDoc onclick = (getOnClick() != null) ? getOnClick() : "";
394
395             if (onclick.lastIndexOf(";") != (onclick.length() - 1)) {
396                 onclick += ";"; // be sure javascript end with ";"
397
}
398
399             setOnClick(onclick + JsValidation());
400         }
401
402         return SKIP_BODY;
403     }
404
405     /**
406      * DOCUMENT ME!
407      *
408      * @return DOCUMENT ME!
409      */

410     protected static synchronized int getUniqueID() {
411         uniqueID++;
412
413         return uniqueID;
414     }
415
416     /**
417      * returns beginnings of tags with attributes defining type/value/[src/alt -
418      * if image]
419      *
420      * @return DOCUMENT ME!
421      */

422     protected String JavaDoc getButtonBegin() {
423         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
424         Locale JavaDoc locale = MessageResources
425                 .getLocale((HttpServletRequest JavaDoc) pageContext.getRequest());
426
427         String JavaDoc privCaption = getCaption();
428         // If the caption is not null and the resources="true" attribut
429
if ((privCaption != null) && getParentForm().hasCaptionResourceSet()) {
430             privCaption = MessageResources.getMessage(privCaption, locale);
431         }
432
433         String JavaDoc privAlt = getAlt();
434         // If the caption is not null and the resources="true" attribut
435
if ((privAlt != null) && getParentForm().hasCaptionResourceSet()) {
436             privAlt = MessageResources.getMessage(privAlt, locale);
437         }
438
439         switch (choosenFlavor) {
440             case FLAVOR_IMAGE:
441                 buf.append("<input type=\"image\" ");
442
443                 if (src != null) {
444                     buf.append(" SRC=\"");
445                     buf.append(src);
446                     buf.append("\" ");
447                 }
448
449                 if (privAlt != null) {
450                     buf.append(" alt=\"");
451                     buf.append(privAlt);
452                     buf.append("\" ");
453                 }
454
455                 break;
456
457             case FLAVOR_MODERN:
458                 buf.append("<button type=\"submit\" ");
459
460                 if (privCaption != null) {
461                     buf.append(" value=\""); // not visible - neither on ie nor
462
// netscape (?) [only tags embedded in
463
// this tags get rendered !]
464
buf.append(privCaption);
465                     buf.append("\" ");
466                 }
467
468                 break;
469
470             default: // FLAVOR_STANDARD
471
buf.append("<input type=\"submit\" ");
472
473                 if (privCaption != null) {
474                     buf.append(" value=\""); // not very useful: this is _not_
475
// visible!
476
buf.append(privCaption);
477                     buf.append("\" ");
478                 }
479         }
480
481         buf.append(prepareStyles());
482         buf.append(prepareEventHandlers());
483
484         return buf.toString();
485     }
486
487     /**
488      * returns beginnings of tags with attributes defining type/value/[src/alt -
489      * if image]
490      *
491      * @return DOCUMENT ME!
492      */

493     protected String JavaDoc getButtonEnd() {
494         switch (choosenFlavor) {
495             case FLAVOR_IMAGE:
496                 return "\"/>";
497
498             case FLAVOR_MODERN:
499                 return "\">";
500
501             default: // FLAVOR_STANDARD
502

503                 return "\"/>";
504         }
505     }
506
507     /**
508      * renders tag containing additional information about that button: ie
509      * followUp, associatedRadio, etc.
510      *
511      * @param primaryTagName
512      * DOCUMENT ME!
513      * @param dataKey
514      * DOCUMENT ME!
515      * @param dataValue
516      * DOCUMENT ME!
517      *
518      * @return DOCUMENT ME!
519      */

520     protected String JavaDoc getDataTag(String JavaDoc primaryTagName, String JavaDoc dataKey,
521             String JavaDoc dataValue) {
522         String JavaDoc s = "";
523
524         if (!Util.isNull(dataValue)) {
525             StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
526             tagBuf.append("<input type=\"hidden\" name=\"data");
527             tagBuf.append(primaryTagName);
528             tagBuf.append("_");
529             tagBuf.append(dataKey);
530             tagBuf.append("\" value =\"");
531             tagBuf.append(dataValue);
532             tagBuf.append("\"/>");
533             s = tagBuf.toString();
534         }
535
536         return s;
537     }
538
539     //20040225 JFM: added
540

541     /**
542      * DOCUMENT ME!
543      *
544      * @return HTML code for the disabled Image
545      */

546     protected String JavaDoc getDisabledImage() {
547         StringBuffer JavaDoc imgBuf = new StringBuffer JavaDoc();
548
549         // image src
550
imgBuf.append("<img SRC=\"");
551         imgBuf.append(getDisabledImageSrc());
552         imgBuf.append("\"");
553
554         // image alt
555
imgBuf.append(" alt=\"");
556         imgBuf.append(getDisabledImageAlt());
557         imgBuf.append("\"");
558
559         // image style
560
imgBuf.append(prepareStyles());
561
562         // image events:
563
//imgBuf.append(prepareEventHandlers());
564
// image width and height
565
if (!Util.isNull(getDisabledImageWidth())) {
566             imgBuf.append(" width=\"");
567             imgBuf.append(getDisabledImageWidth());
568             imgBuf.append("\"");
569         }
570
571         if (!Util.isNull(getDisabledImageHeight())) {
572             imgBuf.append(" height=\"");
573             imgBuf.append(getDisabledImageHeight());
574             imgBuf.append("\"");
575         }
576
577         imgBuf.append(" />");
578
579         return imgBuf.toString();
580     }
581
582     /**
583      * returns the JavaScript validation flags. Will be put into the onClick
584      * event of the main form Must be overloaded by update and delete button
585      *
586      * @return the java script validation vars.
587      */

588     protected String JavaDoc JsValidation() {
589         return ValidatorConstants.JS_CANCEL_VALIDATION + "=false;";
590     }
591
592     /**
593      * Prepares the style attributes for inclusion in the component's HTML tag.
594      *
595      * @return The prepared String for inclusion in the HTML tag.
596      */

597     protected String JavaDoc prepareStyles() {
598         StringBuffer JavaDoc styles = new StringBuffer JavaDoc(super.prepareStyles());
599
600         if (border != null) {
601             styles.append(" border=\"");
602             styles.append(border);
603             styles.append("\"");
604         }
605
606         return styles.toString();
607     }
608 }
609
Popular Tags