Note that it is possible to create a custom converter. You just have
to implement some of the same internal api methods:
package com.xyz.jsf.converter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.apache.myfaces.custom.calendar.HtmlInputCalendar;
import xyz.utilities.business.MDate;
public class MDateConverter implements Converter, StateHolder
{
public static final String CONVERTER_ID =
"com.xyz.jsf.converter.MDateConverter";
private boolean isTransient;
public Object getAsObject(FacesContext facesContext,
UIComponent uiComponent, String value)
{
if (null == value)
{
return null;
}
if (0 == value.trim().length())
{
return null;
}
MDate mDate = new MDate(value);
if (false == mDate.isValidParse())
{
throw new ConverterException("Unable to convert '" + value + "'
to a date.");
}
GregorianCalendar calendar = new GregorianCalendar(
mDate.getYear(),
mDate.getMonth() - 1,
mDate.getDay(),
mDate.getHour(),
mDate.getMinute(),
mDate.getSecond());
return calendar.getTime();
}
public String getAsString(FacesContext facesContext, UIComponent
uiComponent, Object value) throws ConverterException {
Date date = (Date) value;
if (null == date)
{
return null;
}
// if(date==null)
// return getHelperString(uiComponent);
DateFormat dateFormat;
if(uiComponent instanceof HtmlInputCalendar &&
((HtmlInputCalendar) uiComponent).isRenderAsPopup())
{
String popupDateFormat = ((HtmlInputCalendar)
uiComponent).getPopupDateFormat();
dateFormat = new
SimpleDateFormat(createJSPopupFormat(facesContext, popupDateFormat));
}
else
{
dateFormat = createStandardDateFormat(facesContext);
}
dateFormat.setLenient(false);
return dateFormat.format(date);
}
public static String createJSPopupFormat(FacesContext
facesContext, String popupDateFormat)
{
if(popupDateFormat == null)
{
SimpleDateFormat defaultDateFormat =
createStandardDateFormat(facesContext);
popupDateFormat = defaultDateFormat.toPattern();
}
return popupDateFormat;
}
private static SimpleDateFormat
createStandardDateFormat(FacesContext facesContext)
{
DateFormat dateFormat;
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,
facesContext.getViewRoot().getLocale());
if(dateFormat instanceof SimpleDateFormat)
return (SimpleDateFormat) dateFormat;
else
return new SimpleDateFormat("dd.MM.yyyy");
}
//// StateHolder
public boolean isTransient() {
return isTransient;
}
public void setTransient(boolean newTransientValue) {
isTransient = newTransientValue;
}
public Object saveState(FacesContext context) {
return null;
}
public void restoreState(FacesContext context, Object state) {
}
}
On 9/18/06, Sławek Sobótka <[EMAIL PROTECTED]> wrote:
t:inputCalendar works really strange:
when i specify converter than date rendering ignores popupDateFormat
parameter.
i have created my own converter that extends org.apache.myfaces.
custom.convertDateTime.DateTimeConverter.
i have overriden ONLY menthod getAsObject (getAsString remains from
super class).
converter works fine - it "recognizez" my special strings (like t+5)
but
date in component renders in default way (like: Sep 18, 2006)
instead of specified in popupDateFormat parameter format: dd.MM.yyyy
> Yes, if I recall, t:inputCalendar calls some specific methods on the
> date converter.
>
> It would be great if we could have patches to make t:inputCalendar
> work better with the faces-config.xml file changes.
>
> On 9/16/06, Sławek Sobótka <[EMAIL PROTECTED]> wrote:
> > thanks. it works fine for h:inputText but not for t:inputCalendar.
I
> > guess taht inputCalendar has differend model than java.util.Date.
..?
> >
> > >
> > > you can overwrite all entries in your applications faces-config.
xml,
> > > so just insert this
> > >
> > > <converter>
> > > <converter-id>javax.faces.DateTime</converter-id>
> > > <converter-class>[your.converter.class]</converter-
class>
> > > </converter>
> > >
> > > <converter>
> > > <converter-for-class>java.util.Date</converter-for-
class>
> > > <converter-class>[your.converter.class]</converter-
class>
> > > </converter>
> > >
> > > to overwrite the default DateTimeConverter.
> > >
> > > Regards
> > >
> > > volker
> > >
> > > 2006/9/16, SĹawek SobĂłtka <[EMAIL PROTECTED]>:
> > > > i would like to let users input date like this: t+5 (that
means 5
> > days
> > > > from now). i know that i can write ny own converter, register
in i
> > > > faces-config and use it like this: <t:inputCalendar
> > > > converter"Tconverter" ...
> > > >
> > > > but
> > > >
> > > > i dont like it;)
> > > >
> > > > i would like to replace default_jsf_date_converter (or
whatever it
> > > > names) by my own converter. i just dont want to traverse all
my
> > pages
> > > > and paste converter="TConverter" in every input calendar.
> > > >
> > > > how can i do this?
> > > >
> > > > ----------------------------------------------------
> > > > Mega koncert - Duran Duran - Warszawa SĹuĹźewiec 23 wrzeĹnia
> > 2006!
> > > > ZdobÄ
dź bilet dla siebie i swoich przyjacióŠ- Kliknij:
> > > > http://klik.wp.pl/?adr=http%3A%2F%2Fadv.reklama.wp.pl%2Fas%
2Fd2.
> > html&sid=862
> > > >
> > > >
> > > >
> > >
> >
> > ----------------------------------------------------
> > Mega koncert - Duran Duran - Warszawa Służewiec 23 września 2006!
> > Zdobądź bilet dla siebie i swoich przyjaciół - Kliknij:
> > http://klik.wp.pl/?adr=http%3A%2F%2Fadv.reklama.wp.pl%2Fas%2Fd2.
html&sid=862
> >
> >
> >
>
----------------------------------------------------
Mega koncert - Duran Duran - Warszawa Służewiec 23 września 2006!
Zdobądź bilet dla siebie i swoich przyjaciół - Kliknij:
http://klik.wp.pl/?adr=http%3A%2F%2Fadv.reklama.wp.pl%2Fas%2Fd2.html&sid=862