You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2007/12/07 11:50:43 UTC
svn commit: r602067 -
/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java
Author: skitching
Date: Fri Dec 7 02:50:42 2007
New Revision: 602067
URL: http://svn.apache.org/viewvc?rev=602067&view=rev
Log:
Code tidyup - no functional changes
* replace static Log object with non-static one
* factor code from encodeEnd method into separate renderPopup and renderInline methods
* add javadoc
Modified:
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java?rev=602067&r1=602066&r2=602067&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java Fri Dec 7 02:50:42 2007
@@ -54,18 +54,38 @@
import java.util.*;
/**
+ * Render a "calendar" which the user can use to choose a specific day.
+ * <p>
+ * This renderer behaves quite differently for "inline" and "popup" calendars.
+ * <p>
+ * When inline, this component automatically creates child components (text and
+ * link components) to represent all the dates, scrollers, etc. within itself and
+ * renders those children. Clicking on any link in the component causes the
+ * surrounding form to immediately submit.
+ * <p>
+ * When popup, this component just renders an empty span with the component id,
+ * and a dozen or so lines of javascript which create an instance of a
+ * "tomahawk calendar object", set its properties from the properties on the
+ * associated HtmlInputCalendar component, and then invoke it. That javascript
+ * object then dynamically builds DOM objects and attaches them to the empty span.
+ * This component also renders a button or image that toggles the visibility of
+ * that empty span, thereby making the popup appear and disappear.
+ * <p>
+ * Note that the two ways of generating the calendar use totally different code;
+ * one implementation is here and the other is in a javascript resource file. For
+ * obvious reasons the appearance of the two calendars should be similar, so if a
+ * feature is added in one place it is recommended that the other be updated also.
+ * <p>
* @author Martin Marinschek (latest modification by $Author$)
* @version $Revision$ $Date$
*/
public class HtmlCalendarRenderer
extends HtmlRenderer
{
- private static final Log log = LogFactory.getLog(HtmlCalendarRenderer.class);
+ private final Log log = LogFactory.getLog(HtmlCalendarRenderer.class);
private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.calendar.JAVASCRIPT_ENCODED";
- //private static Log log = LogFactory.getLog(HtmlCalendarRenderer.class);
-
public void encodeEnd(FacesContext facesContext, UIComponent component)
throws IOException
{
@@ -102,161 +122,181 @@
DateFormatSymbols symbols = new DateFormatSymbols(currentLocale);
- String[] weekdays = mapShortWeekdays(symbols);
- String[] months = mapMonths(symbols);
-
if(inputCalendar.isRenderAsPopup())
{
- if(inputCalendar.isAddResources())
- addScriptAndCSSResources(facesContext, component);
+ renderPopup(facesContext, inputCalendar, value, timeKeeper, symbols);
+ }
+ else
+ {
+ renderInline(facesContext, inputCalendar, value, timeKeeper, symbols);
+ }
- // Check for an enclosed converter:
- UIInput uiInput = (UIInput) component;
- Converter converter = uiInput.getConverter();
- String dateFormat = null;
- if (converter != null && converter instanceof DateTimeConverter) {
- dateFormat = ((DateTimeConverter) converter).getPattern();
- }
- if (dateFormat == null) {
- dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext,
- inputCalendar.getPopupDateFormat());
- }
-
- Application application = facesContext.getApplication();
-
- HtmlInputTextHelp inputText = getOrCreateInputTextChild(inputCalendar, application);
-
- RendererUtils.copyHtmlInputTextAttributes(inputCalendar, inputText);
-
- inputText.setConverter(null); // value for this transient component will already be converted
- inputText.setTransient(true);
- inputText.setHelpText(inputCalendar.getHelpText());
- inputText.setSelectText(true);
+ component.getChildren().removeAll(component.getChildren());
+ }
- if (value == null && inputCalendar.getSubmittedValue() != null)
- {
- inputText.setValue(inputCalendar.getSubmittedValue());
- }
- else
- {
- inputText.setValue(getConverter(inputCalendar).getAsString(
- facesContext,inputCalendar,value));
- }
- inputText.setDisabled(inputCalendar.isDisabled());
- inputText.setReadonly(inputCalendar.isReadonly());
- inputText.setEnabledOnUserRole(inputCalendar.getEnabledOnUserRole());
- inputText.setVisibleOnUserRole(inputCalendar.getVisibleOnUserRole());
+ private void renderPopup(
+ FacesContext facesContext,
+ HtmlInputCalendar inputCalendar,
+ Date value,
+ Calendar timeKeeper,
+ DateFormatSymbols symbols) throws IOException
+ {
+ if(inputCalendar.isAddResources())
+ addScriptAndCSSResources(facesContext, inputCalendar);
+
+ // Check for an enclosed converter:
+ UIInput uiInput = (UIInput) inputCalendar;
+ Converter converter = uiInput.getConverter();
+ String dateFormat = null;
+ if (converter != null && converter instanceof DateTimeConverter) {
+ dateFormat = ((DateTimeConverter) converter).getPattern();
+ }
+ if (dateFormat == null) {
+ dateFormat = CalendarDateTimeConverter.createJSPopupFormat(facesContext,
+ inputCalendar.getPopupDateFormat());
+ }
+
+ Application application = facesContext.getApplication();
+
+ HtmlInputTextHelp inputText = getOrCreateInputTextChild(inputCalendar, application);
- //This is where two components with the same id are in the tree,
- //so make sure that during the rendering the id is unique.
+ RendererUtils.copyHtmlInputTextAttributes(inputCalendar, inputText);
- inputCalendar.setId(inputCalendar.getId()+"tempId");
+ inputText.setConverter(null); // value for this transient component will already be converted
+ inputText.setTransient(true);
+ inputText.setHelpText(inputCalendar.getHelpText());
+ inputText.setSelectText(true);
- inputCalendar.getChildren().add(inputText);
+ if (value == null && inputCalendar.getSubmittedValue() != null)
+ {
+ inputText.setValue(inputCalendar.getSubmittedValue());
+ }
+ else
+ {
+ inputText.setValue(getConverter(inputCalendar).getAsString(
+ facesContext,inputCalendar,value));
+ }
+ inputText.setDisabled(inputCalendar.isDisabled());
+ inputText.setReadonly(inputCalendar.isReadonly());
+ inputText.setEnabledOnUserRole(inputCalendar.getEnabledOnUserRole());
+ inputText.setVisibleOnUserRole(inputCalendar.getVisibleOnUserRole());
- RendererUtils.renderChild(facesContext, inputText);
+ //This is where two components with the same id are in the tree,
+ //so make sure that during the rendering the id is unique.
- inputCalendar.getChildren().remove(inputText);
+ inputCalendar.setId(inputCalendar.getId()+"tempId");
- //Set back the correct id to the input calendar
- inputCalendar.setId(inputText.getId());
+ inputCalendar.getChildren().add(inputText);
- ResponseWriter writer = facesContext.getResponseWriter();
+ RendererUtils.renderChild(facesContext, inputText);
- writer.startElement(HTML.SPAN_ELEM,inputCalendar);
- writer.writeAttribute(HTML.ID_ATTR,inputCalendar.getClientId(facesContext)+"Span",
- JSFAttr.ID_ATTR);
- writer.endElement(HTML.SPAN_ELEM);
+ inputCalendar.getChildren().remove(inputText);
- if (!inputCalendar.isDisabled())
- {
- writer.startElement(HTML.SCRIPT_ELEM, component);
- writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
+ //Set back the correct id to the input calendar
+ inputCalendar.setId(inputText.getId());
- String calendarVar = JavascriptUtils.getValidJavascriptName(
- inputCalendar.getClientId(facesContext)+"CalendarVar",false);
+ ResponseWriter writer = facesContext.getResponseWriter();
- writer.writeText(calendarVar+"=new org_apache_myfaces_PopupCalendar();\n",null);
- writer.writeText(getLocalizedLanguageScript(facesContext,symbols,
- timeKeeper.getFirstDayOfWeek(),inputCalendar,calendarVar)+"\n",null);
- writer.writeText(calendarVar+".init(document.getElementById('"+
- inputCalendar.getClientId(facesContext)+"Span"+"'));\n",null);
- writer.endElement(HTML.SCRIPT_ELEM);
- if(!inputCalendar.isDisplayValueOnly())
- {
- getScriptBtn(writer, facesContext, inputCalendar,
- dateFormat,inputCalendar.getPopupButtonString(), new FunctionCallProvider(){
- public String getFunctionCall(FacesContext facesContext, UIComponent uiComponent, String dateFormat)
- {
- String clientId = uiComponent.getClientId(facesContext);
+ writer.startElement(HTML.SPAN_ELEM,inputCalendar);
+ writer.writeAttribute(HTML.ID_ATTR,inputCalendar.getClientId(facesContext)+"Span",
+ JSFAttr.ID_ATTR);
+ writer.endElement(HTML.SPAN_ELEM);
- String clientVar = JavascriptUtils.getValidJavascriptName(clientId+"CalendarVar",true);
+ if (!inputCalendar.isDisabled())
+ {
+ writer.startElement(HTML.SCRIPT_ELEM, inputCalendar);
+ writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
- return clientVar+"._popUpCalendar(this,document.getElementById('"+clientId+"'),'"+dateFormat+"')";
- }
- });
- }
+ String calendarVar = JavascriptUtils.getValidJavascriptName(
+ inputCalendar.getClientId(facesContext)+"CalendarVar",false);
+
+ writer.writeText(calendarVar+"=new org_apache_myfaces_PopupCalendar();\n",null);
+ writer.writeText(getLocalizedLanguageScript(facesContext,symbols,
+ timeKeeper.getFirstDayOfWeek(),inputCalendar,calendarVar)+"\n",null);
+ writer.writeText(calendarVar+".init(document.getElementById('"+
+ inputCalendar.getClientId(facesContext)+"Span"+"'));\n",null);
+ writer.endElement(HTML.SCRIPT_ELEM);
+ if(!inputCalendar.isDisplayValueOnly())
+ {
+ getScriptBtn(writer, facesContext, inputCalendar,
+ dateFormat,inputCalendar.getPopupButtonString(), new FunctionCallProvider(){
+ public String getFunctionCall(FacesContext facesContext, UIComponent uiComponent, String dateFormat)
+ {
+ String clientId = uiComponent.getClientId(facesContext);
+
+ String clientVar = JavascriptUtils.getValidJavascriptName(clientId+"CalendarVar",true);
+
+ return clientVar+"._popUpCalendar(this,document.getElementById('"+clientId+"'),'"+dateFormat+"')";
+ }
+ });
}
}
- else
- {
+ }
- int lastDayInMonth = timeKeeper.getActualMaximum(Calendar.DAY_OF_MONTH);
+ private void renderInline(
+ FacesContext facesContext,
+ HtmlInputCalendar inputCalendar,
+ Date value,
+ Calendar timeKeeper,
+ DateFormatSymbols symbols) throws IOException
+ {
+ String[] weekdays = mapShortWeekdays(symbols);
+ String[] months = mapMonths(symbols);
- int currentDay = timeKeeper.get(Calendar.DAY_OF_MONTH);
+ int lastDayInMonth = timeKeeper.getActualMaximum(Calendar.DAY_OF_MONTH);
- if (currentDay > lastDayInMonth)
- currentDay = lastDayInMonth;
+ int currentDay = timeKeeper.get(Calendar.DAY_OF_MONTH);
- timeKeeper.set(Calendar.DAY_OF_MONTH, 1);
+ if (currentDay > lastDayInMonth)
+ currentDay = lastDayInMonth;
- int weekDayOfFirstDayOfMonth = mapCalendarDayToCommonDay(timeKeeper.get(Calendar.DAY_OF_WEEK));
+ timeKeeper.set(Calendar.DAY_OF_MONTH, 1);
- int weekStartsAtDayIndex = mapCalendarDayToCommonDay(timeKeeper.getFirstDayOfWeek());
+ int weekDayOfFirstDayOfMonth = mapCalendarDayToCommonDay(timeKeeper.get(Calendar.DAY_OF_WEEK));
- ResponseWriter writer = facesContext.getResponseWriter();
+ int weekStartsAtDayIndex = mapCalendarDayToCommonDay(timeKeeper.getFirstDayOfWeek());
- HtmlRendererUtils.writePrettyLineSeparator(facesContext);
- HtmlRendererUtils.writePrettyLineSeparator(facesContext);
+ ResponseWriter writer = facesContext.getResponseWriter();
- 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);
+ HtmlRendererUtils.writePrettyLineSeparator(facesContext);
- HtmlRendererUtils.writePrettyLineSeparator(facesContext);
+ writer.startElement(HTML.TABLE_ELEM, inputCalendar);
+ HtmlRendererUtils.renderHTMLAttributes(writer, inputCalendar, HTML.UNIVERSAL_ATTRIBUTES);
+ HtmlRendererUtils.renderHTMLAttributes(writer, inputCalendar, HTML.EVENT_HANDLER_ATTRIBUTES);
+ writer.flush();
- writer.startElement(HTML.TR_ELEM, component);
+ HtmlRendererUtils.writePrettyLineSeparator(facesContext);
- if(inputCalendar.getMonthYearRowClass() != null)
- writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getMonthYearRowClass(), null);
+ writer.startElement(HTML.TR_ELEM, inputCalendar);
- writeMonthYearHeader(facesContext, writer, inputCalendar, timeKeeper,
- currentDay, weekdays, months);
+ if(inputCalendar.getMonthYearRowClass() != null)
+ writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getMonthYearRowClass(), null);
- writer.endElement(HTML.TR_ELEM);
+ writeMonthYearHeader(facesContext, writer, inputCalendar, timeKeeper,
+ currentDay, weekdays, months);
- HtmlRendererUtils.writePrettyLineSeparator(facesContext);
+ writer.endElement(HTML.TR_ELEM);
- writer.startElement(HTML.TR_ELEM, component);
+ HtmlRendererUtils.writePrettyLineSeparator(facesContext);
- if(inputCalendar.getWeekRowClass() != null)
- writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getWeekRowClass(), null);
+ writer.startElement(HTML.TR_ELEM, inputCalendar);
- writeWeekDayNameHeader(weekStartsAtDayIndex, weekdays,
- facesContext, writer, inputCalendar);
+ if(inputCalendar.getWeekRowClass() != null)
+ writer.writeAttribute(HTML.CLASS_ATTR, inputCalendar.getWeekRowClass(), null);
- writer.endElement(HTML.TR_ELEM);
+ writeWeekDayNameHeader(weekStartsAtDayIndex, weekdays,
+ facesContext, writer, inputCalendar);
- HtmlRendererUtils.writePrettyLineSeparator(facesContext);
+ writer.endElement(HTML.TR_ELEM);
- writeDays(facesContext, writer, inputCalendar, timeKeeper,
- currentDay, weekStartsAtDayIndex, weekDayOfFirstDayOfMonth,
- lastDayInMonth, weekdays);
+ HtmlRendererUtils.writePrettyLineSeparator(facesContext);
- writer.endElement(HTML.TABLE_ELEM);
- }
- component.getChildren().removeAll(component.getChildren());
+ writeDays(facesContext, writer, inputCalendar, timeKeeper,
+ currentDay, weekStartsAtDayIndex, weekDayOfFirstDayOfMonth,
+ lastDayInMonth, weekdays);
+
+ writer.endElement(HTML.TABLE_ELEM);
}
private HtmlInputTextHelp getOrCreateInputTextChild(HtmlInputCalendar inputCalendar, Application application)
@@ -654,6 +694,9 @@
}
}
+ /**
+ * Generate components and output for a single "day" cell within the calendar display.
+ */
private void writeCell(FacesContext facesContext,
ResponseWriter writer, UIInput component, String content,
Date valueForLink, String styleClass)
@@ -674,6 +717,16 @@
writer.endElement(HTML.TD_ELEM);
}
+ /**
+ * Create child components to represent a link to a specific date value, and render them.
+ * <p>
+ * For a disabled calendar, this just creates a Text component, attaches it as a child
+ * of the calendar and renders it. The value of the component is the string returned by
+ * valueForLink.getTime().
+ * <p>
+ * For a non-disabled calendar, create an HtmlCommandLink child that wraps the text
+ * returned by valueForLink.getTime(), and add it to the component.
+ */
private void writeLink(String content,
UIInput component,
FacesContext facesContext,
@@ -705,6 +758,8 @@
HtmlInputCalendar calendar = (HtmlInputCalendar)component;
if (calendar.isDisabled() || calendar.isReadonly())
{
+ // In this case, it appears that the link and parameter components are not
+ // used .. so why bother creating them above?
component.getChildren().add(text);
RendererUtils.renderChild(facesContext, text);