001: package org.apache.turbine.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.text.DateFormatSymbols;
023: import java.util.Calendar;
024: import java.util.Date;
025:
026: import org.apache.ecs.ConcreteElement;
027: import org.apache.ecs.ElementContainer;
028: import org.apache.ecs.html.Input;
029: import org.apache.ecs.html.Option;
030: import org.apache.ecs.html.Select;
031:
032: /**
033: * DateSelector is a utility class to handle the creation of a set of
034: * date popup menus. The code is broken into a set of static methods
035: * for quick and easy access to the individual select objects:
036: *
037: * <pre>
038: * ElementContainer ec dateSelect = new ElementContainer();
039: * String myName = "mydate";
040: * ec.addElement(DateSelector.getMonthSelector(myName));
041: * ec.addElement(DateSelector.getDaySelector(myName));
042: * ec.addElement(DateSelector.getYearSelector(myName));
043: * </pre>
044: *
045: * There are also methods which will use attributes to build a
046: * complete month,day,year selector:
047: *
048: * <pre>
049: * DateSelector ds = new DateSelector(myName);
050: * dateSelect = ds.ecsOutput();
051: * </pre>
052: *
053: * The above element container would use the onChange setting and may
054: * hide the selected day if set via showDays().<br>
055: *
056: * @author <a href="mailto:ekkerbj@netscape.net">Jeffrey D. Brekke</a>
057: * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
058: * @author <a href="mailto:leon@clearink.com">Leon Atkinson</a>
059: * @version $Id: DateSelector.java 534527 2007-05-02 16:10:59Z tv $
060: */
061: public class DateSelector {
062: /** Prefix for date names. */
063: public static final String DEFAULT_PREFIX = "DateSelector";
064:
065: /** Suffix for day parameter. */
066: public static final String DAY_SUFFIX = "_day";
067:
068: /** Suffix for month parameter. */
069: public static final String MONTH_SUFFIX = "_month";
070:
071: /** Suffix for year parameter. */
072: public static final String YEAR_SUFFIX = "_year";
073:
074: private Calendar useDate = null;
075: private String selName = null;
076: private static final String[] monthName = new DateFormatSymbols()
077: .getMonths();
078: private String onChange = null;
079: private boolean onChangeSet = false;
080: private boolean showDays = true;
081: private int setDay = 0;
082: private boolean useYears = false;
083: private int firstYear = 0;
084: private int lastYear = 0;
085: private int selectedYear = 0;
086:
087: /**
088: * Constructor defaults to current date and uses the default
089: * prefix: <pre>DateSelector.DEFAULT</pre>
090: */
091: public DateSelector() {
092: this .selName = DEFAULT_PREFIX;
093: this .useDate = Calendar.getInstance();
094: this .useDate.setTime(new Date());
095: }
096:
097: /**
098: * Constructor, uses the date set in a calendar that has been
099: * already passed in (with the date set correctly).
100: *
101: * @param selName A String with the selector name.
102: * @param useDate A Calendar with a date.
103: */
104: public DateSelector(String selName, Calendar useDate) {
105: this .useDate = useDate;
106: this .selName = selName;
107: }
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: this .selName = selName;
116: this .useDate = Calendar.getInstance();
117: this .useDate.setTime(new Date());
118: }
119:
120: /**
121: * Adds the onChange to all of <SELECT> tags. This is limited to
122: * one function for all three popups and is only used when the
123: * output() methods are used. Individual getMonth, getDay,
124: * getYear static methods will not use this setting.
125: *
126: * @param string A String to use for onChange attribute. If null,
127: * then nothing will be set.
128: * @return A DateSelector (self).
129: */
130: public DateSelector setOnChange(String onChange) {
131: if (onChange != null) {
132: this .onChange = onChange;
133: this .onChangeSet = true;
134: } else {
135: this .onChange = null;
136: this .onChangeSet = false;
137: }
138: return this ;
139: }
140:
141: /**
142: * Select the day to be selected if the showDays(false) behavior
143: * is used. Individual getMonth, getDay, getYear static methods
144: * will not use this setting.
145: *
146: * @param day The day.
147: * @return A DateSelector (self).
148: */
149: public DateSelector setDay(int day) {
150: this .setDay = day;
151: this .showDays = false;
152: return this ;
153: }
154:
155: /**
156: * Whether or not to show the days as a popup menu. The days will
157: * be a hidden parameter and the value set with setDay is used.
158: * Individual getMonth, getDay, getYear static methods will not
159: * use this setting.
160: *
161: * @param show True if the day should be shown.
162: * @return A DateSelector (self).
163: */
164: public DateSelector setShowDay(boolean show) {
165: this .showDays = false;
166: return this ;
167: }
168:
169: /**
170: * Set the selector name prefix. Individual getMonth, getDay,
171: * getYear static methods will not use this setting.
172: *
173: * @param selname A String with the select name prefix.
174: */
175: public void setSelName(String selName) {
176: this .selName = selName;
177: }
178:
179: /**
180: * Get the selector name prefix.
181: *
182: * @return A String with the select name prefix.
183: */
184: public String getSelName() {
185: return selName;
186: }
187:
188: /**
189: * Return a month selector.
190: *
191: * @param name The name to use for the selected month.
192: * @return A select object with all the months.
193: */
194: public static Select getMonthSelector(String name) {
195: return (getMonthSelector(name, Calendar.getInstance()));
196: }
197:
198: /**
199: * Return a month selector.
200: *
201: * Note: The values of the month placed into the select list are
202: * the month integers starting at 0 (ie: if the user selects
203: * February, the selected value will be 1).
204: *
205: * @param name The name to use for the selected month.
206: * @param now Calendar to start with.
207: * @return A select object with all the months.
208: */
209: public static Select getMonthSelector(String name, Calendar now) {
210: Select monthSelect = new Select().setName(name);
211:
212: for (int curMonth = 0; curMonth <= 11; curMonth++) {
213: Option o = new Option();
214: o.addElement(monthName[curMonth]);
215: o.setValue(curMonth);
216: if ((now.get(Calendar.MONTH)) == curMonth) {
217: o.setSelected(true);
218: }
219: monthSelect.addElement(o);
220: }
221: return (monthSelect);
222: }
223:
224: /**
225: * Return a day selector.
226: *
227: * @param name The name to use for the selected day.
228: * @return A select object with all the days in a month.
229: */
230: public static Select getDaySelector(String name) {
231: return (getDaySelector(name, Calendar.getInstance()));
232: }
233:
234: /**
235: * Return a day selector.
236: *
237: * @param name The name to use for the selected day.
238: * @param now Calendar to start with.
239: * @return A select object with all the days in a month.
240: */
241: public static Select getDaySelector(String name, Calendar now) {
242: Select daySelect = new Select().setName(name);
243:
244: for (int currentDay = 1; currentDay <= 31; currentDay++) {
245: Option o = new Option();
246: o.addElement(Integer.toString(currentDay));
247: o.setValue(currentDay);
248: if (now.get(Calendar.DAY_OF_MONTH) == currentDay) {
249: o.setSelected(true);
250: }
251: daySelect.addElement(o);
252: }
253: return (daySelect);
254: }
255:
256: /**
257: * Return a year selector.
258: *
259: * @param name The name to use for the selected year.
260: * @return A select object with all the years starting five years
261: * from now and five years before this year.
262: */
263: public static Select getYearSelector(String name) {
264: return (getYearSelector(name, Calendar.getInstance()));
265: }
266:
267: /**
268: * Return a year selector.
269: *
270: * @param name The name to use for the selected year.
271: * @param now Calendar to start with.
272: * @return A select object with all the years starting five years
273: * from now and five years before this year.
274: */
275: public static Select getYearSelector(String name, Calendar now) {
276: int startYear = now.get(Calendar.YEAR);
277: return (getYearSelector(name, startYear - 5, startYear + 5,
278: startYear));
279: }
280:
281: /**
282: * Return a year selector.
283: *
284: * @param name The name to use for the selected year.
285: * @param firstYear the first (earliest) year in the selector.
286: * @param lastYear the last (latest) year in the selector.
287: * @param selectedYear the year initially selected in the Select html.
288: * @return A select object with all the years from firstyear
289: * to lastyear..
290: */
291: public static Select getYearSelector(String name, int firstYear,
292: int lastYear, int selectedYear) {
293: Select yearSelect = new Select().setName(name);
294:
295: for (int currentYear = firstYear; currentYear <= lastYear;
296:
297: currentYear++) {
298: Option o = new Option();
299: o.addElement(Integer.toString(currentYear));
300: o.setValue(currentYear);
301: if (currentYear == selectedYear) {
302: o.setSelected(true);
303: }
304: yearSelect.addElement(o);
305: }
306: return (yearSelect);
307: }
308:
309: /**
310: * Select the day to be selected if the showDays(false) behavior
311: * is used. Individual getMonth, getDay, getYear static methods
312: * will not use this setting.
313: *
314: * @param day The day.
315: * @return A DateSelector (self).
316: */
317: public boolean setYear(int firstYear, int lastYear, int selectedYear) {
318: if (firstYear <= lastYear && firstYear <= selectedYear
319: && selectedYear <= lastYear) {
320: this .useYears = true;
321: this .firstYear = firstYear;
322: this .lastYear = lastYear;
323: this .selectedYear = selectedYear;
324: return true;
325: } else {
326: return false;
327: }
328: }
329:
330: /**
331: * Used to build the popupmenu in HTML. The properties set in the
332: * object are used to generate the correct HTML. The selName
333: * attribute is used to seed the names of the select lists. The
334: * names will be generated as follows:
335: *
336: * <ul>
337: * <li>selName + "_month"</li>
338: * <li>selName + "_day"</li>
339: * <li>selName + "_year"</li>
340: * </ul>
341: *
342: * If onChange was set it is also used in the generation of the
343: * output. The output HTML will list the select lists in the
344: * following order: month day year.
345: *
346: * @return A String with the correct HTML for the date selector.
347: */
348: public String output() {
349: return (ecsOutput().toString());
350: }
351:
352: /**
353: * Used to build the popupmenu in HTML. The properties set in the
354: * object are used to generate the correct HTML. The selName
355: * attribute is used to seed the names of the select lists. The
356: * names will be generated as follows:
357: *
358: * <ul>
359: * <li>selName + "_month"</li>
360: * <li>selName + "_day"</li>
361: * <li>selName + "_year"</li>
362: * </ul>
363: *
364: * The output HTML will list the select lists in the following
365: * order: month day year.
366: *
367: * @return A String with the correct HTML for the date selector.
368: */
369: public String toString() {
370: return (ecsOutput().toString());
371: }
372:
373: /*
374: * Return an ECS container with the month, day, and year select
375: * objects inside.
376: *
377: * @return An ECS container.
378: */
379: public ElementContainer ecsOutput() {
380: if (this .useDate == null) {
381: this .useDate.setTime(new Date());
382: }
383:
384: Select monthSelect = getMonthSelector(selName + MONTH_SUFFIX,
385: useDate);
386: ConcreteElement daySelect = null;
387: if (!showDays) {
388: daySelect = new Input(Input.hidden, selName + DAY_SUFFIX,
389: setDay);
390: } else {
391: Select tmp = getDaySelector(selName + DAY_SUFFIX, useDate);
392: if (onChangeSet) {
393: tmp.setOnChange(onChange);
394: }
395: daySelect = tmp;
396: }
397: Select yearSelect = null;
398: if (useYears) {
399: yearSelect = getYearSelector(selName + YEAR_SUFFIX,
400: firstYear, lastYear, selectedYear);
401: } else {
402: yearSelect = getYearSelector(selName + YEAR_SUFFIX, useDate);
403: }
404: if (onChangeSet) {
405: monthSelect.setOnChange(onChange);
406: yearSelect.setOnChange(onChange);
407: }
408: ElementContainer ec = new ElementContainer();
409: // ec.addElement(new Comment("== BEGIN org.apache.turbine.util.DateSelector.ecsOutput() =="));
410: ec.addElement(monthSelect);
411: ec.addElement(daySelect);
412: ec.addElement(yearSelect);
413: // ec.addElement(new Comment("== END org.apache.turbine.util.DateSelector.ecsOutput() =="));
414: return (ec);
415: }
416: }
|