1

I have a bean with a field of type java.util.Calendar. I'm extending the FormFieldFactory class to render the remaining fields appropriately, but I'm not sure how to use the PopupDateField to render a field to type java.util.Calendar.

Gaurav Sharma
  • 4,032
  • 14
  • 46
  • 72
  • Any particular reason why the field type is Calendar? – miq Nov 03 '12 at 07:57
  • Ease of use mainly. I'm consuming a SOAP web service and the dates that are coming in are in Calendar format. – Gaurav Sharma Nov 05 '12 at 15:49
  • FYI: In Vaadin 7 there is a [Converter](https://vaadin.com/api/7.3.2/com/vaadin/data/util/converter/Converter.html) interface that will solve this problem ([example](http://stackoverflow.com/questions/26547228/vaadin-converter-for-java-sql-timestamp)). – Krayo Oct 30 '14 at 08:56

2 Answers2

1

A while ago I created a custom version of PopupDateField modified for java.sql.Date. I'd personally use java.util.Date, but the decision is not in my hands. The easy part in my case is that java.sql.Date is a subclass of java.util.Date whereas java.util.Calendar is not.

I was going to describe the modifications I made to PopupDateField derived class, but I realized that it won't probably work because of the fact that Calendar is not derived from Date.

I have never played around with BeanItems but you could try to create a custom Property that works as a wrapper for Calendar and perhapse create your own VaadinPropertyDescriptor to bind that field to your custom Property.

This answer was meant to be more definitive but I hope it is at least somewhat helpful. If there's any chance you can affect the bean structure, I'd recommend you to switch Calendar to Date and use the Calendar with the Date only when you need it.

miq
  • 2,746
  • 2
  • 24
  • 35
1

If you are building a web app - you can add a transient Date property to the domain, do the calendar to date conversions on setter & getter to fetch or update the original field. Hope that helps!!!

@Transient
private Datetime calendarExAsDate; // corresponds to @Column... private Calendar calendarEx property of the  domain.

 public DateTime getCalendarExAsDate() {
        if (this.getCalendarEx() == null) return null;
        return new DateTime(this.getCalendarEx().getTime());
    }

    public void setCalendarExAsDate(DateTime calendarExAsDate) {
        if (calendarExAsDate== null) return;
        this.calendarExAsDate = calendarExAsDate;
        Calendar cal = Calendar.getInstance();
        cal.setTime(calendarExAsDate.toDate());
        this.setCalendarEx(cal);
    }