Search in sources :

Example 1 with Options

use of com.haulmont.cuba.gui.components.data.Options in project cuba by cuba-platform.

the class AbstractComponentGenerationStrategy method createEntityField.

@SuppressWarnings("unchecked")
protected Component createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
    String linkAttribute = null;
    Element xmlDescriptor = context.getXmlDescriptor();
    if (xmlDescriptor != null) {
        linkAttribute = xmlDescriptor.attributeValue("link");
    }
    if (!Boolean.parseBoolean(linkAttribute)) {
        Options options = context.getOptions();
        boolean useOptionsLoader = false;
        if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
            DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
            CategoryAttribute attribute = metaProperty.getAttribute();
            CategoryAttributeConfiguration configuration = attribute.getConfiguration();
            if (Boolean.TRUE.equals(attribute.getLookup()) && configuration.hasOptionsLoader()) {
                useOptionsLoader = true;
            }
        }
        PickerField pickerField;
        if (options == null && !useOptionsLoader) {
            pickerField = uiComponents.create(PickerField.class);
            setValueSource(pickerField, context);
            if (mpp.getMetaProperty().getType() == MetaProperty.Type.ASSOCIATION) {
                guiActionSupport.createActionById(pickerField, PickerField.ActionType.LOOKUP.getId());
                if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
                    DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
                    getDynamicAttributesGuiTools().initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
                }
                boolean actionsByMetaAnnotations = guiActionSupport.createActionsByMetaAnnotations(pickerField);
                if (!actionsByMetaAnnotations) {
                    guiActionSupport.createActionById(pickerField, PickerField.ActionType.CLEAR.getId());
                }
            } else {
                guiActionSupport.createActionById(pickerField, PickerField.ActionType.OPEN.getId());
                guiActionSupport.createActionById(pickerField, PickerField.ActionType.CLEAR.getId());
            }
        } else {
            LookupPickerField lookupPickerField = uiComponents.create(LookupPickerField.class);
            setValueSource(lookupPickerField, context);
            if (useOptionsLoader) {
                DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
                CategoryAttribute attribute = metaProperty.getAttribute();
                ValueSource valueSource = context.getValueSource();
                if (valueSource instanceof ContainerValueSource) {
                    setOptionsLoader(attribute, lookupPickerField, (ContainerValueSource) valueSource);
                }
            } else {
                lookupPickerField.setOptions(options);
            }
            pickerField = lookupPickerField;
            guiActionSupport.createActionsByMetaAnnotations(pickerField);
        }
        if (xmlDescriptor != null) {
            String captionProperty = xmlDescriptor.attributeValue("captionProperty");
            if (StringUtils.isNotEmpty(captionProperty)) {
                pickerField.setCaptionMode(CaptionMode.PROPERTY);
                pickerField.setCaptionProperty(captionProperty);
            }
        }
        setValidators(pickerField, context);
        return pickerField;
    } else {
        EntityLinkField linkField = uiComponents.create(EntityLinkField.class);
        setValueSource(linkField, context);
        setLinkFieldAttributes(linkField, context);
        return linkField;
    }
}
Also used : ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) Options(com.haulmont.cuba.gui.components.data.Options) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) Element(org.dom4j.Element)

Example 2 with Options

use of com.haulmont.cuba.gui.components.data.Options in project cuba by cuba-platform.

the class AbstractComponentGenerationStrategy method setOptionsLoader.

