Search in sources :

Example 21 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource 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();
}
Also used : EntityPicker(io.jmix.ui.component.EntityPicker) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) DataContext(io.jmix.ui.model.DataContext) FrameOwner(io.jmix.ui.screen.FrameOwner) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource)

Example 22 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class ValuesSelectAction method execute.

/**
 * Executes the action.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void execute() {
    if (valuesPicker == null) {
        throw new IllegalStateException("Action is not bound to a ValuesPicker");
    }
    if (valuesPicker.getFrame() == null) {
        throw new IllegalStateException("ValuesPicker component is not bound to a frame");
    }
    ScreenBuilder builder = screenBuilders.screen(valuesPicker.getFrame().getFrameOwner());
    builder = screenInitializer.initBuilder(builder);
    Screen screen = builder.build();
    if (!(screen instanceof SelectValueController)) {
        throw new IllegalArgumentException("Select value screen must implement " + "'io.jmix.ui.app.valuespicker.selectvalue.SelectValueController");
    }
    if (valuesPicker.getValueSource() instanceof EntityValueSource) {
        initSelectValueComponentValueType((EntityValueSource) valuesPicker.getValueSource());
    }
    selectValueContext.setInitialValues(valuesPicker.getValue());
    ((SelectValueController<V>) screen).setSelectValueContext(selectValueContext);
    screen.addAfterCloseListener(event -> {
        CloseAction closeAction = event.getCloseAction();
        if (closeAction.equals(WINDOW_COMMIT_AND_CLOSE_ACTION)) {
            SelectValueController<V> selectValueScreen = (SelectValueController<V>) screen;
            valuesPicker.setValueFromUser((selectValueScreen).getValue());
        }
    });
    screen.show();
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) ScreenBuilder(io.jmix.ui.builder.ScreenBuilder) Screen(io.jmix.ui.screen.Screen) SelectValueController(io.jmix.ui.app.valuespicker.selectvalue.SelectValueController) CloseAction(io.jmix.ui.screen.CloseAction)

Example 23 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource 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;
}
Also used : Options(io.jmix.ui.component.data.Options) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) Nested(io.jmix.ui.model.Nested) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions) DataContext(io.jmix.ui.model.DataContext) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit) DataUnit(io.jmix.ui.component.data.DataUnit) HasValueSource(io.jmix.ui.component.data.HasValueSource) Screens(io.jmix.ui.Screens) io.jmix.ui.component(io.jmix.ui.component) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) HasValueSource(io.jmix.ui.component.data.HasValueSource) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit)

Example 24 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class AbstractTextArea method convertToModel.

@Nullable
@Override
protected V convertToModel(@Nullable String componentRawValue) throws ConversionException {
    String value = emptyToNull(componentRawValue);
    if (isTrimming()) {
        value = StringUtils.trimToNull(value);
    }
    if (datatype != null) {
        try {
            return datatype.parse(value, locale);
        } catch (ValueConversionException e) {
            throw new ConversionException(e.getLocalizedMessage(), e);
        } catch (ParseException e) {
            throw new ConversionException(getConversionErrorMessageInternal(), e);
        }
    }
    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
        try {
            return propertyDataType.parse(value, locale);
        } catch (ValueConversionException e) {
            throw new ConversionException(e.getLocalizedMessage(), e);
        } catch (ParseException e) {
            throw new ConversionException(getConversionErrorMessageInternal(), e);
        }
    }
    return super.convertToModel(value);
}
Also used : ValueConversionException(io.jmix.ui.component.data.ValueConversionException) ConversionException(io.jmix.ui.component.data.ConversionException) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) ParseException(java.text.ParseException) ValueConversionException(io.jmix.ui.component.data.ValueConversionException) Nullable(javax.annotation.Nullable)

Example 25 with EntityValueSource

use of io.jmix.ui.component.data.meta.EntityValueSource in project jmix by jmix-framework.

the class AbstractTextArea method valueBindingConnected.

@Override
protected void valueBindingConnected(ValueSource<V> valueSource) {
    super.valueBindingConnected(valueSource);
    if (valueSource instanceof EntityValueSource) {
        DataAwareComponentsTools dataAwareComponentsTools = applicationContext.getBean(DataAwareComponentsTools.class);
        EntityValueSource entityValueSource = (EntityValueSource) valueSource;
        dataAwareComponentsTools.setupCaseConversion(this, entityValueSource);
        dataAwareComponentsTools.setupMaxLength(this, entityValueSource);
    }
}
Also used : EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) DataAwareComponentsTools(io.jmix.ui.component.data.DataAwareComponentsTools)

Aggregations

EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)40 DataAwareComponentsTools (io.jmix.ui.component.data.DataAwareComponentsTools)11 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)10 ValueSource (io.jmix.ui.component.data.ValueSource)9 Nullable (javax.annotation.Nullable)9 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)8 Messages (io.jmix.core.Messages)5 ConversionException (io.jmix.ui.component.data.ConversionException)5 HasValueSource (io.jmix.ui.component.data.HasValueSource)5 Datatype (io.jmix.core.metamodel.datatype.Datatype)4 MetaClass (io.jmix.core.metamodel.model.MetaClass)4 ValueConversionException (io.jmix.ui.component.data.ValueConversionException)4 ParseException (java.text.ParseException)4 UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)3 io.jmix.ui.component (io.jmix.ui.component)3 Nested (io.jmix.ui.model.Nested)3 Range (io.jmix.core.metamodel.model.Range)2 DataContext (io.jmix.ui.model.DataContext)2 DatasourceValueSource (com.haulmont.cuba.gui.components.data.value.DatasourceValueSource)1 LegacyCollectionDsValueSource (com.haulmont.cuba.gui.components.data.value.LegacyCollectionDsValueSource)1