@SuppressWarnings("unchecked") protected <E extends Entity> void handleSelectionWithField(@SuppressWarnings("unused") LookupBuilder<E> builder, HasValue<E> field, Collection<E> selectedItems) { if (!selectedItems.isEmpty()) { Entity newValue = selectedItems.iterator().next(); 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); } } }
public <E extends Entity> void bind() { if (source instanceof EntityOptions && component instanceof HasValue) { this.componentValueChangeSubscription = ((HasValue<?>) component).addValueChangeListener(this::componentValueChanged); this.sourceValueChangeSupscription = ((EntityOptions<E>) source).addValueChangeListener(this::optionsSourceValueChanged); } this.sourceStateChangeSupscription = source.addStateChangeListener(this::optionsSourceStateChanged); this.sourceOptionsChangeSupscription = source.addOptionsChangeListener(this::optionsSourceOptionsChanged); }
protected Collection<V> refreshValueIfNeeded() { EntityOptions<V> options = (EntityOptions<V>) getOptions(); Collection<V> valueSourceValue = getValueSourceValue(); if (options == null || !isRefreshOptionsOnLookupClose()) { return valueSourceValue; } options.refresh(); for (V value : valueSourceValue) { options.getOptions() .filter(option -> Objects.equals(option.getId(), value.getId())) .findFirst() .ifPresent(option -> { valueSourceValue.remove(value); valueSourceValue.add(option); }); } return valueSourceValue; }
@SuppressWarnings("unchecked") protected void afterCommitOpenedEntity(Entity item) { if (pickerField instanceof LookupField) { LookupField lookupPickerField = ((LookupField) pickerField); EntityOptions entityOptionsSource = (EntityOptions) lookupPickerField.getOptions(); if (entityOptionsSource != null && entityOptionsSource.containsItem(item)) { entityOptionsSource.updateItem(item); } } if (pickerField.getValueSource() instanceof DatasourceValueSource) { DatasourceValueSource datasourceValueSource = (DatasourceValueSource) pickerField.getValueSource(); boolean modified = datasourceValueSource.isModified(); pickerField.setValue(item); datasourceValueSource.setModified(modified); } else { pickerField.setValue(item); } }
if (options instanceof EntityOptions && lookupPickerField.isRefreshOptionsOnLookupClose()) { ((EntityOptions) options).refresh();
@Override public void setOptions(Options<V> options) { if (this.optionsBinding != null) { this.optionsBinding.unbind(); this.optionsBinding = null; } if (options != null) { OptionsBinder optionsBinder = beanLocator.get(OptionsBinder.NAME); this.optionsBinding = optionsBinder.bind(options, this, this::setItemsToPresentation); this.optionsBinding.activate(); if (getMetaClass() == null && options instanceof EntityOptions) { setMetaClass(((EntityOptions<V>) options).getEntityMetaClass()); } } }
@SuppressWarnings("unchecked") protected void componentValueChanged(HasValue.ValueChangeEvent event) { // value could be List / Set / something else if (event.getValue() instanceof Entity || event.getValue() == null) { EntityOptions entityOptionsSource = (EntityOptions) this.source; entityOptionsSource.setSelectedItem((Entity) event.getValue()); } }
if (options instanceof EntityOptions) { EntityOptions entityOptions = (EntityOptions) options; if (entityOptions.containsItem(editedEntity)) { entityOptions.updateItem(editedEntity);
@Override public void setOptions(Options<V> options) { if (this.optionsBinding != null) { this.optionsBinding.unbind(); this.optionsBinding = null; } if (options != null) { OptionsBinder optionsBinder = beanLocator.get(OptionsBinder.NAME); this.optionsBinding = optionsBinder.bind(options, this, this::setItemsToPresentation); this.optionsBinding.activate(); if (getMetaClass() == null && options instanceof EntityOptions) { setMetaClass(((EntityOptions<V>) options).getEntityMetaClass()); } } }
@SuppressWarnings("unchecked") protected void handleLookupWindowSelection(Collection items) { if (items.isEmpty()) { return; } Entity item = (Entity) items.iterator().next(); Entity newValue = transformValueFromLookupWindow(item); if (pickerField instanceof LookupPickerField) { LookupPickerField lookupPickerField = (LookupPickerField) pickerField; Options options = lookupPickerField.getOptions(); if (options instanceof EntityOptions) { EntityOptions entityOptionsSource = (EntityOptions) options; if (entityOptionsSource.containsItem(newValue)) { entityOptionsSource.updateItem(newValue); } if (lookupPickerField.isRefreshOptionsOnLookupClose()) { entityOptionsSource.refresh(); } } } // Set the value as if the user had set it pickerField.setValueFromUser(newValue); afterSelect(items); if (afterLookupSelectionHandler != null) { afterLookupSelectionHandler.onSelect(items); } }
protected Class<V> getLookupEntityClass() { Class<V> entityClass; ValueSource<Collection<V>> valueSource = getValueSource(); if (valueSource != null) { if (valueSource instanceof EntityValueSource) { //noinspection unchecked entityClass = ((EntityValueSource) valueSource).getMetaPropertyPath().getRangeJavaClass(); } else { entityClass = ((LegacyCollectionDsValueSource<V>) valueSource).getDatasource().getMetaClass().getJavaClass(); } } else if (getOptions() instanceof EntityOptions) { entityClass = ((EntityOptions<V>) getOptions()).getEntityMetaClass().getJavaClass(); } else { throw new RuntimeException("Unable to determine entity class to open lookup"); } return entityClass; }