public void bind() { this.componentValueChangeSubscription = component.addValueChangeListener(this::componentValueChanged); this.sourceValueChangeSubscription = source.addValueChangeListener(this::sourceValueChanged); this.sourceStateChangeSubscription = source.addStateChangeListener(this::valueSourceStateChanged); if (source instanceof EntityValueSource) { this.sourceInstanceChangeSubscription = ((EntityValueSource<Entity, V>) source).addInstanceChangeListener(this::sourceInstanceChanged); } }
/** * Creates ListEditor component. * * @param valueSource value source * @param propertyId property id of dynamic attribute * @return list editor component or null */ @Nullable public Component generateComponent(ValueSource valueSource, String propertyId) { if (valueSource instanceof EntityValueSource) { MetaClass metaClass = ((EntityValueSource) valueSource).getEntityMetaClass(); MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(metaClass, propertyId); if (metaPropertyPath == null) { log.error("MetaPropertyPath for dynamic attribute {} not found", propertyId); return null; } return generateComponent(valueSource, metaPropertyPath); } return null; }
protected Entity initEntity() { EntityValueSource entityValueSource = (EntityValueSource) pickerField.getValueSource(); Entity entity = AppBeans.get(Metadata.class).create( entityValueSource.getMetaPropertyPath().getMetaProperty().getRange().asClass()); Entity ownerEntity = entityValueSource.getItem(); MetaProperty inverseProp = entityValueSource.getMetaPropertyPath().getMetaProperty().getInverse(); if (inverseProp != null) { entity.setValue(inverseProp.getName(), ownerEntity); } return entity; }
/** * @return return null if value source is not EntityValueSource or value source is not defined */ @Nullable default MetaPropertyPath getMetaPropertyPath() { if (getValueSource() == null) { return null; } ValueSource<FileDescriptor> valueSource = getValueSource(); return valueSource instanceof EntityValueSource ? ((EntityValueSource) valueSource).getMetaPropertyPath() : null; }
MetaPropertyPath propertyPath = entityValueSource.getMetaPropertyPath(); if (valueSource.getState() != BindingState.ACTIVE || propertyPath == null) { return; component.setVisible(security.isEntityAttrReadPermitted(entityValueSource.getEntityMetaClass(), propertyPath.toString())); ((Editable) component).setEditable(security.isEntityAttrUpdatePermitted(entityValueSource.getEntityMetaClass(), propertyPath.toString())); ComponentState componentState = calculateComponentState(entityValueSource.getItem(), propertyPath); if (componentState.hidden) { component.setVisible(false);
EntityValueSource entityValueSource = (EntityValueSource) valueSource; MetaPropertyPath metaPropertyPath = entityValueSource.getMetaPropertyPath(); if (entityValueSource.isDataModelSecurityEnabled()) { if (component instanceof Component.Editable) { if (!security.isEntityAttrUpdatePermitted(metaPropertyPath)) {
entity = (Entity) value; } else if (getValueSource() instanceof EntityValueSource) { entity = ((EntityValueSource) getValueSource()).getItem();
protected MetaProperty getMetaPropertyForEditedValue() { ValueSource<V> valueSource = getValueSource(); if (valueSource instanceof EntityValueSource) { MetaPropertyPath metaPropertyPath = ((EntityValueSource) valueSource).getMetaPropertyPath(); return metaPropertyPath.getMetaProperty(); } return null; }
ownerDs = ((DatasourceImplementation) getCollectionDatasourceFromOwner()); nonModifiedInTable = !ownerDs.getItemsToUpdate().contains( ((EntityValueSource) getValueSource()).getItem()); ownerDsModified = ownerDs.isModified(); } else if (getCollectionContainerFromOwner() != null) {
/** * Sets max length for textual UI component using Entity metadata. * * @param component UI component * @param valueSource value source */ public void setupMaxLength(TextInputField.MaxLengthLimited component, EntityValueSource valueSource) { MetaProperty metaProperty = valueSource.getMetaPropertyPath().getMetaProperty(); Map<String, Object> annotations = metaProperty.getAnnotations(); Integer maxLength = (Integer) annotations.get("length"); if (maxLength != null) { component.setMaxLength(maxLength); } Integer sizeMax = (Integer) annotations.get(Size.class.getName() + "_max"); if (sizeMax != null) { component.setMaxLength(sizeMax); } Integer lengthMax = (Integer) annotations.get(Length.class.getName() + "_max"); if (lengthMax != null) { component.setMaxLength(lengthMax); } }
@Override public Field createField(EntityValueSource valueSource, String property) { MetaClass metaClass = valueSource.getEntityMetaClass(); ComponentGenerationContext context = new ComponentGenerationContext(metaClass, property) .setValueSource(valueSource) .setComponentClass(DataGrid.class); Component component = uiComponentsGenerator.generate(context); if (component instanceof Field) { return (Field) component; } throw new IllegalStateException("Editor field must implement com.haulmont.cuba.gui.components.Field"); } }
protected void bindValueSourceEvents() { if (valueSource == null) { return; } if (valueSource instanceof BeanLocatorAware) { ((BeanLocatorAware) valueSource).setBeanLocator(beanLocator); } valueChangeSubscription = valueSource.addValueChangeListener(event -> updateComponent()); if (valueSource instanceof EntityValueSource) { instanceChangeSubscription = ((EntityValueSource<Entity, FileDescriptor>) valueSource) .addInstanceChangeListener(event -> updateComponent()); } }
@Nonnull protected Collection<V> getValueSourceValue() { ValueSource<Collection<V>> valueSource = getValueSource(); if (valueSource == null) { return Collections.emptyList(); } Collection<V> modelValue; if (valueSource instanceof EntityValueSource) { Class<?> modelCollectionType = ((EntityValueSource) valueSource) .getMetaPropertyPath().getMetaProperty().getJavaType(); Collection<V> valueSourceValue = valueSource.getValue() == null ? Collections.emptyList() : valueSource.getValue(); if (Set.class.isAssignableFrom(modelCollectionType)) { modelValue = new LinkedHashSet<>(valueSourceValue); } else { modelValue = new ArrayList<>(valueSourceValue); } } else { modelValue = new ArrayList<>(valueSource.getValue()); } return modelValue; }
@SuppressWarnings("unchecked") @Nullable @Override protected Options getOptions(EntityValueSource valueSource, String property) { MetaClass metaClass = valueSource.getEntityMetaClass(); MetaPropertyPath metaPropertyPath = metadataTools.resolveMetaPropertyPath(metaClass, property); Table.Column columnConf = webTable.getColumnsInternal().get(metaPropertyPath); CollectionContainer collectionContainer = findOptionsContainer(columnConf); if (collectionContainer != null) { return new ContainerOptions(collectionContainer); } CollectionDatasource ds = findOptionsDatasource(columnConf, property); if (ds != null) { return new DatasourceOptions(ds); } return null; }
public void setupDateRange(HasRange component, EntityValueSource valueSource) { MetaProperty metaProperty = valueSource.getMetaPropertyPath().getMetaProperty(); Class javaType = metaProperty.getRange().asDatatype().getJavaClass(); TemporalType temporalType = getTemporalType(metaProperty, javaType); if (metaProperty.getAnnotations().get(Past.class.getName()) != null) { LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX); ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime, ZoneId.systemDefault()); //noinspection unchecked component.setRangeEnd(dateTimeTransformations.transformFromZDT(zonedDateTime, javaType)); } else if (metaProperty.getAnnotations().get(Future.class.getName()) != null) { LocalDateTime dateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); // In case of date and time we can select the current date with future time, // so we start from the next day only if the time isn't displayed if (temporalType == TemporalType.DATE) { dateTime = dateTime.plusDays(1); } ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime, ZoneId.systemDefault()); //noinspection unchecked component.setRangeStart(dateTimeTransformations.transformFromZDT(zonedDateTime, javaType)); } }
@Override public Component createField(EntityValueSource valueSource, String property, Element xmlDescriptor) { MetaClass metaClass = valueSource.getEntityMetaClass(); ComponentGenerationContext context = new ComponentGenerationContext(metaClass, property) .setValueSource(valueSource) .setOptions(getOptions(valueSource, property)) .setXmlDescriptor(xmlDescriptor) .setComponentClass(Table.class); return componentsGenerator.generate(context); }
@Override protected String convertToPresentation(V modelValue) throws ConversionException { // Vaadin TextField does not permit `null` value if (datatype != null) { return nullToEmpty(datatype.format(modelValue, locale)); } if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) { EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource(); Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype(); return nullToEmpty(propertyDataType.format(modelValue)); } return nullToEmpty(super.convertToPresentation(modelValue)); }
protected String convertToPresentation(V modelValue) { if (formatter != null) { return formatter.apply(modelValue); } if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) { EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource(); MetaProperty metaProperty = entityValueSource.getMetaPropertyPath().getMetaProperty(); return metadataTools.format(modelValue, metaProperty); } return metadataTools.format(modelValue); }
/** * Sets case conversion using {@link CaseConversion} annotation on entity property. * * @param component UI component * @param valueSource value source */ public void setupCaseConversion(TextInputField.CaseConversionSupported component, EntityValueSource valueSource) { MetaProperty metaProperty = valueSource.getMetaPropertyPath().getMetaProperty(); Map<String, Object> annotations = metaProperty.getAnnotations(); String caseConversionAnnotation = CaseConversion.class.getName(); //noinspection unchecked Map<String, Object> caseConversion = (Map<String, Object>) annotations.get(caseConversionAnnotation); if (MapUtils.isNotEmpty(caseConversion)) { ConversionType conversionType = (ConversionType) caseConversion.get("type"); TextInputField.CaseConversion conversion = TextInputField.CaseConversion.valueOf(conversionType.name()); component.setCaseConversion(conversion); } }
@Override public MetaClass getMetaClass() { ValueSource<V> valueSource = getValueSource(); if (valueSource instanceof EntityValueSource) { MetaProperty metaProperty = ((EntityValueSource) valueSource).getMetaPropertyPath().getMetaProperty(); return metaProperty.getRange().asClass(); } else { return metaClass; } }