KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > DateSelector


1 package org.apache.turbine.util;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.text.DateFormatSymbols JavaDoc;
58 import java.util.Calendar JavaDoc;
59 import java.util.Date JavaDoc;
60 import org.apache.ecs.ConcreteElement;
61 import org.apache.ecs.Element;
62 import org.apache.ecs.ElementContainer;
63 import org.apache.ecs.GenericElement;
64 import org.apache.ecs.html.Comment;
65 import org.apache.ecs.html.Input;
66 import org.apache.ecs.html.Option;
67 import org.apache.ecs.html.Select;
68
69 /**
70  * DateSelector is a utility class to handle the creation of a set of
71  * date popup menus. The code is broken into a set of static methods
72  * for quick and easy access to the individual select objects:
73  *
74  * <pre>
75  * ElementContainer ec dateSelect = new ElementContainer();
76  * String myName = "mydate";
77  * ec.addElement(DateSelector.getMonthSelector(myName));
78  * ec.addElement(DateSelector.getDaySelector(myName));
79  * ec.addElement(DateSelector.getYearSelector(myName));
80  * </pre>
81  *
82  * There are also methods which will use attributes to build a
83  * complete month,day,year selector:
84  *
85  * <pre>
86  * DateSelector ds = new DateSelector(myName);
87  * dateSelect = ds.ecsOutput();
88  * </pre>
89  *
90  * The above element container would use the onChange setting and may
91  * hide the selected day if set via showDays().<br>
92  *
93  * @author <a HREF="mailto:ekkerbj@netscape.net">Jeffrey D. Brekke</a>
94  * @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
95  * @author <a HREF="mailto:leon@clearink.com">Leon Atkinson</a>
96  * @version $Id: DateSelector.java,v 1.1.1.1 2001/08/16 04:41:49 jvanzyl Exp $
97  */

