Table Converter: LongToDate

Hi,

my problem is that don’t get it how to use the converter for Table to converte a Long to Date.
My Test.class has just one field called “created” with java type long (it contains a date value ofc)

BeanItemContainer<Test> container = new BeanItemContainer<License>(Test.class, list);
Table table = new Table();
table.setContainerDataSource(container);
table.setConverter("created", new StringToDateConverter());

What can I do to use that kind of “converter” but for LongToDate?? Not StringToDate.

Exception …

Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Date
    at com.vaadin.data.util.converter.StringToDateConverter.convertToPresentation(StringToDateConverter.java:37)
    at com.vaadin.ui.CustomTable.formatPropertyValue(CustomTable.java:4127)
    at com.vaadin.ui.CustomTable.getPropertyValue(CustomTable.java:4069)
    at com.vaadin.ui.CustomTable.parseItemIdToCells(CustomTable.java:2348)
    at com.vaadin.ui.CustomTable.getVisibleCellsNoCache(CustomTable.java:2187)
    at com.vaadin.ui.CustomTable.refreshRenderedCells(CustomTable.java:1709)
    ... 78 more

Update: Added Samplecode

Working Code:

[code]
@Theme(“mytheme”)
public class MyUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
    BeanItemContainer<Test> container = new BeanItemContainer<Test>(Test.class,
            Arrays.asList(new Test { new Test(1489097212000L), new Test(1489097212000L) }));
    Table table = new Table();
    table.setSizeFull();
    table.setContainerDataSource(container);
    // This DOES NOT work 
    // ----> table.setConverter("created", new StringToDateConverter());
    setSizeFull();
    setContent(table);
}

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}

}
[/code]

NOT WORKING WITH EXCEPTION!

[size=2]
[size=3]

@Theme("mytheme")
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        BeanItemContainer<Test> container = new BeanItemContainer<Test>(Test.class,
                Arrays.asList(new Test { new Test(1489097212000L), new Test(1489097212000L) }));
        Table table = new Table();
        table.setSizeFull();
        table.setContainerDataSource(container);
        table.setConverter("created", new StringToDateConverter() {
            @Override
            public Date convertToModel(String value, Class<? extends Date> targetType, Locale locale)
                    throws com.vaadin.data.util.converter.Converter.ConversionException {
                // I cannot use Long because super.conv...() doesn't take a long!
                return super.convertToModel(value, targetType, locale);
            }

            @Override
            public String convertToPresentation(Date value, Class<? extends String> targetType, Locale locale)
                    throws com.vaadin.data.util.converter.Converter.ConversionException {
                // I cannot use Long because super.conv...() doesn't take a long!
                return super.convertToPresentation(value, targetType, locale);
            }
        });
        setSizeFull();
        setContent(table);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

[/size]
[/size]

If I write my own LongToDateConverter class then I get the following error in my IDE Eclipse:

The method setConverter(Object, Converter<String,?>) in the type Table is not applicable for the arguments (String, LongToDateConverter)

Sample Class (ofc empty …)

public class LongToDateConverter implements Converter<Date, Long>{

    @Override
    public Long convertToModel(Date value, Class<? extends Long> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        return null;
    }

    @Override
    public Date convertToPresentation(Long value, Class<? extends Date> targetType, Locale locale)
            throws com.vaadin.data.util.converter.Converter.ConversionException {
        return null;
    }

    @Override
    public Class<Long> getModelType() {
        return null;
    }

    @Override
    public Class<Date> getPresentationType() {
        return null;
    }

}

So the table.setConverter(…) doesn’t want my Converter<Long,Date>

Whats wrong?

Thanks,
Max

I guess you could just create your own Converter implementation; you probably only really need to implement the convertToPresentation method too.

-Olli

Hi Olli,

I tried that before, but the converter can’t take Long as “raw” Input, it requires a String. Thats why I didn’t get it.

You got any suggestions or sample code?

Did you try parsing the String to long with Long.valueOf()?

-Olli

Ofc, but I get always the exception, shown above

So did you try something like this:

Long l = Long.valueOf(string);
Date d = new Date(l);

-Olli

Hehe, ofc im not a total noob :slight_smile:

I meant sample code about the converter. Because if I assign the converter with <Long,Date> to the table,
then Long has to be String … or s.th. like this, just let me create a sample code, I better show you my problem.

Updated first Post with sample codes

Hmm, now that I’m trying it, I’m not sure if it can work with BeanItemContainer without adding a new Property. Probably the easiest solution to work around it is something like the following:

public class MyUI extends UI {

  public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  @Override
  protected void init(VaadinRequest vaadinRequest) {

    Test t = new Test(1489097212000L);


    Table table = new Table();

    table.addContainerProperty("date", String.class, null);
    Item item = table.addItem(t);
    Long l = t.getL();
    Date d = new Date();
    item.getItemProperty("date").setValue(sdf.format(d));

    table.setSizeFull();
    setSizeFull();
    setContent(table);
  }

  @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
  @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
  public static class MyUIServlet extends VaadinServlet {
  }

  public class Test {
    private long l = 0;
    public Test(long l) {
      this.l = l;
    }

    public long getL() {
      return l;
    }

    public void setL(long l) {
      this.l = l;
    }
  }
}

Hi Olli,

that sample which you provided doesn’t help me anything. This has nothing to do with my question about the converter problem. I gave you an exact example which you can paste. Could you please check this sample and figure out whats wrong here? I am pretty sure other ppl has problems with that too, if the property must always be a String if you want to use the converter.

any other idea here ?

Is this really a problem for the (table) converter to use not String values as source of property type?

push