Search in sources :

Example 1 with ItemWrapper

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

the class WebAbstractTable method setClickListener.

@Override
public void setClickListener(String columnId, final CellClickListener clickListener) {
    component.setClickListener(getColumn(columnId).getId(), (itemId, columnId1) -> {
        ItemWrapper wrapper = (ItemWrapper) component.getItem(itemId);
        Entity entity = wrapper.getItem();
        clickListener.onClick(entity, columnId1.toString());
    });
}
Also used : ItemWrapper(com.haulmont.cuba.web.gui.data.ItemWrapper) Entity(com.haulmont.cuba.core.entity.Entity)

Example 2 with ItemWrapper

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

the class WebEntityLinkField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    Preconditions.checkNotNullArgument(datasource, "datasource is null");
    if (this.datasource != null) {
        throw new UnsupportedOperationException("Changing datasource is not supported by the EntityLinkField component");
    }
    // noinspection unchecked
    this.datasource = datasource;
    MetaClass metaClass = datasource.getMetaClass();
    resolveMetaPropertyPath(metaClass, property);
    if (metaProperty.getRange().isClass()) {
        this.metaClass = metaProperty.getRange().asClass();
    }
    ItemWrapper wrapper = createDatasourceWrapper(datasource, Collections.singleton(metaPropertyPath));
    Property itemProperty = wrapper.getItemProperty(metaPropertyPath);
    component.setPropertyDataSource(itemProperty);
    itemChangeListener = e -> {
        Object newValue = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
        setValue(newValue);
    };
    // noinspection unchecked
    datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
    itemPropertyChangeListener = e -> {
        if (e.getProperty().equals(metaPropertyPath.toString())) {
            setValue(e.getValue());
        }
    };
    // noinspection unchecked
    datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
    if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
        if (property.equals(metaPropertyPath.toString())) {
            Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
            setValue(newValue);
        }
    }
    setRequired(metaProperty.isMandatory());
    if (StringUtils.isEmpty(getRequiredMessage())) {
        MessageTools messageTools = AppBeans.get(MessageTools.NAME);
        setRequiredMessage(messageTools.getDefaultRequiredMessage(metaClass, property));
    }
}
Also used : ItemWrapper(com.haulmont.cuba.web.gui.data.ItemWrapper) WeakItemChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemChangeListener) MessageTools(com.haulmont.cuba.core.global.MessageTools) MetaClass(com.haulmont.chile.core.model.MetaClass) WeakItemPropertyChangeListener(com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener) Property(com.vaadin.data.Property)

Example 3 with ItemWrapper

use of com.haulmont.cuba.web.gui.data.ItemWrapper 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

ItemWrapper (com.haulmont.cuba.web.gui.data.ItemWrapper)3 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 Entity (com.haulmont.cuba.core.entity.Entity)1 MessageTools (com.haulmont.cuba.core.global.MessageTools)1 WeakItemChangeListener (com.haulmont.cuba.gui.data.impl.WeakItemChangeListener)1 WeakItemPropertyChangeListener (com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener)1 PropertyWrapper (com.haulmont.cuba.web.gui.data.PropertyWrapper)1 Property (com.vaadin.data.Property)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