You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2010/06/29 01:19:56 UTC
svn commit: r958785 - in
/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom:
calendar/ date/
Author: lu4242
Date: Mon Jun 28 23:19:55 2010
New Revision: 958785
URL: http://svn.apache.org/viewvc?rev=958785&view=rev
Log:
TOMAHAWK-1508 Find a way to convert between the model type required for the renderer, and the model-type that the backing-beans use for t:inputCalendar and t:inputDate
Added:
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlInputCalendarTagHandler.java
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTagHandler.java
Modified:
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/AbstractHtmlInputCalendar.java
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java
Modified: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/AbstractHtmlInputCalendar.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/AbstractHtmlInputCalendar.java?rev=958785&r1=958784&r2=958785&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/AbstractHtmlInputCalendar.java (original)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/AbstractHtmlInputCalendar.java Mon Jun 28 23:19:55 2010
@@ -76,6 +76,7 @@ import org.apache.myfaces.component.html
* name = "t:inputCalendar"
* class = "org.apache.myfaces.custom.calendar.HtmlInputCalendar"
* tagClass = "org.apache.myfaces.custom.calendar.HtmlInputCalendarTag"
+ * tagSuperclass = "org.apache.myfaces.custom.calendar.AbstractHtmlInputCalendarTag"
* @since 1.1.7
* @author Martin Marinschek (latest modification by $Author: lu4242 $)
* @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (mié, 03 sep 2008) $
@@ -93,6 +94,14 @@ public abstract class AbstractHtmlInputC
if (!UserRoleUtils.isVisibleOnUserRole(this)) return false;
return super.isRendered();
}
+
+ /**
+ *
+ * @JSFProperty stateHolder="true" inheritedTag="true"
+ */
+ public abstract DateBusinessConverter getDateBusinessConverter();
+
+ public abstract void setDateBusinessConverter(DateBusinessConverter dateBusinessConverter);
/**
* CSS class to be used on the TR element for the header-row showing month and year.
@@ -296,4 +305,4 @@ public abstract class AbstractHtmlInputC
* defaultValue = "day"
*/
public abstract String getPopupSelectMode();
-}
\ No newline at end of file
+}
Modified: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java?rev=958785&r1=958784&r2=958785&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java (original)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlCalendarRenderer.java Mon Jun 28 23:19:55 2010
@@ -142,8 +142,6 @@ public class HtmlCalendarRenderer
HtmlInputCalendar inputCalendar = (HtmlInputCalendar) component;
Locale currentLocale = facesContext.getViewRoot().getLocale();
- Date value;
-
Map<String, List<ClientBehavior>> behaviors = inputCalendar.getClientBehaviors();
if (!behaviors.isEmpty())
{
@@ -152,9 +150,62 @@ public class HtmlCalendarRenderer
log.debug("current locale:" + currentLocale.toString());
+ String textValue;
+
+ Converter converter = inputCalendar.getConverter();
+ Object submittedValue = inputCalendar.getSubmittedValue();
+
+ Date value;
+
+ if (submittedValue != null)
+ {
+ //Don't need to convert anything, the textValue is the same as the submittedValue
+ textValue = (String) submittedValue;
+
+ if(textValue ==null || textValue.trim().length()==0 || textValue.equals(getHelperString(inputCalendar)))
+ {
+ value = null;
+ }
+ else
+ {
+ try
+ {
+ String formatStr = CalendarDateTimeConverter.createJSPopupFormat(facesContext, inputCalendar.getPopupDateFormat());
+ Calendar timeKeeper = Calendar.getInstance(currentLocale);
+ int firstDayOfWeek = timeKeeper.getFirstDayOfWeek() - 1;
+ org.apache.myfaces.dateformat.DateFormatSymbols symbols = new org.apache.myfaces.dateformat.DateFormatSymbols(currentLocale);
+
+ SimpleDateFormatter dateFormat = new SimpleDateFormatter(formatStr, symbols, firstDayOfWeek);
+ value = dateFormat.parse(textValue);
+ }
+ catch (IllegalArgumentException illegalArgumentException)
+ {
+ value = null;
+ }
+ }
+ }
+ else
+ {
+ if (converter == null)
+ {
+ CalendarDateTimeConverter defaultConverter = new CalendarDateTimeConverter();
+
+ value = (Date) getDateBusinessConverter(inputCalendar).getDateValue(facesContext, component, inputCalendar.getValue());
+
+ textValue = defaultConverter.getAsString(facesContext, inputCalendar, value);
+ }
+ else
+ {
+ //Use converter to retrieve the value.
+ value = (Date) inputCalendar.getValue();
+ textValue = converter.getAsString(facesContext, inputCalendar, value);
+ }
+ }
+ /*
try
{
// value = RendererUtils.getDateValue(inputCalendar);
+
Converter converter = getConverter(inputCalendar);
if (converter instanceof DateConverter)
{
@@ -194,7 +245,7 @@ public class HtmlCalendarRenderer
{
value = null;
}
-
+ */
Calendar timeKeeper = Calendar.getInstance(currentLocale);
timeKeeper.setTime(value!=null?value:new Date());
@@ -203,11 +254,11 @@ public class HtmlCalendarRenderer
if(inputCalendar.isRenderAsPopup())
{
- renderPopup(facesContext, inputCalendar, value, timeKeeper, symbols);
+ renderPopup(facesContext, inputCalendar, textValue, timeKeeper, symbols);
}
else
{
- renderInline(facesContext, inputCalendar, value, timeKeeper, symbols);
+ renderInline(facesContext, inputCalendar, timeKeeper, symbols);
}
component.getChildren().removeAll(component.getChildren());
@@ -216,7 +267,7 @@ public class HtmlCalendarRenderer
private void renderPopup(
FacesContext facesContext,
HtmlInputCalendar inputCalendar,
- Date value,
+ String value,
Calendar timeKeeper,
DateFormatSymbols symbols) throws IOException
{
@@ -246,6 +297,8 @@ public class HtmlCalendarRenderer
inputText.setHelpText(inputCalendar.getHelpText());
inputText.setSelectText(true);
+ inputText.setValue(value);
+ /*
if (value == null && inputCalendar.getSubmittedValue() != null)
{
inputText.setValue(inputCalendar.getSubmittedValue());
@@ -254,7 +307,7 @@ public class HtmlCalendarRenderer
{
inputText.setValue(getConverter(inputCalendar).getAsString(
facesContext,inputCalendar,value));
- }
+ }*/
inputText.setDisabled(inputCalendar.isDisabled());
inputText.setReadonly(inputCalendar.isReadonly());
inputText.setEnabledOnUserRole(inputCalendar.getEnabledOnUserRole());
@@ -320,7 +373,6 @@ public class HtmlCalendarRenderer
private void renderInline(
FacesContext facesContext,
HtmlInputCalendar inputCalendar,
- Date value,
Calendar timeKeeper,
DateFormatSymbols symbols) throws IOException
{
@@ -1028,6 +1080,16 @@ public class HtmlCalendarRenderer
}
return converter;
}
+
+ private DateBusinessConverter getDateBusinessConverter(AbstractHtmlInputCalendar component)
+ {
+ DateBusinessConverter dateBusinessConverter = component.getDateBusinessConverter();
+ if (dateBusinessConverter == null)
+ {
+ dateBusinessConverter = new DefaultDateBusinessConverter();
+ }
+ return dateBusinessConverter;
+ }
private int mapCalendarDayToCommonDay(int day)
{
@@ -1174,7 +1236,7 @@ public class HtmlCalendarRenderer
RendererUtils.checkParamValidity(facesContext, component, HtmlInputCalendar.class);
- String helperString = getHelperString(component);
+ //String helperString = getHelperString(component);
if (!(component instanceof EditableValueHolder)) {
throw new IllegalArgumentException("Component "
@@ -1189,20 +1251,20 @@ public class HtmlCalendarRenderer
{
String value = (String) paramMap.get(clientId);
- if(!value.equalsIgnoreCase(helperString))
- {
+ //if(!value.equalsIgnoreCase(helperString))
+ //{
((EditableValueHolder) component).setSubmittedValue(value);
- }
- else
- {
+ //}
+ //else
+ //{
// The field was initially filled with the "helper string", and has
// not been altered by the user so treat this as if null had been
// passed by the user.
//
// TODO: does this mean the target date is set to todays date?
// And how does this affect the "required" property?
- ((EditableValueHolder) component).setSubmittedValue("");
- }
+ //((EditableValueHolder) component).setSubmittedValue("");
+ //}
}
else
{
@@ -1237,21 +1299,31 @@ public class HtmlCalendarRenderer
{
RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlInputCalendar.class);
- UIInput uiInput = (UIInput) uiComponent;
+ AbstractHtmlInputCalendar uiInput = (AbstractHtmlInputCalendar) uiComponent;
Converter converter = uiInput.getConverter();
+ if (submittedValue != null && !(submittedValue instanceof String))
+ {
+ throw new IllegalArgumentException("Submitted value of type String expected");
+ }
+
+ //Do not convert if submittedValue is helper string
+ if(submittedValue != null && submittedValue.equals(getHelperString(uiComponent)))
+ return null;
+
if(converter==null)
{
converter = new CalendarDateTimeConverter();
+
+ Date date = (Date) converter.getAsObject(facesContext, uiComponent, (String) submittedValue);
+
+ return getDateBusinessConverter(uiInput).getBusinessValue(facesContext, uiComponent, date);
}
-
- if (submittedValue != null && !(submittedValue instanceof String))
+ else
{
- throw new IllegalArgumentException("Submitted value of type String expected");
+ return converter.getAsObject(facesContext, uiComponent, (String) submittedValue);
}
-
- return converter.getAsObject(facesContext, uiComponent, (String) submittedValue);
}
public interface DateConverter extends Converter
Added: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlInputCalendarTagHandler.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlInputCalendarTagHandler.java?rev=958785&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlInputCalendarTagHandler.java (added)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/calendar/HtmlInputCalendarTagHandler.java Mon Jun 28 23:19:55 2010
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 javax.faces.component.UIComponent;
+import javax.faces.view.facelets.ComponentConfig;
+import javax.faces.view.facelets.ComponentHandler;
+import javax.faces.view.facelets.FaceletContext;
+import javax.faces.view.facelets.MetaRule;
+import javax.faces.view.facelets.MetaRuleset;
+import javax.faces.view.facelets.Metadata;
+import javax.faces.view.facelets.MetadataTarget;
+import javax.faces.view.facelets.TagAttribute;
+
+import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
+
+/**
+ *
+ * @since 1.1.10
+ * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
+ * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (mié, 03 sep 2008) $
+ */
+public class HtmlInputCalendarTagHandler extends ComponentHandler
+{
+
+ public HtmlInputCalendarTagHandler(ComponentConfig config)
+ {
+ super(config);
+ }
+
+ protected MetaRuleset createMetaRuleset(Class type)
+ {
+ MetaRuleset m = super.createMetaRuleset(type).alias("class", "styleClass");
+
+ m.addRule(DateBusinessConverterRule.INSTANCE);
+
+ return m;
+ }
+
+ public static class DateBusinessConverterRule extends MetaRule
+ {
+ public final static DateBusinessConverterRule INSTANCE = new DateBusinessConverterRule();
+
+ final static class LiteralConverterMetadata extends Metadata
+ {
+
+ private final Class dateBusinessConverterClass;
+
+ public LiteralConverterMetadata(String dateBusinessConverterClass)
+ {
+ try
+ {
+ this.dateBusinessConverterClass = ClassUtils
+ .classForName(dateBusinessConverterClass);
+ }
+ catch(ClassNotFoundException e)
+ {
+ throw new IllegalArgumentException("Class referenced in calendarConverter not found: "+dateBusinessConverterClass);
+ }
+ catch(ClassCastException e)
+ {
+ throw new IllegalArgumentException("Class referenced in calendarConverter is not instance of org.apache.myfaces.custom.calendar.CalendarConverter: "+dateBusinessConverterClass);
+ }
+ }
+
+ public void applyMetadata(FaceletContext ctx, Object instance)
+ {
+ ((AbstractHtmlInputCalendar) instance)
+ .setDateBusinessConverter((DateBusinessConverter) ClassUtils
+ .newInstance(dateBusinessConverterClass));
+ }
+ }
+
+ final static class DynamicConverterMetadata extends Metadata
+ {
+ private final TagAttribute attr;
+
+ public DynamicConverterMetadata(TagAttribute attr)
+ {
+ this.attr = attr;
+ }
+
+ public void applyMetadata(FaceletContext ctx, Object instance)
+ {
+ ((UIComponent) instance).setValueExpression("dateBusinessConverter",
+ attr.getValueExpression(ctx,
+ DateBusinessConverter.class));
+ }
+ }
+
+ public Metadata applyRule(String name, TagAttribute attribute,
+ MetadataTarget meta)
+ {
+ if (meta.isTargetInstanceOf(AbstractHtmlInputCalendar.class))
+ {
+ if ("dateBusinessConverter".equals(name)) {
+ if (attribute.isLiteral()) {
+ return new LiteralConverterMetadata(attribute.getValue());
+ } else {
+ return new DynamicConverterMetadata(attribute);
+ }
+ }
+ }
+ return null;
+ }
+ }
+}
Modified: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java?rev=958785&r1=958784&r2=958785&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java (original)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlDateRenderer.java Mon Jun 28 23:19:55 2010
@@ -22,6 +22,7 @@ import java.io.IOException;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.util.Calendar;
+import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -39,6 +40,8 @@ import javax.faces.event.ComponentSystem
import javax.faces.event.ListenerFor;
import org.apache.myfaces.component.UserRoleUtils;
+import org.apache.myfaces.custom.calendar.DateBusinessConverter;
+import org.apache.myfaces.custom.calendar.DefaultDateBusinessConverter;
import org.apache.myfaces.custom.calendar.FunctionCallProvider;
import org.apache.myfaces.custom.calendar.HtmlCalendarRenderer;
import org.apache.myfaces.custom.calendar.HtmlCalendarRenderer.CalendarDateTimeConverter;
@@ -553,6 +556,16 @@ public class HtmlDateRenderer extends Ht
HtmlRendererUtils.decodeClientBehaviors(facesContext, inputDate);
}
+
+ private DateBusinessConverter getDateBusinessConverter(AbstractHtmlInputDate component)
+ {
+ DateBusinessConverter dateBusinessConverter = component.getDateBusinessConverter();
+ if (dateBusinessConverter == null)
+ {
+ dateBusinessConverter = new DefaultDateBusinessConverter();
+ }
+ return dateBusinessConverter;
+ }
public Object getConvertedValue(FacesContext context, UIComponent uiComponent, Object submittedValue) throws ConverterException {
@@ -561,12 +574,15 @@ public class HtmlDateRenderer extends Ht
if (inputDate.getConverter() == null)
{
UserData userData = (UserData) submittedValue;
+ Date date = null;
try {
- return userData.parse();
+ date = userData.parse();
} catch (ParseException e) {
Object[] args = {uiComponent.getId()};
throw new ConverterException(MessageUtils.getMessage(Constants.TOMAHAWK_DEFAULT_BUNDLE, FacesMessage.SEVERITY_ERROR, DATE_MESSAGE_ID, args, context));
- }
+ }
+
+ return getDateBusinessConverter(inputDate).getBusinessValue(context, inputDate, date);
}
else
{
Added: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTagHandler.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTagHandler.java?rev=958785&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTagHandler.java (added)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/custom/date/HtmlInputDateTagHandler.java Mon Jun 28 23:19:55 2010
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.date;
+
+import javax.faces.component.UIComponent;
+import javax.faces.view.facelets.ComponentConfig;
+import javax.faces.view.facelets.ComponentHandler;
+import javax.faces.view.facelets.FaceletContext;
+import javax.faces.view.facelets.MetaRule;
+import javax.faces.view.facelets.MetaRuleset;
+import javax.faces.view.facelets.Metadata;
+import javax.faces.view.facelets.MetadataTarget;
+import javax.faces.view.facelets.TagAttribute;
+
+import org.apache.myfaces.custom.calendar.DateBusinessConverter;
+import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
+
+/**
+ *
+ * @since 1.1.10
+ * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
+ * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (mié, 03 sep 2008) $
+ */
+public class HtmlInputDateTagHandler extends ComponentHandler
+{
+
+ public HtmlInputDateTagHandler(ComponentConfig config)
+ {
+ super(config);
+ }
+
+ protected MetaRuleset createMetaRuleset(Class type)
+ {
+ MetaRuleset m = super.createMetaRuleset(type).alias("class", "styleClass");
+
+ m.addRule(DateBusinessConverterRule.INSTANCE);
+
+ return m;
+ }
+
+ public static class DateBusinessConverterRule extends MetaRule
+ {
+ public final static DateBusinessConverterRule INSTANCE = new DateBusinessConverterRule();
+
+ final static class LiteralConverterMetadata extends Metadata
+ {
+
+ private final Class dateBusinessConverterClass;
+
+ public LiteralConverterMetadata(String dateBusinessConverterClass)
+ {
+ try
+ {
+ this.dateBusinessConverterClass = ClassUtils
+ .classForName(dateBusinessConverterClass);
+ }
+ catch(ClassNotFoundException e)
+ {
+ throw new IllegalArgumentException("Class referenced in calendarConverter not found: "+dateBusinessConverterClass);
+ }
+ catch(ClassCastException e)
+ {
+ throw new IllegalArgumentException("Class referenced in calendarConverter is not instance of org.apache.myfaces.custom.calendar.CalendarConverter: "+dateBusinessConverterClass);
+ }
+ }
+
+ public void applyMetadata(FaceletContext ctx, Object instance)
+ {
+ ((AbstractHtmlInputDate) instance)
+ .setDateBusinessConverter((DateBusinessConverter) ClassUtils
+ .newInstance(dateBusinessConverterClass));
+ }
+ }
+
+ final static class DynamicConverterMetadata extends Metadata
+ {
+ private final TagAttribute attr;
+
+ public DynamicConverterMetadata(TagAttribute attr)
+ {
+ this.attr = attr;
+ }
+
+ public void applyMetadata(FaceletContext ctx, Object instance)
+ {
+ ((UIComponent) instance).setValueExpression("dateBusinessConverter",
+ attr.getValueExpression(ctx,
+ DateBusinessConverter.class));
+ }
+ }
+
+ public Metadata applyRule(String name, TagAttribute attribute,
+ MetadataTarget meta)
+ {
+ if (meta.isTargetInstanceOf(AbstractHtmlInputDate.class))
+ {
+ if ("dateBusinessConverter".equals(name)) {
+ if (attribute.isLiteral()) {
+ return new LiteralConverterMetadata(attribute.getValue());
+ } else {
+ return new DynamicConverterMetadata(attribute);
+ }
+ }
+ }
+ return null;
+ }
+ }
+}