Author: vbaranov
Date: 2008-04-11 14:05:38 -0400 (Fri, 11 Apr 2008)
New Revision: 7776
Modified:
trunk/ui/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
Log:
http://jira.jboss.com/jira/browse/RF-2978
Modified:
trunk/ui/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
===================================================================
---
trunk/ui/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2008-04-11
17:27:48 UTC (rev 7775)
+++
trunk/ui/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2008-04-11
18:05:38 UTC (rev 7776)
@@ -24,7 +24,6 @@
import java.io.IOException;
import java.text.DateFormatSymbols;
import java.text.Format;
-import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@@ -35,10 +34,8 @@
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
-import java.util.logging.Level;
import javax.el.ValueExpression;
-import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
@@ -60,6 +57,8 @@
import org.richfaces.component.util.ComponentUtil;
import org.richfaces.event.CurrentDateChangeEvent;
+import com.sun.faces.util.Util;
+
/**
* @author Nick Belaevski - mailto:nbelaevski@exadel.com created 08.06.2007
*
@@ -93,7 +92,7 @@
*
* @see org.ajax4jsf.framework.renderer.RendererBase#getComponentClass()
*/
- protected Class getComponentClass() {
+ protected Class<? extends UIComponent> getComponentClass() {
return UICalendar.class;
}
@@ -101,7 +100,7 @@
UICalendar component) {
AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
- Set ajaxRenderedAreas = ajaxContext.getAjaxRenderedAreas();
+ Set<String> ajaxRenderedAreas = ajaxContext.getAjaxRenderedAreas();
String clientId = component.getClientId(context);
if (ajaxContext.isAjaxRequest() && ajaxRenderedAreas.contains(clientId)) {
@@ -131,40 +130,110 @@
converter = calendar.getConverter();
if ((converter == null) && (valueExpression != null)) {
- Class<?> converterType = valueExpression.getType(context.getELContext());
+ Class<? extends Object> converterType =
valueExpression.getType(context.getELContext());
if((converterType != null) && (converterType != Object.class)) {
// if getType returns a type for which we support a default
// conversion, acquire an appropriate converter instance.
- try {
- Application application = context.getApplication();
- converter = application.createConverter(converterType);
- if (log.isInfoEnabled()) {
- log.info(MessageFormat.format("Created converter ({0}) for type {1} for
component {2}.", new Object[] {
- converter.getClass().getName(), converterType.getClass().getName(),
component.getId() }));
- }
- } catch (Exception e) {
- if (log.isErrorEnabled()) {
- log.error(MessageFormat.format("Could not instantiate converter for type {0}:
{1}", new Object[] {
- converterType, e.toString() }));
- }
- return (null);
- }
+ converter = Util.getConverterForClass(converterType, context);
}
}
-
- if (converter != null) {
- // Use configured converter
- return converter.getAsObject(context, component, newValue);
- } else {
- // in case the converter hasn't been set, try to use default DateTimeConverter
- DateTimeConverter defaultConverter = new DateTimeConverter();
- defaultConverter.setPattern(calendar.getDatePattern());
- defaultConverter.setLocale(calendar.getAsLocale(calendar.getLocale()));
- defaultConverter.setTimeZone(calendar.getTimeZone());
- return defaultConverter.getAsObject(context, component, newValue);
+
+ // in case the converter hasn't been set, try to use default DateTimeConverter
+ if (converter == null) {
+ converter = createDefaultConverter(calendar);
}
+ return converter.getAsObject(context, component, newValue);
}
+
+ /**
+ * Overloads getFormattedValue to take a advantage of a previously
+ * obtained converter.
+ * @param context the FacesContext for the current request
+ * @param component UIComponent of interest
+ * @param currentValue the current value of <code>component</code>
+ * @param converter the component's converter
+ * @return the currentValue after any associated Converter has been
+ * applied
+ *
+ * @throws ConverterException if the value cannot be converted
+ */
+ protected String getFormattedValue(FacesContext context, UIComponent component, Object
currentValue,
+ Converter converter) throws ConverterException {
+ // formatting is supported only for components that support
+ // converting value attributes.
+ if (!(component instanceof UICalendar)) {
+ if (currentValue != null) {
+ return currentValue.toString();
+ }
+ return null;
+ }
+ UICalendar calendar = (UICalendar) component;
+
+ if (converter == null) {
+ // If there is a converter attribute, use it to to ask application
+ // instance for a converter with this identifer.
+ converter = calendar.getConverter();
+ }
+
+ if (converter == null) {
+ // if value is null and no converter attribute is specified, then
+ // return a zero length String.
+ if(currentValue == null) {
+ return "";
+ }
+ // Do not look for "by-type" converters for Strings
+ if (currentValue instanceof String) {
+ return (String) currentValue;
+ }
+
+ // if converter attribute set, try to acquire a converter
+ // using its class type.
+ Class<? extends Object> converterType = currentValue.getClass();
+ converter = Util.getConverterForClass(converterType, context);
+
+ // if there is no default converter available for this identifier,
+ // assume the model type to be String.
+ if (converter == null) {
+ // in case the converter hasn't been set, try to use default DateTimeConverter
+ converter = createDefaultConverter(calendar);
+ }
+ }
+
+ return converter.getAsString(context, calendar, currentValue);
+ }
+
+
+ /**
+ * @param context the FacesContext for the current request
+ * @param component UIComponent of interest
+ * @param currentValue the current value of <code>component</code>
+ *
+ * @return the currentValue after any associated Converter has been
+ * applied
+ *
+ * @throws ConverterException if the value cannot be converted
+ */
+ protected String getFormattedValue(FacesContext context, UIComponent component, Object
currentValue)
+ throws ConverterException {
+ return getFormattedValue(context, component, currentValue, null);
+ }
+
+ /**
+ * Creates default <code>DateTimeConverter</code> for the calendar
+ * @param calendar - calendar component
+ *
+ * @return created converter
+ */
+ protected static Converter createDefaultConverter(UICalendar calendar) {
+ DateTimeConverter defaultConverter = new DateTimeConverter();
+ defaultConverter.setPattern(calendar.getDatePattern());
+ defaultConverter.setLocale(calendar.getAsLocale(calendar.getLocale()));
+ defaultConverter.setTimeZone(calendar.getTimeZone());
+
+ return defaultConverter;
+ }
+
protected void doDecode(FacesContext context, UIComponent component) {
// TODO Auto-generated method stub
super.doDecode(context, component);
@@ -328,7 +397,7 @@
JSFunction ajaxFunction = AjaxRendererUtils.buildAjaxFunction(calendar,
context, AjaxRendererUtils.AJAX_FUNCTION_NAME);
ajaxFunction.addParameter(JSReference.NULL);
- Map options = AjaxRendererUtils.buildEventOptions(context, calendar);
+ Map<String, Object> options = AjaxRendererUtils.buildEventOptions(context,
calendar);
options.put("calendar", JSReference.THIS);
boolean isSingle = ((Boolean) calendar.getAttributes()
.get("ajaxSingle")).booleanValue();
@@ -370,40 +439,35 @@
}
public String getInputValue(FacesContext context, UIComponent component) {
- UICalendar input = (UICalendar) component;
+ UICalendar calendar = (UICalendar) component;
// XXX nick - nick - can contain either Date or String instance
// Fix for myFaces 1.1.x RF-997
Date value = null;
try {
- value = input.getAsDate(input.getSubmittedValue());
+ value = calendar.getAsDate(calendar.getSubmittedValue());
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(" InputValue: " + e.toString(), e);
}
value = null;
}
- Date curVal = input.getAsDate(input.getValue());
-
- DateTimeConverter converter = new DateTimeConverter();
- converter.setPattern(input.getDatePattern());
- Object locale = input.getLocale();
- converter.setLocale(input.getAsLocale(locale));
- converter.setTimeZone(input.getTimeZone());
+ Date curVal = calendar.getAsDate(calendar.getValue());
+
if (value == null) {
- return converter.getAsString(context, input, curVal);
- } else {
- return converter.getAsString(context, input, value);
+ value = curVal;
}
+
+ return getFormattedValue(context, calendar, value);
}
public void writeSymbols(FacesContext facesContext, UICalendar calendar)
throws IOException {
ResponseWriter writer = facesContext.getResponseWriter();
- Map symbolsMap = getSymbolsMap(facesContext, calendar);
- Iterator entryIterator = symbolsMap.entrySet().iterator();
+ Map<String, String[]> symbolsMap = getSymbolsMap(facesContext, calendar);
+ Iterator<Map.Entry<String, String[]>> entryIterator =
symbolsMap.entrySet().iterator();
writer.writeText(", \n", null);
while (entryIterator.hasNext()) {
- Map.Entry entry = (Map.Entry) entryIterator.next();
+ Map.Entry<String, String[]> entry = (Map.Entry<String, String[]>)
entryIterator.next();
writer.writeText(ScriptUtils.toScript(entry.getKey()), null);
writer.writeText(": ", null);
@@ -427,8 +491,8 @@
return shiftedLabels;
}
- protected Map getSymbolsMap(FacesContext facesContext, UICalendar calendar) {
- Map map = new HashMap();
+ protected Map<String, String[]> getSymbolsMap(FacesContext facesContext,
UICalendar calendar) {
+ Map<String, String[]> map = new HashMap<String, String[]>();
Locale locale = calendar.getAsLocale(calendar.getLocale());
Calendar cal = calendar.getCalendar();