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