use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.
the class GuiActionSupport method createActionsByMetaAnnotations.
/**
* Adds actions specified in {@link Lookup} annotation on entity attribute to the given PickerField.
*
* @param pickerField field
* @return true if actions have been added
*/
public boolean createActionsByMetaAnnotations(PickerField pickerField) {
ValueSource valueSource = pickerField.getValueSource();
if (!(valueSource instanceof EntityValueSource)) {
return false;
}
EntityValueSource entityValueSource = (EntityValueSource) pickerField.getValueSource();
MetaPropertyPath mpp = entityValueSource.getMetaPropertyPath();
if (mpp == null) {
return false;
}
String[] actionIds = (String[]) metadataTools.getMetaAnnotationAttributes(mpp.getMetaProperty().getAnnotations(), Lookup.class).get("actions");
if (actionIds != null && actionIds.length > 0) {
for (String actionId : actionIds) {
createActionById(pickerField, actionId);
}
return true;
}
return false;
}
use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.
the class EditorBuilderProcessor method buildEditor.
@SuppressWarnings("unchecked")
public <E extends Entity, S extends Screen> S buildEditor(EditorBuilder<E> builder) {
FrameOwner origin = builder.getOrigin();
Screens screens = getScreenContext(origin).getScreens();
ListComponent<E> listComponent = builder.getListComponent();
CollectionContainer<E> container = builder.getContainer();
if (container == null && listComponent != null) {
DataUnit items = listComponent.getItems();
container = items instanceof ContainerDataUnit ? ((ContainerDataUnit) items).getContainer() : null;
}
E entity = initEntity(builder, container);
if (builder.getMode() == EditMode.EDIT && entity == null) {
throw new IllegalStateException(String.format("Editor of %s cannot be open with mode EDIT, entity is not set", builder.getEntityClass()));
}
Screen screen = createScreen(builder, screens, entity);
EditorScreen<E> editorScreen = (EditorScreen<E>) screen;
editorScreen.setEntityToEdit(entity);
DataContext parentDataContext = setupParentDataContext(origin, screen, container, builder.getParentDataContext());
if (container != null) {
CollectionContainer<E> ct = container;
screen.addAfterCloseListener(event -> {
CloseAction closeAction = event.getCloseAction();
if (isCommitCloseAction(closeAction)) {
E entityFromEditor = getCommittedEntity(editorScreen, parentDataContext);
E reloadedEntity = reloadIfNeeded(entityFromEditor, ct, builder);
E committedEntity = transform(reloadedEntity, builder);
E mergedEntity = merge(committedEntity, origin, parentDataContext);
if (builder.getMode() == EditMode.CREATE) {
boolean addsFirst;
if (!(ct instanceof Nested)) {
addsFirst = clientConfig.getCreateActionAddsFirst();
if (builder.getAddFirst() != null) {
addsFirst = builder.getAddFirst();
}
} else {
addsFirst = false;
}
if (ct instanceof Nested || !addsFirst) {
ct.getMutableItems().add(mergedEntity);
} else {
ct.getMutableItems().add(0, mergedEntity);
}
} else {
ct.replaceItem(mergedEntity);
}
}
if (listComponent instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
((com.haulmont.cuba.gui.components.Component.Focusable) listComponent).focus();
}
});
}
HasValue<E> field = builder.getField();
if (field != null) {
if (parentDataContext == null && field instanceof HasValueSource) {
ValueSource fieldValueSource = ((HasValueSource) field).getValueSource();
if (fieldValueSource instanceof EntityValueSource) {
if (isCompositionProperty((EntityValueSource) fieldValueSource)) {
DataContext thisDataContext = UiControllerUtils.getScreenData(origin).getDataContext();
DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
checkDataContext(screen, dataContext);
dataContext.setParent(thisDataContext);
}
}
}
screen.addAfterCloseListener(event -> {
CloseAction closeAction = event.getCloseAction();
if (isCommitCloseAction(closeAction)) {
E entityFromEditor = editorScreen.getEditedEntity();
E editedEntity = transform(entityFromEditor, builder);
if (field instanceof LookupPickerField) {
LookupPickerField lookupPickerField = ((LookupPickerField) field);
Options options = lookupPickerField.getOptions();
if (options instanceof EntityOptions) {
EntityOptions entityOptions = (EntityOptions) options;
if (entityOptions.containsItem(editedEntity)) {
entityOptions.updateItem(editedEntity);
}
}
}
if (field instanceof SupportsUserAction) {
((SupportsUserAction) field).setValueFromUser(editedEntity);
} else {
field.setValue(editedEntity);
}
}
if (field instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
((com.haulmont.cuba.gui.components.Component.Focusable) field).focus();
}
});
}
if (builder instanceof EditorClassBuilder) {
@SuppressWarnings("unchecked") Consumer<AfterScreenCloseEvent> closeListener = ((EditorClassBuilder) builder).getCloseListener();
if (closeListener != null) {
screen.addAfterCloseListener(new AfterCloseListenerAdapter(closeListener));
}
}
return (S) screen;
}
use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.
the class WebTextField method convertToPresentation.
@SuppressWarnings("unchecked")
@Override
protected String convertToPresentation(V modelValue) throws ConversionException {
if (formatter != null) {
return nullToEmpty(formatter.apply(modelValue));
}
if (datatype != null) {
return nullToEmpty(datatype.format(modelValue, locale));
}
if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
Range range = entityValueSource.getMetaPropertyPath().getRange();
if (range.isDatatype()) {
Datatype<V> propertyDataType = range.asDatatype();
return nullToEmpty(propertyDataType.format(modelValue, locale));
} else {
setEditable(false);
if (modelValue == null)
return "";
if (range.isClass()) {
MetadataTools metadataTools = beanLocator.get(MetadataTools.class);
if (range.getCardinality().isMany()) {
return ((Collection<Entity>) modelValue).stream().map(metadataTools::getInstanceName).collect(Collectors.joining(", "));
} else {
return metadataTools.getInstanceName((Entity) modelValue);
}
} else if (range.isEnum()) {
Messages messages = beanLocator.get(Messages.class);
return messages.getMessage((Enum) modelValue);
}
}
}
return nullToEmpty(super.convertToPresentation(modelValue));
}
use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.
the class WebTextField method valueBindingConnected.
@Override
protected void valueBindingConnected(ValueSource<V> valueSource) {
super.valueBindingConnected(valueSource);
if (valueSource instanceof EntityValueSource) {
DataAwareComponentsTools dataAwareComponentsTools = beanLocator.get(DataAwareComponentsTools.class);
EntityValueSource entityValueSource = (EntityValueSource) valueSource;
dataAwareComponentsTools.setupCaseConversion(this, entityValueSource);
dataAwareComponentsTools.setupMaxLength(this, entityValueSource);
}
}
use of com.haulmont.cuba.gui.components.data.meta.EntityValueSource in project cuba by cuba-platform.
the class WebTextField method getConversionErrorMessageInternal.
protected String getConversionErrorMessageInternal() {
String customErrorMessage = getConversionErrorMessage();
if (StringUtils.isNotEmpty(customErrorMessage)) {
return customErrorMessage;
}
Datatype<V> datatype = this.datatype;
if (datatype == null && valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
datatype = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
}
if (datatype != null) {
String msg = getDatatypeConversionErrorMsg(datatype);
if (StringUtils.isNotEmpty(msg)) {
return msg;
}
}
return beanLocator.get(Messages.class).getMainMessage("databinding.conversion.error");
}
Aggregations