1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
34
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.jsp.JspException;
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 AbstractDbBaseButtonTag extends AbstractDbBaseHandlerTag {
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 private static int uniqueID = 0;
55
56 private String alt;
57
58 private String border;
59
60 private String caption;
61
62
63 private String disabledBehaviour;
64
65 private String disabledImageAlt;
66
67 private String disabledImageHeight;
68
69 private String disabledImageSrc;
70
71 private String disabledImageWidth;
72
73 private String followUp;
74
75 private String followUpOnError;
76
77 private String src;
78
79 private Table table;
80
81 private int choosenFlavor = 0;
82
83 /***
84 * DOCUMENT ME!
85 *
86 * @param src
87 * DOCUMENT ME!
88 */
89 public void setAlt(String src) {
90 this.alt = src;
91 }
92
93 /***
94 * DOCUMENT ME!
95 *
96 * @return DOCUMENT ME!
97 */
98 public String getAlt() {
99 return alt;
100 }
101
102 /***
103 * Sets the border
104 *
105 * @param border
106 * The border to set
107 */
108 public void setBorder(String border) {
109 this.border = border;
110 }
111
112 /***
113 * Gets the border
114 *
115 * @return Returns a String
116 */
117 public String getBorder() {
118 return border;
119 }
120
121 /***
122 * DOCUMENT ME!
123 *
124 * @param caption
125 * DOCUMENT ME!
126 */
127 public void setCaption(String caption) {
128 this.caption = caption;
129 }
130
131 /***
132 * DOCUMENT ME!
133 *
134 * @return DOCUMENT ME!
135 */
136 public String getCaption() {
137 return caption;
138 }
139
140 /***
141 * DOCUMENT ME!
142 *
143 * @return
144 */
145 public int getChoosenFlavor() {
146 return choosenFlavor;
147 }
148
149 /***
150 * DOCUMENT ME!
151 *
152 * @param disabledBehaviour -
153 * possible values: "nohtml", "altimage", "disabled"
154 */
155 public void setDisabledBehaviour(String disabledBehaviour) {
156 this.disabledBehaviour = disabledBehaviour;
157 }
158
159 /***
160 * DOCUMENT ME!
161 *
162 * @return
163 */
164 public String getDisabledBehaviour() {
165 return disabledBehaviour;
166 }
167
168 /***
169 * DOCUMENT ME!
170 *
171 * @param disabledImageAlt -
172 * alternative image if button is disabled and flavor 'image'
173 */
174 public void setDisabledImageAlt(String disabledImageAlt) {
175 this.disabledImageAlt = disabledImageAlt;
176 }
177
178 /***
179 * DOCUMENT ME!
180 *
181 * @return alternative image alt text if button is disabled and flavor
182 * 'image'
183 */
184 public String getDisabledImageAlt() {
185 return disabledImageAlt;
186 }
187
188 /***
189 * DOCUMENT ME!
190 *
191 * @param disabledImageHeight -
192 * the height of disabledImageSrc
193 */
194 public void setDisabledImageHeight(String disabledImageHeight) {
195 this.disabledImageHeight = disabledImageHeight;
196 }
197
198 /***
199 * DOCUMENT ME!
200 *
201 * @return
202 */
203 public String getDisabledImageHeight() {
204 return disabledImageHeight;
205 }
206
207 /***
208 * DOCUMENT ME!
209 *
210 * @param disabledImageSrc -
211 * alternative image alt text if button is disabled and flavor
212 * 'image'
213 */
214 public void setDisabledImageSrc(String disabledImageSrc) {
215 this.disabledImageSrc = disabledImageSrc;
216 }
217
218 /***
219 * DOCUMENT ME!
220 *
221 * @return alternative image src if button is disabled and flavor 'image'
222 */
223 public String getDisabledImageSrc() {
224 return disabledImageSrc;
225 }
226
227 /***
228 * DOCUMENT ME!
229 *
230 * @param disabledImageWidth -
231 * the width of disabledImageSrc
232 */
233 public void setDisabledImageWidth(String disabledImageWidth) {
234 this.disabledImageWidth = disabledImageWidth;
235 }
236
237 /***
238 * DOCUMENT ME!
239 *
240 * @return
241 */
242 public String getDisabledImageWidth() {
243 return disabledImageWidth;
244 }
245
246 /***
247 * DOCUMENT ME!
248 *
249 * @param flavor
250 * DOCUMENT ME!
251 */
252 public void setFlavor(String flavor) {
253 if ("image".equals(flavor)) {
254 choosenFlavor = FLAVOR_IMAGE;
255 } else if ("modern".equals(flavor)) {
256 choosenFlavor = FLAVOR_MODERN;
257 } else {
258 choosenFlavor = FLAVOR_STANDARD;
259 }
260 }
261
262 /***
263 * DOCUMENT ME!
264 *
265 * @param followUp
266 * DOCUMENT ME!
267 */
268 public void setFollowUp(String followUp) {
269 this.followUp = followUp;
270 }
271
272 /***
273 * DOCUMENT ME!
274 *
275 * @return DOCUMENT ME!
276 */
277 public String getFollowUp() {
278 return followUp;
279 }
280
281 /***
282 * Sets the followUpOnError
283 *
284 * @param followUpOnError
285 * The followUpOnError to set
286 */
287 public void setFollowUpOnError(String followUpOnError) {
288 this.followUpOnError = followUpOnError;
289 }
290
291 /***
292 * Gets the followUpOnError
293 *
294 * @return Returns a String
295 */
296 public String getFollowUpOnError() {
297 return followUpOnError;
298 }
299
300 /***
301 * DOCUMENT ME!
302 *
303 * @param parent
304 * DOCUMENT ME!
305 */
306 public void setParent(final javax.servlet.jsp.tagext.Tag parent) {
307 super.setParent(parent);
308 table = getParentForm().getTable();
309 }
310
311 /***
312 * DOCUMENT ME!
313 *
314 * @param src
315 * DOCUMENT ME!
316 */
317 public void setSrc(String src) {
318 this.src = src;
319 }
320
321 /***
322 * DOCUMENT ME!
323 *
324 * @return DOCUMENT ME!
325 */
326 public String getSrc() {
327 return src;
328 }
329
330 /***
331 * DOCUMENT ME!
332 *
333 * @return
334 */
335 public Table getTable() {
336 return table;
337 }
338
339 /***
340 * DOCUMENT ME!
341 *
342 * @return DOCUMENT ME!
343 *
344 * @throws javax.servlet.jsp.JspException
345 * DOCUMENT ME!
346 * @throws JspException
347 * DOCUMENT ME!
348 */
349 public int doEndTag() throws javax.servlet.jsp.JspException {
350 if (choosenFlavor == FLAVOR_MODERN) {
351 try {
352 if (bodyContent != null) {
353 bodyContent.writeOut(bodyContent.getEnclosingWriter());
354 }
355
356 pageContext.getOut().write("</button>");
357 } catch (java.io.IOException ioe) {
358 throw new JspException("IO Error: " + ioe.getMessage());
359 }
360 }
361
362 return EVAL_PAGE;
363 }
364
365 /***
366 * DOCUMENT ME!
367 */
368 public void doFinally() {
369 followUp = null;
370 followUpOnError = null;
371 table = null;
372 choosenFlavor = 0;
373 caption = null;
374 src = null;
375 alt = null;
376 border = null;
377 super.doFinally();
378 }
379
380 /***
381 * DOCUMENT ME!
382 *
383 * @return DOCUMENT ME!
384 *
385 * @throws javax.servlet.jsp.JspException
386 * DOCUMENT ME!
387 */
388 public int doStartTag() throws javax.servlet.jsp.JspException {
389 if ((getParentForm().getFormValidatorName() != null)
390 && (getParentForm().getFormValidatorName().length() > 0)
391 && getParentForm().hasJavascriptValidationSet()) {
392 String onclick = (getOnClick() != null) ? getOnClick() : "";
393
394 if (onclick.lastIndexOf(";") != (onclick.length() - 1)) {
395 onclick += ";";
396 }
397
398 setOnClick(onclick + JsValidation());
399 }
400
401 return SKIP_BODY;
402 }
403
404 /***
405 * DOCUMENT ME!
406 *
407 * @return DOCUMENT ME!
408 */
409 protected static synchronized int getUniqueID() {
410 uniqueID++;
411
412 return uniqueID;
413 }
414
415 /***
416 * returns beginnings of tags with attributes defining type/value/[src/alt -
417 * if image]
418 *
419 * @return DOCUMENT ME!
420 */
421 protected String getButtonBegin() {
422 StringBuffer buf = new StringBuffer();
423 Locale locale = MessageResources
424 .getLocale((HttpServletRequest) pageContext.getRequest());
425
426 String privCaption = getCaption();
427
428 if ((privCaption != null) && getParentForm().hasCaptionResourceSet()) {
429 privCaption = MessageResources.getMessage(privCaption, locale);
430 }
431
432 String privAlt = getAlt();
433
434 if ((privAlt != null) && getParentForm().hasCaptionResourceSet()) {
435 privAlt = MessageResources.getMessage(privAlt, locale);
436 }
437
438 switch (choosenFlavor) {
439 case FLAVOR_IMAGE:
440 buf.append("<input type=\"image\" ");
441
442 if (src != null) {
443 buf.append(" src=\"");
444 buf.append(src);
445 buf.append("\" ");
446 }
447
448 if (privAlt != null) {
449 buf.append(" alt=\"");
450 buf.append(privAlt);
451 buf.append("\" ");
452 }
453
454 break;
455
456 case FLAVOR_MODERN:
457 buf.append("<button type=\"submit\" ");
458
459 if (privCaption != null) {
460 buf.append(" value=\"");
461
462
463 buf.append(privCaption);
464 buf.append("\" ");
465 }
466
467 break;
468
469 default:
470 buf.append("<input type=\"submit\" ");
471
472 if (privCaption != null) {
473 buf.append(" value=\"");
474
475 buf.append(privCaption);
476 buf.append("\" ");
477 }
478 }
479
480 buf.append(prepareStyles());
481 buf.append(prepareEventHandlers());
482
483 return buf.toString();
484 }
485
486 /***
487 * returns beginnings of tags with attributes defining type/value/[src/alt -
488 * if image]
489 *
490 * @return DOCUMENT ME!
491 */
492 protected String getButtonEnd() {
493 switch (choosenFlavor) {
494 case FLAVOR_IMAGE:
495 return "\"/>";
496
497 case FLAVOR_MODERN:
498 return "\">";
499
500 default:
501
502 return "\"/>";
503 }
504 }
505
506 /***
507 * renders tag containing additional information about that button: ie
508 * followUp, associatedRadio, etc.
509 *
510 * @param primaryTagName
511 * DOCUMENT ME!
512 * @param dataKey
513 * DOCUMENT ME!
514 * @param dataValue
515 * DOCUMENT ME!
516 *
517 * @return DOCUMENT ME!
518 */
519 protected String getDataTag(String primaryTagName, String dataKey,
520 String dataValue) {
521 String s = "";
522
523 if (!Util.isNull(dataValue)) {
524 StringBuffer tagBuf = new StringBuffer();
525 tagBuf.append("<input type=\"hidden\" name=\"data");
526 tagBuf.append(primaryTagName);
527 tagBuf.append("_");
528 tagBuf.append(dataKey);
529 tagBuf.append("\" value =\"");
530 tagBuf.append(dataValue);
531 tagBuf.append("\"/>");
532 s = tagBuf.toString();
533 }
534
535 return s;
536 }
537
538
539
540 /***
541 * DOCUMENT ME!
542 *
543 * @return HTML code for the disabled Image
544 */
545 protected String getDisabledImage() {
546 StringBuffer imgBuf = new StringBuffer();
547
548
549 imgBuf.append("<img src=\"");
550 imgBuf.append(getDisabledImageSrc());
551 imgBuf.append("\"");
552
553
554 imgBuf.append(" alt=\"");
555 imgBuf.append(getDisabledImageAlt());
556 imgBuf.append("\"");
557
558
559 imgBuf.append(prepareStyles());
560
561
562
563
564 if (!Util.isNull(getDisabledImageWidth())) {
565 imgBuf.append(" width=\"");
566 imgBuf.append(getDisabledImageWidth());
567 imgBuf.append("\"");
568 }
569
570 if (!Util.isNull(getDisabledImageHeight())) {
571 imgBuf.append(" height=\"");
572 imgBuf.append(getDisabledImageHeight());
573 imgBuf.append("\"");
574 }
575
576 imgBuf.append(" />");
577
578 return imgBuf.toString();
579 }
580
581 /***
582 * returns the JavaScript validation flags. Will be put into the onClick
583 * event of the main form Must be overloaded by update and delete button
584 *
585 * @return the java script validation vars.
586 */
587 protected String JsValidation() {
588 return ValidatorConstants.JS_CANCEL_VALIDATION + "=false;";
589 }
590
591 /***
592 * Prepares the style attributes for inclusion in the component's HTML tag.
593 *
594 * @return The prepared String for inclusion in the HTML tag.
595 */
596 protected String prepareStyles() {
597 StringBuffer styles = new StringBuffer(super.prepareStyles());
598
599 if (border != null) {
600 styles.append(" border=\"");
601 styles.append(border);
602 styles.append("\"");
603 }
604
605 return styles.toString();
606 }
607 }