Search code examples
metawidget

How do I force Metawidget to display a date selection widget instead of a number widget for timestamp properties?


Metawidget creates date selection widgets if the property type is java.util.Date. In my case, the POJO contains timestamps of type long.

public class Node {
    private long created;
    private long lastModified;
    /* ... */
}

Node node = new Node();
VaadinMetawidget metawidget = new VaadinMetawidget();
metawidget.setToInspect(node);

How do I configure Metawidget so that a particular long property is rendered as date selection widget?


Solution

  • You need to hook in a custom WidgetBuilder to do this. Your custom WidgetBuilder only need worry about creating a Date widget for a 'long', and can return null for everything else:

    Your custom WidgetBuilder needs to be something like:

    public class MyWidgetBuilder implements WidgetBuilder<Component, VaadinMetawidget> {
        public Component buildWidget( String elementName, Map<String, String> attributes, VaadinMetawidget metawidget ) {
            if ( "long".equals( attributes.get( TYPE ))) {
                return new PopupDateField();
            }
            return null;
        }
    

    }

    Then you wrap it inside a CompositeWidgetBuilder so that Metawidget 'falls back' to the regular VaadinWidgetBuilder for everything else. Here's an example: http://metawidget.org/doc/reference/en/html/ch02s04.html#section-architecture-widgetbuilders-implementing-your-own

    VaadinMetawidget has some default WidgetBuilders already, so you may want to include those too. Here are the defaults: http://metawidget.org/doc/reference/en/html/ch02s04.html#section-architecture-widgetbuilders-defaults