use of com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener 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.gui.data.impl.WeakItemPropertyChangeListener in project cuba by cuba-platform.
the class WebPickerField method setDatasource.
@SuppressWarnings("unchecked")
@Override
public void setDatasource(Datasource datasource, String property) {
if ((datasource == null && property != null) || (datasource != null && property == null))
throw new IllegalArgumentException("Datasource and property should be either null or not null at the same time");
if (datasource == this.datasource && ((metaPropertyPath != null && metaPropertyPath.toString().equals(property)) || (metaPropertyPath == null && property == null)))
return;
if (this.datasource != null) {
metaProperty = null;
metaPropertyPath = null;
component.setPropertyDataSource(null);
this.datasource.removeItemChangeListener(securityWeakItemChangeListener);
securityWeakItemChangeListener = null;
this.datasource.removeItemChangeListener(weakItemChangeListener);
weakItemChangeListener = null;
this.datasource.removeItemPropertyChangeListener(weakItemPropertyChangeListener);
weakItemPropertyChangeListener = null;
this.datasource = null;
if (itemWrapper != null) {
itemWrapper.unsubscribe();
}
disableBeanValidator();
}
if (datasource != null) {
checkDatasourceProperty(datasource, property);
// noinspection unchecked
this.datasource = datasource;
metaPropertyPath = getResolvedMetaPropertyPath(datasource.getMetaClass(), property);
metaProperty = metaPropertyPath.getMetaProperty();
itemWrapper = createDatasourceWrapper(datasource, Collections.singleton(metaPropertyPath));
Property itemProperty = itemWrapper.getItemProperty(metaPropertyPath);
component.setPropertyDataSource(itemProperty);
itemChangeListener = e -> {
Object newValue = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
setValue(newValue);
};
weakItemChangeListener = new WeakItemChangeListener(datasource, itemChangeListener);
// noinspection unchecked
datasource.addItemChangeListener(weakItemChangeListener);
itemPropertyChangeListener = e -> {
if (!isBuffered() && e.getProperty().equals(metaPropertyPath.toString())) {
setValue(e.getValue());
}
};
weakItemPropertyChangeListener = new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener);
// noinspection unchecked
datasource.addItemPropertyChangeListener(weakItemPropertyChangeListener);
if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
if (property.equals(metaPropertyPath.toString())) {
Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
setValue(newValue);
}
}
initRequired(metaPropertyPath);
if (metaProperty.isReadOnly()) {
setEditable(false);
}
handleFilteredAttributes(this, this.datasource, metaPropertyPath);
securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
securityWeakItemChangeListener = new WeakItemChangeListener(datasource, securityItemChangeListener);
// noinspection unchecked
this.datasource.addItemChangeListener(securityWeakItemChangeListener);
initBeanValidator();
}
}
use of com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener in project cuba by cuba-platform.
the class DesktopAbstractOptionsField method setDatasource.
@Override
public void setDatasource(Datasource datasource, String property) {
this.datasource = datasource;
if (datasource == null) {
setValue(null);
return;
}
MetaClass metaClass = datasource.getMetaClass();
resolveMetaPropertyPath(metaClass, property);
itemChangeListener = e -> {
if (updatingInstance)
return;
Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners(value);
};
// noinspection unchecked
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance)
return;
if (e.getProperty().equals(metaPropertyPath.toString())) {
updateComponent(e.getValue());
fireChangeListeners(e.getValue());
}
};
// noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
initRequired(metaPropertyPath);
if (metaProperty.getRange().isEnum()) {
Enumeration enumeration = metaProperty.getRange().asEnumeration();
@SuppressWarnings("unchecked") Class<Enum> javaClass = enumeration.getJavaClass();
setOptionsList(Arrays.asList(javaClass.getEnumConstants()));
setCaptionMode(CaptionMode.ITEM);
}
if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) {
setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
}
}
if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
Object newValue = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
updateComponent(newValue);
fireChangeListeners(newValue);
}
if (metaProperty.isReadOnly()) {
setEditable(false);
}
handleFilteredAttributes(this, this.datasource, metaPropertyPath);
securityItemChangeListener = e -> handleFilteredAttributes(this, this.datasource, metaPropertyPath);
// noinspection unchecked
this.datasource.addItemChangeListener(new WeakItemChangeListener(this.datasource, securityItemChangeListener));
initBeanValidator();
}
use of com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener in project cuba by cuba-platform.
the class DesktopAbstractTextField method setDatasource.
@Override
public void setDatasource(Datasource datasource, String property) {
this.datasource = datasource;
if (datasource == null) {
setValue(null);
return;
}
resolveMetaPropertyPath(datasource.getMetaClass(), property);
valueFormatter.setMetaProperty(metaProperty);
itemChangeListener = e -> {
if (updatingInstance) {
return;
}
Object value = InstanceUtils.getValueEx(e.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners(value);
};
// noinspection unchecked
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
if (e.getProperty().equals(metaPropertyPath.toString())) {
updateComponent(e.getValue());
fireChangeListeners(e.getValue());
}
};
// noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
initRequired(metaPropertyPath);
if ((datasource.getState() == Datasource.State.VALID) && (datasource.getItem() != null)) {
Object value = InstanceUtils.getValueEx(datasource.getItem(), metaPropertyPath.getPath());
updateComponent(value);
fireChangeListeners();
}
Integer maxLength = (Integer) metaProperty.getAnnotations().get("length");
if (maxLength != null && this instanceof TextInputField.MaxLengthLimited) {
((TextInputField.MaxLengthLimited) this).setMaxLength(maxLength);
}
if (metaProperty.isReadOnly()) {
setEditable(false);
}
initBeanValidator();
}
use of com.haulmont.cuba.gui.data.impl.WeakItemPropertyChangeListener in project cuba by cuba-platform.
the class DesktopDateField method setDatasource.
@Override
public void setDatasource(Datasource datasource, String property) {
this.datasource = datasource;
if (datasource == null) {
setValue(null);
return;
}
resolveMetaPropertyPath(datasource.getMetaClass(), property);
if (metaProperty.getRange().isDatatype() && metaProperty.getRange().asDatatype().getJavaClass().equals(Date.class) && timeZone == null) {
MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
Boolean ignoreUserTimeZone = metadataTools.getMetaAnnotationValue(metaProperty, IgnoreUserTimeZone.class);
if (!Boolean.TRUE.equals(ignoreUserTimeZone)) {
timeZone = userSession.getTimeZone();
}
}
itemChangeListener = e -> {
if (updatingInstance) {
return;
}
Date value = getEntityValue(e.getItem());
updateComponent(value);
fireChangeListeners(value);
};
// noinspection unchecked
datasource.addItemChangeListener(new WeakItemChangeListener(datasource, itemChangeListener));
itemPropertyChangeListener = e -> {
if (updatingInstance) {
return;
}
if (e.getProperty().equals(metaPropertyPath.toString())) {
updateComponent((Date) e.getValue());
fireChangeListeners(e.getValue());
}
};
// noinspection unchecked
datasource.addItemPropertyChangeListener(new WeakItemPropertyChangeListener(datasource, itemPropertyChangeListener));
if (datasource.getState() == Datasource.State.VALID && datasource.getItem() != null) {
if (property.equals(metaPropertyPath.toString())) {
Date value = getEntityValue(datasource.getItem());
updateComponent(value);
fireChangeListeners(value);
}
}
initRequired(metaPropertyPath);
initDateFormat(metaProperty);
if (metaProperty.isReadOnly()) {
setEditable(false);
}
initBeanValidator();
setDateRangeByProperty(metaProperty);
}
Aggregations