Convert from Calendar (XMLGregorianCalendar) to Date

Hi all,

I’m a Vaadin newbie and I’d like to ask some questions about converting fields. I have a JavaBean with a Calendar (XMLGregorianCalendar) attribute. What I wanna do is trying to generate a form from this bean. I have create my own FieldFactory that implements FormFieldFactory. In this class I override the createField method. My code is as follows:


	public Field createField(Item item, Object propertyId, Component uiContext) {
		Class<?> type = item.getItemProperty(propertyId).getType();
		if (Date.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type)
				|| String.class.isAssignableFrom(type) || Long.class.isAssignableFrom(type)) {
			Field field = DefaultFieldFactory.get().createField(item, propertyId, uiContext);
			if (String.class.isAssignableFrom(type)) {
				String valor = "Hardcoding" + (String) propertyId;
				field.setValue(valor);
			}
			return field;
		} 
		if (XMLGregorianCalendar.class.isAssignableFrom(type)) {
                        DateField df = new DateField();
			df.setValue(new java.util.Date());
			df.setCaption((String) propertyId);
			df.setResolution(DateField.RESOLUTION_DAY);
                       return df;			
		}
/*
		else {
			Property property = item.getItemProperty(propertyId);
			String pid = (String) propertyId;
			Select select = new Select(pid);
			select.addItem("1");
			select.addItem("2");
			select.setNewItemsAllowed(true);
			return select;
		}
*/
	}

Instead of showing my Calendar attribute as a TextField (the default field that is generated by the DefaultFieldFactory) I want to create a new DateField component in my form. The problem erases when I add this particular property to my form in the Form.class:


        // Adds all the properties to this form
        for (final Iterator<?> i = propertyIds.iterator(); i.hasNext();) {
            final Object id = i.next();
            final Property property = itemDatasource.getItemProperty(id);
            if (id != null && property != null) {
                final Field f = fieldFactory.createField(itemDatasource, id,
                        this);
                if (f != null) {
                    f.setPropertyDataSource(property);
                    addField(id, f);
                }
            }
        }

In line 10, I’ve already returned my DateField from MyFieldFactory but when I try to set property value to my DateField the following exception is printed:


2011-07-14 14:54:56.091:WARN::/vaadin/
java.lang.IllegalArgumentException: DateField only supports Date properties
	at com.vaadin.ui.DateField.setPropertyDataSource(DateField.java:578)
	at com.vaadin.ui.Form.setItemDataSource(Form.java:769)
	at com.vaadin.ui.Form.setItemDataSource(Form.java:718)
	at mju.ntj.agendaUnificada.vaadin.MyVaadinApplication.init(MyVaadinApplication.java:91)
	at com.vaadin.Application.start(Application.java:554)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1208)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:484)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
	at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
	at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
	at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
	at org.mortbay.jetty.Server.handle(Server.java:326)
	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
	at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
	at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
	at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
	at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
	at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)


    @Override
    public void setPropertyDataSource(Property newDataSource) {
        if (newDataSource == null
                || Date.class.isAssignableFrom(newDataSource.getType())) {
            super.setPropertyDataSource(newDataSource);
        } else {
            throw new IllegalArgumentException(
                    "DateField only supports Date properties");
        }
    }

I’ve tried to figure how solve this out. I have read that it’s possible to convert one to another using a WrapperField. Unfortunately, I couldn’t make it works :frowning: . The funny thing is that if I try to convert my Calendar attribute to a TextField or another UI Component there is no problem. Can somebody explain me how I could fix this issue, please?.

Thnx in advance,
hop2croft

At
mju.ntj.agendaUnificada.vaadin.MyVaadinApplication.init(MyVaadinApplication.java:91)
, what exactly are you passing to
com.vaadin.ui.Form.setItemDataSource(…)
? Can you show me that code? I don’t see any calls to
setItemDataSource(…)
in the code you have pasted here.

You cannot set the field value in the field factory - it is automatically set later by form.setItemDataSource().

See e.g.
this example
on FieldWrapper. Your FormFieldFactory can then call the default factory to create the field to wrap (or create it explicitly) and return it wrapped in a FieldWrapper.