KickJava   Java API By Example, From Geeks To Geeks.

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


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.NumberFormat 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  * TimeSelector is a utility class to handle the creation of a set of
71  * time drop-down 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 timeSelect = new ElementContainer();
76  * String myName = "mytime";
77  * ec.addElement(TimeSelector.getHourSelector(myName));
78  * ec.addElement(TimeSelector.getMinuteSelector(myName));
79  * ec.addElement(TimeSelector.getAMPMSelector(myName));
80  * </pre>
81  *
82  * There are also methods which will use attributes to build a
83  * complete time selector in the default 12hr format (eg HH:MM am/pm):
84  *
85  * <pre>
86  * TimeSelector ts = new TimeSelector(myName);
87  * timeSelect = ts.ecsOutput();
88  * </pre>
89  *
90  * Minutes/Seconds are by default rounded to the nearest 5 units
91  * although this can be easily changed.
92  *
93  * 24hr TimeSelectors can also be produced. The following example
94  * creates a full precision TimeSelector (eg HH:MM:SS):
95  *
96  * <pre>
97  * TimeSelector ts = new TimeSelector(myName);
98  * ts.setTimeFormat(TimeSelector.TWENTY_FOUR_HOUR);
99  * ts.setMinuteInterval(1);
100  * ts.setSecondInterval(1);
101  * ts.setShowSeconds(true);
102  * timeSelect = ts.toString();
103  * </pre>
104  *
105  * @author <a HREF="mailto:ekkerbj@netscape.net">Jeffrey D. Brekke</a>
106  * @author <a HREF="mailto:rich@thenetrevolution.com">Rich Aston</a>
107  * @version $Id: TimeSelector.java,v 1.1.1.1 2001/08/16 04:41:50 jvanzyl Exp $
108  */

