1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.schedule;
21
22
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.text.DateFormat;
26 import java.util.Calendar;
27 import java.util.Iterator;
28
29 import javax.faces.component.UIComponent;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32
33 import org.apache.myfaces.custom.schedule.model.ScheduleDay;
34 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
35
36
37
38
39
40
41
42
43
44
45
46 public class ScheduleCompactMonthRenderer
47 extends AbstractCompactScheduleRenderer
48 implements Serializable
49 {
50
51 private static final long serialVersionUID = 2926607881214603314L;
52
53
54
55
56
57
58
59 public void encodeBegin(
60 FacesContext context,
61 UIComponent component
62 )
63 throws IOException
64 {
65 if (!component.isRendered()) {
66 return;
67 }
68
69 super.encodeBegin(context, component);
70
71 HtmlSchedule schedule = (HtmlSchedule) component;
72 ResponseWriter writer = context.getResponseWriter();
73
74
75 writer.startElement(HTML.DIV_ELEM, schedule);
76 writer.writeAttribute(HTML.CLASS_ATTR, "schedule-compact-" + schedule.getTheme(), null);
77 writer.writeAttribute(
78 HTML.STYLE_ATTR, "border-style: none; overflow: hidden;", null
79 );
80
81 writer.startElement(HTML.TABLE_ELEM, schedule);
82 writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(schedule, "month"), null);
83 writer.writeAttribute(
84 HTML.STYLE_ATTR, "width: 100%;",
85 null
86 );
87 writer.writeAttribute(HTML.CELLPADDING_ATTR, "0", null);
88 writer.writeAttribute(HTML.CELLSPACING_ATTR, "1", null);
89
90 Calendar cal = getCalendarInstance(schedule, schedule.getModel().getSelectedDate());
91
92 String dayOfWeekDateFormat = schedule.getCompactMonthDayOfWeekDateFormat();
93
94 if (dayOfWeekDateFormat != null && dayOfWeekDateFormat.length() > 0) {
95 DateFormat dayOfWeekFormater = getDateFormat(context, schedule, dayOfWeekDateFormat);
96 writer.startElement(HTML.THEAD_ELEM, schedule);
97 writer.startElement(HTML.TR_ELEM, schedule);
98
99 for (Iterator dayIterator = schedule.getModel().iterator(); dayIterator.hasNext();) {
100 ScheduleDay day = (ScheduleDay) dayIterator.next();
101 cal.setTime(day.getDate());
102
103 int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
104
105 writer.startElement(HTML.TH_ELEM, schedule);
106 writer.writeAttribute(HTML.CLASS_ATTR, getStyleClass(schedule, "header"), null);
107
108 if (schedule.isSplitWeekend() && dayOfWeek == Calendar.SATURDAY) {
109
110 writer.endElement(HTML.TH_ELEM);
111 break;
112 } else {
113 writer.writeText(dayOfWeekFormater.format(day.getDate()), null);
114 writer.endElement(HTML.TH_ELEM);
115 }
116 if (dayOfWeek == Calendar.SUNDAY) {
117 break;
118 }
119 }
120 writer.endElement(HTML.TR_ELEM);
121 writer.endElement(HTML.THEAD_ELEM);
122 }
123
124 writer.startElement(HTML.TBODY_ELEM, schedule);
125
126 int selectedMonth = cal.get(Calendar.MONTH);
127
128 for (
129 Iterator dayIterator = schedule.getModel().iterator();
130 dayIterator.hasNext();
131 ) {
132 ScheduleDay day = (ScheduleDay) dayIterator.next();
133 cal.setTime(day.getDate());
134
135 int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
136 int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
137 int currentMonth = cal.get(Calendar.MONTH);
138 boolean isWeekend =
139 (dayOfWeek == Calendar.SATURDAY) ||
140 (dayOfWeek == Calendar.SUNDAY);
141
142 cal.setTime(day.getDate());
143
144 writeDayCell(
145 context, writer, schedule, day, dayOfWeek, dayOfMonth, isWeekend,
146 currentMonth == selectedMonth, (!isWeekend && schedule.isSplitWeekend() ? 2 : 1)
147 );
148
149 }
150
151 writer.endElement(HTML.TBODY_ELEM);
152 writer.endElement(HTML.TABLE_ELEM);
153
154 writer.endElement(HTML.DIV_ELEM);
155 }
156
157
158
159
160 protected int getDefaultRowHeight()
161 {
162 return 120;
163 }
164
165
166
167
168 protected void writeDayCell(
169 FacesContext context,
170 ResponseWriter writer,
171 HtmlSchedule schedule,
172 ScheduleDay day,
173 int dayOfWeek,
174 int dayOfMonth,
175 boolean isWeekend,
176 boolean isCurrentMonth,
177 int rowspan
178 )
179 throws IOException
180 {
181 if ((dayOfWeek == Calendar.MONDAY) || (schedule.isSplitWeekend() && dayOfWeek == Calendar.SUNDAY)) {
182 writer.startElement(HTML.TR_ELEM, schedule);
183 }
184
185 super.writeDayCell(
186 context, writer, schedule, day, 100f / (schedule.isSplitWeekend() ? 6 : 7), dayOfWeek, dayOfMonth,
187 isWeekend, isCurrentMonth, rowspan
188 );
189
190 if ((schedule.isSplitWeekend() && dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY)) {
191 writer.endElement(HTML.TR_ELEM);
192 }
193 }
194
195 protected int getRowHeight(UIScheduleBase schedule)
196 {
197 if (schedule != null) {
198 int height = schedule.getCompactMonthRowHeight();
199 return height == 0 ? getDefaultRowHeight() : height;
200 }
201 return getDefaultRowHeight();
202 }
203 }
204