001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.calendar;
031:
032: import org.blojsom.util.BlojsomUtils;
033:
034: import java.text.DateFormatSymbols;
035: import java.util.Arrays;
036: import java.util.Calendar;
037: import java.util.Date;
038: import java.util.Locale;
039:
040: /**
041: * BlogCalendar
042: *
043: * @author David Czarnecki
044: * @author Mark Lussier
045: * @since blojsom 3.0
046: * @version $Id: BlogCalendar.java,v 1.2 2007/01/17 02:35:08 czarneckid Exp $
047: */
048: public class BlogCalendar {
049:
050: private Calendar _calendar;
051: private Calendar _today;
052: private DateFormatSymbols _symbols;
053: private Locale _locale;
054: private Boolean[] _dayswithentry;
055: private String[] _shortdownames;
056: private String _blogURL;
057: private int currentmonth;
058: private int currentyear;
059: private int currentday;
060: private String _requestedDateKey;
061:
062: /**
063: * Public Constructor
064: *
065: * @param calendar Caledar instance
066: * @param blogurl The blog's url for calendar navigation
067: */
068: public BlogCalendar(Calendar calendar, String blogurl) {
069: this (calendar, blogurl, Locale.getDefault());
070: }
071:
072: /**
073: * Public Constructor
074: *
075: * @param calendar Caledar instance
076: * @param blogurl The blog's url for calendar navigation
077: * @param locale Locale for the Calendar
078: */
079: public BlogCalendar(Calendar calendar, String blogurl, Locale locale) {
080: _locale = locale;
081: _calendar = calendar;
082: _today = Calendar.getInstance(_locale);
083: _today.setTime(new Date());
084: _symbols = new DateFormatSymbols(_locale);
085: _blogURL = blogurl;
086:
087: currentmonth = calendar.get(Calendar.MONTH);
088: currentyear = calendar.get(Calendar.YEAR);
089: currentday = calendar.get(Calendar.DAY_OF_MONTH);
090:
091: _dayswithentry = new Boolean[_calendar
092: .getActualMaximum(Calendar.DAY_OF_MONTH)];
093: Arrays.fill(_dayswithentry, Boolean.FALSE);
094:
095: _shortdownames = new String[7];
096: String[] downames = _symbols.getShortWeekdays();
097:
098: if (_calendar.getFirstDayOfWeek() == Calendar.SUNDAY) {
099: 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 getCaption() {
117: return BlojsomUtils
118: .getFormattedDate(_calendar.getTime(),
119: AbstractCalendarPlugin.BLOJSOM_CALENDAR_FORMAT,
120: _locale);
121: }
122:
123: /**
124: * Returns the day of the week for the 1st of the month occurs on
125: *
126: * @return the day of the week for the 1st of the month as an int
127: */
128: public int getFirstDayOfMonth() {
129: _calendar.set(Calendar.DAY_OF_MONTH, 1);
130: return _calendar.get(Calendar.DAY_OF_WEEK);
131: }
132:
133: /**
134: * Returns the number of days in the current month
135: *
136: * @return days in this month
137: */
138: public int getDaysInMonth() {
139: return _calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
140: }
141:
142: /**
143: * Flag a day in the current month as having entries
144: *
145: * @param dom the day of the month
146: */
147: public void setEntryForDOM(int dom) {
148: if (dom > 0 && dom <= _dayswithentry.length) {
149: _dayswithentry[dom - 1] = Boolean.valueOf(true);
150: }
151: }
152:
153: /**
154: * Flag a day in the current month as NOT having entries
155: *
156: * @param dom the day of the month
157: */
158: public void removeEntryForDOM(int dom) {
159: if (dom > 0 && dom <= _dayswithentry.length) {
160: _dayswithentry[dom - 1] = Boolean.valueOf(false);
161: }
162: }
163:
164: /**
165: * Determines if a day of the month has entries
166: *
167: * @param dom the day of the month
168: * @return a boolean indicating entries exist
169: */
170: public boolean dayHasEntry(int dom) {
171: boolean result = false;
172: if (dom > 0 && dom <= _dayswithentry.length) {
173: result = _dayswithentry[dom - 1].booleanValue();
174: }
175: return result;
176: }
177:
178: /**
179: * Get the array of day of month entry flags
180: *
181: * @return a boolean array of the months entries
182: */
183: public Boolean[] getEntryDates() {
184: return _dayswithentry;
185: }
186:
187: /**
188: * Get the localized name of the given month
189: *
190: * @param month the month (as defined by Calendar)
191: * @return the month as a localized string
192: */
193: public String getMonthName(int month) {
194: return getMonthNames()[month];
195: }
196:
197: /**
198: * Get a list of the month names (localized)
199: *
200: * @return String array of localized month names
201: */
202: public String[] getMonthNames() {
203: return _symbols.getMonths();
204: }
205:
206: /**
207: * Get the localized abbreviated name of the given month
208: *
209: * @param month the month (as defined by Calendar)
210: * @return the abbreviated month as a localized string (ex: Feb)
211: */
212: public String getShortMonthName(int month) {
213: return getShortMonthNames()[month];
214: }
215:
216: /**
217: * Get a list of the abbreviated month names (localized)
218: *
219: * @return String array of localized abbreviated month names
220: */
221: public String[] getShortMonthNames() {
222: return _symbols.getShortMonths();
223: }
224:
225: /**
226: * Get the localized name of a Day of the Week
227: *
228: * @param dow the day of the week (as defined by Calendar)
229: * @return the day of the week as a localized string
230: */
231: public String getDayOfWeekName(int dow) {
232: return getDayOfWeekNames()[dow];
233: }
234:
235: /**
236: * Get a lit of the day of the week names (localized)
237: *
238: * @return String array of localized Day of Week names
239: */
240: public String[] getDayOfWeekNames() {
241: return _symbols.getWeekdays();
242: }
243:
244: /**
245: * Get the localized abbreviated name of a Day of the Week
246: *
247: * @param dow the day of the week (as defined by Calendar)
248: * @return the abbreviated day of the week as a localized string
249: */
250: public String getShortDayOfWeekName(int dow) {
251: return _shortdownames[dow - 1];
252: }
253:
254: /**
255: * Get a lit of the abbreviated day of the week names (localized)
256: *
257: * @return String array of localized abbreviated Day of Week names
258: */
259: public String[] getShortDayOfWeekNames() {
260: return _shortdownames;
261: }
262:
263: /**
264: * Get the Blog URL used by the calendar
265: *
266: * @return the blog url
267: */
268: public String getCalendarUrl() {
269: return _blogURL;
270: }
271:
272: /**
273: * Get the Calendar instance
274: *
275: * @return Calendar instance
276: */
277: public Calendar getCalendar() {
278: return _calendar;
279: }
280:
281: /**
282: * Get today as a Calendar instance
283: *
284: * @return Calendar instance
285: */
286: public Calendar getToday() {
287: return _today;
288: }
289:
290: /**
291: * Gets the current month for this Calendar
292: *
293: * @return current month as an int (as defined by Calendar)
294: */
295: public int getCurrentMonth() {
296: return currentmonth;
297: }
298:
299: /**
300: * Sets the current month for this Calendar
301: *
302: * @param currentmonth current month as an int (as defined by Calendar)
303: */
304: public void setCurrentMonth(int currentmonth) {
305: this .currentmonth = currentmonth;
306: _calendar.set(Calendar.MONTH, currentmonth);
307: }
308:
309: /**
310: * Gets the current year for this Calendar
311: *
312: * @return current year as an int (as defined by Calendar)
313: */
314: public int getCurrentYear() {
315: return currentyear;
316:
317: }
318:
319: /**
320: * Sets the current year for this Calendar
321: *
322: * @param currentyear current year as an int (as defined by Calendar)
323: */
324: public void setCurrentYear(int currentyear) {
325: this .currentyear = currentyear;
326: _calendar.set(Calendar.YEAR, currentyear);
327: }
328:
329: /**
330: * Gets the current day for this Calendar
331: *
332: * @return current day as an int (as defined by Calendar)
333: */
334: public int getCurrentDay() {
335: return currentday;
336: }
337:
338: /**
339: * Sets the current day for this Calendar
340: *
341: * @param currentday current day as an int (as defined by Calendar)
342: */
343: public void setCurrentDay(int currentday) {
344: this .currentday = currentday;
345: _calendar.set(Calendar.DAY_OF_MONTH, currentday);
346: }
347:
348: /**
349: * Gets the current entry date match key (year+month+day)
350: *
351: * @return Date match key as a String
352: */
353: public String getRequestedDateKey() {
354: return _requestedDateKey;
355: }
356:
357: /**
358: * Sets the current entry date match key (year+month+day)
359: *
360: * @param requestedDateKey current entry match key
361: */
362: public void setRequestedDateKey(String requestedDateKey) {
363: _requestedDateKey = requestedDateKey;
364: }
365: }
|