View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbDateFieldTag.java,v 1.37 2006/03/20 20:26:12 hkollmann Exp $
3    * $Revision: 1.37 $
4    * $Date: 2006/03/20 20:26:12 $
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 javax.servlet.http.HttpServletRequest;
27  import javax.servlet.jsp.JspException;
28  
29  /***
30   * <p>
31   * This tag renders a HTML TextArea - Element
32   * </p>
33   * this tag renders a dabase-datadriven textArea, which is an active element -
34   * the user can change data
35   * 
36   * @author Joachim Peer
37   */
38  public class DbDateFieldTag extends AbstractDbBaseInputTag implements javax.servlet.jsp.tagext.TryCatchFinally {
39     /*** Holds value of property jsCalendarDateFormat. */
40     private String jsCalendarDateFormat;
41  
42     /*** Holds value of property useJsCalendar. */
43     private String useJsCalendar;
44  
45     /*** Determinate whether the classic javascript calendar should be used */
46     private String classicCalendar = "true";
47  
48     /***
49      * Setter for property jsCalendarDateFormat.
50      * 
51      * @param jsCalendarDateFormat
52      *           New value of property jsCalendarDateFormat.
53      */
54     public void setJsCalendarDateFormat(String jsCalendarDateFormat) {
55        this.jsCalendarDateFormat = jsCalendarDateFormat;
56     }
57  
58     /***
59      * Getter for property jsCalendarDateFormat.
60      * 
61      * @return Value of property jsCalendarDateFormat.
62      */
63     public String getJsCalendarDateFormat() {
64        return jsCalendarDateFormat;
65     }
66  
67     /***
68      * Setter for property useJsCalendar.
69      * 
70      * @param useJsCalendar
71      *           New value of property useJsCalendar.
72      */
73     public void setUseJsCalendar(String useJsCalendar) {
74        this.useJsCalendar = useJsCalendar;
75     }
76  
77     /***
78      * Getter for property useJsCalendar.
79      * 
80      * @return Value of property useJsCalendar.
81      */
82     public String getUseJsCalendar() {
83        return useJsCalendar;
84     }
85  
86     private void processNewCalendar(HttpServletRequest request, StringBuffer tagBuf) throws JspException {
87        if (!hasHiddenSet() && ("true".equals(this.getUseJsCalendar()))) {
88           tagBuf.append(" <a href=\"javascript:void(0);\" ").append(" onclick=\"");
89           // if date format is not explicitely set for this calendar,
90           // use date format for this form field.
91           if ((jsCalendarDateFormat == null) && (getFormatter() instanceof java.text.SimpleDateFormat)) {
92              java.text.SimpleDateFormat mysdf = (java.text.SimpleDateFormat) getFormatter();
93              jsCalendarDateFormat = parseDateFormatPattern(mysdf.toPattern());
94           }
95           tagBuf.append("return showCalendar('")
96                 .append(getFormFieldName())
97                 .append("', '")
98                 .append(jsCalendarDateFormat)
99                 .append("', '24', true);\">")
100                .append("<img src=\"")
101                .append(request.getContextPath())
102                .append("/dbformslib/jscal2/calendar.gif\"  ") 
103                .append(" border=\"0\"  alt=\"Click on the Calendar to activate the Pop-Up Calendar Window.\">")
104                .append("</img>")
105                .append("</a>");
106       }
107    }
108 
109    /***
110     * process the display of the classic javascript calendar
111     * 
112     * @param request
113     *           the request to process
114     * @param tagBuf
115     *           the buffer containing the rendered html nicolas parise
116     *           2006-02-11
117     */
118    private void processClassicCalendar(HttpServletRequest request, StringBuffer tagBuf) throws JspException {
119       // if property useJSCalendar is set to 'true' we will now add a little
120       // image that can be clicked to popup a small JavaScript Calendar
121       // written by Robert W. Husted to edit the field:
122       if (!hasHiddenSet() && ("true".equals(this.getUseJsCalendar()))) {
123          tagBuf.append(" <a href=\"javascript:doNothing()\" ").append(" onclick=\"");
124 
125          // if date format is not explicitely set for this calendar,
126          // use date format for this form field.
127          if ((jsCalendarDateFormat == null) && (getFormatter() instanceof java.text.SimpleDateFormat)) {
128             java.text.SimpleDateFormat mysdf = (java.text.SimpleDateFormat) getFormatter();
129             jsCalendarDateFormat = mysdf.toPattern();
130 
131             // 2 digit date format pattern is 'dd' in Java, 'DD' in
132             // JavaScript calendar
133             if (jsCalendarDateFormat.indexOf("dd") >= 0) {
134                jsCalendarDateFormat = jsCalendarDateFormat.replace('d', 'D');
135             }
136          }
137          //       JS Date Format set ?
138          if (jsCalendarDateFormat != null)  {
139             tagBuf.append("calDateFormat='" + jsCalendarDateFormat + "';");
140          }
141 
142          tagBuf.append("setDateField(document.dbform['")
143          .append(getFormFieldName())
144          .append("']);")
145          .append(" top.newWin = window.open('")
146          .append(request.getContextPath())
147          .append("/dbformslib/jscal/calendar.html','cal','width=270,height=280')\">")
148          .append("<img src=\"")
149          .append(request.getContextPath())
150          .append("/dbformslib/jscal/calendar.gif\"  ") 
151          .append(" border=\"0\"  alt=\"Click on the Calendar to activate the Pop-Up Calendar Window.\">")
152          .append("</img>")
153          .append("</a>");
154       }
155 
156       // For generation Javascript Validation. Need all original and modified
157       // fields name
158       getParentForm().addChildName(getName(), getFormFieldName());
159    }
160 
161    /***
162     * DOCUMENT ME!
163     * 
164     * @return DOCUMENT ME!
165     * 
166     * @throws javax.servlet.jsp.JspException
167     *            DOCUMENT ME!
168     * @throws JspException
169     *            DOCUMENT ME!
170     */
171    public int doEndTag() throws JspException {
172       HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
173 
174       try {
175 
176          StringBuffer tagBuf = new StringBuffer("<input ");
177          tagBuf.append(prepareType());
178          tagBuf.append(prepareName());
179          tagBuf.append(prepareValue());
180          tagBuf.append(prepareSize());
181          tagBuf.append(prepareKeys());
182          tagBuf.append(prepareStyles());
183          tagBuf.append(prepareEventHandlers());
184          tagBuf.append("/>");
185 
186          if ("true".equals(classicCalendar)) {
187             processClassicCalendar(request, tagBuf);
188          } else {
189             processNewCalendar(request, tagBuf);
190 
191          }
192 
193          pageContext.getOut().write(tagBuf.toString());
194 
195          // Writes out the old field value
196          writeOutSpecialValues();
197       } catch (java.io.IOException ioe) {
198          throw new JspException("IO Error: " + ioe.getMessage());
199       }
200 
201       return EVAL_PAGE;
202    }
203 
204    /***
205     * DOCUMENT ME!
206     */
207    public void doFinally() {
208       super.doFinally();
209    }
210 
211    /***
212     * DOCUMENT ME!
213     * 
214     * @return DOCUMENT ME!
215     * 
216     * @throws javax.servlet.jsp.JspException
217     *            DOCUMENT ME!
218     */
219    public int doStartTag() throws JspException {
220       super.doStartTag();
221 
222       return SKIP_BODY;
223    }
224 
225    /***
226     * Return the JavaScript Calendar pattern for the given Java DateFormat
227     * pattern.
228     * 
229     * @author Malcolm A. Edgar
230     * @param pattern
231     *           the Java DateFormat pattern
232     * @return JavaScript Calendar pattern
233     */
234    private String parseDateFormatPattern(String pattern) {
235       StringBuffer jsPattern = new StringBuffer(20);
236       int tokenStart = -1;
237       int tokenEnd = -1;
238       // boolean debug = false;
239 
240       for (int i = 0; i < pattern.length(); i++) {
241          char aChar = pattern.charAt(i);
242 
243          // If character is in SimpleDateFormat pattern character set
244          if ("GyMwWDdFEaHkKhmsSzZ".indexOf(aChar) == -1) {
245             if (tokenStart > -1) {
246                tokenEnd = i;
247             }
248          } else {
249             if (tokenStart == -1) {
250                tokenStart = i;
251             }
252          }
253 
254          if (tokenStart > -1) {
255 
256             if (tokenEnd == -1 && i == pattern.length() - 1) {
257                tokenEnd = pattern.length();
258             }
259 
260             if (tokenEnd > -1) {
261                String token = pattern.substring(tokenStart, tokenEnd);
262 
263                if ("yyyy".equals(token)) {
264                   jsPattern.append("%Y");
265                } else if ("yy".equals(token)) {
266                   jsPattern.append("%y");
267                } else if ("MMMM".equals(token)) {
268                   jsPattern.append("%B");
269                } else if ("MMM".equals(token)) {
270                   jsPattern.append("%b");
271                } else if ("MM".equals(token)) {
272                   jsPattern.append("%m");
273                } else if ("dd".equals(token)) {
274                   jsPattern.append("%d");
275                } else if ("d".equals(token)) {
276                   jsPattern.append("%e");
277                } else if ("EEEE".equals(token)) {
278                   jsPattern.append("%A");
279                } else if ("EEE".equals(token)) {
280                   jsPattern.append("%a");
281                } else if ("EE".equals(token)) {
282                   jsPattern.append("%a");
283                } else if ("E".equals(token)) {
284                   jsPattern.append("%a");
285                } else if ("aaa".equals(token)) {
286                   jsPattern.append("%p");
287                } else if ("aa".equals(token)) {
288                   jsPattern.append("%p");
289                } else if ("a".equals(token)) {
290                   jsPattern.append("%p");
291                } else if ("HH".equals(token)) {
292                   jsPattern.append("%H");
293                } else if ("H".equals(token)) {
294                   jsPattern.append("%H");
295                } else if ("hh".equals(token)) {
296                   jsPattern.append("%l");
297                } else if ("h".equals(token)) {
298                   jsPattern.append("%l");
299                } else if ("mm".equals(token)) {
300                   jsPattern.append("%M");
301                } else if ("m".equals(token)) {
302                   jsPattern.append("%M");
303                } else if ("ss".equals(token)) {
304                   jsPattern.append("%S");
305                } else if ("s".equals(token)) {
306                   jsPattern.append("%S");
307                }
308 
309                tokenStart = -1;
310                tokenEnd = -1;
311             }
312          }
313 
314          if (tokenStart == -1 && tokenEnd == -1) {
315             if ("GyMwWDdFEaHkKhmsSzZ".indexOf(aChar) == -1) {
316                jsPattern.append(aChar);
317             }
318          }
319       }
320 
321       return jsPattern.toString();
322    }
323 
324    public String getClassicCalendar() {
325       return classicCalendar;
326    }
327 
328    public void setClassicCalendar(String classicCalendar) {
329       this.classicCalendar = classicCalendar;
330    }
331 }