KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > calendar > BlogCalendar


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.calendar;
32
33 import org.blojsom.util.BlojsomUtils;
34
35 import java.text.DateFormatSymbols JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import java.util.Calendar JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 /**
42  * BlogCalendar
43  *
44  * @author David Czarnecki
45  * @author Mark Lussier
46  * @since blojsom 3.0
47  * @version $Id: BlogCalendar.java,v 1.1 2006/03/20 21:30:53 czarneckid Exp $
48  */

49 public class BlogCalendar {
50
51     private Calendar JavaDoc _calendar;
52     private Calendar JavaDoc _today;
53     private DateFormatSymbols JavaDoc _symbols;
54     private Locale JavaDoc _locale;
55     private Boolean JavaDoc[] _dayswithentry;
56     private String JavaDoc[] _shortdownames;
57     private String JavaDoc _blogURL;
58     private int currentmonth;
59     private int currentyear;
60     private int currentday;
61     private String JavaDoc _requestedDateKey;
62
63     /**
64      * Public Constructor
65      *
66      * @param calendar Caledar instance
67      * @param blogurl The blog's url for calendar navigation
68      */

69     public BlogCalendar(Calendar JavaDoc calendar, String JavaDoc blogurl) {
70         this(calendar, blogurl, Locale.getDefault());
71     }
72
73     /**
74      * Public Constructor
75      *
76      * @param calendar Caledar instance
77      * @param blogurl The blog's url for calendar navigation
78      * @param locale Locale for the Calendar
79      */

80     public BlogCalendar(Calendar JavaDoc calendar, String JavaDoc blogurl, Locale JavaDoc locale) {
81         _locale = locale;
82         _calendar = calendar;
83         _today = Calendar.getInstance(_locale);
84         _today.setTime(new Date JavaDoc());
85         _symbols = new DateFormatSymbols JavaDoc(_locale);
86         _blogURL = blogurl;
87
88         currentmonth = calendar.get(Calendar.MONTH);
89         currentyear = calendar.get(Calendar.YEAR);
90         currentday = calendar.get(Calendar.DAY_OF_MONTH);
91
92         _dayswithentry = new Boolean JavaDoc[_calendar.getActualMaximum(Calendar.DAY_OF_MONTH)];
93         Arrays.fill(_dayswithentry, Boolean.FALSE);
94
95         _shortdownames = new String JavaDoc[7];
96         String JavaDoc[] downames = _symbols.getShortWeekdays();
97
98         if (_calendar.getFirstDayOfWeek() == Calendar.SUNDAY) {
99             for (int x = 0; x < _shortdownames.length; x++) {
100                 _shortdownames[x] = downames[x + 1];
101             }
102         } else {
103             for (int x = 2; x <= _shortdownames.length; x++) {
104                 _shortdownames[x - 2] = downames[x];
105             }
106
107             _shortdownames[6] = downames[1];
108         }
109     }
110
111     /**
112      * Returns the current Month as MMMMM yyyy (ex: March 2003)
113      *
114      * @return the current month and year as a string
115      */

116     public String JavaDoc getCaption() {
117         return BlojsomUtils.getFormattedDate(_calendar.getTime(), AbstractCalendarPlugin.BLOJSOM_CALENDAR_FORMAT, _locale);
118     }
119
120     /**
121      * Returns the day of the week for the 1st of the month occurs on
122      *
123      * @return the day of the week for the 1st of the month as an int
124      */

125     public int getFirstDayOfMonth() {
126         _calendar.set(Calendar.DAY_OF_MONTH, 1);
127         return _calendar.get(Calendar.DAY_OF_WEEK);
128     }
129
130     /**
131      * Returns the number of days in the current month
132      *
133      * @return days in this month
134      */

135     public int getDaysInMonth() {
136         return _calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
137     }
138
139     /**
140      * Flag a day in the current month as having entries
141      *
142      * @param dom the day of the month
143      */

144     public void setEntryForDOM(int dom) {
145         if (dom > 0 && dom <= _dayswithentry.length) {
146             _dayswithentry[dom - 1] = Boolean.valueOf(true);
147         }
148     }
149
150     /**
151      * Flag a day in the current month as NOT having entries
152      *
153      * @param dom the day of the month
154      */

155     public void removeEntryForDOM(int dom) {
156         if (dom > 0 && dom <= _dayswithentry.length) {
157             _dayswithentry[dom - 1] = Boolean.valueOf(false);
158         }
159     }
160
161     /**
162      * Determines if a day of the month has entries
163      *
164      * @param dom the day of the month
165      * @return a boolean indicating entries exist
166      */

167     public boolean dayHasEntry(int dom) {
168         boolean result = false;
169         if (dom > 0 && dom <= _dayswithentry.length) {
170             result = _dayswithentry[dom - 1].booleanValue();
171         }
172         return result;
173     }
174
175     /**
176      * Get the array of day of month entry flags
177      *
178      * @return a boolean array of the months entries
179      */

180     public Boolean JavaDoc[] getEntryDates() {
181         return _dayswithentry;
182     }
183
184     /**
185      * Get the localized name of the given month
186      *
187      * @param month the month (as defined by Calendar)
188      * @return the month as a localized string
189      */

190     public String JavaDoc getMonthName(int month) {
191         return getMonthNames()[month];
192     }
193
194     /**
195      * Get a list of the month names (localized)
196      *
197      * @return String array of localized month names
198      */

199     public String JavaDoc[] getMonthNames() {
200         return _symbols.getMonths();
201     }
202
203     /**
204      * Get the localized abbreviated name of the given month
205      *
206      * @param month the month (as defined by Calendar)
207      * @return the abbreviated month as a localized string (ex: Feb)
208      */

209     public String JavaDoc getShortMonthName(int month) {
210         return getShortMonthNames()[month];
211     }
212
213     /**
214      * Get a list of the abbreviated month names (localized)
215      *
216      * @return String array of localized abbreviated month names
217      */

218     public String JavaDoc[] getShortMonthNames() {
219         return _symbols.getShortMonths();
220     }
221
222     /**
223      * Get the localized name of a Day of the Week
224      *
225      * @param dow the day of the week (as defined by Calendar)
226      * @return the day of the week as a localized string
227      */

228     public String JavaDoc getDayOfWeekName(int dow) {
229         return getDayOfWeekNames()[dow];
230     }
231
232     /**
233      * Get a lit of the day of the week names (localized)
234      *
235      * @return String array of localized Day of Week names
236      */

237     public String JavaDoc[] getDayOfWeekNames() {
238         return _symbols.getWeekdays();
239     }
240
241     /**
242      * Get the localized abbreviated name of a Day of the Week
243      *
244      * @param dow the day of the week (as defined by Calendar)
245      * @return the abbreviated day of the week as a localized string
246      */

247     public String JavaDoc getShortDayOfWeekName(int dow) {
248         return _shortdownames[dow - 1];
249     }
250
251     /**
252      * Get a lit of the abbreviated day of the week names (localized)
253      *
254      * @return String array of localized abbreviated Day of Week names
255      */

256     public String JavaDoc[] getShortDayOfWeekNames() {
257         return _shortdownames;
258     }
259
260     /**
261      * Get the Blog URL used by the calendar
262      *
263      * @return the blog url
264      */

265     public String JavaDoc getCalendarUrl() {
266         return _blogURL;
267     }
268
269     /**
270      * Get the Calendar instance
271      *
272      * @return Calendar instance
273      */

274     public Calendar JavaDoc getCalendar() {
275         return _calendar;
276     }
277
278     /**
279      * Get today as a Calendar instance
280      *
281      * @return Calendar instance
282      */

283     public Calendar JavaDoc getToday() {
284         return _today;
285     }
286
287     /**
288      * Gets the current month for this Calendar
289      *
290      * @return current month as an int (as defined by Calendar)
291      */

292     public int getCurrentMonth() {
293         return currentmonth;
294     }
295
296     /**
297      * Sets the current month for this Calendar
298      *
299      * @param currentmonth current month as an int (as defined by Calendar)
300      */

301     public void setCurrentMonth(int currentmonth) {
302         this.currentmonth = currentmonth;
303         _calendar.set(Calendar.MONTH, currentmonth);
304     }
305
306     /**
307      * Gets the current year for this Calendar
308      *
309      * @return current year as an int (as defined by Calendar)
310      */

311     public int getCurrentYear() {
312         return currentyear;
313
314     }
315
316     /**
317      * Sets the current year for this Calendar
318      *
319      * @param currentyear current year as an int (as defined by Calendar)
320      */

321     public void setCurrentYear(int currentyear) {
322         this.currentyear = currentyear;
323         _calendar.set(Calendar.YEAR, currentyear);
324     }
325
326     /**
327      * Gets the current day for this Calendar
328      *
329      * @return current day as an int (as defined by Calendar)
330      */

331     public int getCurrentDay() {
332         return currentday;
333     }
334
335     /**
336      * Sets the current day for this Calendar
337      *
338      * @param currentday current day as an int (as defined by Calendar)
339      */

340     public void setCurrentDay(int currentday) {
341         this.currentday = currentday;
342         _calendar.set(Calendar.DAY_OF_MONTH, currentday);
343     }
344
345     /**
346      * Gets the current entry date match key (year+month+day)
347      *
348      * @return Date match key as a String
349      */

350     public String JavaDoc getRequestedDateKey() {
351         return _requestedDateKey;
352     }
353
354     /**
355      * Sets the current entry date match key (year+month+day)
356      *
357      * @param requestedDateKey current entry match key
358      */

359     public void setRequestedDateKey(String JavaDoc requestedDateKey) {
360         _requestedDateKey = requestedDateKey;
361     }
362 }
363
364
Popular Tags