1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.text.DateFormatSymbols;
20 import java.util.Calendar;
21 import java.util.Date;
22
23 import org.apache.ecs.ConcreteElement;
24 import org.apache.ecs.ElementContainer;
25 import org.apache.ecs.html.Input;
26 import org.apache.ecs.html.Option;
27 import org.apache.ecs.html.Select;
28
29 /***
30 * DateSelector is a utility class to handle the creation of a set of
31 * date popup menus. The code is broken into a set of static methods
32 * for quick and easy access to the individual select objects:
33 *
34 * <pre>
35 * ElementContainer ec dateSelect = new ElementContainer();
36 * String myName = "mydate";
37 * ec.addElement(DateSelector.getMonthSelector(myName));
38 * ec.addElement(DateSelector.getDaySelector(myName));
39 * ec.addElement(DateSelector.getYearSelector(myName));
40 * </pre>
41 *
42 * There are also methods which will use attributes to build a
43 * complete month,day,year selector:
44 *
45 * <pre>
46 * DateSelector ds = new DateSelector(myName);
47 * dateSelect = ds.ecsOutput();
48 * </pre>
49 *
50 * The above element container would use the onChange setting and may
51 * hide the selected day if set via showDays().<br>
52 *
53 * @author <a href="mailto:ekkerbj@netscape.net">Jeffrey D. Brekke</a>
54 * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
55 * @author <a href="mailto:leon@clearink.com">Leon Atkinson</a>
56 * @version $Id: DateSelector.java 264148 2005-08-29 14:21:04Z henning $
57 */
58 public class DateSelector
59 {
60 /*** Prefix for date names. */
61 public static final String DEFAULT_PREFIX = "DateSelector";
62
63 /*** Suffix for day parameter. */
64 public static final String DAY_SUFFIX = "_day";
65
66 /*** Suffix for month parameter. */
67 public static final String MONTH_SUFFIX = "_month";
68
69 /*** Suffix for year parameter. */
70 public static final String YEAR_SUFFIX = "_year";
71
72 private Calendar useDate = null;
73 private String selName = null;
74 private static final String[] monthName =
75 new DateFormatSymbols().getMonths();
76 private String onChange = null;
77 private boolean onChangeSet = false;
78 private boolean showDays = true;
79 private int setDay = 0;
80 private boolean useYears = false;
81 private int firstYear = 0;
82 private int lastYear = 0;
83 private int selectedYear = 0;
84
85 /***
86 * Constructor defaults to current date and uses the default
87 * prefix: <pre>DateSelector.DEFAULT</pre>
88 */
89 public DateSelector()
90 {
91 this.selName = DEFAULT_PREFIX;
92 this.useDate = Calendar.getInstance();
93 this.useDate.setTime(new Date());
94 }
95
96 /***
97 * Constructor, uses the date set in a calendar that has been
98 * already passed in (with the date set correctly).
99 *
100 * @param selName A String with the selector name.
101 * @param useDate A Calendar with a date.
102 */
103 public DateSelector(String selName, Calendar useDate)
104 {
105 this.useDate = useDate;
106 this.selName = selName;
107 }
108
109 /***
110 * Constructor defaults to current date.
111 *
112 * @param selName A String with the selector name.
113 */
114 public DateSelector(String selName)
115 {
116 this.selName = selName;
117 this.useDate = Calendar.getInstance();
118 this.useDate.setTime(new Date());
119 }
120
121 /***
122 * Adds the onChange to all of <SELECT> tags. This is limited to
123 * one function for all three popups and is only used when the
124 * output() methods are used. Individual getMonth, getDay,
125 * getYear static methods will not use this setting.
126 *
127 * @param string A String to use for onChange attribute. If null,
128 * then nothing will be set.
129 * @return A DateSelector (self).
130 */
131 public DateSelector setOnChange(String onChange)
132 {
133 if (onChange != null)
134 {
135 this.onChange = onChange;
136 this.onChangeSet = true;
137 }
138 else
139 {
140 this.onChange = null;
141 this.onChangeSet = false;
142 }
143 return this;
144 }
145
146 /***
147 * Select the day to be selected if the showDays(false) behavior
148 * is used. Individual getMonth, getDay, getYear static methods
149 * will not use this setting.
150 *
151 * @param day The day.
152 * @return A DateSelector (self).
153 */
154 public DateSelector setDay(int day)
155 {
156 this.setDay = day;
157 this.showDays = false;
158 return this;
159 }
160
161 /***
162 * Whether or not to show the days as a popup menu. The days will
163 * be a hidden parameter and the value set with setDay is used.
164 * Individual getMonth, getDay, getYear static methods will not
165 * use this setting.
166 *
167 * @param show True if the day should be shown.
168 * @return A DateSelector (self).
169 */
170 public DateSelector setShowDay(boolean show)
171 {
172 this.showDays = false;
173 return this;
174 }
175
176 /***
177 * Set the selector name prefix. Individual getMonth, getDay,
178 * getYear static methods will not use this setting.
179 *
180 * @param selname A String with the select name prefix.
181 */
182 public void setSelName(String selName)
183 {
184 this.selName = selName;
185 }
186
187 /***
188 * Get the selector name prefix.
189 *
190 * @return A String with the select name prefix.
191 */
192 public String getSelName()
193 {
194 return selName;
195 }
196
197 /***
198 * Return a month selector.
199 *
200 * @param name The name to use for the selected month.
201 * @return A select object with all the months.
202 */
203 public static Select getMonthSelector(String name)
204 {
205 return (getMonthSelector(name, Calendar.getInstance()));
206 }
207
208 /***
209 * Return a month selector.
210 *
211 * Note: The values of the month placed into the select list are
212 * the month integers starting at 0 (ie: if the user selects
213 * February, the selected value will be 1).
214 *
215 * @param name The name to use for the selected month.
216 * @param now Calendar to start with.
217 * @return A select object with all the months.
218 */
219 public static Select getMonthSelector(String name, Calendar now)
220 {
221 Select monthSelect = new Select().setName(name);
222
223 for (int curMonth = 0; curMonth <= 11; curMonth++)
224 {
225 Option o = new Option();
226 o.addElement(monthName[curMonth]);
227 o.setValue(curMonth);
228 if ((now.get(Calendar.MONTH)) == curMonth)
229 {
230 o.setSelected(true);
231 }
232 monthSelect.addElement(o);
233 }
234 return (monthSelect);
235 }
236
237 /***
238 * Return a day selector.
239 *
240 * @param name The name to use for the selected day.
241 * @return A select object with all the days in a month.
242 */
243 public static Select getDaySelector(String name)
244 {
245 return (getDaySelector(name, Calendar.getInstance()));
246 }
247
248 /***
249 * Return a day selector.
250 *
251 * @param name The name to use for the selected day.
252 * @param now Calendar to start with.
253 * @return A select object with all the days in a month.
254 */
255 public static Select getDaySelector(String name, Calendar now)
256 {
257 Select daySelect = new Select().setName(name);
258
259 for (int currentDay = 1; currentDay <= 31; currentDay++)
260 {
261 Option o = new Option();
262 o.addElement(Integer.toString(currentDay));
263 o.setValue(currentDay);
264 if (now.get(Calendar.DAY_OF_MONTH) == currentDay)
265 {
266 o.setSelected(true);
267 }
268 daySelect.addElement(o);
269 }
270 return (daySelect);
271 }
272
273 /***
274 * Return a year selector.
275 *
276 * @param name The name to use for the selected year.
277 * @return A select object with all the years starting five years
278 * from now and five years before this year.
279 */
280 public static Select getYearSelector(String name)
281 {
282 return (getYearSelector(name, Calendar.getInstance()));
283 }
284
285 /***
286 * Return a year selector.
287 *
288 * @param name The name to use for the selected year.
289 * @param now Calendar to start with.
290 * @return A select object with all the years starting five years
291 * from now and five years before this year.
292 */
293 public static Select getYearSelector(String name, Calendar now)
294 {
295 int startYear = now.get(Calendar.YEAR);
296 return (getYearSelector(name, startYear - 5, startYear + 5, startYear));
297 }
298
299 /***
300 * Return a year selector.
301 *
302 * @param name The name to use for the selected year.
303 * @param firstYear the first (earliest) year in the selector.
304 * @param lastYear the last (latest) year in the selector.
305 * @param selectedYear the year initially selected in the Select html.
306 * @return A select object with all the years from firstyear
307 * to lastyear..
308 */
309 public static Select getYearSelector(String name,
310 int firstYear, int lastYear,
311 int selectedYear)
312 {
313 Select yearSelect = new Select().setName(name);
314
315 for (int currentYear = firstYear;
316 currentYear <= lastYear;
317
318 currentYear++)
319 {
320 Option o = new Option();
321 o.addElement(Integer.toString(currentYear));
322 o.setValue(currentYear);
323 if (currentYear == selectedYear)
324 {
325 o.setSelected(true);
326 }
327 yearSelect.addElement(o);
328 }
329 return (yearSelect);
330 }
331
332 /***
333 * Select the day to be selected if the showDays(false) behavior
334 * is used. Individual getMonth, getDay, getYear static methods
335 * will not use this setting.
336 *
337 * @param day The day.
338 * @return A DateSelector (self).
339 */
340 public boolean setYear(int firstYear, int lastYear, int selectedYear)
341 {
342 if (firstYear <= lastYear && firstYear <= selectedYear
343 && selectedYear <= lastYear)
344 {
345 this.useYears = true;
346 this.firstYear = firstYear;
347 this.lastYear = lastYear;
348 this.selectedYear = selectedYear;
349 return true;
350 }
351 else
352 {
353 return false;
354 }
355 }
356
357 /***
358 * Used to build the popupmenu in HTML. The properties set in the
359 * object are used to generate the correct HTML. The selName
360 * attribute is used to seed the names of the select lists. The
361 * names will be generated as follows:
362 *
363 * <ul>
364 * <li>selName + "_month"</li>
365 * <li>selName + "_day"</li>
366 * <li>selName + "_year"</li>
367 * </ul>
368 *
369 * If onChange was set it is also used in the generation of the
370 * output. The output HTML will list the select lists in the
371 * following order: month day year.
372 *
373 * @return A String with the correct HTML for the date selector.
374 */
375 public String output()
376 {
377 return (ecsOutput().toString());
378 }
379
380 /***
381 * Used to build the popupmenu in HTML. The properties set in the
382 * object are used to generate the correct HTML. The selName
383 * attribute is used to seed the names of the select lists. The
384 * names will be generated as follows:
385 *
386 * <ul>
387 * <li>selName + "_month"</li>
388 * <li>selName + "_day"</li>
389 * <li>selName + "_year"</li>
390 * </ul>
391 *
392 * The output HTML will list the select lists in the following
393 * order: month day year.
394 *
395 * @return A String with the correct HTML for the date selector.
396 */
397 public String toString()
398 {
399 return (ecsOutput().toString());
400 }
401
402
403
404
405
406
407
408 public ElementContainer ecsOutput()
409 {
410 if (this.useDate == null)
411 {
412 this.useDate.setTime(new Date());
413 }
414
415 Select monthSelect = getMonthSelector(selName + MONTH_SUFFIX, useDate);
416 ConcreteElement daySelect = null;
417 if (!showDays)
418 {
419 daySelect = new Input(Input.hidden, selName + DAY_SUFFIX, setDay);
420 }
421 else
422 {
423 Select tmp = getDaySelector(selName + DAY_SUFFIX, useDate);
424 if (onChangeSet)
425 {
426 tmp.setOnChange(onChange);
427 }
428 daySelect = tmp;
429 }
430 Select yearSelect = null;
431 if (useYears)
432 {
433 yearSelect = getYearSelector(selName + YEAR_SUFFIX,
434 firstYear, lastYear, selectedYear);
435 }
436 else
437 {
438 yearSelect = getYearSelector(selName + YEAR_SUFFIX, useDate);
439 }
440 if (onChangeSet)
441 {
442 monthSelect.setOnChange(onChange);
443 yearSelect.setOnChange(onChange);
444 }
445 ElementContainer ec = new ElementContainer();
446
447 ec.addElement(monthSelect);
448 ec.addElement(daySelect);
449 ec.addElement(yearSelect);
450
451 return (ec);
452 }
453 }