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.util.Calendar;
035:
036: /**
037: * VelocityHelper is a class used to help render a visual calendar using the VTL.
038: *
039: * @author David Czarnecki
040: * @author Mark Lussier
041: * @since blojsom 3.0
042: * @version $Id: VelocityHelper.java,v 1.2 2007/01/17 02:35:08 czarneckid Exp $
043: */
044: public class VelocityHelper {
045:
046: private BlogCalendar _calendar;
047:
048: // [Row][Col]
049: private String[][] visualcalendar = new String[6][7];
050:
051: private static final String VTL_SPACER = " ";
052:
053: private String HREF_PREFIX = "<a href=\"";
054: private String HREF_SUFFIX = "</a>";
055: private String _today = "Today";
056:
057: /**
058: * Public Constructor
059: */
060: public VelocityHelper() {
061: }
062:
063: /**
064: * Public Constructor
065: *
066: * @param calendar BlogCalendar to render
067: */
068: public VelocityHelper(BlogCalendar calendar) {
069: _calendar = calendar;
070: }
071:
072: /**
073: * Sets the BlogCalendar to render
074: *
075: * @param calendar BlogCalendar
076: */
077: public void setCalendar(BlogCalendar calendar) {
078: _calendar = calendar;
079: }
080:
081: /**
082: * Retrieve the {@link BlogCalendar} object used to construct this object
083: *
084: * @return {@link BlogCalendar}
085: */
086: public BlogCalendar getBlogCalendar() {
087: return _calendar;
088: }
089:
090: /**
091: * Builds the visual calendar model
092: */
093: public void buildCalendar() {
094: int fdow = _calendar.getFirstDayOfMonth()
095: - _calendar.getCalendar().getFirstDayOfWeek();
096: if (fdow == -1) {
097: fdow = 6;
098: }
099: 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(dowoffset)
109: .toString();
110: } else {
111: StringBuffer _url = new StringBuffer(
112: HREF_PREFIX);
113: String _calurl = BlojsomUtils
114: .getCalendarNavigationUrl(_calendar
115: .getCalendarUrl(), (_calendar
116: .getCurrentMonth() + 1),
117: dowoffset, _calendar
118: .getCurrentYear());
119: _url.append(_calurl);
120: _url.append("\"><span>").append(dowoffset)
121: .append("</span>").append(HREF_SUFFIX);
122: visualcalendar[x][y] = _url.toString();
123: }
124:
125: }
126: }
127: }
128: }
129:
130: /**
131: * Get the visual content for a given calendar row. If <code>clazz</code> is null, no <code>class</code> attribute
132: * will be included in the <td> tag.
133: *
134: * @param row the row
135: * @return the visual calendar row
136: */
137: public String getCalendarRow(int row) {
138: return getCalendarRow(row, null);
139: }
140:
141: /**
142: * Get the visual content for a given calendar row. If <code>clazz</code> is null, no <code>class</code> attribute
143: * will be included in the <td> tag.
144: *
145: * @param row the row
146: * @param clazz the css style apply
147: * @return the visual calendar row
148: */
149: public String getCalendarRow(int row, String clazz) {
150: StringBuffer result = new StringBuffer();
151: if (row > 0 && row <= visualcalendar.length) {
152: for (int x = 0; x < 7; x++) {
153: if (clazz != null) {
154: result.append("<td class=\"").append(clazz).append(
155: "\">").append(visualcalendar[row - 1][x])
156: .append("</td>");
157: } else {
158: result.append("<td>").append(
159: visualcalendar[row - 1][x]).append("</td>");
160: }
161: }
162: }
163: return result.toString();
164: }
165:
166: /**
167: * Get the visual control for navigating to Today
168: *
169: * @return the today navigation control
170: */
171: public String getToday() {
172: StringBuffer result = new StringBuffer();
173: result.append(HREF_PREFIX).append(_calendar.getCalendarUrl())
174: .append("\">").append(
175: _calendar.getShortMonthName(_calendar
176: .getCurrentMonth()))
177: .append(HREF_SUFFIX);
178: return result.toString();
179: }
180:
181: /**
182: * Get the visual control for navigating to the previous month
183: *
184: * @return the previous month navigation control
185: */
186: public String getPreviousMonth() {
187: StringBuffer result = new StringBuffer();
188: _calendar.getCalendar().add(Calendar.MONTH, -1);
189: result.append(HREF_PREFIX);
190: String prevurl = BlojsomUtils.getCalendarNavigationUrl(
191: _calendar.getCalendarUrl(), (_calendar.getCalendar()
192: .get(Calendar.MONTH) + 1), -1, _calendar
193: .getCalendar().get(Calendar.YEAR));
194: result.append(prevurl);
195: result.append("\"> <").append(VTL_SPACER).append(VTL_SPACER);
196: result.append(_calendar.getShortMonthName(_calendar
197: .getCalendar().get(Calendar.MONTH)));
198: result.append(HREF_SUFFIX);
199: _calendar.getCalendar().add(Calendar.MONTH, 1);
200: return result.toString();
201: }
202:
203: /**
204: * Get the visual control for navigating to the next month
205: *
206: * @return the next month navigation control
207: */
208: public String getNextMonth() {
209: StringBuffer result = new StringBuffer();
210: _calendar.getCalendar().add(Calendar.MONTH, 1);
211:
212: result.append(HREF_PREFIX);
213: String nexturl = BlojsomUtils.getCalendarNavigationUrl(
214: _calendar.getCalendarUrl(), (_calendar.getCalendar()
215: .get(Calendar.MONTH) + 1), -1, _calendar
216: .getCalendar().get(Calendar.YEAR));
217:
218: result.append(nexturl);
219: result.append("\"> ");
220: result.append(_calendar.getShortMonthName(_calendar
221: .getCalendar().get(Calendar.MONTH)));
222: result.append(VTL_SPACER).append(VTL_SPACER).append(">")
223: .append(HREF_SUFFIX);
224: _calendar.getCalendar().add(Calendar.MONTH, -1);
225:
226: return result.toString();
227: }
228:
229: /**
230: * Get the link for navigating to the previous month
231: *
232: * @return the previous month link
233: */
234: public String getPreviousMonthLink() {
235:
236: StringBuffer result = new StringBuffer();
237: _calendar.getCalendar().add(Calendar.MONTH, -1);
238:
239: String prevurl = BlojsomUtils.getCalendarNavigationUrl(
240: _calendar.getCalendarUrl(), (_calendar.getCalendar()
241: .get(Calendar.MONTH) + 1), -1, _calendar
242: .getCalendar().get(Calendar.YEAR));
243:
244: result.append(prevurl);
245: _calendar.getCalendar().add(Calendar.MONTH, 1);
246:
247: return result.toString();
248: }
249:
250: /**
251: * Get the visual control for navigating to the previous month
252: *
253: * @return the previous month navigation control
254: */
255: public String getPreviousMonthName() {
256: StringBuffer result = new StringBuffer();
257: _calendar.getCalendar().add(Calendar.MONTH, -1);
258:
259: result.append(_calendar.getMonthName(_calendar.getCalendar()
260: .get(Calendar.MONTH)));
261: _calendar.getCalendar().add(Calendar.MONTH, 1);
262:
263: return result.toString();
264: }
265:
266: /**
267: * Get the link for navigating to the current month
268: *
269: * @return the current month link
270: */
271: public String getCurrentMonthLink() {
272:
273: StringBuffer result = new StringBuffer();
274: result.append(_calendar.getCalendarUrl());
275:
276: return result.toString();
277: }
278:
279: /**
280: * Get the link for navigating to the next month
281: *
282: * @return the next month link
283: */
284: public String getCurrentMonthName() {
285: StringBuffer result = new StringBuffer();
286:
287: result.append(_calendar.getMonthName(_calendar.getCalendar()
288: .get(Calendar.MONTH)));
289:
290: return result.toString();
291: }
292:
293: /**
294: * Get the link for navigating to the next month
295: *
296: * @return the next month link
297: */
298: public String getNextMonthLink() {
299:
300: StringBuffer result = new StringBuffer();
301: _calendar.getCalendar().add(Calendar.MONTH, 1);
302:
303: String nexturl = BlojsomUtils.getCalendarNavigationUrl(
304: _calendar.getCalendarUrl(), (_calendar.getCalendar()
305: .get(Calendar.MONTH) + 1), -1, _calendar
306: .getCalendar().get(Calendar.YEAR));
307:
308: result.append(nexturl);
309: _calendar.getCalendar().add(Calendar.MONTH, -1);
310:
311: return result.toString();
312: }
313:
314: /**
315: * Get the name for navigating to the next month
316: *
317: * @return the next month name
318: */
319: public String getNextMonthName() {
320: StringBuffer result = new StringBuffer();
321: _calendar.getCalendar().add(Calendar.MONTH, 1);
322:
323: result.append(_calendar.getMonthName(_calendar.getCalendar()
324: .get(Calendar.MONTH)));
325: _calendar.getCalendar().add(Calendar.MONTH, -1);
326:
327: return result.toString();
328: }
329:
330: /**
331: * Set the text displayed for the "Today" link
332: *
333: * @param today Text for "Today" link
334: */
335: public void setTodayText(String today) {
336: _today = today;
337: }
338:
339: /**
340: * Retrieve the text displayed for the "Today" link
341: *
342: * @return Text for "Today link
343: */
344: public String getTodayText() {
345: return _today;
346: }
347: }
|