KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > webconnector > widgets > OptionalDateWidget


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2005 Paul Spencer <paulspencer at mindspring.com>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License as published by the Free
7  * Software Foundation; either version 2.1 of the License, or (at your option)
8  * any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.webconnector.widgets;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.text.DecimalFormat JavaDoc;
24 import java.text.Format JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.GregorianCalendar JavaDoc;
28
29 import org.lucane.webconnector.WebApp;
30
31 /**
32  * Generate the HTML to input a date. The first CheckBox is an enable/disable
33  * flag. When the box is cheched, then the date will be used. Otherwise the date
34  * is ignored. A null date is defaulted to the current date.
35  *
36  * <br/>Fields generated:
37  * <ul>
38  * <li><code>prefix</code> _enabled - Value of the checkbox.
39  * <code>true</code> if checked.</li>
40  * <li><code>prefix</code> _day</li>
41  * <li><code>prefix</code> _month</li>
42  * <li><code>prefix</code> _year</li>
43  * <li><code>prefix</code> _hour</li>
44  * <li><code>prefix</code> _minute</li>
45  * </ul>
46  * Date and time field values are useable with the Calendar.set(int, int)
47  * method.
48  */

49 public class OptionalDateWidget implements Widget
50 {
51
52   public static final String JavaDoc ENABLED_VALUE = "true";
53
54   private static final String JavaDoc SELECTED_ATTRIBUTE = " selected='true'";
55
56   private Format JavaDoc twoDigitFormat;
57
58   private WebApp app;
59
60   /** Date to be presented to formated */
61   private Date JavaDoc date;
62
63   /** Prefix of the input fields */
64   private String JavaDoc prefix;
65
66   /**
67    * Constructor.
68    *
69    * @param app
70    * WebApp
71    * @param date
72    * Date to format. <code>null</code>= current date.
73    * @param prefix
74    * Field prefix
75    */

76   public OptionalDateWidget(WebApp app, Date JavaDoc date, String JavaDoc prefix)
77   {
78     this.app = app;
79     this.date = date;
80     this.prefix = prefix;
81     this.twoDigitFormat = new DecimalFormat JavaDoc("00");
82   }
83
84   /**
85    * Generate the HTML to writer defined by <code>out</code>.
86    */

87   public void render(Writer JavaDoc out) throws IOException JavaDoc
88   {
89     Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
90     String JavaDoc enabledValue = null;
91
92     if (date != null)
93     {
94       calendar.setTime(date);
95       enabledValue = "checked='" + ENABLED_VALUE + "'";
96     }
97     else
98     {
99       calendar.setTime(new Date JavaDoc());
100       enabledValue = "";
101     }
102
103     int dateDay = calendar.get(Calendar.DAY_OF_MONTH);
104     int dateMonth = calendar.get(Calendar.MONTH);
105     int dateYear = calendar.get(Calendar.YEAR);
106     int dateHour = calendar.get(Calendar.HOUR_OF_DAY);
107     int dateMinute = calendar.get(Calendar.MINUTE);
108
109     out.write("<!-- OptionalDateWidget start -->\n");
110     out.write("<input type='checkbox' name='" + prefix + "_enabled' value='"
111         + ENABLED_VALUE + "' " + enabledValue + "/>\n");
112     out.write("<div class='calendar_dategroup'>\n");
113     out.write(" <select name='" + prefix + "_day'>\n");
114     for (int i = 1; i <= 31; i++)
115     {
116       String JavaDoc selectedValue = "";
117       if (i == dateDay)
118       {
119         selectedValue = SELECTED_ATTRIBUTE;
120       }
121       out.write(" <option value='" + i + "'" + selectedValue + ">"
122           + twoDigitFormat.format(new Integer JavaDoc(i)) + "</option>\n");
123     }
124     out.write(" </select>\n");
125     out.write(" <select name='" + prefix + "_month'>\n");
126     for (int i = 0; i < 12; i++)
127     {
128       String JavaDoc selectedValue = "";
129       if (i == dateMonth)
130       {
131         selectedValue = SELECTED_ATTRIBUTE;
132       }
133       out.write(" <option value='" + i + "'" + selectedValue + ">"
134           + app.tr("month." + (i + 1)) + "</option>\n");
135     }
136     out.write(" </select>\n");
137     out.write(" <select name='" + prefix + "_year'>\n");
138     for (int i = dateYear - 1; i < dateYear + 2; i++)
139     {
140       String JavaDoc selectedValue = "";
141       if (i == dateYear)
142       {
143         selectedValue = SELECTED_ATTRIBUTE;
144       }
145       out.write(" <option value='" + i + "'" + selectedValue + ">" + i
146           + "</option>\n");
147     }
148     out.write(" </select>\n");
149     out.write("</div>\n");
150     out.write("<div class='calendar_hourgroup'>\n");
151     out.write(" <select name='" + prefix + "_hour'>\n");
152     for (int i = 0; i < 24; i++)
153     {
154       String JavaDoc selectedValue = "";
155       if (i == dateHour)
156       {
157         selectedValue = SELECTED_ATTRIBUTE;
158       }
159       out.write(" <option value='" + i + "'" + selectedValue + ">"
160           + twoDigitFormat.format(new Integer JavaDoc(i)) + "</option>\n");
161     }
162     out.write(" </select>\n");
163     out.write(" <select name='" + prefix + "_minute'>\n");
164     for (int i = 0; i < 60; i += 15)
165     {
166       String JavaDoc selectedValue = "";
167       if (i == dateMinute)
168       {
169         selectedValue = SELECTED_ATTRIBUTE;
170       }
171       out.write(" <option value='" + i + "'" + selectedValue + ">"
172           + twoDigitFormat.format(new Integer JavaDoc(i)) + "</option>\n");
173     }
174     out.write(" </select>\n");
175     out.write("</div>\n");
176     out.write("<!-- OptionalDateWidget end -->\n");
177   }
178 }
179
Popular Tags