001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/widgets/src/java/org/sakaiproject/jsf/model/DateModel.java $
003: * $Id: DateModel.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.model;
021:
022: import java.io.*;
023: import java.text.*;
024: import java.util.*;
025: import javax.faces.model.*;
026:
027: /**
028: *
029: * <p>Models Locale specific date chanracteristics.</p>
030: * <p>Includes localized slect lists.</p>
031: * <p>Copyright: Copyright Sakai (c) 2005</p>
032: * @author Ed Smiley
033: * @version $Id: DateModel.java 9278 2006-05-10 23:29:21Z ray@media.berkeley.edu $
034: */
035:
036: public class DateModel implements Serializable {
037: private static final Integer[] months;
038: private static final Integer[] days;
039: private static final Integer[] hours12;
040: private static final Integer[] hours24;
041: private static final Integer[] minutes;
042: private static final Integer[] seconds;
043: // workaround for dfs.getAmPmStrings() returning null!
044: private static final String[] ampm = { "AM", "PM" };
045:
046: // calls conventiece method to set up
047: static {
048: months = makeArray(12, false);
049: days = makeArray(31, false);
050: hours12 = makeArray(12, false);
051: hours24 = makeArray(24, false);
052: minutes = makeArray(60, true);
053: seconds = makeArray(60, true);
054: }
055:
056: private Locale locale;
057: private DateFormatSymbols dfs;
058:
059: public DateModel() {
060: locale = Locale.getDefault();
061: DateFormatSymbols dfs = new DateFormatSymbols(locale);
062:
063: }
064:
065: public DateModel(Locale locale) {
066: this .locale = locale;
067: DateFormatSymbols dfs = new DateFormatSymbols(locale);
068: }
069:
070: /**
071: * Array of year Integers starting from now - rangeBefore to now + rangeAfter
072: * @param rangeBefore int
073: * @param rangeAfter int
074: * @return Integer[]
075: */
076:
077: public Integer[] getYears(int rangeBefore, int rangeAfter) {
078: Date now = new Date();
079: Calendar cal = Calendar.getInstance(locale);
080: int currentYear = cal.get(Calendar.YEAR);
081: int startYear = currentYear - rangeBefore;
082: int noOfYears = rangeBefore + rangeAfter + 1;
083:
084: Integer[] years = new Integer[noOfYears];
085: for (int i = startYear, y = 0; i < startYear + noOfYears; i++, y++) {
086: years[y] = new Integer(startYear + y);
087: }
088:
089: return years;
090: }
091:
092: /**
093: * Localized array of month names
094: * @return String[]
095: */
096: public String[] getMonthNames() {
097: return dfs.getMonths();
098: }
099:
100: /**
101: * Localized array of short month strings
102: * @return String[]
103: */
104: public String[] getMonthShortNames() {
105: DateFormatSymbols dfs = new DateFormatSymbols(locale);
106: return dfs.getShortMonths();
107: }
108:
109: /**
110: * Localized array of day strings
111: * @return String[]
112: */
113: public String[] getDayNames() {
114: return dfs.getWeekdays();
115: }
116:
117: /**
118: * Localized array of short day strings
119: * @return String[]
120: */
121: public String[] getDayShortNames() {
122: return dfs.getShortWeekdays();
123: }
124:
125: /**
126: * (Should be) Localized array of {AM, PM}
127: * @todo localize
128: * Hardcoded workaround for dfs.getAmPmStrings() returning null!
129: * @return String[]
130: */
131: public String[] getAmPm() {
132: // return dfs.getAmPmStrings();
133: return ampm;
134: }
135:
136: /**
137: * Array of month Itegers, starting from 1
138: * @return Integer[]
139: */
140: public Integer[] getMonths() {
141: return months;
142: }
143:
144: /**
145: * Array of day of month Integers, starting from 1
146: * @return Integer[]
147: */
148: public Integer[] getDays() {
149: return days;
150: }
151:
152: /**
153: * Array of hour Integers, starting from 1
154: * @return Integer[]
155: */
156: public Integer[] getHours() {
157: return getHours(true);
158: }
159:
160: /**
161: * Array of hour Integers, starting from 1
162: * @param twentyFourHour, if true use 24 hour clock,
163: * if false use 12 hour clock
164: * @return Integer[]
165: */
166: public Integer[] getHours(boolean twentyFourHour) {
167: return twentyFourHour ? hours24 : hours12;
168: }
169:
170: /**
171: * Array of minute Integers, starting from 0
172: * @return Integer[]
173: */
174: public Integer[] getMinutes() {
175: return minutes;
176: }
177:
178: /**
179: * Array of seconds Integers, starting from 0
180: * @return Integer[]
181: */
182: public Integer[] getSeconds() {
183: return seconds;
184: }
185:
186: /**
187: * unit test
188: * @param args String[]
189: */
190: public static void main(String[] args) {
191: DateModel dateModel1 = new DateModel();
192: Integer[] ye = dateModel1.getYears(2, 2);
193: Integer[] mo = dateModel1.getMonths();
194: Integer[] da = dateModel1.getDays();
195: Integer[] h12 = dateModel1.getHours(false);
196: Integer[] h24 = dateModel1.getHours();
197: Integer[] mi = dateModel1.getMinutes();
198: Integer[] se = dateModel1.getSeconds();
199: String[] ap = dateModel1.getAmPm();
200: System.out.println("ye=" + ye);
201: System.out.println("mo=" + mo);
202: System.out.println("da=" + da);
203: for (int i = 0; i < ye.length; i++) {
204: System.out.println("year: " + ye[i]);
205: }
206: for (int i = 0; i < mo.length; i++) {
207: System.out.println("month: " + mo[i]);
208: }
209: for (int i = 0; i < da.length; i++) {
210: System.out.println("day: " + da[i]);
211: }
212: for (int i = 0; i < h12.length; i++) {
213: System.out.println("hour 12: " + h12[i]);
214: }
215: for (int i = 0; i < h24.length; i++) {
216: System.out.println("hour 24: " + h24[i]);
217: }
218: for (int i = 0; i < mi.length; i++) {
219: System.out.println("minutes: " + mi[i]);
220: }
221: for (int i = 0; i < se.length; i++) {
222: System.out.println("seconds: " + se[i]);
223: }
224: for (int i = 0; i < se.length; i++) {
225: String zs = dateModel1.zeroPad(se[i].toString());
226: System.out.println("zero pad seconds=" + zs);
227: }
228: for (int i = 0; i < ap.length; i++) {
229: System.out.println("am pm: " + ap[i]);
230: }
231:
232: // test select items
233: System.out.println("testing select items 12hr");
234: unitTestSelectItemList(dateModel1.get12HourSelectItems());
235: System.out.println("testing select items 24hr");
236: unitTestSelectItemList(dateModel1.get24HourSelectItems());
237: System.out.println("testing select items am pm");
238: unitTestSelectItemList(dateModel1.getAmPmSelectItems());
239: System.out.println("testing select items day");
240: unitTestSelectItemList(dateModel1.getDaySelectItems());
241: System.out.println("testing select items minute");
242: unitTestSelectItemList(dateModel1.getMinuteSelectItems());
243: System.out.println("testing select items month");
244: unitTestSelectItemList(dateModel1.getMonthSelectItems());
245: System.out.println("testing select items second");
246: unitTestSelectItemList(dateModel1.getSecondsSelectItems());
247: System.out.println("testing select items year");
248: unitTestSelectItemList(dateModel1.getYearSelectItems(2, 2));
249: }
250:
251: private static void unitTestSelectItemList(List list) {
252: for (Iterator iter = list.iterator(); iter.hasNext();) {
253: SelectItem item = (SelectItem) iter.next();
254: System.out.println("item.getLabel()=" + item.getLabel());
255: System.out.println("item.getValue()=" + item.getValue());
256: }
257: }
258:
259: /**
260: * Select list for range of years
261: * @param rangeBefore int
262: * @param rangeAfter int
263: * @return List of SelectItems
264: */
265: public List getYearSelectItems(int rangeBefore, int rangeAfter) {
266: List selectYears = new ArrayList();
267: Integer[] years = this .getYears(rangeBefore, rangeAfter);
268: for (int i = 0; i < years.length; i++) {
269: SelectItem selectYear = new SelectItem(years[i], years[i]
270: .toString());
271: selectYears.add(selectYear);
272: }
273: return selectYears;
274: }
275:
276: /**
277: * Select list for range of months
278: * @return List of SelectItems
279: */
280: public List getMonthSelectItems() {
281: List selectMonths = new ArrayList();
282: Integer[] m = this .getMonths();
283: for (int i = 0; i < m.length; i++) {
284: SelectItem selectMonth = new SelectItem(m[i], m[i]
285: .toString());
286: selectMonths.add(selectMonth);
287: }
288: return selectMonths;
289: }
290:
291: /**
292: * Select list for range of days
293: * @return List of SelectItems
294: */
295: public List getDaySelectItems() {
296: List selectDays = new ArrayList();
297: Integer[] d = this .getDays();
298: for (int i = 0; i < d.length; i++) {
299: SelectItem selectDay = new SelectItem(d[i], d[i].toString());
300: }
301: return selectDays;
302: }
303:
304: /**
305: * Select list for range of hours on 24 hour clock.
306: * @return List of SelectItems
307: */
308: public List get24HourSelectItems() {
309: List selectHours = new ArrayList();
310: Integer[] h = this .getHours();
311: for (int i = 0; i < h.length; i++) {
312: String hourStr = zeroPad(h[i].toString());
313: SelectItem selectHour = new SelectItem(h[i], hourStr);
314: selectHours.add(selectHour);
315: }
316: return selectHours;
317: }
318:
319: /**
320: * Select list for range of hours on 12 hour clock.
321: * Used in conjunction with AM/PM.
322: * @return List of SelectItem
323: */
324: public List get12HourSelectItems() {
325: List selectHours = new ArrayList();
326: Integer[] h = this .getHours(false);
327: for (int i = 0; i < h.length; i++) {
328: String hourStr = zeroPad(h[i].toString());
329: SelectItem selectHour = new SelectItem(h[i], hourStr);
330: selectHours.add(selectHour);
331: }
332: return selectHours;
333: }
334:
335: /**
336: * Localized AM/PM select list
337: * @return the List of SelectItems
338: */
339: public List getAmPmSelectItems() {
340: List ampmList = new ArrayList();
341: String[] ampm = getAmPm();
342: for (int i = 0; i < ampm.length; i++) {
343: ampmList.add(new SelectItem(new Integer(i), ampm[i]));
344: }
345: return ampmList;
346: }
347:
348: /**
349: * Select list for range of hours on 12 hour clock.
350: * Used in conjunction with AM/PM.
351: * @return List
352: */
353: public List getMinuteSelectItems() {
354: List selectMinutes = new ArrayList();
355: Integer[] m = this .getMinutes();
356: for (int i = 0; i < m.length; i++) {
357: String minStr = zeroPad(m[i].toString());
358: SelectItem selectHour = new SelectItem(m[i], minStr);
359: selectMinutes.add(selectHour);
360: }
361: return selectMinutes;
362: }
363:
364: /**
365: * Select list for range of hours on 12 hour clock.
366: * Used in conjunction with AM/PM.
367: * @return List
368: */
369: public List getSecondsSelectItems() {
370: List selectSeconds = new ArrayList();
371: Integer[] s = this .getSeconds();
372: for (int i = 0; i < s.length; i++) {
373: String secStr = zeroPad(s[i].toString());
374: SelectItem selectHour = new SelectItem(s[i], secStr);
375: selectSeconds.add(selectHour);
376: }
377: return selectSeconds;
378: }
379:
380: /**
381: * utility method
382: * @param entries
383: * @return
384: */
385: private static Integer[] makeArray(int entries, boolean zeroBase) {
386: Integer[] mk = new Integer[entries];
387: int incr = 1;
388: if (zeroBase) {
389: incr = 0;
390: }
391: for (int i = 0; i < entries; i++) {
392: mk[i] = new Integer(i + incr);
393: }
394: return mk;
395: }
396:
397: /**
398: * helper method
399: * @param str a string
400: * @return
401: */
402: private String zeroPad(String str) {
403: if (str == null) {
404: return "00";
405: }
406: if (str.length() < 2) {
407: str = "0" + str;
408: }
409: return str;
410: }
411:
412: }
|