Search in sources :

Example 1 with PropertyWrapper

use of com.haulmont.cuba.web.gui.data.PropertyWrapper in project cuba by cuba-platform.

the class WebTimeField method createDatasourceWrapper.

@Override
protected ItemWrapper createDatasourceWrapper(Datasource datasource, Collection<MetaPropertyPath> propertyPaths) {
    return new ItemWrapper(datasource, datasource.getMetaClass(), propertyPaths) {

        private static final long serialVersionUID = 1729450322469573679L;

        @Override
        protected PropertyWrapper createPropertyWrapper(Object item, MetaPropertyPath propertyPath) {
            return new PropertyWrapper(item, propertyPath) {

                private static final long serialVersionUID = -4481934193197224070L;

                @Override
                public String getFormattedValue() {
                    Object value = this.getValue();
                    if (value instanceof Date) {
                        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
                        return sdf.format(value);
                    }
                    return super.getFormattedValue();
                }

                @Override
                protected Object valueOf(Object newValue) throws Converter.ConversionException {
                    if (newValue instanceof String) {
                        if (StringUtils.isNotEmpty((String) newValue) && !newValue.equals(placeholder)) {
                            try {
                                SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
                                Date date = sdf.parse((String) newValue);
                                if (component.getComponentError() != null) {
                                    component.setComponentError(null);
                                }
                                return date;
                            } catch (Exception e) {
                                LoggerFactory.getLogger(WebTimeField.class).debug("Unable to parse value of component " + getId() + "\n" + e.getMessage());
                                component.setComponentError(new UserError("Invalid value"));
                                return null;
                            }
                        } else
                            return null;
                    } else
                        return newValue;
                }
            };
        }
    };
}
Also used : ItemWrapper(com.haulmont.cuba.web.gui.data.ItemWrapper) PropertyWrapper(com.haulmont.cuba.web.gui.data.PropertyWrapper) UserError(com.vaadin.server.UserError) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) Converter(com.vaadin.data.util.converter.Converter) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParseException(java.text.ParseException)

Aggregations

MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 ItemWrapper (com.haulmont.cuba.web.gui.data.ItemWrapper)1 PropertyWrapper (com.haulmont.cuba.web.gui.data.PropertyWrapper)1 Converter (com.vaadin.data.util.converter.Converter)1 UserError (com.vaadin.server.UserError)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1