⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htmlcalendarrenderer.java

📁 一个使用struts+hibernate+spring开发的完的网站源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        script.append("\n}");        return script.toString();    }    private void writeMonthYearHeader(FacesContext facesContext, ResponseWriter writer, UIInput inputComponent, Calendar timeKeeper,                                      int currentDay, String[] weekdays,                                      String[] months)            throws IOException    {        Calendar cal = shiftMonth(facesContext, timeKeeper, currentDay, -1);        writeCell(facesContext, writer, inputComponent, "<", cal.getTime(), null);        writer.startElement(HTML.TD_ELEM, inputComponent);        writer.writeAttribute(HTML.COLSPAN_ATTR, new Integer(weekdays.length - 2), null);        writer.writeText(months[timeKeeper.get(Calendar.MONTH)] + " " + timeKeeper.get(Calendar.YEAR), null);        writer.endElement(HTML.TD_ELEM);        cal = shiftMonth(facesContext, timeKeeper, currentDay, 1);        writeCell(facesContext, writer, inputComponent, ">", cal.getTime(), null);    }    private Calendar shiftMonth(FacesContext facesContext,                                Calendar timeKeeper, int currentDay, int shift)    {        Calendar cal = copyCalendar(facesContext, timeKeeper);        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + shift);        if(currentDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH))            currentDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);        cal.set(Calendar.DAY_OF_MONTH, currentDay);        return cal;    }    private Calendar copyCalendar(FacesContext facesContext, Calendar timeKeeper)    {        Calendar cal = Calendar.getInstance(facesContext.getViewRoot().getLocale());        cal.set(Calendar.YEAR, timeKeeper.get(Calendar.YEAR));        cal.set(Calendar.MONTH, timeKeeper.get(Calendar.MONTH));        cal.set(Calendar.HOUR_OF_DAY, timeKeeper.get(Calendar.HOUR_OF_DAY));        cal.set(Calendar.MINUTE, timeKeeper.get(Calendar.MINUTE));        cal.set(Calendar.SECOND, timeKeeper.get(Calendar.SECOND));        cal.set(Calendar.MILLISECOND, timeKeeper.get(Calendar.MILLISECOND));        return cal;    }    private void writeWeekDayNameHeader(int weekStartsAtDayIndex, String[] weekdays, FacesContext facesContext, ResponseWriter writer, UIInput inputComponent)            throws IOException    {        for (int i = weekStartsAtDayIndex; i < weekdays.length; i++)            writeCell(facesContext,                    writer, inputComponent, weekdays[i], null, null);        for (int i = 0; i < weekStartsAtDayIndex; i++)            writeCell(facesContext, writer,                    inputComponent, weekdays[i], null, null);    }    private void writeDays(FacesContext facesContext, ResponseWriter writer,                           HtmlInputCalendar inputComponent, Calendar timeKeeper, int currentDay, int weekStartsAtDayIndex,                           int weekDayOfFirstDayOfMonth, int lastDayInMonth, String[] weekdays)            throws IOException    {        Calendar cal;        int space = (weekStartsAtDayIndex < weekDayOfFirstDayOfMonth) ? (weekDayOfFirstDayOfMonth - weekStartsAtDayIndex)                : (weekdays.length - weekStartsAtDayIndex + weekDayOfFirstDayOfMonth);        if (space == weekdays.length)            space = 0;        int columnIndexCounter = 0;        for (int i = 0; i < space; i++)        {            if (columnIndexCounter == 0)            {                writer.startElement(HTML.TR_ELEM, inputComponent);            }            writeCell(facesContext, writer, inputComponent, "",                    null, inputComponent.getDayCellClass());            columnIndexCounter++;        }        for (int i = 0; i < lastDayInMonth; i++)        {            if (columnIndexCounter == 0)            {                writer.startElement(HTML.TR_ELEM, inputComponent);            }            cal = copyCalendar(facesContext, timeKeeper);            cal.set(Calendar.DAY_OF_MONTH, i + 1);            String cellStyle = inputComponent.getDayCellClass();            if((currentDay - 1) == i)                cellStyle = inputComponent.getCurrentDayCellClass();            writeCell(facesContext, writer,                    inputComponent, String.valueOf(i + 1), cal.getTime(),                    cellStyle);            columnIndexCounter++;            if (columnIndexCounter == weekdays.length)            {                writer.endElement(HTML.TR_ELEM);                HtmlRendererUtils.writePrettyLineSeparator(facesContext);                columnIndexCounter = 0;            }        }        if (columnIndexCounter != 0)        {            for (int i = columnIndexCounter; i < weekdays.length; i++)            {                writeCell(facesContext, writer,                        inputComponent, "", null, inputComponent.getDayCellClass());            }            writer.endElement(HTML.TR_ELEM);            HtmlRendererUtils.writePrettyLineSeparator(facesContext);        }    }    private void writeCell(FacesContext facesContext,                           ResponseWriter writer, UIInput component, String content,                           Date valueForLink, String styleClass)            throws IOException    {        writer.startElement(HTML.TD_ELEM, component);        if (styleClass != null)            writer.writeAttribute(HTML.CLASS_ATTR, styleClass, null);        if (valueForLink == null)            writer.writeText(content, JSFAttr.VALUE_ATTR);        else        {            writeLink(content, component, facesContext, valueForLink);        }        writer.endElement(HTML.TD_ELEM);    }    private void writeLink(String content,                           UIInput component,                           FacesContext facesContext,                           Date valueForLink)            throws IOException    {        Converter converter = getConverter(component);        Application application = facesContext.getApplication();        HtmlCommandLink link                = (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE);        link.setId(component.getId() + "_" + valueForLink.getTime() + "_link");        link.setTransient(true);        link.setImmediate(component.isImmediate());        HtmlOutputText text                = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE);        text.setValue(content);        text.setId(component.getId() + "_" + valueForLink.getTime() + "_text");        text.setTransient(true);        UIParameter parameter                = (UIParameter)application.createComponent(UIParameter.COMPONENT_TYPE);        parameter.setId(component.getId() + "_" + valueForLink.getTime() + "_param");        parameter.setTransient(true);        parameter.setName(component.getClientId(facesContext));        parameter.setValue(converter.getAsString(facesContext, component, valueForLink));        component.getChildren().add(link);        link.getChildren().add(parameter);        link.getChildren().add(text);        RendererUtils.renderChild(facesContext, link);    }    private Converter getConverter(UIInput component)    {        Converter converter = component.getConverter();        if (converter == null)        {            converter = new CalendarDateTimeConverter();        }        return converter;    }    private int mapCalendarDayToCommonDay(int day)    {        switch (day)        {            case Calendar.TUESDAY:                return 1;            case Calendar.WEDNESDAY:                return 2;            case Calendar.THURSDAY:                return 3;            case Calendar.FRIDAY:                return 4;            case Calendar.SATURDAY:                return 5;            case Calendar.SUNDAY:                return 6;            default:                return 0;        }    }    private static String[] mapWeekdays(DateFormatSymbols symbols)    {        String[] weekdays = new String[7];        String[] localeWeekdays = symbols.getShortWeekdays();        weekdays[0] = localeWeekdays[Calendar.MONDAY];        weekdays[1] = localeWeekdays[Calendar.TUESDAY];        weekdays[2] = localeWeekdays[Calendar.WEDNESDAY];        weekdays[3] = localeWeekdays[Calendar.THURSDAY];        weekdays[4] = localeWeekdays[Calendar.FRIDAY];        weekdays[5] = localeWeekdays[Calendar.SATURDAY];        weekdays[6] = localeWeekdays[Calendar.SUNDAY];        return weekdays;    }    private static String[] mapWeekdaysStartingWithSunday(DateFormatSymbols symbols)    {        String[] weekdays = new String[7];        String[] localeWeekdays = symbols.getShortWeekdays();        weekdays[0] = localeWeekdays[Calendar.SUNDAY];        weekdays[1] = localeWeekdays[Calendar.MONDAY];        weekdays[2] = localeWeekdays[Calendar.TUESDAY];        weekdays[3] = localeWeekdays[Calendar.WEDNESDAY];        weekdays[4] = localeWeekdays[Calendar.THURSDAY];        weekdays[5] = localeWeekdays[Calendar.FRIDAY];        weekdays[6] = localeWeekdays[Calendar.SATURDAY];        return weekdays;    }    public static String[] mapMonths(DateFormatSymbols symbols)    {        String[] months = new String[12];        String[] localeMonths = symbols.getMonths();        months[0] = localeMonths[Calendar.JANUARY];        months[1] = localeMonths[Calendar.FEBRUARY];        months[2] = localeMonths[Calendar.MARCH];        months[3] = localeMonths[Calendar.APRIL];        months[4] = localeMonths[Calendar.MAY];        months[5] = localeMonths[Calendar.JUNE];        months[6] = localeMonths[Calendar.JULY];        months[7] = localeMonths[Calendar.AUGUST];        months[8] = localeMonths[Calendar.SEPTEMBER];        months[9] = localeMonths[Calendar.OCTOBER];        months[10] = localeMonths[Calendar.NOVEMBER];        months[11] = localeMonths[Calendar.DECEMBER];        return months;    }    public void decode(FacesContext facesContext, UIComponent component)    {        RendererUtils.checkParamValidity(facesContext, component, HtmlInputCalendar.class);        HtmlRendererUtils.decodeUIInput(facesContext, component);    }    public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException    {        RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlInputCalendar.class);        UIInput uiInput = (UIInput) uiComponent;        Converter converter = uiInput.getConverter();        if(converter==null)            converter = new CalendarDateTimeConverter();        if (!(submittedValue == null || submittedValue instanceof String))        {            throw new IllegalArgumentException("Submitted value of type String expected");        }        return converter.getAsObject(facesContext, uiComponent, (String) submittedValue);    }    public static class CalendarDateTimeConverter implements Converter    {        public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s)        {            if(s==null || s.trim().length()==0)                return null;            DateFormat dateFormat = null;            if(uiComponent instanceof HtmlInputCalendar && ((HtmlInputCalendar) uiComponent).isRenderAsPopup())            {                String popupDateFormat = ((HtmlInputCalendar) uiComponent).getPopupDateFormat();                dateFormat = new SimpleDateFormat(createJSPopupFormat(facesContext, popupDateFormat));            }            else            {                dateFormat = createStandardDateFormat(facesContext);            }            try            {                return dateFormat.parse(s);            }            catch (ParseException e)            {                ConverterException ex = new ConverterException(e);                throw ex;            }        }        public static String createJSPopupFormat(FacesContext facesContext, String popupDateFormat)        {            if(popupDateFormat == null)            {                SimpleDateFormat defaultDateFormat = createStandardDateFormat(facesContext);                popupDateFormat = defaultDateFormat.toPattern();            }            StringBuffer jsPopupDateFormat = new StringBuffer();            for(int i=0;i<popupDateFormat.length();i++)            {                char c = popupDateFormat.charAt(i);                if(c=='M')                    jsPopupDateFormat.append('M');                else if(c=='d')                    jsPopupDateFormat.append('d');                else if(c=='y')                    jsPopupDateFormat.append('y');                else if(c==' ')                    jsPopupDateFormat.append(' ');                else if(c=='.')                    jsPopupDateFormat.append('.');                else if(c=='/')                    jsPopupDateFormat.append('/');            }            return jsPopupDateFormat.toString().trim();        }        public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o)        {            Date date = (Date) o;            if(date==null)                return null;            DateFormat dateFormat = null;            if(uiComponent instanceof HtmlInputCalendar && ((HtmlInputCalendar) uiComponent).isRenderAsPopup())            {                String popupDateFormat = ((HtmlInputCalendar) uiComponent).getPopupDateFormat();                dateFormat = new SimpleDateFormat(createJSPopupFormat(facesContext, popupDateFormat));            }            else            {                dateFormat = createStandardDateFormat(facesContext);            }            return dateFormat.format(date);        }        private static SimpleDateFormat createStandardDateFormat(FacesContext facesContext)        {            DateFormat dateFormat;            dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,                    facesContext.getViewRoot().getLocale());            if(dateFormat instanceof SimpleDateFormat)                return (SimpleDateFormat) dateFormat;            else                return new SimpleDateFormat("dd.MM.yyyy");        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -