1 17 package org.alfresco.web.ui.common.renderer; 18 19 import java.io.IOException ; 20 import java.text.DateFormatSymbols ; 21 import java.util.ArrayList ; 22 import java.util.Calendar ; 23 import java.util.Date ; 24 import java.util.GregorianCalendar ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 30 import javax.faces.component.EditableValueHolder; 31 import javax.faces.component.UIComponent; 32 import javax.faces.component.ValueHolder; 33 import javax.faces.context.FacesContext; 34 import javax.faces.context.ResponseWriter; 35 import javax.faces.convert.ConverterException; 36 import javax.faces.model.SelectItem; 37 38 import org.alfresco.web.app.Application; 39 40 47 public class DatePickerRenderer extends BaseRenderer 48 { 49 private static final String FIELD_YEAR = "_year"; 50 private static final String FIELD_MONTH = "_month"; 51 private static final String FIELD_DAY = "_day"; 52 private static final String FIELD_HOUR = "_hour"; 53 private static final String FIELD_MINUTE = "_minute"; 54 55 62 public void decode(FacesContext context, UIComponent component) 63 { 64 try 65 { 66 String clientId = component.getClientId(context); 68 Map params = context.getExternalContext().getRequestParameterMap(); 69 String year = (String )params.get(clientId + FIELD_YEAR); 70 if (year != null) 71 { 72 String month = (String )params.get(clientId + FIELD_MONTH); 74 String day = (String )params.get(clientId + FIELD_DAY); 75 String hour = (String )params.get(clientId + FIELD_HOUR); 76 String minute = (String )params.get(clientId + FIELD_MINUTE); 77 78 int[] parts = new int[5]; 80 parts[0] = Integer.parseInt(year); 81 parts[1] = Integer.parseInt(month); 82 parts[2] = Integer.parseInt(day); 83 parts[3] = Integer.parseInt(hour); 84 parts[4] = Integer.parseInt(minute); 85 86 ((EditableValueHolder)component).setSubmittedValue(parts); 89 } 90 } 91 catch (NumberFormatException nfe) 92 { 93 } 95 } 96 97 106 public Object getConvertedValue(FacesContext context, UIComponent component, Object val) throws ConverterException 107 { 108 int[] parts = (int[])val; 109 Calendar date = new GregorianCalendar (parts[0], parts[1], parts[2], parts[3], parts[4]); 110 return date.getTime(); 111 } 112 113 120 public void encodeBegin(FacesContext context, UIComponent component) 121 throws IOException 122 { 123 if (component.isRendered() == true) 125 { 126 Date date = null; 127 128 int[] submittedValue = (int[])((EditableValueHolder)component).getSubmittedValue(); 131 if (submittedValue != null) 132 { 133 date = (Date )getConvertedValue(context, component, submittedValue); 134 } 135 else 136 { 137 Object value = ((ValueHolder)component).getValue(); 139 date = value instanceof Date ? (Date )value : new Date (); 141 } 142 143 int nStartYear; 145 Integer startYear = (Integer )component.getAttributes().get("startYear"); 146 if (startYear != null) 147 { 148 nStartYear = startYear.intValue(); 149 } 150 else 151 { 152 nStartYear = new Date ().getYear() + 1900 + 2; } 154 155 int nYearCount = 25; 156 Integer yearCount = (Integer )component.getAttributes().get("yearCount"); 157 if (yearCount != null) 158 { 159 nYearCount = yearCount.intValue(); 160 } 161 162 String clientId = component.getClientId(context); 166 ResponseWriter out = context.getResponseWriter(); 167 168 Calendar calendar = new GregorianCalendar (); 171 calendar.setTime(date); 172 renderMenu(out, component, getDays(), calendar.get(Calendar.DAY_OF_MONTH), clientId + FIELD_DAY); 173 renderMenu(out, component, getMonths(), calendar.get(Calendar.MONTH), clientId + FIELD_MONTH); 174 renderMenu(out, component, getYears(nStartYear, nYearCount), calendar.get(Calendar.YEAR), clientId + FIELD_YEAR); 175 176 Boolean showTime = (Boolean )component.getAttributes().get("showTime"); 178 if (showTime == null) 179 { 180 showTime = Boolean.FALSE; 181 } 182 183 out.write(" "); 184 renderTimeElement(out, component, calendar.get(Calendar.HOUR_OF_DAY), clientId + FIELD_HOUR, showTime.booleanValue()); 185 if (showTime.booleanValue()) 186 { 187 out.write(" : "); 188 } 189 renderTimeElement(out, component, calendar.get(Calendar.MINUTE), clientId + FIELD_MINUTE, showTime.booleanValue()); 190 } 191 } 192 193 204 private void renderMenu(ResponseWriter out, UIComponent component, List items, 205 int selected, String clientId) 206 throws IOException 207 { 208 out.write("<select"); 209 outputAttribute(out, clientId, "name"); 210 211 if (component.getAttributes().get("styleClass") != null) 212 { 213 outputAttribute(out, component.getAttributes().get("styleClass"), "class"); 214 } 215 if (component.getAttributes().get("style") != null) 216 { 217 outputAttribute(out, component.getAttributes().get("style"), "style"); 218 } 219 if (component.getAttributes().get("disabled") != null) 220 { 221 outputAttribute(out, component.getAttributes().get("disabled"), "disabled"); 222 } 223 out.write(">"); 224 225 for (Iterator i=items.iterator(); i.hasNext(); ) 226 { 227 SelectItem item = (SelectItem)i.next(); 228 Integer value = (Integer )item.getValue(); 229 out.write("<option"); 230 outputAttribute(out, value, "value"); 231 232 if (value.intValue() == selected) 234 { 235 outputAttribute(out, "selected", "selected"); 236 } 237 out.write(">"); 238 out.write(item.getLabel()); 239 out.write("</option>"); 240 } 241 out.write("</select>"); 242 } 243 244 251 private void renderTimeElement(ResponseWriter out, UIComponent component, 252 int currentValue, String clientId, boolean showTime) throws IOException 253 { 254 out.write("<input"); 255 outputAttribute(out, clientId, "name"); 256 257 if (showTime) 258 { 259 out.write(" type='text' size='1' maxlength='2'"); 260 261 if (component.getAttributes().get("disabled") != null) 262 { 263 outputAttribute(out, component.getAttributes().get("disabled"), "disabled"); 264 } 265 } 266 else 267 { 268 out.write(" type='hidden'"); 269 } 270 271 String strValue = Integer.toString(currentValue); 273 if (strValue.length() == 1) 274 { 275 strValue = "0" + strValue; 276 } 277 278 outputAttribute(out, strValue, "value"); 279 out.write("/>"); 280 } 281 282 private List getYears(int startYear, int yearCount) 283 { 284 List <SelectItem> years = new ArrayList <SelectItem>(); 285 for (int i=startYear; i>startYear - yearCount; i--) 286 { 287 Integer year = Integer.valueOf(i); 288 years.add(new SelectItem(year, year.toString())); 289 } 290 return years; 291 } 292 293 private List getMonths() 294 { 295 Locale locale = Application.getLanguage(FacesContext.getCurrentInstance()); 297 if (locale == null) 298 { 299 locale = locale.getDefault(); 300 } 301 DateFormatSymbols dfs = new DateFormatSymbols (locale); 302 String [] names = dfs.getMonths(); 303 List <SelectItem> months = new ArrayList <SelectItem>(12); 304 for (int i=0; i<12; i++) 305 { 306 Integer key = Integer.valueOf(i); 307 months.add(new SelectItem(key, names[i])); 308 } 309 return months; 310 } 311 312 private List getDays() 313 { 314 List <SelectItem> days = new ArrayList <SelectItem>(31); 315 for (int i=1; i<32; i++) 316 { 317 Integer day = Integer.valueOf(i); 318 days.add(new SelectItem(day, day.toString())); 319 } 320 return days; 321 } 322 } 323 | Popular Tags |