109 public class TimeSelector
110 {
111     /** Prefix for time names. */
112     public static final String JavaDoc DEFAULT_PREFIX = "TimeSelector";
113
114     /** Suffix for hour parameter. */
115     public static final String JavaDoc HOUR_SUFFIX = "_hour";
116
117     /** Suffix for minute parameter. */
118     public static final String JavaDoc MINUTE_SUFFIX = "_minute";
119
120     /** Suffix for second parameter. */
121     public static final String JavaDoc SECOND_SUFFIX = "_second";
122
123     /** Suffix for am/pm parameter. */
124     public static final String JavaDoc AMPM_SUFFIX = "_ampm";
125
126     /** Constant for 12hr format */
127     public static final int TWELVE_HOUR = 0;
128
129     /** Constant for 24hr format */
130     public static final int TWENTY_FOUR_HOUR = 1;
131
132     /** TODO: Add ability to specify Locale. */
133     private static final NumberFormat JavaDoc nbrFmt;
134
135     private static final int DEFAULT_MINUTE_INTERVAL = 5;
136     private static final int DEFAULT_SECOND_INTERVAL = 5;
137     private static final int DEFAULT_TIME_FORMAT = TWELVE_HOUR;
138
139     private int timeFormat = DEFAULT_TIME_FORMAT;
140     private int minuteInterval = DEFAULT_MINUTE_INTERVAL;
141     private int secondInterval = DEFAULT_SECOND_INTERVAL;
142
143     private Calendar JavaDoc useDate = null;
144     private String JavaDoc selName = null;
145     private String JavaDoc onChange = null;
146     private boolean onChangeSet = false;
147     private boolean showSeconds = false;
148     private int setSeconds = 0;
149
150     static
151     {
152         nbrFmt = NumberFormat.getInstance();
153         nbrFmt.setMinimumIntegerDigits(2);
154         nbrFmt.setMaximumIntegerDigits(2);
155     }
156
157     /**
158      * Constructor defaults to current date/time and uses the default
159      * prefix: <pre>TimeSelector.DEFAULT</pre>
160      */

161     public TimeSelector( )
162     {
163         this.selName = DEFAULT_PREFIX;
164         this.useDate = Calendar.getInstance();
165         this.useDate.setTime ( new Date JavaDoc() );
166     }
167
168     /**
169      * Constructor, uses the date/time set in the calendar
170      * passed in (with the date/time set correctly).
171      *
172      * @param selName A String with the selector name.
173      * @param useDate A Calendar with a date/time.
174      */

175     public TimeSelector( String JavaDoc selName, Calendar JavaDoc useDate )
176     {
177         this.useDate = useDate;
178         this.selName = selName;
179     }
180
181     /**
182      * Constructor defaults to current date/time.
183      *
184      * @param selName A String with the selector name.
185      */

186     public TimeSelector( String JavaDoc selName )
187     {
188         this.selName = selName;
189         this.useDate = Calendar.getInstance();
190         this.useDate.setTime ( new Date JavaDoc() );
191     }
192
193     /**
194      * Adds the onChange to all of <code>&lt;SELECT&gt;</code> tags.
195      * This is limited to one function for all three popups and is only
196      * used when the output() methods are used. Individual getHour,
197      * getMinute, getSecond, getAMPM static methods will not use this
198      * setting.
199      *
200      * @param onChange A String to use for onChange attribute. If null,
201      * then nothing will be set.
202      * @return A TimeSelector (self).
203      */

204     public TimeSelector setOnChange ( String JavaDoc onChange )
205     {
206         if (onChange != null)
207         {
208             this.onChange = onChange;
209             this.onChangeSet = true;
210         }
211         else
212         {
213             this.onChange = null;
214             this.onChangeSet = false;
215         }
216         return this;
217     }
218
219     /**
220      * Select the second to be selected if the showSeconds(false) behavior
221      * is used. Individual getHour, getMinute, getSecond, getAMPM
222      * static methods will not use this setting.
223      *
224      * @param seconds The second.
225      * @return A TimeSelector (self).
226      */

227     public TimeSelector setSeconds( int seconds )
228     {
229         this.setSeconds = seconds;
230         this.showSeconds = false;
231         return this;
232     }
233
234     /**
235      * Set the interval between options in the minute select box.
236      * Individual getHour, getMinute, getSecond, getAMPM static methods
237      * will not use this setting.
238      *
239      * @param minutes Interval in minutes.
240      * @return A TimeSelector (self).
241      */

242     public TimeSelector setMinuteInterval( int minutes )
243     {
244         this.minuteInterval = minutes;
245         return this;
246     }
247
248     /**
249      * Set the interval between options in the second select box.
250      * Individual getHour, getMinute, getSecond, getAMPM static methods
251      * will not use this setting.
252      *
253      * @param seconds Interval in seconds.
254      * @return A TimeSelector (self).
255      */

256     public TimeSelector setSecondInterval( int seconds )
257     {
258         this.secondInterval = seconds;
259         return this;
260     }
261
262     /**
263      * Set the time format to 12 or 24 hour. Individual getHour,
264      * getMinute, getSecond, getAMPM static methods
265      * will not use this setting.
266      *
267      * @param format Time format.
268      * @return A TimeSelector (self).
269      */

270     public TimeSelector setTimeFormat( int format )
271     {
272         this.timeFormat = format;
273         return this;
274     }
275
276     /**
277      * Whether or not to show the seconds as a popup menu. The seconds will
278      * be a hidden parameter and the value set with setSeconds is used.
279      * Individual getHour, getMinute, getSecond, getAMPM static methods
280      * will not use this setting.
281      *
282      * @param show True if the second should be shown.
283      * @return A TimeSelector (self).
284      */

285     public TimeSelector setShowSeconds ( boolean show )
286     {
287         this.showSeconds = show;
288         return this;
289     }
290
291     /**
292      * Set the selector name prefix. Individual getHour, getMinute,
293      * getSeconds, getAMPM static methods will not use this setting.
294      *
295      * @param selname A String with the select name prefix.
296      */

297     public void setSelName( String JavaDoc selName )
298     {
299         this.selName = selName;
300     }
301
302     /**
303      * Get the selector name prefix.
304      *
305      * @return A String with the select name prefix.
306      */

307     public String JavaDoc getSelName()
308     {
309         return selName;
310     }
311
312     /**
313      * Return a second selector.
314      *
315      * @param name The name to use for the selected second.
316      * @return A select object with second options.
317      */

318     public static Select getSecondSelector(String JavaDoc name)
319     {
320         return(getSecondSelector(name, Calendar.getInstance()));
321     }
322
323     /**
324      * Return a second selector.
325      *
326      * @param name The name to use for the selected second.
327      * @param now Calendar to start with.
328      * @return A select object with second options.
329      */

330     public static Select getSecondSelector(String JavaDoc name, Calendar JavaDoc now)
331     {
332         return(getSecondSelector(name, Calendar.getInstance(),
333                DEFAULT_SECOND_INTERVAL));
334     }
335
336     /**
337      * Return a second selector.
338      *
339      * @param name The name to use for the selected second.
340      * @param now Calendar to start with.
341      * @param interval Interval between options.
342      * @return A select object with second options.
343      */

344     public static Select getSecondSelector(String JavaDoc name, Calendar JavaDoc now,
345                                               int interval)
346     {
347         Select secondSelect = new Select().setName(name);
348
349         for(int currentSecond=0; currentSecond <= 59; currentSecond+=interval)
350         {
351             Option o = new Option();
352             o.addElement(nbrFmt.format(currentSecond));
353             o.setValue(currentSecond);
354             int nearestSecond =
355                 ((now.get(Calendar.SECOND) / interval) * interval);
356
357             if (nearestSecond == currentSecond)
358             {
359                 o.setSelected(true);
360             }
361             secondSelect.addElement(o);
362         }
363         return(secondSelect);
364     }
365
366     /**
367      * Return a minute selector.
368      *
369      * @param name The name to use for the selected minute.
370      * @return A select object with minute options.
371      */

372     public static Select getMinuteSelector(String JavaDoc name)
373     {
374         return(getMinuteSelector(name, Calendar.getInstance()));
375     }
376
377     /**
378      * Return a minute selector.
379      *
380      * @param name The name to use for the selected minute.
381      * @return A select object with minute options.
382      */

383     public static Select getMinuteSelector(String JavaDoc name, Calendar JavaDoc now)
384     {
385         return(getMinuteSelector(name, now, DEFAULT_MINUTE_INTERVAL));
386     }
387
388     /**
389      * Return a minute selector.
390      *
391      * @param name The name to use for the selected minute.
392      * @param now Calendar to start with.
393      * @param interval Interval between options.
394      * @return A select object with minute options.
395      */

396     public static Select getMinuteSelector(String JavaDoc name, Calendar JavaDoc now,
397                                               int interval)
398     {
399         Select minuteSelect = new Select().setName(name);
400
401         for (int curMinute = 0;curMinute <= 59; curMinute += interval)
402         {
403             Option o = new Option();
404             o.addElement(nbrFmt.format(curMinute));
405             o.setValue(curMinute);
406             int nearestMinute =
407                 ((now.get(Calendar.MINUTE)) / interval) * interval;
408
409             if (nearestMinute == curMinute)
410             {
411                 o.setSelected(true);
412             }
413             minuteSelect.addElement(o);
414         }
415         return(minuteSelect);
416     }
417
418     /**
419      * Return an 12 hour selector.
420      *
421      * @param name The name to use for the selected hour.
422      * @return A select object with all the hours.
423      */

424     public static Select getHourSelector(String JavaDoc name)
425     {
426         return(getHourSelector(name, Calendar.getInstance()));
427     }
428
429     /**
430      * Return an 12 hour selector.
431      *
432      * @param name The name to use for the selected hour.
433      * @param now Calendar to start with.
434      * @return A select object with all the hours.
435      */

436     public static Select getHourSelector(String JavaDoc name, Calendar JavaDoc now)
437     {
438         return(getHourSelector(name, Calendar.getInstance(), TWELVE_HOUR));
439     }
440
441     /**
442      * Return an hour selector (either 12hr or 24hr depending on
443      * <code>format</code>.
444      *
445      * @param name The name to use for the selected hour.
446      * @param now Calendar to start with.
447      * @param format Time format.
448      * @return A select object with all the hours.
449      */

450     public static Select getHourSelector(String JavaDoc name, Calendar JavaDoc now, int format)
451     {
452         Select hourSelect = new Select().setName(name);
453
454         if (format == TWENTY_FOUR_HOUR)
455         {
456             for (int currentHour = 0; currentHour <= 23; currentHour++)
457             {
458                 Option o = new Option();
459                 o.addElement(nbrFmt.format(currentHour));
460                 o.setValue(currentHour);
461                 if (now.get(Calendar.HOUR_OF_DAY) == currentHour)
462                 {
463                     o.setSelected(true);
464                 }
465                 hourSelect.addElement(o);
466             }
467         }
468         else
469         {
470             for (int curHour = 1;curHour <= 12; curHour++)
471             {
472                 Option o = new Option();
473
474                 o.addElement(nbrFmt.format((long)curHour));
475                 o.setValue(curHour);
476                 if (now.get(Calendar.AM_PM) == Calendar.AM)
477                 {
478                     if (((now.get(Calendar.HOUR_OF_DAY)) == 0) &&
479                         (curHour == 12))
480                     {
481                         o.setSelected(true);
482                     }
483                     else
484                     {
485                         if (now.get(Calendar.HOUR_OF_DAY) == curHour)
486                         {
487                             o.setSelected(true);
488                         }
489                     }
490                 }
491                 else
492                 {
493                     if (((now.get(Calendar.HOUR_OF_DAY)) == 12) &&
494                         (curHour == 12))
495                     {
496                         o.setSelected(true);
497                     }
498                     else
499                     {
500                         if (now.get(Calendar.HOUR_OF_DAY) == curHour+12)
501                         {
502                             o.setSelected(true);
503                         }
504                     }
505                 }
506                 hourSelect.addElement(o);
507             }
508         }
509         return(hourSelect);
510     }
511
512     /**
513      * Return an am/pm selector.
514      *
515      * @param name The name to use for the selected am/pm.
516      * @return A select object with am/pm
517      */

518     public static Select getAMPMSelector(String JavaDoc name)
519     {
520         Calendar JavaDoc c = Calendar.getInstance();
521         c.setTime(new Date JavaDoc());
522         return(getAMPMSelector(name, c));
523     }
524
525     /**
526      * Return an am/pm selector.
527      *
528      * @param name The name to use for the selected am/pm.
529      * @param now Calendar to start with.
530      * @return A select object with am/pm.
531      */

532     public static Select getAMPMSelector(String JavaDoc name,
533                                          Calendar JavaDoc now)
534     {
535         Select ampmSelect = new Select().setName(name);
536
537         Option o = new Option();
538         o.addElement("am");
539         o.setValue(Calendar.AM);
540         if (now.get(Calendar.AM_PM) == Calendar.AM)
541         {
542             o.setSelected(true);
543         }
544         ampmSelect.addElement(o);
545
546         o = new Option();
547         o.addElement("pm");
548         o.setValue(Calendar.PM);
549         if (now.get(Calendar.AM_PM) == Calendar.PM)
550         {
551             o.setSelected(true);
552         }
553         ampmSelect.addElement(o);
554
555         return(ampmSelect);
556     }
557
558     /**
559      * Used to build the popupmenu in HTML. The properties set in the
560      * object are used to generate the correct HTML. The selName
561      * attribute is used to seed the names of the select lists. The
562      * names will be generated as follows:
563      *
564      * <ul>
565      * <li>selName + "_hour"</li>
566      * <li>selName + "_minute"</li>
567      * <li>selName + "_ampm"</li>
568      * </ul>
569      *
570      * If onChange was set it is also used in the generation of the
571      * output. The output HTML will list the select lists in the
572      * following order: hour minute ampm.
573      *
574      * If setShowSeconds(true) is used then an addition second select box
575      * is produced after the minute select box.
576      *
577      * If setTimeFormat(TimeSelector.TWENTY_FOUR_HOUR) is used then
578      * the ampm select box is omitted.
579      *
580      * @return A String with the correct HTML for the date selector.
581      */

582     public String JavaDoc output()
583     {
584         return(ecsOutput().toString());
585     }
586
587     /**
588      * Used to build the popupmenu in HTML. The properties set in the
589      * object are used to generate the correct HTML. The selName
590      * attribute is used to seed the names of the select lists. The
591      * names will be generated as follows:
592      *
593      * <ul>
594      * <li>selName + "_hour"</li>
595      * <li>selName + "_minute"</li>
596      * <li>selName + "_ampm"</li>
597      * </ul>
598      *
599      * If onChange was set it is also used in the generation of the
600      * output. The output HTML will list the select lists in the
601      * following order: hour minute ampm.
602      *
603      * If setShowSeconds(true) is used then an addition second select box
604      * is produced after the minute select box.
605      *
606      * If setTimeFormat(TimeSelector.TWENTY_FOUR_HOUR) is used then
607      * the ampm select box is omitted.
608      *
609      * @return A String with the correct HTML for the date selector.
610      */

611     public String JavaDoc toString()
612     {
613         return(ecsOutput().toString());
614     }
615
616     /**
617      * Return an ECS container with the select objects inside.
618      *
619      * @return An ECS container.
620      */

621     public ElementContainer ecsOutput()
622     {
623         if ( this.useDate == null )
624         {
625             this.useDate = Calendar.getInstance();
626             this.useDate.setTime ( new Date JavaDoc() );
627         }
628
629         ConcreteElement secondSelect = null;
630
631         Select ampmSelect = getAMPMSelector(selName + AMPM_SUFFIX, useDate);
632
633         Select hourSelect = getHourSelector(selName + HOUR_SUFFIX,
634                                             useDate, this.timeFormat);
635
636         Select minuteSelect = getMinuteSelector(selName + MINUTE_SUFFIX,
637                                             useDate, this.minuteInterval);
638
639         if (this.showSeconds)
640         {
641             Select tmp = getSecondSelector(selName + SECOND_SUFFIX, useDate,
642                                                         this.secondInterval);
643             if (onChangeSet)
644                 tmp.setOnChange(onChange);
645             secondSelect = tmp;
646         }
647         else
648         {
649             secondSelect = new Input(Input.hidden,
650                                   selName + SECOND_SUFFIX,
651                                   setSeconds);
652         }
653
654         if (onChangeSet)
655         {
656             hourSelect.setOnChange(onChange);
657             minuteSelect.setOnChange(onChange);
658             ampmSelect.setOnChange(onChange);
659         }
660
661         ElementContainer ec = new ElementContainer();
662         ec.addElement(new Comment(
663             "== BEGIN org.apache.turbine.util.TimeSelector.ecsOutput() =="));
664         ec.addElement(hourSelect);
665         ec.addElement(":");
666         ec.addElement(minuteSelect);
667         if (this.showSeconds == true)
668             ec.addElement(":");
669         ec.addElement(secondSelect);
670         if (this.timeFormat == this.TWELVE_HOUR)
671         {
672             ec.addElement(ampmSelect);
673         }
674         ec.addElement(new Comment(
675             "== END org.apache.turbine.util.TimeSelector.ecsOutput() =="));
676         return (ec);
677     }
678 }
679
Popular Tags