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); } }
protected void setValueToSource(V value) { if (source.getState() == BindingState.ACTIVE && !source.isReadOnly()) { source.setValue(value); } }
@Override public boolean isModified() { return !Objects.equals(component.getValue(), source.getValue()); }
protected void clearValue() { for (CubaTokenListLabel label : new ArrayList<>(itemComponents.values())) { T item = componentItems.get(label); if (item != null) { itemComponents.remove(item); componentItems.remove(label); } if (owner.itemChangeHandler != null) { owner.itemChangeHandler.removeItem(item); } } if (owner.itemChangeHandler == null) { ValueSource<Collection<T>> valueSource = owner.getValueSource(); if (valueSource != null) { Collection<T> vsv = owner.getValueSourceValue(); if (Set.class.isAssignableFrom(vsv.getClass())) { valueSource.setValue(new LinkedHashSet<>()); } else { valueSource.setValue(new ArrayList<>()); } } else { owner.setValue(new ArrayList<>()); } } }
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()); } }
@Override protected Collection<V> convertToModel(Set<V> componentRawValue) throws ConversionException { if (valueBinding != null) { Class<?> targetType = valueBinding.getSource().getType(); if (List.class.isAssignableFrom(targetType)) { return new ArrayList<>(componentRawValue != null ? componentRawValue : Collections.emptyList()); } else if (Set.class.isAssignableFrom(targetType)) { return new LinkedHashSet<>(componentRawValue != null ? componentRawValue : Collections.emptySet()); } } return new LinkedHashSet<>(componentRawValue != null ? componentRawValue : Collections.emptySet()); }
((Component.Editable) component).setEditable(!valueSource.isReadOnly());
if (valueSource.getState() != BindingState.ACTIVE || propertyPath == null) { return;
@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; }
protected void handleSelection(Collection<V> selected) { if (itemChangeHandler != null) { selected.forEach(itemChangeHandler::addItem); } else { ValueSource<Collection<V>> valueSource = getValueSource(); if (valueSource != null) { Collection<V> modelValue = refreshValueIfNeeded(); for (V newItem : selected) { if (!modelValue.contains(newItem)) { modelValue.add(newItem); } } valueSource.setValue(modelValue); } else { Collection<V> value = getValue(); if (value == null) { value = new ArrayList<>(); } value.addAll(selected); setValue(value); } } }
@Override protected Collection<V> convertToModel(Set<V> componentRawValue) throws ConversionException { Stream<V> items = optionsBinding == null ? Stream.empty() : optionsBinding.getSource().getOptions().filter(componentRawValue::contains); if (valueBinding != null) { Class<?> targetType = valueBinding.getSource().getType(); if (List.class.isAssignableFrom(targetType)) { return items.collect(Collectors.toList()); } else if (Set.class.isAssignableFrom(targetType)) { return items.collect(Collectors.toCollection(LinkedHashSet::new)); } } return items.collect(Collectors.toCollection(LinkedHashSet::new)); }
protected void valueSourceStateChanged(ValueSource.StateChangeEvent event) { if (event.getState() == BindingState.ACTIVE) { // read value to component component.setValue(source.getValue()); } }
protected void doRemove(CubaTokenListLabel source) { T item = componentItems.get(source); if (item != null) { itemComponents.remove(item); componentItems.remove(source); if (owner.itemChangeHandler != null) { owner.itemChangeHandler.removeItem(item); } else { if (owner.getValueSource() != null) { Collection<T> value = owner.getValueSourceValue(); value.remove(item); owner.getValueSource().setValue(value); } else { Collection<T> value = new ArrayList<>(owner.getValue()); value.remove(item); owner.setValue(value); } } } }
@SuppressWarnings("unchecked") @Override protected V convertToModel(Object componentRawValue) { if (isMultiSelect()) { Set collectionValue = (Set) componentRawValue; List<I> itemIds = getCurrentItems(); Stream<I> selectedItemsStream = itemIds.stream() .filter(collectionValue::contains); if (valueBinding != null) { Class<V> targetType = valueBinding.getSource().getType(); if (List.class.isAssignableFrom(targetType)) { return (V) selectedItemsStream.collect(Collectors.toList()); } if (Set.class.isAssignableFrom(targetType)) { return (V) selectedItemsStream.collect(Collectors.toCollection(LinkedHashSet::new)); } } return (V) selectedItemsStream.collect(Collectors.toCollection(LinkedHashSet::new)); } return super.convertToModel(componentRawValue); }
@Override public boolean isEmpty() { return owner.getValueSource() != null ? CollectionUtils.isEmpty(owner.getValueSource().getValue()) : CollectionUtils.isEmpty(getValue()); }
@SuppressWarnings("unchecked") @Override protected V convertToModel(Object componentRawValue) { if (isMultiSelect()) { Set collectionValue = (Set) componentRawValue; List<I> itemIds = getCurrentItems(); Stream<I> selectedItemsStream = itemIds.stream() .filter(collectionValue::contains); if (valueBinding != null) { Class<V> targetType = valueBinding.getSource().getType(); if (List.class.isAssignableFrom(targetType)) { return (V) selectedItemsStream.collect(Collectors.toList()); } if (Set.class.isAssignableFrom(targetType)) { return (V) selectedItemsStream.collect(Collectors.toCollection(LinkedHashSet::new)); } } return (V) selectedItemsStream.collect(Collectors.toCollection(LinkedHashSet::new)); } return super.convertToModel(componentRawValue); }