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