001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013:
014: package org.rapla.components.calendar;
015:
016: import java.util.Calendar;
017: import java.util.ArrayList;
018: import java.util.Locale;
019: import java.util.Date;
020: import java.util.TimeZone;
021:
022: /** The model of the obligatory MVC approach is a wrapper arround an Calendar object.
023: */
024: final class TimeModel {
025: Calendar m_calendar;
026: Locale m_locale;
027:
028: ArrayList m_listenerList = new ArrayList();
029:
030: public TimeModel(Locale locale, TimeZone timeZone) {
031: m_locale = locale;
032: m_calendar = Calendar.getInstance(timeZone, m_locale);
033: trim(m_calendar);
034: m_calendar.setLenient(true);
035: }
036:
037: public void addDateChangeListener(DateChangeListener listener) {
038: m_listenerList.add(listener);
039: }
040:
041: public void removeDateChangeListener(DateChangeListener listener) {
042: m_listenerList.remove(listener);
043: }
044:
045: public Locale getLocale() {
046: return m_locale;
047: }
048:
049: public Date getTime() {
050: return m_calendar.getTime();
051: }
052:
053: // #TODO Property change listener for TimeZone
054: public void setTimeZone(TimeZone timeZone) {
055: m_calendar.setTimeZone(timeZone);
056: }
057:
058: public TimeZone getTimeZone() {
059: return m_calendar.getTimeZone();
060: }
061:
062: public void setTime(int hours, int minutes) {
063: m_calendar.set(Calendar.HOUR_OF_DAY, hours);
064: m_calendar.set(Calendar.MINUTE, minutes);
065: trim(m_calendar);
066: fireDateChanged();
067: }
068:
069: public void setTime(Date date) {
070: m_calendar.setTime(date);
071: trim(m_calendar);
072: fireDateChanged();
073: }
074:
075: public boolean sameTime(Date date) {
076: Calendar calendar = Calendar.getInstance(getTimeZone(),
077: getLocale());
078: calendar.setTime(date);
079: trim(calendar);
080: return calendar.getTime().equals(getTime());
081: }
082:
083: private void trim(Calendar calendar) {
084: calendar.set(Calendar.DATE, 0);
085: calendar.set(Calendar.MONTH, 0);
086: calendar.set(Calendar.YEAR, 0);
087: calendar.set(Calendar.SECOND, 0);
088: calendar.set(Calendar.MILLISECOND, 0);
089: }
090:
091: public DateChangeListener[] getDateChangeListeners() {
092: return (DateChangeListener[]) m_listenerList
093: .toArray(new DateChangeListener[] {});
094: }
095:
096: protected void fireDateChanged() {
097: DateChangeListener[] listeners = getDateChangeListeners();
098: DateChangeEvent evt = new DateChangeEvent(this , getTime());
099: for (int i = 0; i < listeners.length; i++) {
100: listeners[i].dateChanged(evt);
101: }
102: }
103: }
|