98 public class DateSelector
99 {
100     /** Prefix for date names. */
101     public static final String JavaDoc DEFAULT_PREFIX = "DateSelector";
102
103     /** Suffix for day parameter. */
104     public static final String JavaDoc DAY_SUFFIX = "_day";
105
106     /** Suffix for month parameter. */
107     public static final String JavaDoc MONTH_SUFFIX = "_month";
108
109     /** Suffix for year parameter. */
110     public static final String JavaDoc YEAR_SUFFIX = "_year";
111
112     private Calendar JavaDoc useDate = null;
113     private String JavaDoc selName = null;
114     private static final String JavaDoc[] monthName =
115         new DateFormatSymbols JavaDoc().getMonths();
116     private String JavaDoc onChange = null;
117     private boolean onChangeSet = false;
118     private boolean showDays = true;
119     private int setDay = 0;
120     private boolean useYears = false;
121     private int firstYear = 0;
122     private int lastYear = 0;
123     private int selectedYear = 0;
124
125     /**
126      * Constructor defaults to current date and uses the default
127      * prefix: <pre>DateSelector.DEFAULT</pre>
128      */

129     public DateSelector( )
130     {
131         this.selName = DEFAULT_PREFIX;
132         this.useDate = Calendar.getInstance();
133         this.useDate.setTime ( new Date JavaDoc() );
134     }
135
136     /**
137      * Constructor, uses the date set in a calendar that has been
138      * already passed in (with the date set correctly).
139      *
140      * @param selName A String with the selector name.
141      * @param useDate A Calendar with a date.
142      */

143     public DateSelector( String JavaDoc selName, Calendar JavaDoc useDate )
144     {
145         this.useDate = useDate;
146         this.selName = selName;
147     }
148
149     /**
150      * Constructor defaults to current date.
151      *
152      * @param selName A String with the selector name.
153      */

154     public DateSelector( String JavaDoc selName )
155     {
156         this.selName = selName;
157         this.useDate = Calendar.getInstance();
158         this.useDate.setTime ( new Date JavaDoc() );
159     }
160
161     /**
162      * Adds the onChange to all of <SELECT> tags. This is limited to
163      * one function for all three popups and is only used when the
164      * output() methods are used. Individual getMonth, getDay,
165      * getYear static methods will not use this setting.
166      *
167      * @param string A String to use for onChange attribute. If null,
168      * then nothing will be set.
169      * @return A DateSelector (self).
170      */

171     public DateSelector setOnChange ( String JavaDoc onChange )
172     {
173         if (onChange != null)
174         {
175             this.onChange = onChange;
176             this.onChangeSet = true;
177         }
178         else
179         {
180             this.onChange = null;
181             this.onChangeSet = false;
182         }
183         return this;
184     }
185
186     /**
187      * Select the day to be selected if the showDays(false) behavior
188      * is used. Individual getMonth, getDay, getYear static methods
189      * will not use this setting.
190      *
191      * @param day The day.
192      * @return A DateSelector (self).
193      */

194     public DateSelector setDay( int day )
195     {
196         this.setDay = day;
197         this.showDays = false;
198         return this;
199     }
200
201     /**
202      * Whether or not to show the days as a popup menu. The days will
203      * be a hidden parameter and the value set with setDay is used.
204      * Individual getMonth, getDay, getYear static methods will not
205      * use this setting.
206      *
207      * @param show True if the day should be shown.
208      * @return A DateSelector (self).
209      */

210     public DateSelector setShowDay ( boolean show )
211     {
212         this.showDays = false;
213         return this;
214     }
215
216     /**
217      * Set the selector name prefix. Individual getMonth, getDay,
218      * getYear static methods will not use this setting.
219      *
220      * @param selname A String with the select name prefix.
221      */

222     public void setSelName( String JavaDoc selName )
223     {
224         this.selName = selName;
225     }
226
227     /**
228      * Get the selector name prefix.
229      *
230      * @return A String with the select name prefix.
231      */

232     public String JavaDoc getSelName()
233     {
234         return selName;
235     }
236
237     /**
238      * Return a month selector.
239      *
240      * @param name The name to use for the selected month.
241      * @return A select object with all the months.
242      */

243     public static Select getMonthSelector(String JavaDoc name)
244     {
245         return(getMonthSelector(name, Calendar.getInstance()));
246     }
247
248     /**
249      * Return a month selector.
250      *
251      * Note: The values of the month placed into the select list are
252      * the month integers starting at 0 (ie: if the user selects
253      * February, the selected value will be 1).
254      *
255      * @param name The name to use for the selected month.
256      * @param now Calendar to start with.
257      * @return A select object with all the months.
258      */

259     public static Select getMonthSelector(String JavaDoc name,
260                                           Calendar JavaDoc now)
261     {
262         Select monthSelect = new Select().setName(name);
263
264         for (int curMonth = 0;curMonth <= 11; curMonth++)
265         {
266             Option o = new Option();
267             o.addElement(monthName[curMonth]);
268             o.setValue(curMonth);
269             if ((now.get(Calendar.MONTH)) == curMonth)
270             {
271                 o.setSelected(true);
272             }
273             monthSelect.addElement(o);
274         }
275         return(monthSelect);
276     }
277
278     /**
279      * Return a day selector.
280      *
281      * @param name The name to use for the selected day.
282      * @return A select object with all the days in a month.
283      */

284     public static Select getDaySelector(String JavaDoc name)
285     {
286         return(getDaySelector(name, Calendar.getInstance()));
287     }
288
289     /**
290      * Return a day selector.
291      *
292      * @param name The name to use for the selected day.
293      * @param now Calendar to start with.
294      * @return A select object with all the days in a month.
295      */

296     public static Select getDaySelector(String JavaDoc name,
297                                         Calendar JavaDoc now)
298     {
299         Select daySelect = new Select().setName(name);
300
301         for(int currentDay=1; currentDay <= 31; currentDay++)
302         {
303             Option o = new Option();
304             o.addElement(Integer.toString(currentDay));
305             o.setValue(currentDay);
306             if (now.get(Calendar.DAY_OF_MONTH) == currentDay)
307             {
308                 o.setSelected(true);
309             }
310             daySelect.addElement(o);
311         }
312         return(daySelect);
313     }
314
315     /**
316      * Return a year selector.
317      *
318      * @param name The name to use for the selected year.
319      * @return A select object with all the years starting five years
320      * from now and five years before this year.
321      */

322     public static Select getYearSelector(String JavaDoc name)
323     {
324         return(getYearSelector(name, Calendar.getInstance()));
325     }
326
327     /**
328      * Return a year selector.
329      *
330      * @param name The name to use for the selected year.
331      * @param now Calendar to start with.
332      * @return A select object with all the years starting five years
333      * from now and five years before this year.
334      */

335     public static Select getYearSelector(String JavaDoc name,
336                                          Calendar JavaDoc now)
337     {
338         int startYear = now.get(Calendar.YEAR);
339         return(getYearSelector(name, startYear-5, startYear+5, startYear));
340     }
341
342     /**
343      * Return a year selector.
344      *
345      * @param name The name to use for the selected year.
346      * @param firstYear the first (earliest) year in the selector.
347      * @param lastYear the last (latest) year in the selector.
348      * @param selectedYear the year initially selected in the Select html.
349      * @return A select object with all the years from firstyear
350      * to lastyear..
351      */

352     public static Select getYearSelector(String JavaDoc name,
353                                          int firstYear, int lastYear,
354                                          int selectedYear )
355     {
356         Select yearSelect = new Select().setName(name);
357
358         for(int currentYear = firstYear;
359             currentYear <= lastYear;
360             currentYear++)
361         {
362             Option o = new Option();
363             o.addElement(Integer.toString(currentYear));
364             o.setValue(currentYear);
365             if (currentYear == selectedYear)
366             {
367                 o.setSelected(true);
368             }
369             yearSelect.addElement(o);
370         }
371         return(yearSelect);
372     }
373
374
375     /**
376      * Select the day to be selected if the showDays(false) behavior
377      * is used. Individual getMonth, getDay, getYear static methods
378      * will not use this setting.
379      *
380      * @param day The day.
381      * @return A DateSelector (self).
382      */

383     public boolean setYear( int firstYear, int lastYear, int selectedYear )
384     {
385         if (firstYear <= lastYear && firstYear <= selectedYear
386             && selectedYear <= lastYear)
387         {
388             this.useYears = true;
389             this.firstYear = firstYear;
390             this.lastYear = lastYear;
391             this.selectedYear = selectedYear;
392             return true;
393         }
394         else
395         {
396             return false;
397         }
398     }
399
400     /**
401      * Used to build the popupmenu in HTML. The properties set in the
402      * object are used to generate the correct HTML. The selName
403      * attribute is used to seed the names of the select lists. The
404      * names will be generated as follows:
405      *
406      * <ul>
407      * <li>selName + "_month"</li>
408      * <li>selName + "_day"</li>
409      * <li>selName + "_year"</li>
410      * </ul>
411      *
412      * If onChange was set it is also used in the generation of the
413      * output. The output HTML will list the select lists in the
414      * following order: month day year.
415      *
416      * @return A String with the correct HTML for the date selector.
417      */

418     public String JavaDoc output()
419     {
420         return(ecsOutput().toString());
421     }
422
423     /**
424      * Used to build the popupmenu in HTML. The properties set in the
425      * object are used to generate the correct HTML. The selName
426      * attribute is used to seed the names of the select lists. The
427      * names will be generated as follows:
428      *
429      * <ul>
430      * <li>selName + "_month"</li>
431      * <li>selName + "_day"</li>
432      * <li>selName + "_year"</li>
433      * </ul>
434      *
435      * The output HTML will list the select lists in the following
436      * order: month day year.
437      *
438      * @return A String with the correct HTML for the date selector.
439      */

440     public String JavaDoc toString()
441     {
442         return(ecsOutput().toString());
443     }
444
445     /*
446      * Return an ECS container with the month, day, and year select
447      * objects inside.
448      *
449      * @return An ECS container.
450      */

451     public ElementContainer ecsOutput()
452     {
453         if ( this.useDate == null )
454         {
455             this.useDate.setTime ( new Date JavaDoc() );
456         }
457
458         Select monthSelect = getMonthSelector(selName + MONTH_SUFFIX, useDate);
459         ConcreteElement daySelect = null;
460         if (!showDays)
461         {
462             daySelect = new Input(Input.hidden,
463                                   selName + DAY_SUFFIX,
464                                   setDay);
465         }
466         else
467         {
468             Select tmp = getDaySelector(selName + DAY_SUFFIX, useDate);
469             if (onChangeSet)
470                 tmp.setOnChange(onChange);
471             daySelect = tmp;
472         }
473         Select yearSelect = null;
474         if (useYears)
475         {
476             yearSelect = getYearSelector(selName + YEAR_SUFFIX,
477                             firstYear, lastYear, selectedYear);
478         }
479         else
480         {
481             yearSelect = getYearSelector(selName + YEAR_SUFFIX, useDate);
482         }
483         if (onChangeSet)
484         {
485             monthSelect.setOnChange(onChange);
486             yearSelect.setOnChange(onChange);
487         }
488         ElementContainer ec = new ElementContainer();
489         // ec.addElement(new Comment("== BEGIN org.apache.turbine.util.DateSelector.ecsOutput() =="));
490
ec.addElement(monthSelect);
491         ec.addElement(daySelect);
492         ec.addElement(yearSelect);
493         // ec.addElement(new Comment("== END org.apache.turbine.util.DateSelector.ecsOutput() =="));
494
return (ec);
495     }
496 }
497
Popular Tags