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.tobago.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.component.UICalendar;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
26  import org.apache.myfaces.tobago.model.CalendarModel;
27  import org.apache.myfaces.tobago.model.DateModel;
28  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
29  import org.apache.myfaces.tobago.renderkit.css.Classes;
30  import org.apache.myfaces.tobago.renderkit.css.Style;
31  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
34  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
35  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
36  
37  import javax.faces.component.UIComponent;
38  import javax.faces.context.FacesContext;
39  import java.io.IOException;
40  import java.text.SimpleDateFormat;
41  import java.util.Calendar;
42  import java.util.Date;
43  import java.util.GregorianCalendar;
44  import java.util.Locale;
45  
46  public class CalendarRenderer extends LayoutComponentRendererBase {
47  
48    @Override
49    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
50  
51      UICalendar output = (UICalendar) component;
52      String id = output.getClientId(facesContext);
53  
54      Locale locale = facesContext.getViewRoot().getLocale();
55  
56      Object value = output.getValue();
57      Calendar calendar;
58      if (value instanceof Calendar) {
59        calendar = (Calendar) value;
60      } else { 
61        calendar = new GregorianCalendar();
62        if (value instanceof Date) {
63          calendar.setTime((Date) value);
64        }
65      }
66      CalendarModel model = new CalendarModel(calendar);
67  
68      // rendering
69      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
70  
71      writer.startElement(HtmlElements.DIV, component);
72      writer.writeIdAttribute(id);
73      writer.writeClassAttribute(Classes.create(output));
74      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
75      Style style = new Style(facesContext, output);
76      writer.writeStyleAttribute(style);
77      String dateInputId = (String) output.getAttributes().get(Attributes.DATE_INPUT_ID);
78      if (dateInputId != null) {
79        writer.writeAttribute(DataAttributes.DATE_INPUT_ID, dateInputId, false);
80      }
81      writer.writeAttribute(DataAttributes.DAY, Integer.toString(calendar.get(Calendar.YEAR)), false);
82      writer.writeAttribute(DataAttributes.MONTH, Integer.toString(1 + calendar.get(Calendar.MONTH)), false);
83      writer.writeAttribute(DataAttributes.YEAR, Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)), false);
84  
85      writer.writeAttribute(DataAttributes.FIRST_DAY_OF_WEEK, Integer.toString(calendar.getFirstDayOfWeek()), false);
86      writer.writeAttribute(DataAttributes.MONTH_NAMES, getMonthNames(locale), false);
87  
88      // begin header
89      writer.startElement(HtmlElements.DIV, null);
90      writer.writeClassAttribute(Classes.create(output, "row"));
91  
92      writer.startElement(HtmlElements.IMG, null);
93      writer.writeClassAttribute(Classes.create(output, "header"));
94      writer.writeAttribute(HtmlAttributes.ALT, "", false);
95      writer.writeAttribute(HtmlAttributes.SRC,
96          ResourceManagerUtils.getImageWithPath(facesContext, "image/calendarFastPrev.gif"), false);
97      writer.writeAttribute(DataAttributes.COMMAND, "fastPrev", false);
98      writer.endElement(HtmlElements.IMG);
99  
100     writer.startElement(HtmlElements.IMG, null);
101     writer.writeClassAttribute(Classes.create(output, "header"));
102     writer.writeAttribute(HtmlAttributes.ALT, "", false);
103     writer.writeAttribute(HtmlAttributes.SRC,
104         ResourceManagerUtils.getImageWithPath(facesContext, "image/calendarPrev.gif"), false);
105     writer.writeAttribute(DataAttributes.COMMAND, "prev", false);
106     writer.endElement(HtmlElements.IMG);
107 
108     writer.startElement(HtmlElements.SPAN, null);
109     writer.writeClassAttribute(Classes.create(output, "header"));
110 
111     writer.startElement(HtmlElements.SPAN, null);
112     writer.writeAttribute(DataAttributes.COMMAND, "month", false);
113     writer.writeText(new SimpleDateFormat("MMMMM", locale).format(calendar.getTime()));
114     writer.endElement(HtmlElements.SPAN);
115 
116     writer.writeText(" "); // non breaking space
117 
118     writer.startElement(HtmlElements.SPAN, null);
119     writer.writeAttribute(DataAttributes.COMMAND, "year", false);
120     writer.writeText(new SimpleDateFormat("yyyy", locale).format(calendar.getTime()));
121     writer.endElement(HtmlElements.SPAN);
122 
123     writer.endElement(HtmlElements.SPAN);
124 
125     writer.startElement(HtmlElements.IMG, null);
126     writer.writeClassAttribute(Classes.create(output, "header"));
127     writer.writeAttribute(HtmlAttributes.ALT, "", false);
128     writer.writeAttribute(HtmlAttributes.SRC,
129         ResourceManagerUtils.getImageWithPath(facesContext, "image/calendarNext.gif"), false);
130     writer.writeAttribute(DataAttributes.COMMAND, "next", false);
131     writer.endElement(HtmlElements.IMG);
132 
133     writer.startElement(HtmlElements.IMG, null);
134     writer.writeClassAttribute(Classes.create(output, "header"));
135     writer.writeAttribute(HtmlAttributes.ALT, "", false);
136     writer.writeAttribute(HtmlAttributes.SRC,
137         ResourceManagerUtils.getImageWithPath(facesContext, "image/calendarFastNext.gif"), false);
138     writer.writeAttribute(DataAttributes.COMMAND, "fastNext", false);
139     writer.endElement(HtmlElements.IMG);
140 
141     writer.endElement(HtmlElements.DIV);
142     // end header
143 
144     // begin weeks
145     writer.startElement(HtmlElements.DIV, null);
146     writer.writeClassAttribute(Classes.create(output, "row"));
147 
148     SimpleDateFormat dayInWeekFormat = new SimpleDateFormat("E", locale);
149     for (int dayIt = 0; dayIt < 7; ++dayIt) {
150       DateModel date = model.getDate(0, dayIt);
151       String dayName = dayInWeekFormat.format(date.getCalendar().getTime());
152       if (dayName != null) {
153         dayName = dayName.substring(0, 2);
154       }
155 
156       writer.startElement(HtmlElements.SPAN, null);
157       writer.writeClassAttribute(Classes.create(output, "dayOfWeek"));
158       writer.writeText(dayName);
159       writer.endElement(HtmlElements.SPAN);
160     }
161 
162     writer.endElement(HtmlElements.DIV);
163     // end weeks
164 
165     // begin grid
166     writer.startElement(HtmlElements.DIV, null);
167     writer.writeClassAttribute(Classes.create(output, "grid"));
168     for (int week = 0; week < 6; ++week) {
169       writer.startElement(HtmlElements.DIV, null);
170       writer.writeClassAttribute(Classes.create(output, "row"));
171 
172       for (int dayIt = 0; dayIt < 7; ++dayIt) {
173         DateModel date = model.getDate(week, dayIt);
174         String dayDescription = String.valueOf(date.getDay());
175 
176         writer.startElement(HtmlElements.SPAN, null);
177         writer.writeClassAttribute(
178             Classes.create(output, "day", date.getMonth() == model.getMonth() ? null : Markup.DISABLED));
179 
180         writer.writeText(dayDescription);
181 
182         writer.endElement(HtmlElements.SPAN);
183       }
184       writer.endElement(HtmlElements.DIV);
185     }
186     writer.endElement(HtmlElements.DIV);
187     // end grid
188 
189     writer.endElement(HtmlElements.DIV);
190   }
191   
192   private String getMonthNames(Locale locale) {
193     SimpleDateFormat dateFormat = new SimpleDateFormat("MMMMM", locale);
194     StringBuilder buffer = new StringBuilder(64);
195     Calendar calendar = Calendar.getInstance();
196     calendar.set(2000, Calendar.JANUARY, 1);
197     for (int month = 0; month < 12; ++month) {
198       if (month > 0) {
199         buffer.append(',');
200       }
201       buffer.append(dateFormat.format(calendar.getTime()));
202       calendar.add(java.util.Calendar.MONTH, 1);
203     }
204     return buffer.toString();
205   }
206 
207 }