View Javadoc

1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.common.taglib;
11  
12  import com.jcorporate.expresso.core.controller.Input;
13  import com.jcorporate.expresso.core.dbobj.ValidValue;
14  import com.jcorporate.expresso.core.misc.StringUtil;
15  import com.jcorporate.expresso.ext.taglib.BlockTag;
16  import com.jcorporate.expresso.ext.taglib.ElementIterator;
17  import com.jcorporate.expresso.ext.taglib.ExpressoTagSupport;
18  import com.jcorporate.expresso.kernel.util.FastStringBuffer;
19  import org.apache.log4j.Logger;
20  
21  import javax.servlet.jsp.JspTagException;
22  import javax.servlet.jsp.JspWriter;
23  import javax.servlet.jsp.tagext.Tag;
24  import java.util.List;
25  
26  
27  /***
28   * An adaptation of Expresso's version of InputTag to
29   * customize and add functionality as well as fix errors.
30   *
31   * @author Zaz Harris
32   */
33  public class InputTag extends ExpressoTagSupport {
34      // Input types that are explicitly handled by InputTag and rendered as...
35      //   input type=checkbox
36  
37      /***
38  	 * 
39  	 */
40  	private static final long serialVersionUID = 1L;
41  
42  	/***
43       * Input type checkbox.
44       */
45      public static final String TYPE_CHECKBOX = "checkbox";
46  
47      /***
48       * Input type checkbox-vertical.
49       */
50      public static final String TYPE_CHECKBOX_VERT = "checkbox-vertical";
51  
52      //   input type=hidden
53  
54      /***
55       * Input type hidden.
56       */
57      public static final String TYPE_HIDDEN = "hidden";
58  
59      /***
60       * Input type read only input.
61       */
62      public static final String TYPE_READONLY = "readonly";
63  
64      //   input type=radio
65      /***
66       * Input type radio.
67       */
68      public static final String TYPE_RADIO = "radio";
69  
70      /***
71       * Input type radio vertical.
72       */
73      public static final String TYPE_RADIO_VERT = "radio-vertical";
74  
75      //   select list
76      /***
77       * Input type select list: dropdown.
78       */
79      public static final String TYPE_DROPDOWN = "dropdown";
80  
81      /***
82       * Input type select list: listbox.
83       */
84      public static final String TYPE_LISTBOX = "listbox";
85  
86      //   textarea
87  
88      /***
89       * Input type text area.
90       */
91      public static final String TYPE_TEXTAREA = "textarea";
92  
93      // Input types not explicitly handled by InputTag but often used and could
94      // be explicitly handle in the future
95      /***
96       * Input type file. [Not yet supported].
97       */
98      public static final String TYPE_FILE = "file";
99  
100     /***
101      * Input type password [Not yet supported].
102      */
103     public static final String TYPE_PASSWORD = "password";
104 
105     /***
106      * Input type text [Not yet supported].
107      */
108     public static final String TYPE_TEXT = "text";
109 
110     // Style Sheet classes
111 
112     /***
113      * Medium Bold Stylesheet class.
114      */
115     public static final String STYLE_SHEET_MEDIUM_BOLD = "mediumbold";
116 
117     /***
118      * Medium text stylesheet class.
119      */
120     public static final String STYLE_SHEET_MEDIUM_TEXT = "mediumtext";
121 
122     // private data members
123 
124 
125     /***
126      * Name parameter, by default is controller.
127      */
128     private java.lang.String name = null; //by default, name is controller
129 
130     /***
131      * Value by default empty string.
132      */
133     private java.lang.String value = null; //by default, empty string
134 
135     /***
136      * Type to use, override of type of control.
137      */
138     private java.lang.String type = null;
139 
140     /***
141      * Size.
142      */
143     private java.lang.String size = null;
144 
145     /***
146      * Maximum length allowed for text boses.
147      */
148     private java.lang.String maxlength = null;
149 
150     /***
151      * Show label?  By default do not show.
152      */
153     private java.lang.String showlabel = null; //by default, do not show a label
154     private java.lang.String label = null;
155 
156     /***
157      * By default name to use is the controller.
158      */
159     private java.lang.String nameToUse = null; //by default, name is controller
160 
161     /***
162      * Value to use is the empty string.
163      */
164     private java.lang.String valueToUse = null; //by default, empty string
165 
166     /***
167      * Type to use, override of type of control.
168      */
169     private java.lang.String typeToUse = null;
170 
171     /***
172      * Size to use.
173      */
174     private java.lang.String sizeToUse = null;
175 
176     /***
177      * Max length to use.
178      */
179     private java.lang.String maxlengthToUse = null;
180 
181     /***
182      * Label to use.
183      */
184     private java.lang.String labelToUse = null;
185 
186     /***
187      * The current input.
188      */
189     private Input oneInput = null;
190 
191     /***
192      * The log4j logger.
193      */
194     private static Logger log = Logger.getLogger(InputTag.class);
195 
196     // handle Javascript (zh)
197     /***
198      * OnKeypress javascript.
199      */
200     private java.lang.String onKeyPress = null;
201 
202     /***
203      * onClick javascript.
204      */
205     private java.lang.String onClick = null;
206 
207     /***
208      * Default Constructor.
209      */
210     public InputTag() {
211         super();
212     }
213 
214     /* InputTag() */
215 
216     /***
217      * We examine the Input object and populate the instance variables
218      *
219      * @throws Exception upon error.
220      */
221     private void analyzeInput() throws Exception {
222         if (oneInput == null) {
223             throw new Exception("The Input object was null");
224         }
225 
226         //any attribute arguments from the page take
227         // precedence over Expresso Input
228         //attributes
229         if (nameToUse == null) {
230             nameToUse = StringUtil.notNull(oneInput.getName());
231         }
232 
233         if (valueToUse == null) {
234             valueToUse = StringUtil.notNull(oneInput.getDefaultValue());
235         }
236 
237         if (typeToUse == null) {
238             typeToUse = StringUtil.notNull(oneInput.getType());
239         }
240 
241         if (sizeToUse == null) {
242             sizeToUse = StringUtil.notNull("" + oneInput.getDisplayLength());
243         }
244 
245         if (maxlengthToUse == null) {
246             maxlengthToUse = StringUtil.notNull("" + oneInput.getMaxLength());
247         }
248 
249         if (labelToUse == null) {
250             labelToUse = StringUtil.notNull(oneInput.getLabel());
251         }
252     }
253 
254     /* analyzeInput() */
255 
256     /***
257      * Get the javascript actions.
258      * Return the different JavaScript actions : onKeyPress, e.a.
259      * By Zaz Harris
260      * Used a method so you can add more javascript actions to it if needed.
261      *
262      * @return java.lang.String
263      * @todo This method should ignore event handlers that don't apply to
264      * a given input... e.g. onKeyPress should be ignored for inputs
265      * that are not a text, textarea, password or file and onClick
266      * for inputs that are not checkboxes or radio buttons
267      * @since 6/17/2002
268      */
269     protected java.lang.String getJavaScript() {
270         if ((onKeyPress != null) || (onClick != null)) {
271             FastStringBuffer fsb = new FastStringBuffer(64);
272 
273             if (onKeyPress != null) {
274                 fsb.append(" onKeyPress=\"");
275                 fsb.append(onKeyPress);
276                 fsb.append("\"");
277             }
278 
279             if (onClick != null) {
280                 fsb.append(" onClick=\"");
281                 fsb.append(onClick);
282                 fsb.append("\"");
283             }
284 
285             return fsb.toString();
286         }
287 
288         return "";
289     }
290 
291     /***
292      * Builds a box.
293      *
294      * @param writer the JSP Write
295      * @throws Exception upon error.
296      */
297     private void buildBox(final JspWriter writer) throws Exception {
298         if (writer == null) {
299             throw new Exception("JspWriter was null");
300         }
301 
302         writer.print("<input type=\"" + typeToUse + "\" name=\"" + nameToUse + "\" size=\"" + sizeToUse
303                 + "\" maxlength=\"" + maxlengthToUse + "\" value=\"" + valueToUse + "\" class=\"" + STYLE_SHEET_MEDIUM_TEXT
304                 + "\"" + getJavaScript() + ">");
305     }
306 
307     /* buildBox(JSPWriter) */
308 
309     /***
310      * Builds a checkbox from the input object.
311      *
312      * @param writer   the JSP Write
313      * @param vertical true if you want vertical radio buttons.
314      * @throws Exception upon error.
315      */
316     private void buildCheckBox(final JspWriter writer, final boolean vertical) throws Exception {
317         if (writer == null) {
318             throw new Exception("JspWriter was null");
319         }
320 
321         List validList = oneInput.getValidValuesList();
322         ValidValue oneValue = null;
323         String checkFlag = null;
324 
325         for (int i = 0; i < validList.size(); i++) {
326             oneValue = (ValidValue) validList.get(i);
327 
328             if (valueToUse.equals(oneValue.getValue())) {
329                 checkFlag = "CHECKED";
330             } else {
331                 checkFlag = "";
332             }
333 
334             writer.println("<input type=\"checkbox\" " + "name=\"" + nameToUse
335                     + "\" " + checkFlag + " value=\"" + oneValue.getValue()
336                     + "\" "
337                     + "class=\"" + STYLE_SHEET_MEDIUM_TEXT + "\" "
338                     + getJavaScript()
339                     + "> " + oneValue.getDescription());
340 
341             if (vertical) {
342                 writer.println("<br>");
343             }
344         }
345     }
346 
347     /* buildCheckBox(JSPWriter) */
348 
349     /***
350      * Build a hidden input.
351      *
352      * @param writer the JSP Writer.
353      * @throws Exception upon error.
354      */
355     private void buildHiddenBox(final JspWriter writer) throws Exception {
356         if (writer == null) {
357             throw new Exception("JspWriter was null");
358         }
359 
360         writer.print("<input type=\"hidden\" name=\"" + nameToUse
361                 + "\" size=\"" + sizeToUse + "\" maxlength=\"" + maxlengthToUse
362                 + "\" value=\"" + valueToUse + "\">");
363     }
364 
365     /* buildHiddenBox(JSPWriter) */
366 
367     /***
368      * Builds a radio input field from the input object.
369      *
370      * @param writer   The JSP Writer.
371      * @param vertical true if you want vertically aligned radios.
372      * @throws Exception upon error.
373      */
374     private void buildRadioButton(final JspWriter writer, final boolean vertical) throws Exception {
375         if (writer == null) {
376             throw new Exception("JspWriter was null");
377         }
378 
379         List validVector = oneInput.getValidValuesList();
380         ValidValue oneValue = null;
381         String checkFlag = null;
382 
383         for (int i = 0; i < validVector.size(); i++) {
384             oneValue = (ValidValue) validVector.get(i);
385 
386             if (valueToUse.equals(oneValue.getValue())) {
387                 checkFlag = "CHECKED";
388             } else {
389                 checkFlag = "";
390             }
391 
392             writer.println("<input type=\"radio\" " + "name=\"" + nameToUse
393                     + "\" " + checkFlag + " value=\"" + oneValue.getValue() +
394                     "\" class=\"" + STYLE_SHEET_MEDIUM_TEXT + "\" "
395                     + getJavaScript() + "> " + oneValue.getDescription());
396 
397             if (vertical) {
398                 writer.println("    <br>");
399             }
400         }
401     }
402 
403     /* buildRadioButton(JSPWriter) */
404 
405     /***
406      * Builds a typical drop down list from the input object.
407      *
408      * @param writer the Jsp Writer.
409      * @throws Exception upon error.
410      */
411     private void buildSelectList(final JspWriter writer) throws Exception {
412         if (writer == null) {
413             throw new Exception("JspWriter was null");
414         }
415 
416         List validVector = oneInput.getValidValuesList();
417         ValidValue oneValue = null;
418         writer.println("<select name=\"" + nameToUse + "\" " + "class=\"" +
419                 STYLE_SHEET_MEDIUM_BOLD + "\" " + getJavaScript() + ">");
420 
421         String selectFlag = null;
422 
423         for (int i = 0; i < validVector.size(); i++) {
424             oneValue = (ValidValue) validVector.get(i);
425 
426             if (valueToUse.equals(oneValue.getValue())) {
427                 selectFlag = "SELECTED";
428             } else {
429                 selectFlag = "";
430             }
431 
432             writer.println("  <option value=\"" + oneValue.getValue() + "\" "
433                     + selectFlag + ">" + oneValue.getDescription() + "</option>");
434         }
435 
436         writer.println("</select>");
437     }
438 
439     /* buildSelectList(JSPWriter) */
440 
441     /***
442      * Builds a text area.
443      *
444      * @param writer the JSP Writer.
445      * @throws Exception upon error.
446      */
447     private void buildTextArea(final JspWriter writer) throws Exception {
448         if (writer == null) {
449             throw new Exception("JspWriter was null");
450         }
451 
452         int numLines = oneInput.getLines();
453         writer.print("<textarea name=\"" + nameToUse + "\" rows=\"" + numLines
454                 + "\" cols=\"" + sizeToUse + "\" class=\""
455                 + STYLE_SHEET_MEDIUM_TEXT
456                 + "\" " + getJavaScript() + ">" + valueToUse + "</textarea>");
457     }
458 
459     /* buildTextArea(JSPWriter) */
460 
461     /***
462      * Standard doEndTag.
463      *
464      * @return int as defined by the JSP API Specification.
465      * @throws JspTagException upon error.
466      */
467     public int doEndTag() throws javax.servlet.jsp.JspTagException {
468         nameToUse = name;
469         valueToUse = value;
470         typeToUse = type;
471         sizeToUse = size;
472         maxlengthToUse = maxlength;
473         labelToUse = label;
474         getControllerResponse();
475 
476         try {
477             // An InputTag can be contained either directly in the ControllerResponse,
478             // or, by a parent ElementIterator or Block tag.
479             Tag container = getContainer();
480 
481             if (container == null) {
482                 oneInput = ctlrResp.getInput(nameToUse);
483             } else {
484                 if (container instanceof ElementIterator) {
485                     oneInput = (Input) ((ElementIterator) container).getElement();
486                     nameToUse = oneInput.getName();
487                 } else if (container instanceof BlockTag) {
488                     oneInput = (Input) ((BlockTag) container).getBlock()
489                             .getContent(getName());
490                 } else {
491                     throw new JspTagException("InputTag: cannot handle "
492                             + "container tag" +
493                             container.getClass().getName());
494                 }
495             }
496 
497             JspWriter writer = pageContext.getOut();
498 
499             if (oneInput != null) {
500                 analyzeInput();
501 
502                 if ("true".equalsIgnoreCase(showlabel) && !"".equals(labelToUse)) {
503                     //first write out the label for this input
504                     //by default, this is false
505                     writer.print(labelToUse + "  ");
506                 }
507 
508                 if (TYPE_LISTBOX.equalsIgnoreCase(typeToUse) || TYPE_DROPDOWN.equalsIgnoreCase(typeToUse)) {
509                     buildSelectList(writer);
510                 } else if (TYPE_CHECKBOX.equalsIgnoreCase(typeToUse)) {
511                     buildCheckBox(writer, false);
512                 } else if (TYPE_CHECKBOX_VERT.equalsIgnoreCase(typeToUse)) {
513                     buildCheckBox(writer, true);
514                 } else if (TYPE_RADIO.equalsIgnoreCase(typeToUse)) {
515                     buildRadioButton(writer, false);
516                 } else if (TYPE_RADIO_VERT.equalsIgnoreCase(typeToUse)) {
517                     buildRadioButton(writer, true);
518                 } else if (TYPE_TEXTAREA.equalsIgnoreCase(typeToUse)) {
519                     buildTextArea(writer);
520                 } else if (TYPE_HIDDEN.equalsIgnoreCase(typeToUse) || TYPE_READONLY.equalsIgnoreCase(typeToUse)) {
521                     buildHiddenBox(writer);
522                 } else {
523                     //assume normal box
524                     buildBox(writer);
525                 }
526             } else {
527                 writer.println("The Input Named " + nameToUse + " Could Not Be Displayed");
528             }
529         } catch (Exception e) {
530             log.error(e);
531             throw new JspTagException("InputTag: " + e.getMessage());
532         }
533 
534         return EVAL_PAGE;
535     }
536 
537     /* doEndTag() */
538 
539     /***
540      * Do nothing until end tag.
541      *
542      * @return int
543      * @throws javax.servlet.jsp.JspTagException
544      *          The exception description.
545      */
546     public int doStartTag() throws javax.servlet.jsp.JspTagException {
547         return SKIP_BODY;
548     }
549 
550     /* doStartTag() */
551 
552     /***
553      * Getter method for maxlength.
554      *
555      * @return java.lang.String
556      */
557     public java.lang.String getMaxlength() {
558         return maxlengthToUse;
559     }
560 
561     /* getMaxLength() */
562 
563     /***
564      * Getter method for Name.
565      *
566      * @return java.lang.String
567      */
568     public java.lang.String getName() {
569         return nameToUse;
570     }
571 
572     /* getName() */
573 
574     /***
575      * Getter Method for showlabel.
576      *
577      * @return java.lang.String
578      */
579     public java.lang.String getShowlabel() {
580         return showlabel;
581     }
582 
583     /* getShowlabel() */
584 
585     /***
586      * Getter method for size.
587      *
588      * @return java.lang.String
589      */
590     public java.lang.String getSize() {
591         return sizeToUse;
592     }
593 
594     /* getSize() */
595 
596     /***
597      * Getter method for Type.
598      *
599      * @return java.lang.String
600      */
601     public java.lang.String getType() {
602         return typeToUse;
603     }
604 
605     /* getType() */
606 
607     /***
608      * Getter Method for Value.
609      * Creation date: (12/29/2000 %r)
610      *
611      * @return java.lang.String
612      */
613     public java.lang.String getValue() {
614         return valueToUse;
615     }
616 
617     /* getValue() */
618 
619     /***
620      * Getter method for javascript : onKeyPress.
621      * by Zaz Harris
622      *
623      * @return java.lang.String
624      * @since 6/17/2002
625      */
626     public java.lang.String getOnKeyPress() {
627         return onKeyPress;
628     }
629 
630     /* getOnKeyPress() */
631 
632     /***
633      * Setter method for javascript : onKeyPress.
634      * by Zaz Harris
635      *
636      * @param newValue java.lang.String
637      * @since 6/17/2002
638      */
639     public void setOnKeyPress(final java.lang.String newValue) {
640         onKeyPress = newValue;
641     }
642 
643     /* setOnclick(java.lang.String) */
644 
645     /***
646      * Getter method for javascript onClick method.
647      * by Zaz Harris
648      *
649      * @return java.lang.String
650      * @since 7/24/2002
651      */
652     public java.lang.String getOnClick() {
653         return onClick;
654     }
655 
656     /* getOnClick() */
657 
658     /***
659      * Setter method for javascript onClick method.
660      * by Zaz Harris
661      *
662      * @param newValue java.lang.String
663      * @since 7/24/2002
664      */
665     public void setOnClick(final java.lang.String newValue) {
666         onClick = newValue;
667     }
668 
669     /* setOnclick(java.lang.String) */
670 
671     /***
672      * Setter method for maxlength.
673      *
674      * @param newMaxlength java.lang.String
675      */
676     public void setMaxlength(final java.lang.String newMaxlength) {
677         maxlength = newMaxlength;
678     }
679 
680     /* setMaxlength(java.lang.String) */
681 
682     /***
683      * Sets the name parameter to retreive from the ControllerResponse.
684      *
685      * @param newName java.lang.String
686      */
687     public void setName(final java.lang.String newName) {
688         name = newName;
689     }
690 
691     /* setName(java.lang.String) */
692 
693     /***
694      * Retrieve the input for the control.
695      *
696      * @return Input
697      */
698     public Input getInput() {
699         return oneInput;
700     }
701 
702     /***
703      * Setter method for showlabel.
704      *
705      * @param newShowlabel java.lang.String
706      */
707     public void setShowlabel(final java.lang.String newShowlabel) {
708         showlabel = newShowlabel;
709     }
710 
711     /* setShowlabel(java.lang.String) */
712 
713     /***
714      * Setter method for size.
715      *
716      * @param newSize java.lang.String
717      */
718     public void setSize(final java.lang.String newSize) {
719         size = newSize;
720     }
721 
722     /* setSize(java.lang.String) */
723 
724     /***
725      * Setter method for type.
726      *
727      * @param newType java.lang.String
728      */
729     public void setType(final java.lang.String newType) {
730         type = newType;
731     }
732 
733     /* setType(java.lang.String) */
734 
735     /***
736      * Setter method for value.
737      *
738      * @param newValue java.lang.String
739      */
740     public void setValue(final java.lang.String newValue) {
741         value = newValue;
742     }
743     /* setValue(java.lang.String) */
744 
745 }