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());
});
}
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));
}
}
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;
}
};
}
};
}
Aggregations