Java JComboBox Model hasDataChanged(List newDatasets, ComboBoxModel currentModel)

Here you can find the source of hasDataChanged(List newDatasets, ComboBoxModel currentModel)

Description

Checks whether the data has changed and the model needs updating.

License

Open Source License

Parameter

Parameter Description
newDatasets the new list of datasets
currentModel the current model

Return

true if changed

Declaration

public static boolean hasDataChanged(List<String> newDatasets, ComboBoxModel<String> currentModel) 

Method Source Code


//package com.java2s;
/*//w  w w.  jav  a2  s .  c om
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.ComboBoxModel;

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

public class Main {
    /**
     * Checks whether the data has changed and the model needs updating.
     *
     * @param newDatasets      the new list of datasets
     * @param currentModel   the current model
     * @return         true if changed
     */
    public static boolean hasDataChanged(List<String> newDatasets, ComboBoxModel<String> currentModel) {
        boolean result;
        int i;
        Set<String> setDatasets;
        Set<String> setModel;

        setDatasets = new HashSet<>(newDatasets);
        setModel = new HashSet<>();
        for (i = 0; i < currentModel.getSize(); i++)
            setModel.add(currentModel.getElementAt(i));

        // different datasets?
        result = (setDatasets.size() != setModel.size())
                || !(setDatasets.containsAll(setModel) && setModel.containsAll(setDatasets));

        // different order?
        if (!result) {
            for (i = 0; i < newDatasets.size(); i++) {
                if (!newDatasets.get(i).equals(currentModel.getElementAt(i))) {
                    result = true;
                    break;
                }
            }
        }

        return result;
    }
}

Related

  1. generateComboModel(Object[] values)
  2. generateInsertWhereComboBoxModel(String label)
  3. getComboModelList(ComboBoxModel model)
  4. getNewDefaultComboBoxModel(ArrayList itemsToStream)
  5. getSelectedItemfromModel(Object combo)
  6. modelToList(DefaultComboBoxModel model)
  7. updateComboBoxModel(ComboBoxModel model, List values)
  8. updateComboBoxModel(final JComboBox aComboBox, final Vector aValues)