Search in sources :

Example 1 with EntityValueSource

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;
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DatasourceValueSource(com.haulmont.cuba.gui.components.data.value.DatasourceValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 2 with EntityValueSource

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;
}
Also used : EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) Options(com.haulmont.cuba.gui.components.data.Options) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) Screens(com.haulmont.cuba.gui.Screens) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)

Example 3 with EntityValueSource

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));
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) Messages(com.haulmont.cuba.core.global.Messages) Collection(java.util.Collection) Range(com.haulmont.chile.core.model.Range)

Example 4 with EntityValueSource

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);
    }
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) DataAwareComponentsTools(com.haulmont.cuba.gui.components.data.DataAwareComponentsTools)

Example 5 with 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");
}
Also used : EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) Messages(com.haulmont.cuba.core.global.Messages)

Aggregations

EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)39 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 DataAwareComponentsTools (com.haulmont.cuba.gui.components.data.DataAwareComponentsTools)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)9 HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)6 Messages (com.haulmont.cuba.core.global.Messages)5 ConversionException (com.haulmont.cuba.gui.components.data.ConversionException)5 Datatype (com.haulmont.chile.core.datatypes.Datatype)4 ValueConversionException (com.haulmont.chile.core.datatypes.ValueConversionException)4 Entity (com.haulmont.cuba.core.entity.Entity)4 ParseException (java.text.ParseException)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Range (com.haulmont.chile.core.model.Range)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 Field (com.haulmont.cuba.gui.components.Field)2 DatasourceValueSource (com.haulmont.cuba.gui.components.data.value.DatasourceValueSource)2 Pair (com.haulmont.bali.datastruct.Pair)1 Subscription (com.haulmont.bali.events.Subscription)1 Preconditions (com.haulmont.bali.util.Preconditions)1