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.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$
97 */
98 public class DateSelector
99 {
100 /*** Prefix for date names. */
101 public static final String DEFAULT_PREFIX = "DateSelector";
102
103 /*** Suffix for day parameter. */
104 public static final String DAY_SUFFIX = "_day";
105
106 /*** Suffix for month parameter. */
107 public static final String MONTH_SUFFIX = "_month";
108
109 /*** Suffix for year parameter. */
110 public static final String YEAR_SUFFIX = "_year";
111
112 private Calendar useDate = null;
113 private String selName = null;
114 private static final String[] monthName =
115 new DateFormatSymbols().getMonths();
116 private String onChange = null;
117 private boolean onChangeSet = false;
118 private boolean showDays = true;
119 private int setDay = 0;
120
121
122 /***
123 * Constructor defaults to current date and uses the default
124 * prefix: <pre>DateSelector.DEFAULT</pre>
125 */
126 public DateSelector( )
127 {
128 this.selName = DEFAULT_PREFIX;
129 this.useDate = Calendar.getInstance();
130 this.useDate.setTime ( new Date() );
131 }
132
133 /***
134 * Constructor, uses the date set in a calendar that has been
135 * already passed in (with the date set correctly).
136 *
137 * @param selName A String with the selector name.
138 * @param useDate A Calendar with a date.
139 */
140 public DateSelector( String selName, Calendar useDate )
141 {
142 this.useDate = useDate;
143 this.selName = selName;
144 }
145
146 /***
147 * Constructor defaults to current date.
148 *
149 * @param selName A String with the selector name.
150 */
151 public DateSelector( String selName )
152 {
153 this.selName = selName;
154 this.useDate = Calendar.getInstance();
155 this.useDate.setTime ( new Date() );
156 }
157
158 /***
159 * Adds the onChange to all of <SELECT> tags. This is limited to
160 * one function for all three popups and is only used when the
161 * output() methods are used. Individual getMonth, getDay,
162 * getYear static methods will not use this setting.
163 *
164 * @param string A String to use for onChange attribute. If null,
165 * then nothing will be set.
166 * @return A DateSelector (self).
167 */
168 public DateSelector setOnChange ( String onChange )
169 {
170 if (onChange != null)
171 {
172 this.onChange = onChange;
173 this.onChangeSet = true;
174 }
175 else
176 {
177 this.onChange = null;
178 this.onChangeSet = false;
179 }
180 return this;
181 }
182
183 /***
184 * Select the day to be selected if the showDays(false) behavior
185 * is used. Individual getMonth, getDay, getYear static methods
186 * will not use this setting.
187 *
188 * @param day The day.
189 * @return A DateSelector (self).
190 */
191 public DateSelector setDay( int day )
192 {
193 this.setDay = day;
194 this.showDays = false;
195 return this;
196 }
197
198 /***
199 * Whether or not to show the days as a popup menu. The days will
200 * be a hidden parameter and the value set with setDay is used.
201 * Individual getMonth, getDay, getYear static methods will not
202 * use this setting.
203 *
204 * @param show True if the day should be shown.
205 * @return A DateSelector (self).
206 */
207 public DateSelector setShowDay ( boolean show )
208 {
209 this.showDays = false;
210 return this;
211 }
212
213 /***
214 * Set the selector name prefix. Individual getMonth, getDay,
215 * getYear static methods will not use this setting.
216 *
217 * @param selname A String with the select name prefix.
218 */
219 public void setSelName( String selName )
220 {
221 this.selName = selName;
222 }
223
224 /***
225 * Get the selector name prefix.
226 *
227 * @return A String with the select name prefix.
228 */
229 public String getSelName()
230 {
231 return selName;
232 }
233
234 /***
235 * Return a month selector.
236 *
237 * @param name The name to use for the selected month.
238 * @return A select object with all the months.
239 */
240 public static Select getMonthSelector(String name)
241 {
242 return(getMonthSelector(name, Calendar.getInstance()));
243 }
244
245 /***
246 * Return a month selector.
247 *
248 * Note: The values of the month placed into the select list are
249 * the month integers starting at 0 (ie: if the user selects
250 * February, the selected value will be 1).
251 *
252 * @param name The name to use for the selected month.
253 * @param now Calendar to start with.
254 * @return A select object with all the months.
255 */
256 public static Select getMonthSelector(String name,
257 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,
294 Calendar now)
295 {
296 Select daySelect = new Select().setName(name);
297
298 for(int currentDay=1; currentDay <= 31; currentDay++)
299 {
300 Option o = new Option();
301 o.addElement(Integer.toString(currentDay));
302 o.setValue(currentDay);
303 if (now.get(Calendar.DAY_OF_MONTH) == currentDay)
304 {
305 o.setSelected(true);
306 }
307 daySelect.addElement(o);
308 }
309 return(daySelect);
310 }
311
312 /***
313 * Return a year selector.
314 *
315 * @param name The name to use for the selected year.
316 * @return A select object with all the years starting five years
317 * from now and five years before this year.
318 */
319 public static Select getYearSelector(String name)
320 {
321 return(getYearSelector(name, Calendar.getInstance()));
322 }
323
324 /***
325 * Return a year selector.
326 *
327 * @param name The name to use for the selected year.
328 * @param now Calendar to start with.
329 * @return A select object with all the years starting five years
330 * from now and five years before this year.
331 */
332 public static Select getYearSelector(String name,
333 Calendar now)
334 {
335 Select yearSelect = new Select().setName(name);
336
337 int startYear = now.get(Calendar.YEAR);
338 for(int currentYear = startYear-5;
339 currentYear <= startYear+5;
340 currentYear++)
341 {
342 Option o = new Option();
343 o.addElement(Integer.toString(currentYear));
344 o.setValue(currentYear);
345 if (startYear == currentYear)
346 {
347 o.setSelected(true);
348 }
349 yearSelect.addElement(o);
350 }
351 return(yearSelect);
352 }
353
354 /***
355 * Used to build the popupmenu in HTML. The properties set in the
356 * object are used to generate the correct HTML. The selName
357 * attribute is used to seed the names of the select lists. The
358 * names will be generated as follows:
359 *
360 * <ul>
361 * <li>selName + "_month"</li>
362 * <li>selName + "_day"</li>
363 * <li>selName + "_year"</li>
364 * </ul>
365 *
366 * If onChange was set it is also used in the generation of the
367 * output. The output HTML will list the select lists in the
368 * following order: month day year.
369 *
370 * @return A String with the correct HTML for the date selector.
371 */
372 public String output()
373 {
374 return(ecsOutput().toString());
375 }
376
377 /***
378 * Used to build the popupmenu in HTML. The properties set in the
379 * object are used to generate the correct HTML. The selName
380 * attribute is used to seed the names of the select lists. The
381 * names will be generated as follows:
382 *
383 * <ul>
384 * <li>selName + "_month"</li>
385 * <li>selName + "_day"</li>
386 * <li>selName + "_year"</li>
387 * </ul>
388 *
389 * The output HTML will list the select lists in the following
390 * order: month day year.
391 *
392 * @return A String with the correct HTML for the date selector.
393 */
394 public String toString()
395 {
396 return(ecsOutput().toString());
397 }
398
399 /*
400 * Return an ECS container with the month, day, and year select
401 * objects inside.
402 *
403 * @return An ECS container.
404 */
405 public ElementContainer ecsOutput()
406 {
407 if ( this.useDate == null )
408 {
409 this.useDate.setTime ( new Date() );
410 }
411
412 Select monthSelect = getMonthSelector(selName + MONTH_SUFFIX, useDate);
413 ConcreteElement daySelect = null;
414 if (!showDays)
415 {
416 daySelect = new Input(Input.hidden,
417 selName + DAY_SUFFIX,
418 setDay);
419 }
420 else
421 {
422 Select tmp = getDaySelector(selName + DAY_SUFFIX, useDate);
423 if (onChangeSet)
424 tmp.setOnChange(onChange);
425 daySelect = tmp;
426 }
427 Select yearSelect = getYearSelector(selName + YEAR_SUFFIX, useDate);
428 if (onChangeSet)
429 {
430 monthSelect.setOnChange(onChange);
431 yearSelect.setOnChange(onChange);
432 }
433 ElementContainer ec = new ElementContainer();
434 ec.addElement(new Comment("== BEGIN org.apache.turbine.util.DateSelector.ecsOutput() =="));
435 ec.addElement(monthSelect);
436 ec.addElement(daySelect);
437 ec.addElement(yearSelect);
438 ec.addElement(new Comment("== END org.apache.turbine.util.DateSelector.ecsOutput() =="));
439 return (ec);
440 }
441 }
This page was automatically generated by Maven