📄 htmlinputdaterenderer.java
字号:
/* * HtmlInputDateRenderer.java * * Created on 2007-9-24, 22:15:06 * * To change this template, choose Tools | Templates * and open the template in the editor. */package biz.tbuy.common.components.test;import java.io.IOException;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Map;import javax.faces.component.EditableValueHolder;import javax.faces.component.UIComponent;import javax.faces.component.UIInput;import javax.faces.context.FacesContext;import javax.faces.context.ResponseWriter;import javax.faces.convert.Converter;import javax.faces.render.Renderer;/** * * @author Administrator */public class HtmlInputDateRenderer extends Renderer{ public HtmlInputDateRenderer() { super(); } private static final String MONTH = ".month"; private static final String DAY = ".day"; private static final String YEAR = ".year"; // Renderer default values private static final int defaultInputFieldSize = 10; private static final int defaultStartYear = 1980; private static final int defaultEndYear = 2025; // In a production component, these would be localized names; static String monthnames[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; // Rendering begins here @Override public void encodeBegin(FacesContext fc, UIComponent ui) throws IOException { // DEclare threadsafe method variables for current month, day & year int currMonth, currDay, monthMaxDay, currYear; // Initalize current date values EditableValueHolder inputdate = (EditableValueHolder)ui; Calendar tempcal = new GregorianCalendar(); // Get current value of Date and apply - if null set date to today Date dateval = (Date)inputdate.getValue(); if (dateval != null) { tempcal.setTime(dateval); } else { tempcal.setTime(new java.util.Date()); } currMonth = tempcal.get(Calendar.MONTH); currDay = tempcal.get(Calendar.DAY_OF_MONTH); monthMaxDay = tempcal.getActualMaximum(Calendar.DAY_OF_MONTH); currYear = tempcal.get(Calendar.YEAR); ResponseWriter rw = fc.getResponseWriter(); String clientId = ui.getClientId(fc); encodeInputField(rw, clientId, fc, ui); encodeMonthSelect(rw, clientId, ui, currMonth); encodeDaySelect(rw, clientId, ui, currDay, monthMaxDay); encodeYearSelect(rw, clientId, ui, currYear); } private void encodeInputField(ResponseWriter rw, String clientId, FacesContext fc, UIComponent ui) throws IOException { // Render a read-only input field with component's current date value rw.startElement("input", ui); rw.writeAttribute("name", clientId, "clientId"); Object val = ((UIInput)ui).getValue(); // 获得组件的值 if (val != null) { Converter conv = ((UIInput)ui).getConverter(); // 检查组件是否有转换器 if (conv != null) { // 有则先进行转换再写入 rw.writeAttribute("value", conv.getAsString(fc, ui, val), "value"); } else { // 否则直接写入值 rw.writeAttribute("value", val, "value"); } } else { // 如果组件无值,则直接写入空值 rw.writeAttribute("value", "", "value"); } // 获取组件的长度值 Integer size = (Integer)ui.getAttributes().get("size"); if (size != null) { rw.writeAttribute("size", size, "size"); } else { rw.writeAttribute("size", defaultInputFieldSize, "size"); } rw.writeAttribute("readonly", Boolean.TRUE, null); rw.endElement("input"); } private void encodeMonthSelect(ResponseWriter rw, String clientId, UIComponent ui, int currMonth) throws IOException { rw.startElement("select", ui); rw.writeAttribute("size", "1", null); rw.writeAttribute("name", clientId + MONTH, null); for (int i = 0; i < 12; i++) { rw.startElement("option", ui); rw.writeAttribute("value", "" + i, null); if (i == currMonth) { rw.writeAttribute("selected", Boolean.TRUE, null); } rw.writeText(monthnames[i], null); rw.endElement("option"); } rw.endElement("select"); } private void encodeDaySelect(ResponseWriter rw, String clientId, UIComponent ui, int currDay, int monthMaxDay) throws IOException { rw.startElement("select", ui); rw.writeAttribute("size", "1", null); rw.writeAttribute("name", clientId + DAY, null); for (int i = 0; i < monthMaxDay; i++) { rw.startElement("option", ui); rw.writeAttribute("value", "" + i, null); if (i == currDay) { rw.writeAttribute("selected", Boolean.TRUE, null); } rw.writeText("" + i, null); rw.endElement("option"); } rw.endElement("select"); } private void encodeYearSelect(ResponseWriter rw, String clientId, UIComponent ui, int currYear) throws IOException{ Integer startyear = null; Integer endyear = null; startyear = (Integer)ui.getAttributes().get("startyear"); if (startyear == null) { startyear = new Integer(defaultStartYear); // 使用默认的年份 } endyear = (Integer)ui.getAttributes().get("endyear"); if (endyear == null) { endyear = new Integer(defaultEndYear); } rw.startElement("select", ui); rw.writeAttribute("size", "1", null); rw.writeAttribute("name", clientId + YEAR, null); for (int i = startyear; i <= endyear; i++) { rw.startElement("option", ui); rw.writeAttribute("value", "" + i, null); if (i == currYear) { rw.writeAttribute("selected", Boolean.TRUE, null); } rw.writeText("" + i, null); rw.endElement("option"); } rw.endElement("select"); } /** decode ****************************************************************/ @Override public void decode(FacesContext fc, UIComponent ui) { EditableValueHolder inputdate = (EditableValueHolder)ui; Map map = fc.getExternalContext().getRequestParameterMap(); String clientId = ui.getClientId(fc); Calendar calDate = new GregorianCalendar(); if (map.containsKey(clientId + MONTH) && map.containsKey(clientId + DAY) && map.containsKey(clientId + YEAR)) { int yy = Integer.parseInt((String)map.get(clientId + YEAR)); int mm = Integer.parseInt((String)map.get(clientId + MONTH)); int dd = Integer.parseInt((String)map.get(clientId + DAY)); calDate.set(yy, mm, dd); inputdate.setSubmittedValue(calDate.getTime()); } else { inputdate.setSubmittedValue(new java.util.Date()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -