KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > core > renderer > html > DateInputRenderer


1 /*******************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. * Please look
3  * at license.txt in info directory for more license detail. *
4  ******************************************************************************/

5 package org.exoplatform.faces.core.renderer.html;
6
7 import java.io.IOException JavaDoc;
8 import java.util.Calendar JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
11 import javax.faces.component.UIComponent;
12 import javax.faces.context.FacesContext;
13 import javax.faces.context.ResponseWriter;
14 import org.exoplatform.faces.core.component.UIDateInput;
15 import org.exoplatform.faces.core.component.UISimpleForm;
16
17 public class DateInputRenderer extends HtmlBasicRenderer {
18
19   private static final String JavaDoc DAY_EXTENSION = "_dd";
20
21   private static final String JavaDoc MONTH_EXTENSION = "_mm";
22
23   private static final String JavaDoc YEAR_EXTENSION = "_yyyy";
24
25   public void decode(FacesContext context, UIComponent component) {
26     UIDateInput uiInput = (UIDateInput) component;
27     Calendar JavaDoc cal = uiInput.getCalendar();
28     Map JavaDoc paramMap = context.getExternalContext().getRequestParameterMap();
29     try {
30       String JavaDoc value = (String JavaDoc) paramMap.get(uiInput.getName() + DAY_EXTENSION);
31       cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(value));
32       value = (String JavaDoc) paramMap.get(uiInput.getName() + MONTH_EXTENSION);
33       cal.set(Calendar.MONTH, Integer.parseInt(value));
34       value = (String JavaDoc) paramMap.get(uiInput.getName() + YEAR_EXTENSION);
35       cal.set(Calendar.YEAR, Integer.parseInt(value));
36     } catch (NumberFormatException JavaDoc ex) {
37       ex.printStackTrace();
38     }
39   }
40
41   public void encodeBegin(FacesContext context, UIComponent component) throws IOException JavaDoc {
42     ResponseWriter w = context.getResponseWriter() ;
43     UIDateInput uiInput = (UIDateInput) component;
44     StringBuffer JavaDoc b = new StringBuffer JavaDoc(200) ;
45     ResourceBundle JavaDoc res = getApplicationResourceBundle(context.getExternalContext());
46     String JavaDoc formName = ((UISimpleForm) uiInput.getParent()).getFormName() ;
47     String JavaDoc name = uiInput.getName() ;
48     renderJavascript(b, uiInput.getName(), formName, res.getLocale().getLanguage());
49     Calendar JavaDoc cal = uiInput.getCalendar();
50     renderField(b, uiInput, name + DAY_EXTENSION,
51                 cal.get(Calendar.DAY_OF_MONTH), 1,
52                 cal.getActualMaximum(Calendar.DAY_OF_MONTH));
53     renderField(b, uiInput, name + MONTH_EXTENSION,
54                 cal.get(Calendar.MONTH), buildMonthArray(res));
55     renderField(b, uiInput, name + YEAR_EXTENSION, cal.get(Calendar.YEAR),
56                 uiInput.getMinYearRange(), uiInput.getMaxYearRange());
57     renderImage(b, name);
58     w.write(b.toString()) ;
59     if (uiInput.hasError()) {
60       w.write("<span style='color: red'>*</span>");
61     }
62   }
63   
64   private String JavaDoc[] buildMonthArray(ResourceBundle JavaDoc res){
65     String JavaDoc[] months = new String JavaDoc[12];
66     for (int i = 0; i < months.length; i++) {
67       months[i] = res.getString("UIDateInput.month-" + i);
68     }
69     return months;
70   }
71
72   private void renderJavascript(StringBuffer JavaDoc b, String JavaDoc name, String JavaDoc formName, String JavaDoc locale) {
73     b.append("<script language=\"JavaScript\" SRC=\"/skin/js/calendar/calendar.js\">");
74     b.append("</script>");
75     b.append("<script language=\"JavaScript\" SRC=\"/skin/js/calendar/calendar-setup.js\">");
76     b.append("</script>");
77     b.append("<script language=\"JavaScript\" SRC=\"/skin/js/calendar/lang/calendar-").
78         append(locale).append(".js\">");
79     b.append("</script>");
80
81     b.append("<script language='JavaScript'>");
82
83     b.append("var ").append(name).append(
84         "_calendar = new Calendar(false, null,");
85     b.append(name).append("_calendar_onSelect,");
86     b.append(name).append("_calendar_onClose);");
87     b.append(name).append("_calendar.weekNumbers = false;");
88     b.append(name).append("_calendar.firstDayOfWeek = 0;");
89     b.append(name).append("_calendar.setTtDateFormat(\"%A, %B %e, %Y\");");
90     b.append(name).append("_calendar.setRange(2000, 2010);");
91
92     b.append("function setSelectedValue(col, value) {");
93     b.append("for (var i = 0; i < col.length; i++) {");
94     b.append("if (col[i].value == value) {");
95     b.append("col.selectedIndex = i;");
96     b.append("break;");
97     b.append("}}}");
98
99     b.append("function ").append(name).append("_calendar_onClick(id) {");
100     b.append(name).append("_calendar.create();");
101     b.append(name).append("_calendar.setDate(new Date(");
102     b.append("document.").append(formName).append(".");
103     b.append(name).append(YEAR_EXTENSION);
104     b.append(".value, ");
105     b.append("document.").append(formName).append(".");
106     b.append(name).append(MONTH_EXTENSION);
107     b.append(".value, ");
108     b.append("document.").append(formName).append(".");
109     b.append(name).append(DAY_EXTENSION);
110     b.append(".value));");
111     b.append(name).append("_calendar.showAtElement(document.getElementById('");
112     b.append(name);
113     b.append("_calendarImage'), 'br');");
114     b.append("}");
115
116     b.append("function ").append(name).append(
117         "_calendar_onClose(cal) {cal.hide();}");
118
119     b.append("function ").append(name).append("_calendar_onSelect(cal) {");
120     b.append("if (cal.dateClicked) {");
121     b.append("setSelectedValue(");
122     b.append("document.").append(formName).append(".");
123     b.append(name).append(MONTH_EXTENSION).append(",");
124     b.append("cal.date.getMonth());");
125     b.append("setSelectedValue(");
126     b.append("document.").append(formName).append(".");
127     b.append(name).append(DAY_EXTENSION).append(",");
128     b.append("cal.date.getDate());");
129     b.append("setSelectedValue(");
130     b.append("document.").append(formName).append(".");
131     b.append(name).append(YEAR_EXTENSION).append(",");
132     b.append("cal.date.getFullYear());");
133     b.append("cal.callCloseHandler();");
134     b.append("}");
135     b.append("}");
136
137     b.append("</script>");
138   }
139
140   private void renderImage(StringBuffer JavaDoc b, String JavaDoc name) {
141     b.append("<img id=\"");
142     b.append(name);
143     b.append("_calendarImage\" SRC=\"");
144     b.append("/skin/images/calendar/calendar.gif\"");
145     b.append(" onClick=\"").append(name).append("_calendar_onClick('").append(
146         name).append("_calendar');\"/>");
147   }
148
149   private void renderField(StringBuffer JavaDoc b, UIDateInput uiInput, String JavaDoc name,
150                            int value, int minRange, int maxRange) {
151     b.append("<select style='width: auto;' name='").append(name).append("'");
152     if (!uiInput.isEditable() || uiInput.isReadonly()) {
153       b.append(" disabled='true'");
154     }
155     if (uiInput.getClazz() != null) {
156       b.append(" class='").append(uiInput.getClazz()).append("'");
157     }
158     b.append(">\n");
159     for (int i = minRange; i <= maxRange; i++) {
160       if (i == value) {
161         b.append("<option selected=\"selected\" value=\"").append(i).append(
162             "\">").append(i).append("</option>\n");
163       } else {
164         b.append("<option value=\"").append(i).append("\">").append(i).append(
165             "</option>\n");
166       }
167     }
168     b.append("</select>\n");
169   }
170
171   private void renderField(StringBuffer JavaDoc b, UIDateInput uiInput, String JavaDoc name,
172                            int value, String JavaDoc[] values) {
173     b.append("<select style='width: auto;' name='").append(name).append("'");
174     if (!uiInput.isEditable() || uiInput.isReadonly()) {
175       b.append(" disabled='true'");
176     }
177     if (uiInput.getClazz() != null) {
178       b.append(" class='").append(uiInput.getClazz()).append("'");
179     }
180     b.append(">\n");
181     for (int i = 0; i < values.length; i++) {
182       if (i == value) {
183         b.append("<option selected=\"selected\" value=\"").append(i).append(
184             "\">").append(values[i]).append("</option>\n");
185       } else {
186         b.append("<option value=\"").append(i).append("\">").append(values[i]).append(
187             "</option>\n");
188       }
189     }
190     b.append("</select>\n");
191   }
192
193   public void encodeChildren(FacesContext context, UIComponent component)
194       throws IOException JavaDoc {
195   }
196
197   public void encodeEnd(FacesContext context, UIComponent component)
198       throws IOException JavaDoc {
199
200   }
201 }
Popular Tags