View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * <p>
39   * Renderer for the month view of the Schedule component
40   * </p>
41   * @since 1.1.7
42   * @author Jurgen Lust (latest modification by $Author: schof $)
43   * @author Bruno Aranda (adaptation of Jurgen's code to myfaces)
44   * @version $Revision: 382051 $
45   */
46  public class ScheduleCompactMonthRenderer
47      extends AbstractCompactScheduleRenderer
48      implements Serializable
49  {
50  
51      private static final long serialVersionUID = 2926607881214603314L;
52      
53      //~ Methods ----------------------------------------------------------------
54  
55      /**
56       * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext,
57       *      javax.faces.component.UIComponent)
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          //container div for the schedule grid
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                     // Don't label the weekend
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      * @see AbstractCompactScheduleRenderer#getDefaultRowHeight()
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 //The End