001: /*
002: * Created on Feb 3, 2005
003: *
004: */
005: package net.sf.thingamablog.generator;
006:
007: import java.text.DateFormatSymbols;
008: import java.util.ArrayList;
009: import java.util.Calendar;
010: import java.util.Date;
011: import java.util.Hashtable;
012: import java.util.List;
013: import java.util.Vector;
014:
015: import net.sf.thingamablog.blog.ArchiveRange;
016: import net.sf.thingamablog.blog.TBWeblog;
017:
018: /**
019: * @author Owner
020: */
021: public class CalendarContainer implements TemplateContainer {
022:
023: static final int TOP_PAGE = 0;
024: /** Constant for an archive page */
025: static final int ARC_PAGE = 1;
026: /** Constant for a category page */
027: static final int CAT_PAGE = 2;
028:
029: private String category;
030: private ArchiveRange archive;
031:
032: public static final String NAME = "Calendar";
033:
034: /**
035: * The month attribute 1 - 12
036: */
037: public static final String MONTH = "month";
038: /**
039: * The year attribute
040: */
041: public static final String YEAR = "year";
042:
043: private Calendar calendar;
044: private TBWeblog blog;
045: private int pageType;
046: private TemplateTag monthLabelTag = new DateTag("MonthLabel");
047:
048: private int month, year;
049:
050: /**
051: * Constructs a CalendarContainer for a top level page
052: *
053: * @param wb
054: */
055: public CalendarContainer(TBWeblog wb) {
056: _init(wb, TOP_PAGE);
057: }
058:
059: public CalendarContainer(TBWeblog wb, String cat) {
060: category = cat;
061: _init(wb, CAT_PAGE);
062: }
063:
064: public CalendarContainer(TBWeblog wb, ArchiveRange arc) {
065: archive = arc;
066: _init(wb, ARC_PAGE);
067: }
068:
069: private void _init(TBWeblog wb, int type) {
070: blog = wb;
071: pageType = type;
072: calendar = Calendar.getInstance(blog.getLocale());
073: Hashtable ht = monthLabelTag.getDefaultAttributes();
074: ht.put(DateTag.FORMAT, "MMMM yyyy");
075: }
076:
077: private int getCurrentMonth() {
078: Date d = new Date();
079: if (pageType == ARC_PAGE)
080: d = archive.getStartDate();
081: calendar.setTime(d);
082: return calendar.get(Calendar.MONTH) + 1;//we want 1 - 12 based months
083: }
084:
085: private int getCurrentYear() {
086: Date d = new Date();
087: if (pageType == ARC_PAGE)
088: d = archive.getStartDate();
089: calendar.setTime(d);
090: return calendar.get(Calendar.YEAR);
091: }
092:
093: /* (non-Javadoc)
094: * @see net.sf.thingamablog.generator.TemplateContainer#init(java.util.Hashtable)
095: */
096: public void initialize(Hashtable attribs) {
097: month = getCurrentMonth();
098: year = getCurrentYear();
099:
100: //get the month attrib
101: try {
102: int n = Integer.parseInt(attribs.get(MONTH).toString());
103: if (n > 0 && n <= 12)
104: month = n;
105: } catch (Exception ex) {
106: }
107:
108: //get the year attrib
109: try {
110: int n = Integer.parseInt(attribs.get(YEAR).toString());
111: if (n >= 1900 && n <= 2100)
112: year = n;
113: } catch (Exception ex) {
114: }
115:
116: //java Calendar months are 0 based (0 - 11),
117: //since attribs go from 1 - 12 decrement month by one
118: month--;
119: }
120:
121: /* (non-Javadoc)
122: * @see net.sf.thingamablog.generator.TemplateContainer#getValueForTag(net.sf.thingamablog.generator.TemplateTag)
123: */
124: public Object getValueForTag(TemplateTag t) {
125: if (t == monthLabelTag) {
126: Calendar c = Calendar.getInstance(blog.getLocale());
127: c.set(year, month, 1, 1, 1, 1);
128: return c.getTime();
129: }
130: return "";
131: }
132:
133: /* (non-Javadoc)
134: * @see net.sf.thingamablog.generator.TemplateContainer#getTags()
135: */
136: public List getTags() {
137: Vector v = new Vector();
138: v.add(monthLabelTag);
139: return v;
140: }
141:
142: /* (non-Javadoc)
143: * @see net.sf.thingamablog.generator.TemplateContainer#getContainers()
144: */
145: public List getContainers() {
146: ArrayList v = new ArrayList(2);
147: v.add(new WeekDayContainer());
148: v.add(new CalendarWeekContainer(month, year));
149: return v;
150: }
151:
152: /* (non-Javadoc)
153: * @see net.sf.thingamablog.generator.TemplateContainer#processAgain()
154: */
155: public boolean processAgain() {
156: return false;
157: }
158:
159: /* (non-Javadoc)
160: * @see net.sf.thingamablog.generator.TemplateContainer#isVisible()
161: */
162: public boolean isVisible() {
163: return true;
164: }
165:
166: /* (non-Javadoc)
167: * @see net.sf.thingamablog.generator.TemplateContainer#prefix()
168: */
169: public String prefix() {
170: return null;
171: }
172:
173: /* (non-Javadoc)
174: * @see net.sf.thingamablog.generator.TemplateContainer#postfix()
175: */
176: public String postfix() {
177: return null;
178: }
179:
180: /* (non-Javadoc)
181: * @see net.sf.thingamablog.generator.TemplateElement#getName()
182: */
183: public String getName() {
184: return NAME;
185: }
186:
187: /* (non-Javadoc)
188: * @see net.sf.thingamablog.generator.TemplateElement#getDefaultAttributes()
189: */
190: public Hashtable getDefaultAttributes() {
191: //the defaults are the current month/year
192: Hashtable ht = new Hashtable();
193: ht.put(MONTH, getCurrentMonth() + "");
194: ht.put(YEAR, getCurrentYear() + "");
195: return ht;
196: }
197:
198: private class WeekDayContainer extends ListContainer {
199: private Vector weekDayLabels = new Vector();
200: private HyperTextTag weekDay = new HyperTextTag("WeekDay");
201: private ArrayList containers = new ArrayList(0);
202: private ArrayList tags = new ArrayList(1);
203:
204: public WeekDayContainer() {
205: super ("WeekDays");
206: getDefaultAttributes().put("long", "0");
207: tags.add(weekDay);
208: }
209:
210: public void initListData(boolean asc, Hashtable attribs) {
211: boolean longNames = false;
212: if (attribs.get("long") != null
213: && attribs.get("long").equals("1"))
214: longNames = true;
215:
216: int weekDays[] = new int[7];
217: weekDays[0] = Calendar.SUNDAY;
218: weekDays[1] = Calendar.MONDAY;
219: weekDays[2] = Calendar.TUESDAY;
220: weekDays[3] = Calendar.WEDNESDAY;
221: weekDays[4] = Calendar.THURSDAY;
222: weekDays[5] = Calendar.FRIDAY;
223: weekDays[6] = Calendar.SATURDAY;
224: if (calendar.getFirstDayOfWeek() == Calendar.MONDAY) {
225: weekDays[0] = Calendar.MONDAY;
226: weekDays[1] = Calendar.TUESDAY;
227: weekDays[2] = Calendar.WEDNESDAY;
228: weekDays[3] = Calendar.THURSDAY;
229: weekDays[4] = Calendar.FRIDAY;
230: weekDays[5] = Calendar.SATURDAY;
231: weekDays[6] = Calendar.SUNDAY;
232: }
233:
234: DateFormatSymbols dfs = new DateFormatSymbols(blog
235: .getLocale());
236: String days[];
237: if (longNames)
238: days = dfs.getWeekdays();
239: else
240: days = dfs.getShortWeekdays();
241: for (int i = 0; i < weekDays.length; i++)
242: weekDayLabels.add(days[weekDays[i]]);
243: }
244:
245: public List getTags() {
246: return tags;
247: }
248:
249: public List getContainers() {
250: return containers;
251: }
252:
253: public int getListDataSize() {
254: return weekDayLabels.size();
255: }
256:
257: public Object getValueForTag(TemplateTag t, int index) {
258: if (t == weekDay) {
259: return weekDayLabels.elementAt(index);
260: }
261: return "";
262: }
263:
264: public boolean isVisible() {
265: return true;
266: }
267: }
268:
269: private class CalendarWeekContainer extends ListContainer {
270: private int w_month, w_year;
271: private Calendar cal;
272: private Vector weekStarts = new Vector();
273: private DateTag dateOfWeek = new DateTag("DateOfWeek");
274: private ArrayList tags = new ArrayList(1);
275:
276: public CalendarWeekContainer(int m, int y) {
277: super ("CalendarWeek");
278: w_month = m;
279: w_year = y;
280: cal = Calendar.getInstance(blog.getLocale());
281: dateOfWeek.setLocale(blog.getLocale());
282: tags.add(dateOfWeek);
283: }
284:
285: public void initListData(boolean asc, Hashtable attribs) {
286: cal.set(w_year, w_month, 1, 0, 0, 0);
287: int curMonth = cal.get(Calendar.MONTH);
288:
289: //roll back the calendar to the first day of the week
290: while (cal.get(Calendar.DAY_OF_WEEK) != cal
291: .getFirstDayOfWeek())
292: cal.add(Calendar.HOUR, -24);
293:
294: weekStarts.removeAllElements();
295: //loop 5 or 6 times so the cal grid is big enuff
296: for (int week = 0; week < 6; week++) {
297: boolean monthChanged = false;
298: weekStarts.add(cal.getTime());
299: for (int days = 1; days <= 7; days++) {
300: cal.add(Calendar.DAY_OF_YEAR, 1);//next day
301: monthChanged = week >= 4
302: && cal.get(Calendar.MONTH) != curMonth;
303: }
304:
305: //we don't need to generate another week
306: if (monthChanged)
307: break;
308: }
309: }
310:
311: public int getListDataSize() {
312: return weekStarts.size();
313: }
314:
315: public List getTags() {
316: return tags;
317: }
318:
319: public List getContainers() {
320: ArrayList v = new ArrayList(1);
321: Date d = (Date) weekStarts.elementAt(currentIndex());
322: TemplateContainer tc;
323: if (pageType == ARC_PAGE)
324: tc = new CalendarDayContainer(blog, d, archive, w_month);
325: else if (pageType == CAT_PAGE)
326: tc = new CalendarDayContainer(blog, d, category,
327: w_month);
328: else
329: tc = new CalendarDayContainer(blog, d, w_month);
330:
331: v.add(tc);
332: return v;
333: }
334:
335: public Object getValueForTag(TemplateTag t, int index) {
336: if (t == dateOfWeek)
337: return weekStarts.elementAt(index);
338: return "";
339: }
340:
341: public boolean isVisible() {
342: return true;
343: }
344: }
345: }
|