KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tags > calendar > CalendarTag


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.core.tags.calendar;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.struts.Globals;
25 import org.apache.struts.config.ModuleConfig;
26 import org.apache.struts.util.MessageResources;
27 import org.apache.struts.util.RequestUtils;
28 import org.apache.roller.ui.core.tags.HybridTag;
29 import org.apache.roller.util.DateUtil;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Calendar JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.jsp.JspException JavaDoc;
41 import javax.servlet.jsp.tagext.Tag JavaDoc;
42 import org.apache.roller.ui.core.RollerContext;
43
44
45 /**
46  * Calendar tag.
47  * @jsp.tag name="Calendar"
48  */

49 public class CalendarTag extends HybridTag {
50     private static Log mLogger =
51             LogFactory.getFactory().getInstance(CalendarTag.class);
52     
53     // JSP Attributes
54

55     /** @jsp.attribute required="true" */
56     public String JavaDoc getName() { return mName; }
57     public void setName( String JavaDoc name ) { mName = name; }
58     private String JavaDoc mName = null;
59     
60     /* @jsp.attribute description="Date in yyyyMMdd format"
61     public String getDate() { return mDate; }
62     public void setDate( String s ) { mDate = s; }
63     private String mDate = null;
64      */

65     
66     /** @jsp.attribute */
67     public String JavaDoc getModel() { return mModelName; }
68     public void setModel( String JavaDoc s ) { mModelName= s; }
69     private String JavaDoc mModelName = null;
70     
71     /** @jsp.attribute */
72     public String JavaDoc getClassSuffix() { return mClassSuffix; }
73     public void setClassSuffix( String JavaDoc s ) { mClassSuffix= s; }
74     private String JavaDoc mClassSuffix = "";
75     
76     // not a tag attribute
77
public void setLocale(Locale JavaDoc locale) {
78         if (locale != null)
79             mLocale = locale;
80     }
81     private Locale JavaDoc mLocale = Locale.getDefault();
82     
83     // not a tag attribute
84
/*
85     private TimeZone mTimeZone = TimeZone.getDefault();
86     public void setTimeZone(TimeZone zone) {
87         if (zone != null)
88             mTimeZone = zone;
89     }
90     private TimeZone getTimeZone()
91     {
92         // I've seen TimeZone.getDefault() return null. -Lance
93         if (mTimeZone == null)
94             mTimeZone = TimeZone.getTimeZone("America/New_York");
95         return mTimeZone;
96     }
97      */

98     
99     private String JavaDoc[] mDayNames = null;
100     
101     public CalendarTag() {
102         /*
103          * Empty constructor.
104          *
105          * Used to build the day names, but the correct locale
106          * was not set at this stage. Day-name-building has moved to the
107          * doStartTag() method.
108          */

109     }
110     
111     //------------------------------------------------------------------------
112
/**
113      * Write to a PrintWriter so that tag may be used from Velocity
114      */

115     public int doStartTag( PrintWriter JavaDoc pw ) throws JspException JavaDoc {
116         try {
117             // build week day names
118
this.buildDayNames();
119             
120             Date JavaDoc day=null; // day to be displayed
121
Calendar JavaDoc dayCal; // set to day to be displayed
122
Calendar JavaDoc cal; // for iterating through days of month
123
Calendar JavaDoc todayCal; // for iterating through days of month
124
CalendarModel model; // the calendar model
125

126             // ---------------------------------
127
// --- initialize date variables ---
128
// ---------------------------------
129

130             // check for parameter map and target url
131
StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(mModelName,".");
132             String JavaDoc tok1 = toker.nextToken();
133             if (toker.hasMoreTokens()) {
134                 String JavaDoc tok2 = toker.nextToken();
135                 Object JavaDoc bean = pageContext.findAttribute(tok1);
136                 model = (CalendarModel)PropertyUtils.getProperty(bean, tok2);
137             } else {
138                 model = (CalendarModel)pageContext.findAttribute( mModelName );
139             }
140             
141             // no model specified, nothing to generate
142
if (model == null) {
143                 return SKIP_BODY;
144             }
145             
146             day = model.getDay();
147             
148             // ceate object to represent today
149
todayCal = model.getCalendar();
150             todayCal.setTime( new Date JavaDoc() );
151             
152             // formatter Month-Year title of calendar
153
SimpleDateFormat JavaDoc formatTitle = new SimpleDateFormat JavaDoc("MMMM yyyy", mLocale);
154             
155             HttpServletRequest JavaDoc request =
156                     (HttpServletRequest JavaDoc)pageContext.getRequest();
157             
158             // get Resource Bundle
159
MessageResources resources = getResources(request);
160             
161             // go back to first day in month
162
cal = model.getCalendar();
163             day = DateUtil.getNoonOfDay(day, cal);
164             cal.set( Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH) );
165             
166             // go back to sunday before that: the first sunday in the calendar
167
while ( cal.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY ) {
168                 cal.add( Calendar.DATE, -1 );
169             }
170             
171             // create table of 5 weeks, 7 days per row
172
dayCal = model.getCalendar();
173             dayCal.setTime( day );
174             
175             // -------------------------
176
// --- draw the calendar ---
177
// -------------------------
178
pw.print("<table cellspacing=\"0\" border=\"0\" ");
179             pw.print(" summary=\""
180                     +resources.getMessage(mLocale, "calendar.summary")
181                     +"\" class=\"hCalendarTable"
182                     +mClassSuffix+"\">");
183             pw.print("<tr>");
184             pw.print("<td colspan=\"7\" align=\"center\" "+
185                     "class=\"hCalendarMonthYearRow"+mClassSuffix+"\">");
186             pw.print("<a HREF=\"" + model.computePrevMonthUrl()
187             + "\" title=\"" + resources.getMessage(mLocale, "calendar.prev")
188             + "\" class=\"hCalendarNavBar\">&laquo;</a> ");
189             pw.print( formatTitle.format(day) );
190             if (todayCal.getTime().compareTo(model.getNextMonth()) >= 0) {
191                 pw.print(" <a HREF=\"" + model.computeNextMonthUrl()
192                 + "\" title=\"" + resources.getMessage(mLocale, "calendar.next")
193                 + "\" class=\"hCalendarNavBar\">&raquo;</a>");
194             }
195             pw.print("</td></tr>");
196             
197             // emit the HTML calendar
198
for ( int w=-1; w<6; w++ ) {
199                 pw.print("<tr>");
200                 for ( int d=0; d<7; d++ ) {
201                     if ( w == -1 ) {
202                         pw.print(
203                                 "<th class=\"hCalendarDayNameRow"
204                                 +mClassSuffix+"\" align=\"center\">");
205                         pw.print( mDayNames[d] );
206                         pw.print("</th>");
207                         continue;
208                     }
209                     
210                     // determine URL for this calendar day
211
Date JavaDoc tddate = cal.getTime();
212                     String JavaDoc url = model.computeUrl(tddate, false, false);
213                     String JavaDoc content = model.getContent( tddate );
214                     
215                     if // day is in calendar month
216
((cal.get(Calendar.MONTH) == dayCal.get(Calendar.MONTH))
217                             && (cal.get(Calendar.YEAR) == dayCal.get(Calendar.YEAR))) {
218                         if // day is today then use today style
219
(( cal.get(Calendar.DAY_OF_MONTH)
220                                 == todayCal.get(Calendar.DAY_OF_MONTH))
221                                 && ( cal.get(Calendar.MONTH)
222                                 == todayCal.get(Calendar.MONTH))
223                                 && ( cal.get(Calendar.YEAR)
224                                 == todayCal.get(Calendar.YEAR))) {
225                             printToday(pw, cal, url, content);
226                         } else {
227                             printDayInThisMonth(pw, cal, url, content);
228                         }
229                     } else // apply day-not-in-month style ;-)
230
{
231                         printDayNotInMonth(pw, cal);
232                     }
233                     
234                     // increment calendar by one day
235
cal.add( Calendar.DATE, 1 );
236                 }
237                 pw.print("</tr>");
238             }
239             
240             pw.print("<tr class=\"hCalendarNextPrev"
241                     +mClassSuffix+"\">");
242             pw.print("<td colspan=\"7\" align=\"center\">");
243             
244             pw.print("<a HREF=\""+model.computeTodayMonthUrl()
245             +"\" class=\"hCalendarNavBar\">"
246                     +resources.getMessage(mLocale, "calendar.today")
247                     +"</a>");
248             
249             pw.print("</td>");
250             pw.print("</tr>");
251             
252             pw.print("</table>");
253         } catch (Exception JavaDoc e) {
254             pw.print("<span class=\"error\">");
255             pw.print("<p><b>An ERROR has occured CalendarTag</b></p>");
256             pw.print("</span>");
257             mLogger.error("Calendar tag exception",e);
258         }
259         return Tag.SKIP_BODY;
260     }
261     
262     private void printDayNotInMonth(PrintWriter JavaDoc pw, Calendar JavaDoc cal) {
263         pw.print("<td class=\"hCalendarDayNotInMonth"+mClassSuffix+"\">");
264         //pw.print(cal.get(Calendar.DAY_OF_MONTH));
265
pw.print("&nbsp;");
266         pw.print("</td>");
267     }
268     
269     private void printDayInThisMonth(PrintWriter JavaDoc pw, Calendar JavaDoc cal, String JavaDoc url, String JavaDoc content) {
270         if ( content!=null ) {
271             pw.print("<td class=\"hCalendarDayCurrent"
272                     +mClassSuffix+"\">");
273             pw.print( content );
274             pw.print("</td>");
275         } else if (url!=null) {
276             pw.print("<td class=\"hCalendarDayLinked"
277                     +mClassSuffix+"\">");
278             pw.print("<div class=\"hCalendarDayTitle"
279                     +mClassSuffix+"\">");
280             pw.print("<a HREF=\""+url+"\">");
281             pw.print(cal.get(Calendar.DAY_OF_MONTH));
282             pw.print("</a></div>");
283             pw.print("</td>");
284         } else {
285             pw.print("<td class=\"hCalendarDay"
286                     +mClassSuffix+"\">");
287             pw.print("<div class=\"hCalendarDayTitle"
288                     +mClassSuffix+"\">");
289             pw.print(cal.get(Calendar.DAY_OF_MONTH));
290             pw.print("</div>");
291             pw.print("</td>");
292         }
293     }
294     
295     private void printToday(PrintWriter JavaDoc pw, Calendar JavaDoc cal, String JavaDoc url, String JavaDoc content) {
296         if ( content!=null ) {
297             pw.print("<td class=\"hCalendarDayCurrent"
298                     +mClassSuffix+"\">");
299             pw.print( content );
300             pw.print("</td>");
301         } else if (url!=null) {
302             pw.print("<td class=\"hCalendarDayCurrent"
303                     +mClassSuffix+"\">");
304             pw.print("<a HREF=\""+url+"\" "
305                     +"class=\"hCalendarDayTitle"+mClassSuffix+"\">");
306             pw.print(cal.get(Calendar.DAY_OF_MONTH));
307             pw.print("</a>");
308             pw.print("</td>");
309         } else {
310             pw.print("<td class=\"hCalendarDayCurrent"
311                     +mClassSuffix+"\">");
312             pw.print("<div class=\"hCalendarDayTitle"
313                     +mClassSuffix+"\">");
314             pw.print(cal.get(Calendar.DAY_OF_MONTH));
315             pw.print("</div></td>");
316         }
317     }
318     private MessageResources getResources(HttpServletRequest JavaDoc request) {
319         ServletContext JavaDoc app = RollerContext.getServletContext();
320         ModuleConfig moduleConfig = RequestUtils.getModuleConfig(request, app);
321         return (MessageResources)app.getAttribute(Globals.MESSAGES_KEY +
322                 moduleConfig.getPrefix());
323     }
324     
325     /**
326      * Helper method to build the names of the weekdays. This
327      * used to take place in the <code>CalendarTag</code> constructor,
328      * but there, <code>mLocale</code> doesn't have the correct value yet.
329      */

330     private void buildDayNames() {
331         // build array of names of days of week
332
mDayNames = new String JavaDoc[7];
333         Calendar JavaDoc dayNameCal = Calendar.getInstance(mLocale);
334         SimpleDateFormat JavaDoc dayFormatter = new SimpleDateFormat JavaDoc("EEE", mLocale);
335         dayNameCal.set(Calendar.DAY_OF_WEEK, dayNameCal.getFirstDayOfWeek());
336         for (int dnum = 0; dnum < 7; dnum++) {
337             mDayNames[dnum] = dayFormatter.format(dayNameCal.getTime());
338             dayNameCal.add(Calendar.DATE, 1);
339         }
340     }
341     
342 }
343
344
Popular Tags