View Javadoc

1   /***
2    * Calendar Tag Library
3    * Copyright (C) 2005 James Smith
4    * 
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   * You should be able to download a copy of the LGPL from
19   * www.opensource.org/licenses/lgpl-license.php
20   */
21  package org.calendartag.decorator;
22  
23  import javax.servlet.jsp.PageContext;
24  import java.util.Calendar;
25  
26  /***
27   * @author James Smith
28   * @see org.calendartag.tags.CalendarTag org.calendartag.decorators.CalendarDecorator
29   * The default implementation of <code>CalendarDecorator</code> used when a decorator is not defined.
30   * @since Aug 23, 2004
31   *
32   */
33  public class DefaultCalendarDecorator implements CalendarDecorator {
34  
35      private final static String ACTIVE_DAY_STYLE = "calendarActiveDayStyle";
36      private final static String ODD_MONTH_STYLE = "calendarOddMonthStyle";
37      private final static String EVEN_MONTH_STYLE = "calendarEvenMonthStyle";
38      
39      protected String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
40      protected String[] days = {"", "S", "M", "T", "W", "T", "F", "S"};
41  
42      protected PageContext pageContext;
43      protected Calendar calendar;
44      protected Calendar start;
45      protected Calendar end;
46  
47      /***
48       * Returns an appropriate style class depending on whether the day is selected or unselected.  If the day is selected
49       * calendarActiveDayStyle is returned.  If they day is not selected either calendarOddMonthStyle or calendarEvenMonthStyle
50       * is returned depending on <code>CalendarTag</code>'s internal record.  By default the month is considered odd.
51       * @see org.calendartag.decorator.CalendarDecorator#getDayStyleClass(boolean, boolean)
52       */
53      public String getDayStyleClass(boolean isOddMonth, boolean isSelectedDay) {
54          if (isSelectedDay) {
55              return ACTIVE_DAY_STYLE;
56          } else if(isOddMonth) {
57              return ODD_MONTH_STYLE;
58          } else {
59              return EVEN_MONTH_STYLE;
60          }
61      }
62  
63      /***
64       * Empty implementation
65       * @see org.calendartag.decorator.CalendarDecorator#initializeCalendar()
66       */
67      public void initializeCalendar() {
68          // nothing done in default calendar decorator
69      }
70      
71      /***
72       * Returns a "<<" hyperlink using the passed url
73       * @see org.calendartag.decorator.CalendarDecorator#getPreviousLink(java.lang.String)
74       */
75      public String getPreviousLink(String url) {
76          return "<a href=\"" + url + "\"><<</a>";
77      }
78  
79      /***
80       * Returns a ">>" hyperlink using the passed url
81       * @see org.calendartag.decorator.CalendarDecorator#getNextLink(java.lang.String)
82       */
83      public String getNextLink(String url) {
84          return "<a href=\"" + url + "\">>></a>";
85      }
86  
87      /***
88       * Returns the calendar's title using the month names and years.  It uses the protected months
89       * array to generate text for each month indexed from 0 to 11
90       * @see org.calendartag.decorator.CalendarDecorator#getCalendarTitle()
91       */
92      public String getCalendarTitle() {
93  
94          if (start.get(Calendar.MONTH) == end.get(Calendar.MONTH) &&
95                  start.get(Calendar.YEAR) == end.get(Calendar.YEAR)) {
96              return months[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.YEAR);
97          } else {
98              if (start.get(Calendar.YEAR) == end.get(Calendar.YEAR)) {
99                  return months[start.get(Calendar.MONTH)] + " - " +
100                         months[end.get(Calendar.MONTH)] + " " +
101                         calendar.get(Calendar.YEAR);
102             } else {
103                 return months[start.get(Calendar.MONTH)] + " " +
104                         start.get(Calendar.YEAR) + " - " +
105                         months[end.get(Calendar.MONTH)] + " " +
106                         calendar.get(Calendar.YEAR);
107             }
108         }
109 
110     }
111 
112     /***
113      * Returns the weekday title as assigned to the protected days array.  By default the array
114      * consists of S, M, T, W, T, F, S and is protected so it can be easily overridden by your
115      * implementation.  The array is index from 1 to 7
116      * @see org.calendartag.decorator.CalendarDecorator#getWeekdayTitle(int)
117      */
118     public String getWeekdayTitle(int day) {
119         return days[day];
120     }
121 
122     /***
123      * Returns empty content
124      * @see org.calendartag.decorator.CalendarDecorator#getEmptyDay()
125      */
126     public String getEmptyDay() {
127         return "";
128     }
129 
130     /***
131      * Returns a string representing the day of the month.  If the calendar's start and end range span
132      * multiple months it also returns the month of the year.  The date is a hyperlink using the url passed
133      * @see org.calendartag.decorator.CalendarDecorator#getDay(java.lang.String)
134      */
135     public String getDay(String url) {
136         if (calendar.get(Calendar.DATE) != 1 ||
137                 (start.get(Calendar.MONTH) == end.get(Calendar.MONTH) &&
138                 start.get(Calendar.YEAR) == end.get(Calendar.YEAR))) {
139             return "<a href=\"" + url + "\">" +
140                     calendar.get(Calendar.DATE) + "</a>";
141         } else {
142             return "<a href=\"" + url + "\">" +
143                     calendar.get(Calendar.DATE) + "</a> " +
144                     "<i>" + months[calendar.get(Calendar.MONTH)] + "</i>";
145         }
146     }
147 
148     /***
149      * Sets the page context to the variable pageContext
150      * @see org.calendartag.decorator.CalendarDecorator#setPageContext(javax.servlet.jsp.PageContext)
151      */
152     public void setPageContext(PageContext pageContext) {
153         this.pageContext = pageContext;
154     }
155 
156     /***
157      * Sets the calendar to the variable calendar
158      * @see org.calendartag.decorator.CalendarDecorator#setCalendar(java.util.Calendar)
159      */
160     public void setCalendar(Calendar calendar) {
161         this.calendar = calendar;
162     }
163     
164     /*** 
165      * Sets the start calendar to the variable start
166      * @see org.calendartag.decorator.CalendarDecorator#setStart(java.util.Calendar)
167      */
168     public void setStart(Calendar start) {
169         this.start = start;
170     }
171 
172     /***
173      * Sets the end calendar to the variable end
174      * @see org.calendartag.decorator.CalendarDecorator#setEnd(java.util.Calendar)
175      */
176     public void setEnd(Calendar end) {
177         this.end = end;
178     }
179 
180 }
181