001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.ui.navigation;
019:
020: import java.util.Calendar;
021: import java.util.GregorianCalendar;
022:
023: import javax.swing.JComponent;
024: import javax.swing.event.EventListenerList;
025:
026: import org.columba.calendar.model.DateRange;
027: import org.columba.calendar.model.api.IDateRange;
028: import org.columba.calendar.ui.navigation.api.DateRangeChangedEvent;
029: import org.columba.calendar.ui.navigation.api.ICalendarNavigationView;
030: import org.columba.calendar.ui.navigation.api.IDateRangeChangedListener;
031:
032: import com.miginfocom.calendar.datearea.DateArea;
033: import com.miginfocom.util.dates.BoundaryRounder;
034: import com.miginfocom.util.dates.DateChangeEvent;
035: import com.miginfocom.util.dates.DateChangeListener;
036: import com.miginfocom.util.dates.DateRangeI;
037: import com.miginfocom.util.dates.ImmutableDateRange;
038:
039: /**
040: * @author fdietz
041: *
042: */
043: public class NavigationController implements ICalendarNavigationView {
044:
045: public static final String MINI_CONTEXT = "mini";
046:
047: public static final int SELECTION_MODE_DAY = 0;
048:
049: public static final int SELECTION_MODE_WEEK = 1;
050:
051: public static final int SELECTION_MODE_WORK_WEEK = 2;
052:
053: public static final int SELECTION_MODE_MONTH = 3;
054:
055: private EventListenerList listenerList = new EventListenerList();
056:
057: private com.miginfocom.beans.DateAreaBean dateAreaBean;
058:
059: public NavigationController() {
060:
061: dateAreaBean = DateAreaBeanFactory.initDateArea();
062:
063: // enable selection
064: dateAreaBean.setSelectionType(DateArea.SELECTION_TYPE_NORMAL);
065:
066: Calendar today = Calendar.getInstance();
067: long startMillis = new GregorianCalendar(today
068: .get(java.util.Calendar.YEAR), 0, 0).getTimeInMillis();
069: long endMillis = new GregorianCalendar(today
070: .get(java.util.Calendar.YEAR), 12, 31)
071: .getTimeInMillis();
072: ImmutableDateRange dr = new ImmutableDateRange(startMillis,
073: endMillis, false, null, null);
074:
075: dateAreaBean.getDateArea().setVisibleDateRange(dr);
076: dateAreaBean.repaint();
077:
078: dateAreaBean.getDateArea().addDateChangeListener(
079: new DateChangeListener() {
080: public void dateRangeChanged(DateChangeEvent e) {
081: if (e.getType() == DateChangeEvent.SELECTED) {
082:
083: fireSelectionChanged(new DateRange(e
084: .getNewRange().getStartMillis(), e
085: .getNewRange().getEndMillis(false)));
086:
087: }
088: }
089: }, false);
090: }
091:
092: public JComponent getView() {
093: return dateAreaBean;
094: }
095:
096: /**
097: * Adds a listener.
098: */
099: public void addSelectionChangedListener(
100: IDateRangeChangedListener listener) {
101: listenerList.add(IDateRangeChangedListener.class, listener);
102: }
103:
104: /**
105: * Removes a previously registered listener.
106: */
107: public void removeSelectionChangedListener(
108: IDateRangeChangedListener listener) {
109: listenerList.remove(IDateRangeChangedListener.class, listener);
110: }
111:
112: /**
113: * Propagates an event to all registered listeners notifying them that this
114: * folder has been renamed.
115: */
116: public void fireSelectionChanged(IDateRange dateRange) {
117: DateRangeChangedEvent e = new DateRangeChangedEvent(this ,
118: dateRange);
119: // Guaranteed to return a non-null array
120: Object[] listeners = listenerList.getListenerList();
121:
122: // Process the listeners last to first, notifying
123: // those that are interested in this event
124: for (int i = listeners.length - 2; i >= 0; i -= 2) {
125: if (listeners[i] == IDateRangeChangedListener.class) {
126: ((IDateRangeChangedListener) listeners[i + 1])
127: .selectionChanged(e);
128: }
129: }
130: }
131:
132: public void setSelectionMode(int mode) {
133:
134: int selectionBoundary = -1;
135: int unitCount = -1;
136:
137: switch (mode) {
138: case SELECTION_MODE_DAY:
139: selectionBoundary = DateRangeI.RANGE_TYPE_DAY;
140: unitCount = 1;
141:
142: break;
143: case SELECTION_MODE_WEEK:
144: selectionBoundary = DateRangeI.RANGE_TYPE_WEEK;
145: unitCount = 1;
146:
147: break;
148: case SELECTION_MODE_WORK_WEEK:
149: selectionBoundary = DateRangeI.RANGE_TYPE_DAY;
150: unitCount = 5;
151:
152: break;
153: case SELECTION_MODE_MONTH:
154: selectionBoundary = DateRangeI.RANGE_TYPE_MONTH;
155: unitCount = 1;
156:
157: break;
158: }
159:
160: dateAreaBean.setSelectionBoundaryType(selectionBoundary);
161: dateAreaBean.getDateArea().setSelectionRounder(
162: new BoundaryRounder(selectionBoundary, true, true,
163: false, unitCount, unitCount, null));
164:
165: }
166:
167: }
|