protected void setOptionsLoader(CategoryAttribute categoryAttribute, LookupField lookupField, ContainerValueSource valueSource) {
    InstanceContainer<?> container = valueSource.getContainer();
    Entity entity = container.getItemOrNull();
    if (entity != null) {
        List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) entity, categoryAttribute);
        // noinspection unchecked
        lookupField.setOptions(new ListOptions(options));
    }
    container.addItemChangeListener(e -> {
        List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
        // noinspection unchecked
        lookupField.setOptions(new ListOptions(options));
    });
    List<CategoryAttribute> dependsOnAttributes = categoryAttribute.getConfiguration().getDependsOnAttributes();
    if (dependsOnAttributes != null && !dependsOnAttributes.isEmpty()) {
        List<String> dependsOnAttributesCodes = dependsOnAttributes.stream().map(a -> DynamicAttributesUtils.encodeAttributeCode(a.getCode())).collect(Collectors.toList());
        container.addItemPropertyChangeListener(e -> {
            if (dependsOnAttributesCodes.contains(e.getProperty())) {
                List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
                // noinspection unchecked
                lookupField.setOptions(new ListOptions(options));
                if (!options.contains(lookupField.getValue())) {
                    // noinspection unchecked
                    lookupField.setValue(null);
                }
            }
        });
    }
}
Also used : Resolution(com.haulmont.cuba.gui.components.DateField.Resolution) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) Time(java.sql.Time) Date(java.util.Date) ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) BooleanUtils(org.apache.commons.lang3.BooleanUtils) InstanceContainer(com.haulmont.cuba.gui.model.InstanceContainer) AppBeans(com.haulmont.cuba.core.global.AppBeans) StringUtils(org.apache.commons.lang3.StringUtils) MetaClass(com.haulmont.chile.core.model.MetaClass) java.time(java.time) Datatype(com.haulmont.chile.core.datatypes.Datatype) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) com.haulmont.cuba.core.app.dynamicattributes(com.haulmont.cuba.core.app.dynamicattributes) Method(java.lang.reflect.Method) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) Nullable(javax.annotation.Nullable) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Collection(java.util.Collection) Options(com.haulmont.cuba.gui.components.data.Options) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) Messages(com.haulmont.cuba.core.global.Messages) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) CurrencyValue(com.haulmont.cuba.core.entity.annotation.CurrencyValue) GuiActionSupport(com.haulmont.cuba.gui.components.actions.GuiActionSupport) Consumer(java.util.function.Consumer) List(java.util.List) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) Element(org.dom4j.Element) UiComponents(com.haulmont.cuba.gui.UiComponents) ListOptions(com.haulmont.cuba.gui.components.data.options.ListOptions) List(java.util.List)

Example 3 with Options

use of com.haulmont.cuba.gui.components.data.Options 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 4 with Options

use of com.haulmont.cuba.gui.components.data.Options in project cuba by cuba-platform.

the class LookupBuilderProcessor method handleSelectionWithField.

@SuppressWarnings("unchecked")
protected <E extends Entity> void handleSelectionWithField(@SuppressWarnings("unused") LookupBuilder<E> builder, HasValue<E> field, Collection<E> itemsFromLookup) {
    if (itemsFromLookup.isEmpty()) {
        return;
    }
    Collection<E> selectedItems = transform(itemsFromLookup, builder);
    Entity newValue = selectedItems.iterator().next();
    View viewForField = clientConfig.getReloadUnfetchedAttributesFromLookupScreens() && metadataTools.isPersistent(newValue.getClass()) ? getViewForField(field) : null;
    if (viewForField != null && !entityStates.isLoadedWithView(newValue, viewForField)) {
        newValue = dataManager.reload(newValue, viewForField);
    }
    if (field instanceof LookupPickerField) {
        LookupPickerField lookupPickerField = (LookupPickerField) field;
        Options options = lookupPickerField.getOptions();
        if (options instanceof EntityOptions) {
            EntityOptions entityOptions = (EntityOptions) options;
            if (entityOptions.containsItem(newValue)) {
                entityOptions.updateItem(newValue);
            }
            if (lookupPickerField.isRefreshOptionsOnLookupClose()) {
                entityOptions.refresh();
            }
        }
    }
    // In case of PickerField set the value as if the user had set it
    if (field instanceof SupportsUserAction) {
        ((SupportsUserAction<E>) field).setValueFromUser((E) newValue);
    } else {
        field.setValue((E) newValue);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) Options(com.haulmont.cuba.gui.components.data.Options) SupportsUserAction(com.haulmont.cuba.gui.components.SupportsUserAction) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions)

Example 5 with Options

