All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.flow.component.combobox.ComboBoxDataCommunicator Maven / Gradle / Ivy

There is a newer version: 24.5.4
Show newest version
/*
 * Copyright 2000-2024 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.flow.component.combobox;

import java.util.HashSet;
import java.util.Set;

import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.data.provider.DataCommunicator;
import com.vaadin.flow.data.provider.DataGenerator;
import com.vaadin.flow.data.provider.KeyMapper;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.internal.StateNode;

import elemental.json.JsonArray;

/**
 * Customized data communicator that uses a custom key mapper for preserving
 * keys of selected items when using lazy-loading.
 *
 * @param 
 *            The type of the item selectable in the combo box
 */
public class ComboBoxDataCommunicator extends DataCommunicator {

    /**
     * Customized key mapper that does not remove key for an item as long as it
     * is selected.
     *
     * @param 
     *            The type of the item selectable in the combo box
     */
    protected static class SelectionPreservingKeyMapper
            extends KeyMapper {

        private final ComboBoxBase comboBox;

        private final Set itemsMarkedForRemoval = new HashSet<>();

        public SelectionPreservingKeyMapper(
                ComboBoxBase comboBox) {
            this.comboBox = comboBox;
        }

        @Override
        public void remove(TItem item) {
            // Do not remove keys for selected items
            if (!comboBox.isSelected(item)) {
                super.remove(item);
            } else {
                // Mark item for removal as soon as it is not selected anymore
                itemsMarkedForRemoval.add(item);
            }
        }

        @Override
        public String key(TItem item) {
            // Unmark item when it becomes active again
            itemsMarkedForRemoval.remove(item);
            return super.key(item);
        }

        public void purgeItems() {
            // Try purging items that we were not able to remove before
            HashSet itemsToRemove = new HashSet<>(itemsMarkedForRemoval);
            itemsToRemove.forEach(item -> {
                if (!comboBox.isSelected(item)) {
                    super.remove(item);
                    itemsMarkedForRemoval.remove(item);
                }
            });
        }
    }

    public ComboBoxDataCommunicator(ComboBoxBase comboBox,
            DataGenerator dataGenerator, ArrayUpdater arrayUpdater,
            SerializableConsumer dataUpdater, StateNode stateNode,
            boolean fetchEnabled) {
        super(dataGenerator, arrayUpdater, dataUpdater, stateNode,
                fetchEnabled);

        setKeyMapper(new SelectionPreservingKeyMapper<>(comboBox));
    }

    public void notifySelectionChanged() {
        ((SelectionPreservingKeyMapper) getKeyMapper()).purgeItems();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy