1 16 package org.apache.myfaces.custom.date; 17 18 import java.io.IOException ; 19 import java.text.DateFormatSymbols ; 20 import java.text.ParseException ; 21 import java.util.Calendar ; 22 import java.util.Locale ; 23 import java.util.Map ; 24 25 import javax.faces.application.FacesMessage; 26 import javax.faces.component.UIComponent; 27 import javax.faces.context.FacesContext; 28 import javax.faces.context.ResponseWriter; 29 import javax.faces.convert.ConverterException; 30 31 import org.apache.myfaces.component.UserRoleUtils; 32 import org.apache.myfaces.custom.calendar.HtmlCalendarRenderer; 33 import org.apache.myfaces.custom.calendar.HtmlCalendarRenderer.CalendarDateTimeConverter; 34 import org.apache.myfaces.custom.date.HtmlInputDate.UserData; 35 import org.apache.myfaces.renderkit.RendererUtils; 36 import org.apache.myfaces.renderkit.html.HTML; 37 import org.apache.myfaces.renderkit.html.HtmlRenderer; 38 import org.apache.myfaces.renderkit.html.HtmlRendererUtils; 39 import org.apache.myfaces.util.MessageUtils; 40 41 89 public class HtmlDateRenderer extends HtmlRenderer { 90 94 public static final String DATE_MESSAGE_ID = "org.apache.myfaces.Date.INVALID"; 95 96 private static final String ID_DAY_POSTFIX = ".day"; 97 private static final String ID_MONTH_POSTFIX = ".month"; 98 private static final String ID_YEAR_POSTFIX = ".year"; 99 private static final String ID_HOURS_POSTFIX = ".hours"; 100 private static final String ID_MINUTES_POSTFIX = ".minutes"; 101 private static final String ID_SECONDS_POSTFIX = ".seconds"; 102 103 protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) { 104 if( !UserRoleUtils.isEnabledOnUserRole(uiComponent) ) 105 return false; 106 107 return ((HtmlInputDate)uiComponent).isDisabled(); 108 } 109 110 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException { 111 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlInputDate.class); 112 113 HtmlInputDate inputDate = (HtmlInputDate) uiComponent; 114 Locale currentLocale = facesContext.getViewRoot().getLocale(); 115 UserData userData = (UserData) inputDate.getSubmittedValue(); 116 if( userData == null ) 117 userData = inputDate.getUserData(currentLocale); 118 String type = inputDate.getType(); 119 String clientId = uiComponent.getClientId(facesContext); 120 121 boolean disabled = isDisabled(facesContext, uiComponent); 122 123 ResponseWriter writer = facesContext.getResponseWriter(); 124 125 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 126 127 if( ! type.equals("time")){ 128 encodeInputDay(uiComponent, writer, clientId, userData, disabled); 129 encodeInputMonth(uiComponent, writer, clientId, userData, currentLocale, disabled); 130 encodeInputYear(uiComponent, writer, clientId, userData, disabled); 131 132 if( inputDate.isPopupCalendar() && ! disabled ) 133 encodePopupCalendarButton(facesContext, writer, clientId, currentLocale); 134 } 135 if( type.equals("both") ){ 136 writer.write(" "); 137 } 138 if( ! type.equals("date")){ 139 encodeInputHours(uiComponent, writer, clientId, userData, disabled); 140 writer.write(":"); 141 encodeInputMinutes(uiComponent, writer, clientId, userData, disabled); 142 writer.write(":"); 143 encodeInputSeconds(uiComponent, writer, clientId, userData, disabled); 144 } 145 } 146 147 private static void encodeInputField(UIComponent uiComponent, ResponseWriter writer, String id, String value, int size, boolean disabled) throws IOException { 148 writer.startElement(HTML.INPUT_ELEM, uiComponent); 149 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES); 150 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.EVENT_HANDLER_ATTRIBUTES); 151 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_ATTRIBUTES); 152 153 if (disabled) { 154 writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null); 155 } 156 157 writer.writeAttribute(HTML.ID_ATTR, id, null); 158 writer.writeAttribute(HTML.NAME_ATTR, id, null); 159 writer.writeAttribute(HTML.SIZE_ATTR, Integer.toString(size), null); 160 writer.writeAttribute(HTML.MAXLENGTH_ATTR, Integer.toString(size), null); 161 if (value != null) { 162 writer.writeAttribute(HTML.VALUE_ATTR, value, null); 163 } 164 writer.endElement(HTML.INPUT_ELEM); 165 } 166 167 private static void encodeInputDay(UIComponent uiComponent, ResponseWriter writer, String clientId, UserData userData, boolean disabled) 168 throws IOException { 169 encodeInputField(uiComponent, writer, clientId + ID_DAY_POSTFIX, userData.getDay(), 2, disabled); 170 } 171 172 private static void encodeInputMonth(UIComponent uiComponent, ResponseWriter writer, String clientId, UserData userData, Locale currentLocale, 173 boolean disabled) throws IOException { 174 writer.startElement(HTML.SELECT_ELEM, uiComponent); 175 writer.writeAttribute(HTML.ID_ATTR, clientId + ID_MONTH_POSTFIX, null); 176 writer.writeAttribute(HTML.NAME_ATTR, clientId + ID_MONTH_POSTFIX, null); 177 writer.writeAttribute(HTML.SIZE_ATTR, "1", null); 178 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES); 179 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.EVENT_HANDLER_ATTRIBUTES); 180 181 if (disabled) { 182 writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null); 183 } 184 185 int selectedMonth = userData.getMonth() == null ? -1 : Integer.parseInt(userData.getMonth())-1; 186 187 String [] months = HtmlCalendarRenderer.mapMonths(new DateFormatSymbols (currentLocale)); 188 for (int i = 0; i < months.length; i++) { 189 String monthName = months[i]; 190 String monthNumber = Integer.toString(i+1); 191 192 writer.write("\t\t"); 193 writer.startElement(HTML.OPTION_ELEM, null); 194 writer.writeAttribute(HTML.VALUE_ATTR, monthNumber, null); 195 196 if (i == selectedMonth) 197 writer.writeAttribute(HTML.SELECTED_ATTR, HTML.SELECTED_ATTR, null); 198 199 writer.writeText(monthName, null); 200 201 writer.endElement(HTML.OPTION_ELEM); 202 } 203 204 writer.writeText("", null); 206 writer.endElement(HTML.SELECT_ELEM); 207 } 208 209 private static void encodeInputYear(UIComponent uiComponent, ResponseWriter writer, String clientId, UserData userData, boolean disabled) throws IOException { 210 encodeInputField(uiComponent, writer, clientId + ID_YEAR_POSTFIX, userData.getYear(), 4, disabled); 211 } 212 213 private static void encodeInputHours(UIComponent uiComponent, ResponseWriter writer, String clientId, UserData userData, boolean disabled) throws IOException { 214 encodeInputField(uiComponent, writer, clientId + ID_HOURS_POSTFIX, userData.getHours(), 2, disabled); 215 } 216 217 private static void encodeInputMinutes(UIComponent uiComponent, ResponseWriter writer, String clientId, UserData userData, boolean disabled) throws IOException { 218 encodeInputField(uiComponent, writer, clientId + ID_MINUTES_POSTFIX, userData.getMinutes(), 2, disabled); 219 } 220 221 private static void encodeInputSeconds(UIComponent uiComponent, ResponseWriter writer, String clientId, UserData userData, boolean disabled) throws IOException { 222 encodeInputField(uiComponent, writer, clientId + ID_SECONDS_POSTFIX, userData.getSeconds(), 2, disabled); 223 } 224 225 private void encodePopupCalendarButton(FacesContext facesContext, ResponseWriter writer, String clientId, Locale currentLocale) throws IOException { 226 HtmlCalendarRenderer.addScriptAndCSSResources(facesContext); 227 228 DateFormatSymbols symbols = new DateFormatSymbols (currentLocale); 229 230 String localizedLanguageScript = HtmlCalendarRenderer.getLocalizedLanguageScript( 231 symbols, 232 HtmlCalendarRenderer.mapMonths(symbols), 233 Calendar.getInstance(currentLocale).getFirstDayOfWeek(), 234 null); 235 236 writer.startElement(HTML.SCRIPT_ELEM,null); 237 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null); 238 writer.write(localizedLanguageScript); 239 243 writer.endElement(HTML.SCRIPT_ELEM); 244 245 String dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext, null); 246 writer.write("<input type='button' onclick='jscalendarPopUpCalendarForInputDate(\""+clientId+"\",\""+dateFormat+"\")' value='...'/>"); 247 } 248 249 public void decode(FacesContext facesContext, UIComponent uiComponent) { 250 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlInputDate.class); 251 252 if( isDisabled(facesContext, uiComponent) ) return; 254 255 HtmlInputDate inputDate = (HtmlInputDate) uiComponent; 256 Locale currentLocale = facesContext.getViewRoot().getLocale(); 257 UserData userData = (UserData) inputDate.getSubmittedValue(); 258 if( userData == null ) 259 userData = inputDate.getUserData(currentLocale); 260 String clientId = inputDate.getClientId(facesContext); 261 String type = inputDate.getType(); 262 Map requestMap = facesContext.getExternalContext().getRequestParameterMap(); 263 264 if( ! type.equals( "time" ) ){ 265 userData.setDay( (String ) requestMap.get(clientId + ID_DAY_POSTFIX) ); 266 userData.setMonth( (String ) requestMap.get(clientId + ID_MONTH_POSTFIX) ); 267 userData.setYear( (String ) requestMap.get(clientId + ID_YEAR_POSTFIX) ); 268 } 269 270 if( ! type.equals( "date" ) ){ 271 userData.setHours( (String ) requestMap.get(clientId + ID_HOURS_POSTFIX) ); 272 userData.setMinutes( (String ) requestMap.get(clientId + ID_MINUTES_POSTFIX) ); 273 userData.setSeconds( (String ) requestMap.get(clientId + ID_SECONDS_POSTFIX) ); 274 } 275 276 inputDate.setSubmittedValue( userData ); 277 } 278 279 public Object getConvertedValue(FacesContext context, UIComponent uiComponent, Object submittedValue) throws ConverterException { 280 UserData userData = (UserData) submittedValue; 281 try { 282 return userData.parse(); 283 } catch (ParseException e) { 284 Object [] args = {uiComponent.getId()}; 285 throw new ConverterException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, DATE_MESSAGE_ID, args)); 286 } 287 } 288 } | Popular Tags |