001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013:
014: package org.rapla.components.calendarview.html;
015:
016: import java.util.*;
017:
018: import org.rapla.components.calendarview.Block;
019: import org.rapla.components.calendarview.Builder;
020: import org.rapla.components.calendarview.CalendarView;
021: import org.rapla.components.calendarview.WeekdayMapper;
022: import org.rapla.components.calendarview.BlockComparator;
023:
024: public abstract class AbstractHTMLView implements CalendarView {
025: public static String COLOR_NO_RESOURCE = "#BBEEBB";
026: protected Collection builders = new ArrayList();
027: /** shared calendar instance. Only used for temporary stored values. */
028: Calendar blockCalendar;
029: WeekdayMapper weekdayMapper;
030: Date startDate;
031: Date endDate;
032: String m_html;
033: protected Collection excludeDays = Collections.EMPTY_SET;
034:
035: Locale locale;
036: TimeZone timeZone;
037: Builder builder;
038:
039: public void setTimeZone(TimeZone timeZone) {
040: this .timeZone = timeZone;
041: if (locale != null) {
042: blockCalendar = createCalendar();
043: }
044: }
045:
046: public void setLocale(Locale locale) {
047: this .locale = locale;
048: if (timeZone != null) {
049: blockCalendar = createCalendar();
050: }
051: weekdayMapper = new WeekdayMapper(locale);
052: }
053:
054: public TimeZone getTimeZone() {
055: return timeZone;
056: }
057:
058: public void addBuilder(Builder b) {
059: builders.add(b);
060: }
061:
062: public void removeBuilder(Builder b) {
063: builders.remove(b);
064: }
065:
066: public void rebuild(Builder builder) {
067: try {
068: addBuilder(builder);
069: rebuild();
070: } finally {
071: removeBuilder(builder);
072: }
073: }
074:
075: public void setToDate(Date weekDate) {
076: calcMinMaxDates(weekDate);
077: }
078:
079: public void setExcludeDays(Collection excludeDays) {
080: this .excludeDays = excludeDays;
081: if (startDate != null)
082: calcMinMaxDates(startDate);
083: }
084:
085: abstract public Collection getBlocks();
086:
087: abstract void calcMinMaxDates(Date date);
088:
089: Calendar createCalendar() {
090: return Calendar.getInstance(getTimeZone(), locale);
091: }
092:
093: void checkBlock(Block bl) {
094: if (!bl.getStart().before(this .endDate)) {
095: throw new IllegalStateException("Start-date "
096: + bl.getStart()
097: + " must be before calendar end at " + this .endDate);
098: }
099: }
100:
101: public Date getStartDate() {
102: return startDate;
103: }
104:
105: public Date getEndDate() {
106: return endDate;
107: }
108:
109: public String getHtml() {
110: return m_html;
111: }
112:
113: protected class HTMLSmallDaySlot extends ArrayList {
114: private static final long serialVersionUID = 1L;
115:
116: private String date;
117:
118: public HTMLSmallDaySlot(String date) {
119: super (2);
120: this .date = date;
121: }
122:
123: public void putBlock(Block block) {
124: add(block);
125: }
126:
127: public void sort() {
128: Collections.sort(this , BlockComparator.COMPARATOR);
129: }
130:
131: public void paint(StringBuffer out) {
132: out.append("<div valign=\"top\" align=\"right\">");
133: out.append(date);
134: out.append("</div>\n");
135: for (int i = 0; i < size(); i++) {
136: Block block = (Block) get(i);
137: out.append("<div valign=\"top\" class=\"month_block\"");
138: if (block instanceof HTMLBlock) {
139: out.append(" style=\"background-color:"
140: + ((HTMLBlock) block).getBackgroundColor()
141: + ";\"");
142: }
143: out.append(">");
144: out.append(block.toString());
145: out.append("</div>\n");
146: }
147: }
148: }
149:
150: }
|