Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.ui;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import com.vaadin.data.provider.CallbackDataProvider;
import com.vaadin.data.provider.DataChangeEvent;
import com.vaadin.data.provider.DataCommunicator;
import com.vaadin.data.provider.DataGenerator;
import com.vaadin.data.provider.DataKeyMapper;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.data.provider.InMemoryDataProvider;
import com.vaadin.data.provider.ListDataProvider;
import org.jsoup.nodes.Element;
import com.vaadin.data.HasFilterableDataProvider;
import com.vaadin.data.HasValue;
import com.vaadin.data.ValueProvider;
import com.vaadin.event.FieldEvents;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcDecorator;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.server.ConnectorResource;
import com.vaadin.server.KeyMapper;
import com.vaadin.server.Resource;
import com.vaadin.server.ResourceReference;
import com.vaadin.server.SerializableBiPredicate;
import com.vaadin.server.SerializableConsumer;
import com.vaadin.server.SerializableFunction;
import com.vaadin.server.SerializableToIntFunction;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.ui.combobox.ComboBoxClientRpc;
import com.vaadin.shared.ui.combobox.ComboBoxConstants;
import com.vaadin.shared.ui.combobox.ComboBoxServerRpc;
import com.vaadin.shared.ui.combobox.ComboBoxState;
import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext;
import com.vaadin.ui.declarative.DesignFormatter;
import elemental.json.JsonObject;
/**
* A filtering dropdown single-select. Items are filtered based on user input.
* Supports the creation of new items when a handler is set by the user.
*
* @param
* item (bean) type in ComboBox
* @author Vaadin Ltd
*/
@SuppressWarnings("serial")
public class ComboBox extends AbstractSingleSelect
implements FieldEvents.BlurNotifier, FieldEvents.FocusNotifier,
HasFilterableDataProvider {
/**
* A callback method for fetching items. The callback is provided with a
* non-null string filter, offset index and limit.
*
* @param
* item (bean) type in ComboBox
* @since 8.0
*/
@FunctionalInterface
public interface FetchItemsCallback extends Serializable {
/**
* Returns a stream of items that match the given filter, limiting the
* results with given offset and limit.
*
* This method is called after the size of the data set is asked from a
* related size callback. The offset and limit are promised to be within
* the size of the data set.
*
* @param filter
* a non-null filter string
* @param offset
* the first index to fetch
* @param limit
* the fetched item count
* @return stream of items
*/
public Stream fetchItems(String filter, int offset, int limit);
}
/**
* Handler that adds a new item based on user input when the new items
* allowed mode is active.
*
* NOTE 1: If the new item is rejected the client must be notified of the
* fact via ComboBoxClientRpc or selection handling won't complete.
*
*
* NOTE 2: Selection handling must be completed separately if filtering the
* data source with the same value won't include the new item in the initial
* list of suggestions. Failing to do so will lead to selection handling
* never completing and previous selection remaining on the server.
*
*
* @since 8.0
* @deprecated Since 8.4 replaced by {@link NewItemProvider}.
*/
@Deprecated
@FunctionalInterface
public interface NewItemHandler extends SerializableConsumer {
}
/**
* Provider function that adds a new item based on user input when the new
* items allowed mode is active. After the new item handling is complete,
* this function should return {@code Optional.of(text)} for the completion
* of automatic selection handling. If automatic selection is not wished
* for, always return {@code Optional.isEmpty()}.
*
* @since 8.4
*/
@FunctionalInterface
public interface NewItemProvider
extends SerializableFunction> {
}
/**
* Item style generator class for declarative support.
*
* Provides a straightforward mapping between an item and its style.
*
* @param
* item type
* @since 8.0
*/
protected static class DeclarativeStyleGenerator
implements StyleGenerator {
private StyleGenerator fallback;
private Map styles = new HashMap<>();
public DeclarativeStyleGenerator(StyleGenerator fallback) {
this.fallback = fallback;
}
@Override
public String apply(T item) {
return styles.containsKey(item) ? styles.get(item)
: fallback.apply(item);
}
/**
* Sets a {@code style} for the {@code item}.
*
* @param item
* a data item
* @param style
* a style for the {@code item}
*/
protected void setStyle(T item, String style) {
styles.put(item, style);
}
}
private ComboBoxServerRpc rpc = new ComboBoxServerRpc() {
@Override
public void createNewItem(String itemValue) {
// New option entered
boolean added = false;
if (itemValue != null && !itemValue.isEmpty()) {
if (getNewItemProvider() != null) {
Optional item = getNewItemProvider().apply(itemValue);
added = item.isPresent();
// Fixes issue
// https://github.com/vaadin/framework/issues/11343
// Update the internal selection state immediately to avoid
// client side hanging. This is needed for cases that user
// interaction fires multi events (like adding and deleting)
// on a new item during the same round trip.
item.ifPresent(value -> {
setSelectedItem(value, true);
getDataCommunicator().reset();
});
} else if (getNewItemHandler() != null) {
getNewItemHandler().accept(itemValue);
// Up to the user to tell if no item was added.
added = true;
}
}
if (!added) {
// New item was not handled.
getRpcProxy(ComboBoxClientRpc.class).newItemNotAdded(itemValue);
}
}
@Override
public void setFilter(String filterText) {
getState().currentFilterText = filterText;
filterSlot.accept(filterText);
}
@Override
public void resetForceDataSourceUpdate() {
getState().forceDataSourceUpdate = false;
}
};
/**
* Handler for new items entered by the user.
*/
@Deprecated
private NewItemHandler newItemHandler;
/**
* Provider function for new items entered by the user.
*/
private NewItemProvider newItemProvider;
private StyleGenerator itemStyleGenerator = item -> null;
private SerializableConsumer filterSlot = filter -> {
// Just ignore when neither setDataProvider nor setItems has been called
};
private Registration dataProviderListener = null;
/**
* Constructs an empty combo box without a caption. The content of the combo
* box can be set with {@link #setDataProvider(DataProvider)} or
* {@link #setItems(Collection)}
*/
public ComboBox() {
this(new DataCommunicator() {
@Override
protected DataKeyMapper createKeyMapper(
ValueProvider identifierGetter) {
return new KeyMapper(identifierGetter) {
@Override
public void remove(T removeobj) {
// never remove keys from ComboBox to support selection
// of items that are not currently visible
}
};
}
});
}
/**
* Constructs an empty combo box, whose content can be set with
* {@link #setDataProvider(DataProvider)} or {@link #setItems(Collection)}.
*
* @param caption
* the caption to show in the containing layout, null for no
* caption
*/
public ComboBox(String caption) {
this();
setCaption(caption);
}
/**
* Constructs a combo box with a static in-memory data provider with the
* given options.
*
* @param caption
* the caption to show in the containing layout, null for no
* caption
* @param options
* collection of options, not null
*/
public ComboBox(String caption, Collection options) {
this(caption);
setItems(options);
}
/**
* Constructs and initializes an empty combo box.
*
* @param dataCommunicator
* the data comnunicator to use with this ComboBox
* @since 8.5
*/
protected ComboBox(DataCommunicator dataCommunicator) {
super(dataCommunicator);
init();
}
/**
* Initialize the ComboBox with default settings and register client to
* server RPC implementation.
*/
private void init() {
registerRpc(rpc);
registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));
addDataGenerator(new DataGenerator() {
/**
* Map for storing names for icons.
*/
private Map