KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > navigation > NavigationController


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.ui.navigation;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.GregorianCalendar JavaDoc;
22
23 import javax.swing.JComponent JavaDoc;
24 import javax.swing.event.EventListenerList JavaDoc;
25
26 import org.columba.calendar.model.DateRange;
27 import org.columba.calendar.model.api.IDateRange;
28 import org.columba.calendar.ui.navigation.api.DateRangeChangedEvent;
29 import org.columba.calendar.ui.navigation.api.ICalendarNavigationView;
30 import org.columba.calendar.ui.navigation.api.IDateRangeChangedListener;
31
32 import com.miginfocom.calendar.datearea.DateArea;
33 import com.miginfocom.util.dates.BoundaryRounder;
34 import com.miginfocom.util.dates.DateChangeEvent;
35 import com.miginfocom.util.dates.DateChangeListener;
36 import com.miginfocom.util.dates.DateRangeI;
37 import com.miginfocom.util.dates.ImmutableDateRange;
38
39 /**
40  * @author fdietz
41  *
42  */

43 public class NavigationController implements ICalendarNavigationView {
44
45     public static final String JavaDoc MINI_CONTEXT = "mini";
46
47     public static final int SELECTION_MODE_DAY = 0;
48
49     public static final int SELECTION_MODE_WEEK = 1;
50
51     public static final int SELECTION_MODE_WORK_WEEK = 2;
52
53     public static final int SELECTION_MODE_MONTH = 3;
54
55     private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
56
57     private com.miginfocom.beans.DateAreaBean dateAreaBean;
58
59     public NavigationController() {
60
61         dateAreaBean = DateAreaBeanFactory.initDateArea();
62
63         // enable selection
64
dateAreaBean.setSelectionType(DateArea.SELECTION_TYPE_NORMAL);
65
66         Calendar JavaDoc today = Calendar.getInstance();
67         long startMillis = new GregorianCalendar JavaDoc(today.get(java.util.Calendar.YEAR), 0, 0).getTimeInMillis();
68         long endMillis = new GregorianCalendar JavaDoc(today.get(java.util.Calendar.YEAR), 12, 31).getTimeInMillis();
69         ImmutableDateRange dr = new ImmutableDateRange(startMillis, endMillis,
70                 false, null, null);
71         
72         dateAreaBean.getDateArea().setVisibleDateRange(dr);
73         dateAreaBean.repaint();
74
75         dateAreaBean.getDateArea().addDateChangeListener(
76                 new DateChangeListener() {
77                     public void dateRangeChanged(DateChangeEvent e) {
78                         if (e.getType() == DateChangeEvent.SELECTED) {
79
80                             fireSelectionChanged(new DateRange(e.getNewRange()
81                                     .getStartMillis(), e.getNewRange()
82                                     .getEndMillis(false)));
83
84                         }
85                     }
86                 }, false);
87     }
88
89     public JComponent JavaDoc getView() {
90         return dateAreaBean;
91     }
92
93     /**
94      * Adds a listener.
95      */

96     public void addSelectionChangedListener(IDateRangeChangedListener listener) {
97         listenerList.add(IDateRangeChangedListener.class, listener);
98     }
99
100     /**
101      * Removes a previously registered listener.
102      */

103     public void removeSelectionChangedListener(
104             IDateRangeChangedListener listener) {
105         listenerList.remove(IDateRangeChangedListener.class, listener);
106     }
107
108     /**
109      * Propagates an event to all registered listeners notifying them that this
110      * folder has been renamed.
111      */

112     public void fireSelectionChanged(IDateRange dateRange) {
113         DateRangeChangedEvent e = new DateRangeChangedEvent(this, dateRange);
114         // Guaranteed to return a non-null array
115
Object JavaDoc[] listeners = listenerList.getListenerList();
116
117         // Process the listeners last to first, notifying
118
// those that are interested in this event
119
for (int i = listeners.length - 2; i >= 0; i -= 2) {
120             if (listeners[i] == IDateRangeChangedListener.class) {
121                 ((IDateRangeChangedListener) listeners[i + 1])
122                         .selectionChanged(e);
123             }
124         }
125     }
126
127     public void setSelectionMode(int mode) {
128
129         int selectionBoundary = -1;
130         int unitCount = -1;
131
132         switch (mode) {
133         case SELECTION_MODE_DAY:
134             selectionBoundary = DateRangeI.RANGE_TYPE_DAY;
135             unitCount = 1;
136
137             break;
138         case SELECTION_MODE_WEEK:
139             selectionBoundary = DateRangeI.RANGE_TYPE_WEEK;
140             unitCount = 1;
141
142             break;
143         case SELECTION_MODE_WORK_WEEK:
144             selectionBoundary = DateRangeI.RANGE_TYPE_DAY;
145             unitCount = 5;
146
147             break;
148         case SELECTION_MODE_MONTH:
149             selectionBoundary = DateRangeI.RANGE_TYPE_MONTH;
150             unitCount = 1;
151
152             break;
153         }
154
155         dateAreaBean.setSelectionBoundaryType(selectionBoundary);
156         dateAreaBean.getDateArea().setSelectionRounder(
157                 new BoundaryRounder(selectionBoundary, true, true, false,
158                         unitCount, unitCount, null));
159
160     }
161
162 }
Popular Tags