Search in sources :

Example 1 with UserError

use of com.vaadin.server.UserError in project cuba by cuba-platform.

the class WebLookupPickerField method createComponent.

@Override
protected void createComponent() {
    super.createComponent();
    // delegate error indication
    this.componentErrorHandler = message -> {
        if (message instanceof UserError) {
            return false;
        }
        pickerField.component.setComponentError(message);
        return true;
    };
    component.setCustomValueEquals(InstanceUtils::propertyValueEquals);
    final ComboBox selectComponent = component;
    final WebPickerField.Picker picker = new WebPickerField.Picker(this, component) {

        @Override
        public void setRequired(boolean required) {
            super.setRequired(required);
            selectComponent.setNullSelectionAllowed(!required);
        }
    };
    pickerField = new WebPickerField(picker);
    // Required for custom components in fieldgroup
    initValueSync(selectComponent, picker);
}
Also used : UserError(com.vaadin.server.UserError) ComboBox(com.vaadin.ui.ComboBox) InstanceUtils(com.haulmont.chile.core.model.utils.InstanceUtils)

Example 2 with UserError

use of com.vaadin.server.UserError 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

UserError (com.vaadin.server.UserError)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 InstanceUtils (com.haulmont.chile.core.model.utils.InstanceUtils)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 ComboBox (com.vaadin.ui.ComboBox)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1