use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
the class EntityClearAction method execute.
/**
* Executes the action.
*/
@Override
public void execute() {
EntityPicker entityPicker = getEntityPicker();
// remove entity if it is a composition
Object value = entityPicker.getValue();
ValueSource valueSource = entityPicker.getValueSource();
if (value != null && !value.equals(entityPicker.getEmptyValue()) && valueSource instanceof EntityValueSource) {
EntityValueSource entityValueSource = (EntityValueSource) entityPicker.getValueSource();
Object entity = entityPicker.getValue();
if (entityValueSource.getMetaPropertyPath() != null && entityValueSource.getMetaPropertyPath().getMetaProperty().getType() == MetaProperty.Type.COMPOSITION) {
FrameOwner screen = entityPicker.getFrame().getFrameOwner();
DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
dataContext.remove(entity);
}
}
super.execute();
}
use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
the class EditorBuilderProcessor method buildEditor.
@SuppressWarnings("unchecked")
public <E, 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 = transformForCollectionContainer(entityFromEditor, ct);
E committedEntity = transform(reloadedEntity, builder);
E mergedEntity = merge(committedEntity, origin, parentDataContext);
if (builder.getMode() == EditMode.CREATE) {
boolean addsFirst;
if (!(ct instanceof Nested)) {
addsFirst = screenProperties.isCreateActionAddsFirst();
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 io.jmix.ui.component.Component.Focusable) {
((io.jmix.ui.component.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 reloadedEntity = transformForField(entityFromEditor, field);
E editedEntity = transform(reloadedEntity, builder);
if (field instanceof EntityComboBox) {
EntityComboBox entityComboBox = ((EntityComboBox) field);
Options options = entityComboBox.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 io.jmix.ui.component.Component.Focusable) {
((io.jmix.ui.component.Component.Focusable) field).focus();
}
});
}
if (builder instanceof EditorClassBuilder) {
Consumer<AfterScreenShowEvent> afterShowListener = ((EditorClassBuilder) builder).getAfterShowListener();
if (afterShowListener != null) {
screen.addAfterShowListener(new AfterShowListenerAdapter(afterShowListener));
}
Consumer<AfterScreenCloseEvent> closeListener = ((EditorClassBuilder) builder).getAfterCloseListener();
if (closeListener != null) {
screen.addAfterCloseListener(new AfterCloseListenerAdapter(closeListener));
}
}
return (S) screen;
}
use of io.jmix.ui.component.data.ValueSource in project jmix by jmix-framework.
the class AppSettingsGridLayoutBuilder method addRowToGrid.
protected void addRowToGrid(InstanceContainer container, GridLayout gridLayout, int currentRow, MetaProperty metaProperty) {
MetaClass metaClass = container.getEntityMetaClass();
Range range = metaProperty.getRange();
UiEntityAttributeContext attributeContext = new UiEntityAttributeContext(metaClass, metaProperty.getName());
accessManager.applyRegisteredConstraints(attributeContext);
if (!attributeContext.canView()) {
return;
}
if (range.isClass()) {
UiEntityContext entityContext = new UiEntityContext(range.asClass());
accessManager.applyRegisteredConstraints(entityContext);
if (!entityContext.isViewPermitted()) {
return;
}
}
// add label
Label fieldLabel = uiComponents.create(Label.class);
fieldLabel.setValue(getPropertyCaption(metaClass, metaProperty));
fieldLabel.setAlignment(io.jmix.ui.component.Component.Alignment.MIDDLE_LEFT);
gridLayout.add(fieldLabel, 0, currentRow + 1);
// current field
ValueSource valueSource = new ContainerValueSource<>(container, metaProperty.getName());
ComponentGenerationContext componentContext = new ComponentGenerationContext(metaClass, metaProperty.getName());
componentContext.setValueSource(valueSource);
gridLayout.add(createField(metaProperty, range, componentContext), 1, currentRow + 1);
// default value
ComponentGenerationContext componentContextForDefaultField = new ComponentGenerationContext(metaClass, metaProperty.getName());
ValueSource valueSourceForDefaultField = new ContainerValueSource<>(dataComponents.createInstanceContainer(metaClass.getJavaClass()), metaProperty.getName());
componentContextForDefaultField.setValueSource(valueSourceForDefaultField);
Field defaultValueField = createField(metaProperty, range, componentContextForDefaultField);
defaultValueField.setValue(appSettingsTools.getDefaultPropertyValue(metaClass.getJavaClass(), metaProperty.getName()));
defaultValueField.setEditable(false);
gridLayout.add(defaultValueField, 2, currentRow + 1);
}
Aggregations