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;
58 import java.util.Calendar;
59 import java.util.Date;
60 import org.apache.ecs.ConcreteElement;
61 import org.apache.ecs.ElementContainer;
62 import org.apache.ecs.html.Comment;
63 import org.apache.ecs.html.Input;
64 import org.apache.ecs.html.Option;
65 import org.apache.ecs.html.Select;
66
67 /***
68 * TimeSelector is a utility class to handle the creation of a set of
69 * time drop-down menus. The code is broken into a set of static methods
70 * for quick and easy access to the individual select objects:
71 *
72 * <pre>
73 * ElementContainer ec timeSelect = new ElementContainer();
74 * String myName = "mytime";
75 * ec.addElement(TimeSelector.getHourSelector(myName));
76 * ec.addElement(TimeSelector.getMinuteSelector(myName));
77 * ec.addElement(TimeSelector.getAMPMSelector(myName));
78 * </pre>
79 *
80 * There are also methods which will use attributes to build a
81 * complete time selector in the default 12hr format (eg HH:MM am/pm):
82 *
83 * <pre>
84 * TimeSelector ts = new TimeSelector(myName);
85 * timeSelect = ts.ecsOutput();
86 * </pre>
87 *
88 * Minutes/Seconds are by default rounded to the nearest 5 units
89 * although this can be easily changed.
90 *
91 * 24hr TimeSelectors can also be produced. The following example
92 * creates a full precision TimeSelector (eg HH:MM:SS):
93 *
94 * <pre>
95 * TimeSelector ts = new TimeSelector(myName);
96 * ts.setTimeFormat(TimeSelector.TWENTY_FOUR_HOUR);
97 * ts.setMinuteInterval(1);
98 * ts.setSecondInterval(1);
99 * ts.setShowSeconds(true);
100 * timeSelect = ts.toString();
101 * </pre>
102 *
103 * @author <a href="mailto:ekkerbj@netscape.net">Jeffrey D. Brekke</a>
104 * @author <a href="mailto:rich@thenetrevolution.com">Rich Aston</a>
105 * @version $Id: TimeSelector.java,v 1.2 2002/07/11 16:53:21 mpoeschl Exp $
106 */
107 public class TimeSelector
108 {
109 /*** Prefix for time names. */
110 public static final String DEFAULT_PREFIX = "TimeSelector";
111
112 /*** Suffix for hour parameter. */
113 public static final String HOUR_SUFFIX = "_hour";
114
115 /*** Suffix for minute parameter. */
116 public static final String MINUTE_SUFFIX = "_minute";
117
118 /*** Suffix for second parameter. */
119 public static final String SECOND_SUFFIX = "_second";
120
121 /*** Suffix for am/pm parameter. */
122 public static final String AMPM_SUFFIX = "_ampm";
123
124 /*** Constant for 12hr format */
125 public static final int TWELVE_HOUR = 0;
126
127 /*** Constant for 24hr format */
128 public static final int TWENTY_FOUR_HOUR = 1;
129
130 /*** TODO: Add ability to specify Locale. */
131 private static final NumberFormat nbrFmt;
132
133 private static final int DEFAULT_MINUTE_INTERVAL = 5;
134 private static final int DEFAULT_SECOND_INTERVAL = 5;
135 private static final int DEFAULT_TIME_FORMAT = TWELVE_HOUR;
136
137 private int timeFormat = DEFAULT_TIME_FORMAT;
138 private int minuteInterval = DEFAULT_MINUTE_INTERVAL;
139 private int secondInterval = DEFAULT_SECOND_INTERVAL;
140
141 private Calendar useDate = null;
142 private String selName = null;
143 private String onChange = null;
144 private boolean onChangeSet = false;
145 private boolean showSeconds = false;
146 private int setSeconds = 0;
147
148 static
149 {
150 nbrFmt = NumberFormat.getInstance();
151 nbrFmt.setMinimumIntegerDigits(2);
152 nbrFmt.setMaximumIntegerDigits(2);
153 }
154
155 /***
156 * Constructor defaults to current date/time and uses the default
157 * prefix: <pre>TimeSelector.DEFAULT</pre>
158 */
159 public TimeSelector()
160 {
161 this.selName = DEFAULT_PREFIX;
162 this.useDate = Calendar.getInstance();
163 this.useDate.setTime(new Date());
164 }
165
166 /***
167 * Constructor, uses the date/time set in the calendar
168 * passed in (with the date/time set correctly).
169 *
170 * @param selName A String with the selector name.
171 * @param useDate A Calendar with a date/time.
172 */
173 public TimeSelector(String selName, Calendar useDate)
174 {
175 this.useDate = useDate;
176 this.selName = selName;
177 }
178
179 /***
180 * Constructor defaults to current date/time.
181 *
182 * @param selName A String with the selector name.
183 */
184 public TimeSelector(String selName)
185 {
186 this.selName = selName;
187 this.useDate = Calendar.getInstance();
188 this.useDate.setTime(new Date());
189 }
190
191 /***
192 * Adds the onChange to all of <code><SELECT></code> tags.
193 * This is limited to one function for all three popups and is only
194 * used when the output() methods are used. Individual getHour,
195 * getMinute, getSecond, getAMPM static methods will not use this
196 * setting.
197 *
198 * @param onChange A String to use for onChange attribute. If null,
199 * then nothing will be set.
200 * @return A TimeSelector (self).
201 */
202 public TimeSelector setOnChange(String onChange)
203 {
204 if (onChange != null)
205 {
206 this.onChange = onChange;
207 this.onChangeSet = true;
208 }
209 else
210 {
211 this.onChange = null;
212 this.onChangeSet = false;
213 }
214 return this;
215 }
216
217 /***
218 * Select the second to be selected if the showSeconds(false) behavior
219 * is used. Individual getHour, getMinute, getSecond, getAMPM
220 * static methods will not use this setting.
221 *
222 * @param seconds The second.
223 * @return A TimeSelector (self).
224 */
225 public TimeSelector setSeconds(int seconds)
226 {
227 this.setSeconds = seconds;
228 this.showSeconds = false;
229 return this;
230 }
231
232 /***
233 * Set the interval between options in the minute select box.
234 * Individual getHour, getMinute, getSecond, getAMPM static methods
235 * will not use this setting.
236 *
237 * @param minutes Interval in minutes.
238 * @return A TimeSelector (self).
239 */
240 public TimeSelector setMinuteInterval(int minutes)
241 {
242 this.minuteInterval = minutes;
243 return this;
244 }
245
246 /***
247 * Set the interval between options in the second select box.
248 * Individual getHour, getMinute, getSecond, getAMPM static methods
249 * will not use this setting.
250 *
251 * @param seconds Interval in seconds.
252 * @return A TimeSelector (self).
253 */
254 public TimeSelector setSecondInterval(int seconds)
255 {
256 this.secondInterval = seconds;
257 return this;
258 }
259
260 /***
261 * Set the time format to 12 or 24 hour. Individual getHour,
262 * getMinute, getSecond, getAMPM static methods
263 * will not use this setting.
264 *
265 * @param format Time format.
266 * @return A TimeSelector (self).
267 */
268 public TimeSelector setTimeFormat(int format)
269 {
270 this.timeFormat = format;
271 return this;
272 }
273
274 /***
275 * Whether or not to show the seconds as a popup menu. The seconds will
276 * be a hidden parameter and the value set with setSeconds is used.
277 * Individual getHour, getMinute, getSecond, getAMPM static methods
278 * will not use this setting.
279 *
280 * @param show True if the second should be shown.
281 * @return A TimeSelector (self).
282 */
283 public TimeSelector setShowSeconds(boolean show)
284 {
285 this.showSeconds = show;
286 return this;
287 }
288
289 /***
290 * Set the selector name prefix. Individual getHour, getMinute,
291 * getSeconds, getAMPM static methods will not use this setting.
292 *
293 * @param selname A String with the select name prefix.
294 */
295 public void setSelName(String selName)
296 {
297 this.selName = selName;
298 }
299
300 /***
301 * Get the selector name prefix.
302 *
303 * @return A String with the select name prefix.
304 */
305 public String getSelName()
306 {
307 return selName;
308 }
309
310 /***
311 * Return a second selector.
312 *
313 * @param name The name to use for the selected second.
314 * @return A select object with second options.
315 */
316 public static Select getSecondSelector(String name)
317 {
318 return(getSecondSelector(name, Calendar.getInstance()));
319 }
320
321 /***
322 * Return a second selector.
323 *
324 * @param name The name to use for the selected second.
325 * @param now Calendar to start with.
326 * @return A select object with second options.
327 */
328 public static Select getSecondSelector(String name, Calendar now)
329 {
330 return(getSecondSelector(name, Calendar.getInstance(),
331 DEFAULT_SECOND_INTERVAL));
332 }
333
334 /***
335 * Return a second selector.
336 *
337 * @param name The name to use for the selected second.
338 * @param now Calendar to start with.
339 * @param interval Interval between options.
340 * @return A select object with second options.
341 */
342 public static Select getSecondSelector(String name, Calendar now,
343 int interval)
344 {
345 Select secondSelect = new Select().setName(name);
346
347 for (int currentSecond = 0; currentSecond <= 59; currentSecond += interval)
348 {
349 Option o = new Option();
350 o.addElement(nbrFmt.format(currentSecond));
351 o.setValue(currentSecond);
352 int nearestSecond =
353 ((now.get(Calendar.SECOND) / interval) * interval);
354
355 if (nearestSecond == currentSecond)
356 {
357 o.setSelected(true);
358 }
359 secondSelect.addElement(o);
360 }
361 return(secondSelect);
362 }
363
364 /***
365 * Return a minute selector.
366 *
367 * @param name The name to use for the selected minute.
368 * @return A select object with minute options.
369 */
370 public static Select getMinuteSelector(String name)
371 {
372 return(getMinuteSelector(name, Calendar.getInstance()));
373 }
374
375 /***
376 * Return a minute selector.
377 *
378 * @param name The name to use for the selected minute.
379 * @return A select object with minute options.
380 */
381 public static Select getMinuteSelector(String name, Calendar now)
382 {
383 return(getMinuteSelector(name, now, DEFAULT_MINUTE_INTERVAL));
384 }
385
386 /***
387 * Return a minute selector.
388 *
389 * @param name The name to use for the selected minute.
390 * @param now Calendar to start with.
391 * @param interval Interval between options.
392 * @return A select object with minute options.
393 */
394 public static Select getMinuteSelector(String name, Calendar now,
395 int interval)
396 {
397 Select minuteSelect = new Select().setName(name);
398
399 for (int curMinute = 0;curMinute <= 59; curMinute += interval)
400 {
401 Option o = new Option();
402 o.addElement(nbrFmt.format(curMinute));
403 o.setValue(curMinute);
404 int nearestMinute =
405 ((now.get(Calendar.MINUTE)) / interval) * interval;
406
407 if (nearestMinute == curMinute)
408 {
409 o.setSelected(true);
410 }
411 minuteSelect.addElement(o);
412 }
413 return(minuteSelect);
414 }
415
416 /***
417 * Return an 12 hour selector.
418 *
419 * @param name The name to use for the selected hour.
420 * @return A select object with all the hours.
421 */
422 public static Select getHourSelector(String name)
423 {
424 return(getHourSelector(name, Calendar.getInstance()));
425 }
426
427 /***
428 * Return an 12 hour selector.
429 *
430 * @param name The name to use for the selected hour.
431 * @param now Calendar to start with.
432 * @return A select object with all the hours.
433 */
434 public static Select getHourSelector(String name, Calendar now)
435 {
436 return(getHourSelector(name, Calendar.getInstance(), TWELVE_HOUR));
437 }
438
439 /***
440 * Return an hour selector (either 12hr or 24hr depending on
441 * <code>format</code>.
442 *
443 * @param name The name to use for the selected hour.
444 * @param now Calendar to start with.
445 * @param format Time format.
446 * @return A select object with all the hours.
447 */
448 public static Select getHourSelector(String name, Calendar now, int format)
449 {
450 Select hourSelect = new Select().setName(name);
451
452 if (format == TWENTY_FOUR_HOUR)
453 {
454 for (int currentHour = 0; currentHour <= 23; currentHour++)
455 {
456 Option o = new Option();
457 o.addElement(nbrFmt.format(currentHour));
458 o.setValue(currentHour);
459 if (now.get(Calendar.HOUR_OF_DAY) == currentHour)
460 {
461 o.setSelected(true);
462 }
463 hourSelect.addElement(o);
464 }
465 }
466 else
467 {
468 for (int curHour = 1;curHour <= 12; curHour++)
469 {
470 Option o = new Option();
471
472 o.addElement(nbrFmt.format((long)curHour));
473 o.setValue(curHour);
474 if (now.get(Calendar.AM_PM) == Calendar.AM)
475 {
476 if (((now.get(Calendar.HOUR_OF_DAY)) == 0) &&
477 (curHour == 12))
478 {
479 o.setSelected(true);
480 }
481 else
482 {
483 if (now.get(Calendar.HOUR_OF_DAY) == curHour)
484 {
485 o.setSelected(true);
486 }
487 }
488 }
489 else
490 {
491 if (((now.get(Calendar.HOUR_OF_DAY)) == 12) &&
492 (curHour == 12))
493 {
494 o.setSelected(true);
495 }
496 else
497 {
498 if (now.get(Calendar.HOUR_OF_DAY) == curHour+12)
499 {
500 o.setSelected(true);
501 }
502 }
503 }
504 hourSelect.addElement(o);
505 }
506 }
507 return(hourSelect);
508 }
509
510 /***
511 * Return an am/pm selector.
512 *
513 * @param name The name to use for the selected am/pm.
514 * @return A select object with am/pm
515 */
516 public static Select getAMPMSelector(String name)
517 {
518 Calendar c = Calendar.getInstance();
519 c.setTime(new Date());
520 return(getAMPMSelector(name, c));
521 }
522
523 /***
524 * Return an am/pm selector.
525 *
526 * @param name The name to use for the selected am/pm.
527 * @param now Calendar to start with.
528 * @return A select object with am/pm.
529 */
530 public static Select getAMPMSelector(String name,
531 Calendar now)
532 {
533 Select ampmSelect = new Select().setName(name);
534
535 Option o = new Option();
536 o.addElement("am");
537 o.setValue(Calendar.AM);
538 if (now.get(Calendar.AM_PM) == Calendar.AM)
539 {
540 o.setSelected(true);
541 }
542 ampmSelect.addElement(o);
543
544 o = new Option();
545 o.addElement("pm");
546 o.setValue(Calendar.PM);
547 if (now.get(Calendar.AM_PM) == Calendar.PM)
548 {
549 o.setSelected(true);
550 }
551 ampmSelect.addElement(o);
552
553 return(ampmSelect);
554 }
555
556 /***
557 * Used to build the popupmenu in HTML. The properties set in the
558 * object are used to generate the correct HTML. The selName
559 * attribute is used to seed the names of the select lists. The
560 * names will be generated as follows:
561 *
562 * <ul>
563 * <li>selName + "_hour"</li>
564 * <li>selName + "_minute"</li>
565 * <li>selName + "_ampm"</li>
566 * </ul>
567 *
568 * If onChange was set it is also used in the generation of the
569 * output. The output HTML will list the select lists in the
570 * following order: hour minute ampm.
571 *
572 * If setShowSeconds(true) is used then an addition second select box
573 * is produced after the minute select box.
574 *
575 * If setTimeFormat(TimeSelector.TWENTY_FOUR_HOUR) is used then
576 * the ampm select box is omitted.
577 *
578 * @return A String with the correct HTML for the date selector.
579 */
580 public String output()
581 {
582 return(ecsOutput().toString());
583 }
584
585 /***
586 * Used to build the popupmenu in HTML. The properties set in the
587 * object are used to generate the correct HTML. The selName
588 * attribute is used to seed the names of the select lists. The
589 * names will be generated as follows:
590 *
591 * <ul>
592 * <li>selName + "_hour"</li>
593 * <li>selName + "_minute"</li>
594 * <li>selName + "_ampm"</li>
595 * </ul>
596 *
597 * If onChange was set it is also used in the generation of the
598 * output. The output HTML will list the select lists in the
599 * following order: hour minute ampm.
600 *
601 * If setShowSeconds(true) is used then an addition second select box
602 * is produced after the minute select box.
603 *
604 * If setTimeFormat(TimeSelector.TWENTY_FOUR_HOUR) is used then
605 * the ampm select box is omitted.
606 *
607 * @return A String with the correct HTML for the date selector.
608 */
609 public String toString()
610 {
611 return(ecsOutput().toString());
612 }
613
614 /***
615 * Return an ECS container with the select objects inside.
616 *
617 * @return An ECS container.
618 */
619 public ElementContainer ecsOutput()
620 {
621 if ( this.useDate == null )
622 {
623 this.useDate = Calendar.getInstance();
624 this.useDate.setTime ( new Date() );
625 }
626
627 ConcreteElement secondSelect = null;
628
629 Select ampmSelect = getAMPMSelector(selName + AMPM_SUFFIX, useDate);
630
631 Select hourSelect = getHourSelector(selName + HOUR_SUFFIX,
632 useDate, this.timeFormat);
633
634 Select minuteSelect = getMinuteSelector(selName + MINUTE_SUFFIX,
635 useDate, this.minuteInterval);
636
637 if (this.showSeconds)
638 {
639 Select tmp = getSecondSelector(selName + SECOND_SUFFIX, useDate,
640 this.secondInterval);
641 if (onChangeSet)
642 tmp.setOnChange(onChange);
643 secondSelect = tmp;
644 }
645 else
646 {
647 secondSelect = new Input(Input.hidden,
648 selName + SECOND_SUFFIX,
649 setSeconds);
650 }
651
652 if (onChangeSet)
653 {
654 hourSelect.setOnChange(onChange);
655 minuteSelect.setOnChange(onChange);
656 ampmSelect.setOnChange(onChange);
657 }
658
659 ElementContainer ec = new ElementContainer();
660 ec.addElement(new Comment(
661 "== BEGIN org.apache.turbine.util.TimeSelector.ecsOutput() =="));
662 ec.addElement(hourSelect);
663 ec.addElement(":");
664 ec.addElement(minuteSelect);
665 if (this.showSeconds == true)
666 ec.addElement(":");
667 ec.addElement(secondSelect);
668 if (this.timeFormat == this.TWELVE_HOUR)
669 {
670 ec.addElement(ampmSelect);
671 }
672 ec.addElement(new Comment(
673 "== END org.apache.turbine.util.TimeSelector.ecsOutput() =="));
674 return (ec);
675 }
676 }
This page was automatically generated by Maven