1
2
3
4
5
6
7
8 package net.stff.ical;
9
10 import java.util.Calendar;
11 import java.util.ResourceBundle;
12 import java.util.Vector;
13
14 import net.stff.ical.beans.CalendarData;
15 import net.stff.ical.beans.CalendarDay;
16 import net.stff.ical.beans.IEvent;
17 import net.stff.ical.persistance.ICalDAO;
18
19 /***
20 * This class calculates all the data required by a given view
21 *
22 * @author buntekuh
23 *
24 *
25 */
26 public class View{
27 /***
28 * Calculates the data required by the month view
29 *
30 * @param data collates all data currently known
31 * @param user The name of the user logged in
32 */
33 public static void monthView(CalendarData data, String user){
34
35 Calendar calendar = data.getCalendar();
36
37
38 Calendar respectiveDay = Calendar.getInstance(data.getLocale());
39 respectiveDay.set(Calendar.HOUR_OF_DAY, 0);
40 respectiveDay.set(Calendar.MINUTE, 0);
41 respectiveDay.set(Calendar.SECOND, 0);
42
43
44 respectiveDay.setLenient(true);
45
46
47 int firstBox;
48
49
50
51 int currYear;
52 int currMonth;
53 int countingDays = 0;
54
55 currMonth = calendar.get(Calendar.MONTH);
56 currYear = calendar.get(Calendar.YEAR);
57
58 respectiveDay.set(currYear, currMonth, 1);
59 firstBox = respectiveDay.get(Calendar.DAY_OF_WEEK);
60 countingDays = -data.getDayOffset(firstBox) + 1;
61
62 int currDay = -11;
63 String tColor;
64 Vector weeks = new Vector(6);
65
66 while (true){
67 Vector days = new Vector(7);
68
69 for (int d = 0; d < 7; d++){
70 CalendarDay day;
71
72 respectiveDay.set(currYear, currMonth, countingDays);
73 currDay = respectiveDay.get(Calendar.DATE);
74
75 if (respectiveDay.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)){
76
77 if ((respectiveDay.get(Calendar.DATE) == data.getToday().get(Calendar.DATE)) && (respectiveDay.get(Calendar.MONTH) == data.getToday().get(Calendar.MONTH))
78 && (respectiveDay.get(Calendar.YEAR) == data.getToday().get(Calendar.YEAR))){
79
80 tColor = CalendarDay.TODAY;
81 }
82 else{
83 tColor = CalendarDay.THISMONTH;
84 }
85 }
86 else{
87
88 tColor = CalendarDay.ANOTHERMONTH;
89 }
90
91 day = new CalendarDay(tColor, currDay, countingDays, respectiveDay);
92 Calendar tomorrow= (Calendar)respectiveDay.clone();
93 tomorrow.add(Calendar.DATE, 1);
94 day.setEvents(data.getEventsForPeriod(respectiveDay.getTime(), tomorrow.getTime(), user, ICalDAO.ALLEVENTS));
95 days.add(day);
96 countingDays++;
97 }
98
99 weeks.addElement(days);
100
101 if (countingDays > calendar.getActualMaximum(Calendar.DAY_OF_MONTH)){
102 break;
103 }
104 }
105
106 data.setDays(weeks);
107 data.setMode(CalendarData.MONTHMODE);
108 }
109
110 /***
111 * Calculates the data required by the week view
112 *
113 * @param data collates all data currently known
114 * @param user The name of the user logged in
115 */
116 public static void weekView(CalendarData data, String user){
117
118 ResourceBundle bundle= ResourceBundle.getBundle("resources.application");
119 int starthour= Integer.parseInt(bundle.getString("ical.view.week.starthour"));
120 int startminute= Integer.parseInt(bundle.getString("ical.view.week.startminute"));
121 int resolution= Integer.parseInt(bundle.getString("ical.view.week.resolution"));
122 int iterations= Integer.parseInt(bundle.getString("ical.view.week.iterations"));
123 Calendar calendar = data.getCalendar();
124 Calendar respectiveDay = Calendar.getInstance(data.getLocale());
125 respectiveDay.setTime(IEvent.getStartOfDay(respectiveDay.getTime()).getTime());
126
127 respectiveDay.setLenient(true);
128
129 int firstBox;
130 int currYear;
131 int currMonth;
132 int countingDays = 0;
133
134 currMonth = calendar.get(Calendar.MONTH);
135 currYear = calendar.get(Calendar.YEAR);
136 respectiveDay.set(currYear, currMonth, 1);
137
138 firstBox = calendar.get(Calendar.DAY_OF_WEEK);
139 countingDays = -data.getDayOffset(firstBox) + calendar.get(Calendar.DATE);
140
141 int currDay = -11;
142 String tType;
143
144 Vector days = new Vector(7);
145
146 for (int d = 0; d < 7; d++){
147 CalendarDay day;
148
149 respectiveDay.set(currYear, currMonth, countingDays);
150 currDay = respectiveDay.get(Calendar.DATE);
151
152 if (respectiveDay.get(Calendar.MONTH) == data.getToday().get(Calendar.MONTH)){
153 if ((respectiveDay.get(Calendar.DATE) == data.getToday().get(Calendar.DATE)) && (respectiveDay.get(Calendar.MONTH) == data.getToday().get(Calendar.MONTH))
154 && (respectiveDay.get(Calendar.YEAR) == data.getToday().get(Calendar.YEAR))){
155
156 tType = CalendarDay.TODAY;
157 }
158 else{
159 tType = CalendarDay.THISMONTH;
160 }
161 }
162 else{
163
164 tType = CalendarDay.ANOTHERMONTH;
165 }
166
167 day = new CalendarDay(tType, currDay, countingDays, respectiveDay);
168 Calendar tomorrow= (Calendar)respectiveDay.clone();
169 tomorrow.add(Calendar.DATE, 1);
170 Calendar startCal= (Calendar)respectiveDay.clone();
171 startCal.set(Calendar.HOUR, starthour);
172 startCal.set(Calendar.MINUTE, startminute);
173 day.setAlldayEvents(data.getEventsForPeriod(respectiveDay.getTime(), tomorrow.getTime(), user, ICalDAO.ALLDAYEVENTS));
174 day.schedule(data.getEventsForPeriod(respectiveDay.getTime(), tomorrow.getTime(), user, ICalDAO.TIMEDEVENTS), startCal, iterations, resolution);
175
176 days.add(day);
177 countingDays++;
178 }
179
180 data.setDays(days);
181 data.setMode(CalendarData.WEEKMODE);
182
183 }
184
185 /***
186 * Calculates the data required by the day view
187 *
188 * @param data collates all data currently known
189 * @param user The name of the user logged in
190 */
191 public static void dayView(CalendarData data, String user){
192 CalendarDay day;
193 Calendar calendar = data.getCalendar();
194 calendar= IEvent.getStartOfDay(calendar.getTime());
195
196 ResourceBundle bundle= ResourceBundle.getBundle("resources.application");
197 int starthour= Integer.parseInt(bundle.getString("ical.view.day.starthour"));
198 int startminute= Integer.parseInt(bundle.getString("ical.view.day.startminute"));
199 int resolution= Integer.parseInt(bundle.getString("ical.view.day.resolution"));
200 int iterations= Integer.parseInt(bundle.getString("ical.view.day.iterations"));
201
202 day = new CalendarDay(CalendarDay.THISMONTH, calendar.get(Calendar.DATE), calendar.get(Calendar.DATE), calendar);
203 Calendar tomorrow= (Calendar)calendar.clone();
204 tomorrow.add(Calendar.DATE, 1);
205 Calendar startCal= (Calendar)calendar.clone();
206 startCal.set(Calendar.HOUR, starthour);
207 startCal.set(Calendar.MINUTE, startminute);
208 day.setAlldayEvents(data.getEventsForPeriod(calendar.getTime(), tomorrow.getTime(), user, ICalDAO.ALLDAYEVENTS));
209 day.schedule(data.getEventsForPeriod(calendar.getTime(), tomorrow.getTime(), user, ICalDAO.TIMEDEVENTS), startCal, iterations, resolution);
210
211 data.setDays(day);
212 data.setMode(CalendarData.DAYMODE);
213 }
214 }