KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Calendar JavaDoc;
36
37 /**
38  * VelocityHelper is a class used to help render a visual calendar using the VTL.
39  *
40  * @author David Czarnecki
41  * @author Mark Lussier
42  * @since blojsom 3.0
43  * @version $Id: VelocityHelper.java,v 1.1 2006/03/20 21:30:53 czarneckid Exp $
44  */

45 public class VelocityHelper {
46
47     private BlogCalendar _calendar;
48
49     // [Row][Col]
50
private String JavaDoc[][] visualcalendar = new String JavaDoc[6][7];
51
52     private static final String JavaDoc VTL_SPACER = " ";
53
54     private String JavaDoc HREF_PREFIX = "<a HREF=\"";
55     private String JavaDoc HREF_SUFFIX = "</a>";
56     private String JavaDoc _today = "Today";
57
58     /**
59      * Public Constructor
60      */

61     public VelocityHelper() {
62     }
63
64     /**
65      * Public Constructor
66      *
67      * @param calendar BlogCalendar to render
68      */

69     public VelocityHelper(BlogCalendar calendar) {
70         _calendar = calendar;
71     }
72
73     /**
74      * Sets the BlogCalendar to render
75      *
76      * @param calendar BlogCalendar
77      */

78     public void setCalendar(BlogCalendar calendar) {
79         _calendar = calendar;
80     }
81
82     /**
83      * Retrieve the {@link BlogCalendar} object used to construct this object
84      *
85      * @return {@link BlogCalendar}
86      */

87     public BlogCalendar getBlogCalendar() {
88         return _calendar;
89     }
90
91     /**
92      * Builds the visual calendar model
93      */

94     public void buildCalendar() {
95         int fdow = _calendar.getFirstDayOfMonth() - _calendar.getCalendar().getFirstDayOfWeek();
96         if (fdow == -1) {
97             fdow = 6;
98         }
99         int ldom = _calendar.getDaysInMonth();
100         int dowoffset = 0;
101         for (int x = 0; x < 6; x++) {
102             for (int y = 0; y < 7; y++) {
103                 if ((x == 0 && y < fdow) || (dowoffset >= ldom)) {
104                     visualcalendar[x][y] = VTL_SPACER;
105                 } else {
106                     dowoffset += 1;
107                     if (!_calendar.dayHasEntry(dowoffset)) {
108                         visualcalendar[x][y] = new Integer JavaDoc(dowoffset).toString();
109                     } else {
110                         StringBuffer JavaDoc _url = new StringBuffer JavaDoc(HREF_PREFIX);
111                         String JavaDoc _calurl = BlojsomUtils.getCalendarNavigationUrl(_calendar.getCalendarUrl(), (_calendar.getCurrentMonth() + 1), dowoffset, _calendar.getCurrentYear());
112                         _url.append(_calurl);
113                         _url.append("\"><span>").append(dowoffset).append("</span>").append(HREF_SUFFIX);
114                         visualcalendar[x][y] = _url.toString();
115                     }
116
117                 }
118             }
119         }
120     }
121
122     /**
123      * Get the visual content for a given calendar row. If <code>clazz</code> is null, no <code>class</code> attribute
124      * will be included in the &lt;td&gt; tag.
125      *
126      * @param row the row
127      * @return the visual calendar row
128      */

129     public String JavaDoc getCalendarRow(int row) {
130         return getCalendarRow(row, null);
131     }
132
133     /**
134      * Get the visual content for a given calendar row. If <code>clazz</code> is null, no <code>class</code> attribute
135      * will be included in the &lt;td&gt; tag.
136      *
137      * @param row the row
138      * @param clazz the css style apply
139      * @return the visual calendar row
140      */

141     public String JavaDoc getCalendarRow(int row, String JavaDoc clazz) {
142         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
143         if (row > 0 && row <= visualcalendar.length) {
144             for (int x = 0; x < 7; x++) {
145                 if (clazz != null) {
146                     result.append("<td class=\"").append(clazz).append("\">").append(visualcalendar[row - 1][x]).append("</td>");
147                 } else {
148                     result.append("<td>").append(visualcalendar[row - 1][x]).append("</td>");
149                 }
150             }
151         }
152         return result.toString();
153     }
154
155     /**
156      * Get the visual control for navigating to Today
157      *
158      * @return the today navigation control
159      */

160     public String JavaDoc getToday() {
161         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
162         result.append(HREF_PREFIX).append(_calendar.getCalendarUrl()).append("\">").append(_calendar.getShortMonthName(_calendar.getCurrentMonth())).append(HREF_SUFFIX);
163         return result.toString();
164     }
165
166     /**
167      * Get the visual control for navigating to the previous month
168      *
169      * @return the previous month navigation control
170      */

171     public String JavaDoc getPreviousMonth() {
172         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
173         _calendar.getCalendar().add(Calendar.MONTH, -1);
174         result.append(HREF_PREFIX);
175         String JavaDoc prevurl = BlojsomUtils.getCalendarNavigationUrl(_calendar.getCalendarUrl(),
176                 (_calendar.getCalendar().get(Calendar.MONTH) + 1),
177                 -1, _calendar.getCalendar().get(Calendar.YEAR));
178         result.append(prevurl);
179         result.append("\"> &lt;").append(VTL_SPACER).append(VTL_SPACER);
180         result.append(_calendar.getShortMonthName(_calendar.getCalendar().get(Calendar.MONTH)));
181         result.append(HREF_SUFFIX);
182         _calendar.getCalendar().add(Calendar.MONTH, 1);
183         return result.toString();
184     }
185
186     /**
187      * Get the visual control for navigating to the next month
188      *
189      * @return the next month navigation control
190      */

191     public String JavaDoc getNextMonth() {
192         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
193         _calendar.getCalendar().add(Calendar.MONTH, 1);
194
195         result.append(HREF_PREFIX);
196         String JavaDoc nexturl = BlojsomUtils.getCalendarNavigationUrl(_calendar.getCalendarUrl(),
197                 (_calendar.getCalendar().get(Calendar.MONTH) + 1),
198                 -1, _calendar.getCalendar().get(Calendar.YEAR));
199
200         result.append(nexturl);
201         result.append("\"> ");
202         result.append(_calendar.getShortMonthName(_calendar.getCalendar().get(Calendar.MONTH)));
203         result.append(VTL_SPACER).append(VTL_SPACER).append("&gt;").append(HREF_SUFFIX);
204         _calendar.getCalendar().add(Calendar.MONTH, -1);
205
206         return result.toString();
207     }
208
209     /**
210      * Get the link for navigating to the previous month
211      *
212      * @return the previous month link
213      */

214     public String JavaDoc getPreviousMonthLink() {
215
216         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
217         _calendar.getCalendar().add(Calendar.MONTH, -1);
218
219         String JavaDoc prevurl =
220                 BlojsomUtils.getCalendarNavigationUrl(_calendar.getCalendarUrl(),
221                         (_calendar.getCalendar().get(Calendar.MONTH) + 1),
222                         -1, _calendar.getCalendar().get(Calendar.YEAR));
223
224         result.append(prevurl);
225         _calendar.getCalendar().add(Calendar.MONTH, 1);
226
227         return result.toString();
228     }
229
230     /**
231      * Get the visual control for navigating to the previous month
232      *
233      * @return the previous month navigation control
234      */

235     public String JavaDoc getPreviousMonthName() {
236         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
237         _calendar.getCalendar().add(Calendar.MONTH, -1);
238
239         result.append(_calendar.getMonthName(_calendar.getCalendar().get(Calendar.MONTH)));
240         _calendar.getCalendar().add(Calendar.MONTH, 1);
241
242         return result.toString();
243     }
244
245     /**
246      * Get the link for navigating to the current month
247      *
248      * @return the current month link
249      */

250     public String JavaDoc getCurrentMonthLink() {
251
252         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
253         result.append(_calendar.getCalendarUrl());
254
255         return result.toString();
256     }
257
258     /**
259      * Get the link for navigating to the next month
260      *
261      * @return the next month link
262      */

263     public String JavaDoc getCurrentMonthName() {
264         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
265
266         result.append(_calendar.getMonthName(_calendar.getCalendar().get(Calendar.MONTH)));
267
268         return result.toString();
269     }
270
271
272     /**
273      * Get the link for navigating to the next month
274      *
275      * @return the next month link
276      */

277     public String JavaDoc getNextMonthLink() {
278
279         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
280         _calendar.getCalendar().add(Calendar.MONTH, 1);
281
282         String JavaDoc nexturl =
283                 BlojsomUtils.getCalendarNavigationUrl(_calendar.getCalendarUrl(),
284                         (_calendar.getCalendar().get(Calendar.MONTH) + 1),
285                         -1, _calendar.getCalendar().get(Calendar.YEAR));
286
287         result.append(nexturl);
288         _calendar.getCalendar().add(Calendar.MONTH, -1);
289
290         return result.toString();
291     }
292
293     /**
294      * Get the name for navigating to the next month
295      *
296      * @return the next month name
297      */

298     public String JavaDoc getNextMonthName() {
299         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
300         _calendar.getCalendar().add(Calendar.MONTH, 1);
301
302         result.append(_calendar.getMonthName(_calendar.getCalendar().get(Calendar.MONTH)));
303         _calendar.getCalendar().add(Calendar.MONTH, -1);
304
305         return result.toString();
306     }
307
308     /**
309      * Set the text displayed for the "Today" link
310      *
311      * @param today Text for "Today" link
312      */

313     public void setTodayText(String JavaDoc today) {
314         _today = today;
315     }
316
317     /**
318      * Retrieve the text displayed for the "Today" link
319      *
320      * @return Text for "Today link
321      */

322     public String JavaDoc getTodayText() {
323         return _today;
324     }
325 }
326
Popular Tags