use of com.haulmont.cuba.gui.components.data.Options in project cuba by cuba-platform.

the class DataGridEditorComponentGenerationStrategy method createEntityField.

@SuppressWarnings("unchecked")
@Override
protected Field createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
    Options options = context.getOptions();
    Lookup lookupAnnotation;
    if ((lookupAnnotation = mpp.getMetaProperty().getAnnotatedElement().getAnnotation(Lookup.class)) != null && lookupAnnotation.type() == LookupType.DROPDOWN) {
        MetaClass metaClass = mpp.getMetaProperty().getRange().asClass();
        CollectionContainer<Entity> container = dataComponents.createCollectionContainer(metaClass.getJavaClass());
        CollectionLoader<Entity> loader = dataComponents.createCollectionLoader();
        loader.setQuery("select e from " + metaClass.getName() + " e");
        loader.setView(View.MINIMAL);
        loader.setContainer(container);
        loader.load();
        options = new ContainerOptions(container);
    }
    if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
        DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
        CategoryAttribute attribute = metaProperty.getAttribute();
        if (Boolean.TRUE.equals(attribute.getLookup())) {
            DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
            CollectionDatasource optionsDatasource = dynamicAttributesGuiTools.createOptionsDatasourceForLookup(metaProperty.getRange().asClass(), attribute.getJoinClause(), attribute.getWhereClause());
            options = new DatasourceOptions(optionsDatasource);
        }
    }
    PickerField<?> pickerField;
    if (options == null) {
        pickerField = uiComponents.create(PickerField.class);
        setValueSource(pickerField, context);
        guiActionSupport.createActionById(pickerField, PickerField.ActionType.LOOKUP.getId());
        if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
            DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
            DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
            dynamicAttributesGuiTools.initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
        }
        boolean actionsByMetaAnnotations = guiActionSupport.createActionsByMetaAnnotations(pickerField);
        if (!actionsByMetaAnnotations) {
            guiActionSupport.createActionById(pickerField, PickerField.ActionType.CLEAR.getId());
        }
    } else {
        LookupPickerField<?> lookupPickerField = uiComponents.create(LookupPickerField.class);
        setValueSource(lookupPickerField, context);
        lookupPickerField.setOptions(options);
        pickerField = lookupPickerField;
        guiActionSupport.createActionsByMetaAnnotations(pickerField);
    }
    setupPickerFieldActions(pickerField);
    return pickerField;
}
Also used : MapScreenOptions(com.haulmont.cuba.gui.screen.MapScreenOptions) ContainerOptions(com.haulmont.cuba.gui.components.data.options.ContainerOptions) Options(com.haulmont.cuba.gui.components.data.Options) DatasourceOptions(com.haulmont.cuba.gui.components.data.options.DatasourceOptions) Entity(com.haulmont.cuba.core.entity.Entity) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) ContainerOptions(com.haulmont.cuba.gui.components.data.options.ContainerOptions) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) Lookup(com.haulmont.cuba.core.entity.annotation.Lookup) DatasourceOptions(com.haulmont.cuba.gui.components.data.options.DatasourceOptions)

Aggregations

Options (com.haulmont.cuba.gui.components.data.Options)5 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 Entity (com.haulmont.cuba.core.entity.Entity)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 EntityOptions (com.haulmont.cuba.gui.components.data.meta.EntityOptions)2 ListOptions (com.haulmont.cuba.gui.components.data.options.ListOptions)2 ContainerValueSource (com.haulmont.cuba.gui.components.data.value.ContainerValueSource)2 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)2 Element (org.dom4j.Element)2 Datatype (com.haulmont.chile.core.datatypes.Datatype)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 Range (com.haulmont.chile.core.model.Range)1 com.haulmont.cuba.core.app.dynamicattributes (com.haulmont.cuba.core.app.dynamicattributes)1 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)1 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)1 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)1 CurrencyValue (com.haulmont.cuba.core.entity.annotation.CurrencyValue)1 Lookup (com.haulmont.cuba.core.entity.annotation.Lookup)1