📄 htmlcalendarrenderer.java
字号:
/* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.myfaces.custom.calendar;import org.apache.myfaces.component.html.ext.HtmlInputText;import org.apache.myfaces.component.html.util.AddResource;import org.apache.myfaces.renderkit.JSFAttr;import org.apache.myfaces.renderkit.RendererUtils;import org.apache.myfaces.renderkit.html.HTML;import org.apache.myfaces.renderkit.html.HtmlRenderer;import org.apache.myfaces.renderkit.html.HtmlRendererUtils;import org.apache.myfaces.renderkit.html.util.JavascriptUtils;import javax.faces.application.Application;import javax.faces.component.UIComponent;import javax.faces.component.UIInput;import javax.faces.component.UIParameter;import javax.faces.component.html.HtmlCommandLink;import javax.faces.component.html.HtmlOutputText;import javax.faces.context.FacesContext;import javax.faces.context.ResponseWriter;import javax.faces.convert.Converter;import javax.faces.convert.ConverterException;import java.io.IOException;import java.text.DateFormat;import java.text.DateFormatSymbols;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.List;/** * $Log: HtmlCalendarRenderer.java,v $ * Revision 1.21 2005/04/08 02:02:26 schof * Fixes MYFACES-167 (Thanks to David Heffelfinger for reporting the bug and supplying the patch.) * * Revision 1.20 2005/02/11 14:11:04 mmarinschek * fixed jira 97 * * Revision 1.19 2005/02/11 13:24:01 mmarinschek * fix jira 60, showing fix in examples * * Revision 1.18 2005/01/17 10:08:44 mmarinschek * trying to fix problem with added resources to header; css and js was not changeable by the user anymore... * * Revision 1.17 2005/01/09 18:15:12 mmarinschek * small changes - better error handling, label renderer supports more hooks for sub-classes * * Revision 1.16 2004/12/27 04:11:11 mmarinschek * Data Table stores the state of facets of children; script tag is rendered with type attribute instead of language attribute, popup works better as a column in a data table * * Revision 1.15 2004/12/10 02:08:54 svieujot * Share code with HtmlInputDate. * * Revision 1.14 2004/12/09 12:18:43 mmarinschek * changes in Calendar-Renderer to check for submitted-value first * * Revision 1.13 2004/12/09 05:13:02 svieujot * Mark potential bugs where we use the backing bean's value, and do not check for submitted value * * Revision 1.12 2004/12/02 22:26:23 svieujot * Simplify the AddResource methods * * Revision 1.11 2004/12/01 20:25:10 svieujot * Make the Extensions filter support css and image resources. * Convert the popup calendar to use this new filter. * * Revision 1.10 2004/11/29 13:57:56 mmarinschek * changes for input-calendar bugs * * Revision 1.9 2004/10/13 11:50:57 matze * renamed packages to org.apache * * Revision 1.8 2004/09/06 08:41:48 tinytoony * changes to calendar - rendering wrong weekday, check output-text behavior * * Revision 1.7 2004/07/28 18:00:47 tinytoony * calendar; revisited again for complete i18 * * Revision 1.6 2004/07/28 17:13:54 tinytoony * new calendar popup, revisited, global variables renamed to help with uniqueness * * Revision 1.5 2004/07/27 16:48:02 tinytoony * new calendar popup, revisited * * Revision 1.4 2004/07/27 06:28:32 tinytoony * new calendar component as a popup * * Revision 1.3 2004/07/01 21:53:11 mwessendorf * ASF switch * * Revision 1.2 2004/04/20 14:39:12 royalts * writeLink: added text.setValue(content), removed link.setValue(content) * * Revision 1.1 2004/03/31 11:58:34 manolito * custom component refactoring * * Revision 1.6 2004/03/29 15:06:17 manolito * no longer depends on specific LinkRenderer * * @author Martin Marinschek (latest modification by $Author: schof $) * @version $Revision: 1.21 $ $Date: 2005/04/08 02:02:26 $ */public class HtmlCalendarRenderer extends HtmlRenderer{ //private static Log log = LogFactory.getLog(HtmlCalendarRenderer.class); public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException { RendererUtils.checkParamValidity(facesContext, component, HtmlInputCalendar.class); HtmlInputCalendar inputCalendar = (HtmlInputCalendar) component; Locale currentLocale = facesContext.getViewRoot().getLocale(); Date value; try { value = RendererUtils.getDateValue(inputCalendar); } catch (IllegalArgumentException illegalArgumentException) { value = null; } Calendar timeKeeper = Calendar.getInstance(currentLocale); timeKeeper.setTime(value!=null?value:new Date()); DateFormatSymbols symbols = new DateFormatSymbols(currentLocale); String[] weekdays = mapWeekdays(symbols); String[] months = mapMonths(symbols); if(inputCalendar.isRenderAsPopup()) { if(inputCalendar.isAddResources()) addScriptAndCSSResources(facesContext); String dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext, inputCalendar.getPopupDateFormat()); Application application = facesContext.getApplication(); HtmlInputText inputText = null; List li = inputCalendar.getChildren(); for (int i = 0; i < li.size(); i++) { UIComponent uiComponent = (UIComponent) li.get(i); if(uiComponent instanceof HtmlInputText) { inputText = (HtmlInputText) uiComponent; break; } } if(inputText == null) { inputText = (HtmlInputText) application.createComponent(HtmlInputText.COMPONENT_TYPE); } RendererUtils.copyHtmlInputTextAttributes(inputCalendar, inputText); inputText.setTransient(true); if (value == null && inputCalendar.getSubmittedValue() != null) { inputText.setValue(inputCalendar.getSubmittedValue()); } else { inputText.setValue(getConverter(inputCalendar).getAsString( facesContext,inputCalendar,value)); } inputText.setEnabledOnUserRole(inputCalendar.getEnabledOnUserRole()); inputText.setVisibleOnUserRole(inputCalendar.getVisibleOnUserRole()); inputCalendar.getChildren().add(inputText); RendererUtils.renderChild(facesContext, inputText); inputCalendar.getChildren().remove(inputText); ResponseWriter writer = facesContext.getResponseWriter(); writer.startElement(HTML.SCRIPT_ELEM,null); writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null); writer.write("<!--\n"); writer.writeText(getLocalizedLanguageScript(symbols, months, timeKeeper.getFirstDayOfWeek(),inputCalendar),null); writer.writeText(getScriptBtnText(inputCalendar.getClientId(facesContext), dateFormat,inputCalendar.getPopupButtonString()),null); writer.write("\n-->"); writer.endElement(HTML.SCRIPT_ELEM);/* writer.startElement(HTML.INPUT_ELEM,null); writer.writeAttribute(HTML.TYPE_ATTR,HTML.INPUT_TYPE_BUTTON,null); writer.writeAttribute(HTML.ONCLICK_ATTR,"popUpCalendar(this, "+inputText.getClientId(facesContext)+ ", \\\"dd.mm.yyyy\\\")",null); writer.endElement(HTML.INPUT_TYPE_BUTTON);*/ } else { int lastDayInMonth = timeKeeper.getActualMaximum(Calendar.DAY_OF_MONTH); int currentDay = timeKeeper.get(Calendar.DAY_OF_MONTH); if (currentDay > lastDayInMonth) currentDay = lastDayInMonth; timeKeeper.set(Calendar.DAY_OF_MONTH, 1); int weekDayOfFirstDayOfMonth = mapCalendarDayToCommonDay(timeKeeper.get(Calendar.DAY_OF_WEEK)); int weekStartsAtDayIndex = mapCalendarDayToCommonDay(timeKeeper.getFirstDayOfWeek()); ResponseWriter writer = facesContext.getResponseWriter(); HtmlRendererUtils.writePrettyLineSeparator(facesContext); HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TABLE_ELEM, component); HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.UNIVERSAL_ATTRIBUTES); HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.EVENT_HANDLER_ATTRIBUTES); writer.flush(); HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TR_ELEM, component); if(inputCalendar.getMonthYearRowClass() != null) writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getMonthYearRowClass(), null); writeMonthYearHeader(facesContext, writer, inputCalendar, timeKeeper, currentDay, weekdays, months); writer.endElement(HTML.TR_ELEM); HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TR_ELEM, component); if(inputCalendar.getWeekRowClass() != null) writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getWeekRowClass(), null); writeWeekDayNameHeader(weekStartsAtDayIndex, weekdays, facesContext, writer, inputCalendar); writer.endElement(HTML.TR_ELEM); HtmlRendererUtils.writePrettyLineSeparator(facesContext); writeDays(facesContext, writer, inputCalendar, timeKeeper, currentDay, weekStartsAtDayIndex, weekDayOfFirstDayOfMonth, lastDayInMonth, weekdays); writer.endElement(HTML.TABLE_ELEM); } } /** * Used by the x:inputDate renderer : HTMLDateRenderer * @throws IOException */ static public void addScriptAndCSSResources(FacesContext facesContext) throws IOException{ // Add the javascript and CSS pages AddResource.addStyleSheet(HtmlCalendarRenderer.class, "WH/theme.css", facesContext); AddResource.addStyleSheet(HtmlCalendarRenderer.class, "DB/theme.css", facesContext); AddResource.addJavaScriptToHeader(HtmlCalendarRenderer.class, "popcalendar.js", facesContext); ResponseWriter writer = facesContext.getResponseWriter(); writer.startElement(HTML.SCRIPT_ELEM, null); writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null); writer.write( "jscalendarSetImageDirectory(\"" +JavascriptUtils.encodeString( AddResource.getResourceMappedPath(HtmlCalendarRenderer.class, "DB/", facesContext) ) +"\")"); writer.endElement(HTML.SCRIPT_ELEM); } public static String getLocalizedLanguageScript(DateFormatSymbols symbols, String[] months, int firstDayOfWeek, HtmlInputCalendar inputCalendar) { int realFirstDayOfWeek = firstDayOfWeek-1/*Java has different starting-point*/; String[] weekDays; if(realFirstDayOfWeek==0) { weekDays = mapWeekdaysStartingWithSunday(symbols); } else if(realFirstDayOfWeek==1) { weekDays = mapWeekdays(symbols); } else throw new IllegalStateException("Week may only start with sunday or monday."); StringBuffer script = new StringBuffer(); defineStringArray(script, "jscalendarMonthName", months); defineStringArray(script, "jscalendarDayName", weekDays); setIntegerVariable(script, "jscalendarStartAt",realFirstDayOfWeek); if( inputCalendar != null ){ // To allow null parameter for inputDate tag. if(inputCalendar.getPopupGotoString()!=null) setStringVariable(script, "jscalendarGotoString",inputCalendar.getPopupGotoString()); if(inputCalendar.getPopupTodayString()!=null) setStringVariable(script, "jscalendarTodayString",inputCalendar.getPopupTodayString()); if(inputCalendar.getPopupWeekString()!=null) setStringVariable(script, "jscalendarWeekString",inputCalendar.getPopupWeekString()); if(inputCalendar.getPopupScrollLeftMessage()!=null) setStringVariable(script, "jscalendarScrollLeftMessage",inputCalendar.getPopupScrollLeftMessage()); if(inputCalendar.getPopupScrollRightMessage()!=null) setStringVariable(script, "jscalendarScrollRightMessage",inputCalendar.getPopupScrollRightMessage()); if(inputCalendar.getPopupSelectMonthMessage()!=null) setStringVariable(script, "jscalendarSelectMonthMessage",inputCalendar.getPopupSelectMonthMessage()); if(inputCalendar.getPopupSelectYearMessage()!=null) setStringVariable(script, "jscalendarSelectYearMessage",inputCalendar.getPopupSelectYearMessage()); if(inputCalendar.getPopupSelectDateMessage()!=null) setStringVariable(script, "jscalendarSelectDateMessage",inputCalendar.getPopupSelectDateMessage()); } return script.toString(); } private static void setIntegerVariable(StringBuffer script, String name, int value) { script.append(name); script.append(" = "); script.append(value); script.append(";\n"); } private static void setStringVariable(StringBuffer script, String name, String value) { script.append(name); script.append(" = \""); script.append(value); script.append("\";\n"); } private static void defineStringArray(StringBuffer script, String arrayName, String[] array) { script.append(arrayName); script.append(" = new Array("); for(int i=0;i<array.length;i++) { if(i!=0) script.append(","); script.append("\""); script.append(array[i]); script.append("\""); } script.append(");"); } private String getScriptBtnText(String clientId, String dateFormat, String popupButtonString) { StringBuffer script = new StringBuffer(); script.append("if (!document.layers) {\n"); script.append("document.write("); script.append("\"<input type='button' onclick='jscalendarPopUpCalendar(this,this.form.elements[\\\""); script.append(clientId); script.append("\\\"],\\\""); script.append(dateFormat); script.append("\\\")' value='"); if(popupButtonString==null) popupButtonString="..."; script.append(popupButtonString); script.append("'/>\""); script.append(");");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -