001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.widgets;
003
004import java.util.ArrayList;
005import java.util.List;
006
007import org.openstreetmap.josm.gui.tagging.ac.AutoCompComboBoxModel;
008import org.openstreetmap.josm.spi.preferences.Config;
009
010/**
011 * A data model for the {@link HistoryComboBox}.
012 * <p>
013 * This model is an {@link AutoCompComboBoxModel} specialized in {@code String}s. It offers
014 * convenience functions to serialize to and from the JOSM preferences.
015 *
016 * @since 18173
017 */
018public class HistoryComboBoxModel extends AutoCompComboBoxModel<String> {
019
020    HistoryComboBoxModel() {
021        // The user's preference for max. number of items in histories.
022        setSize(Config.getPref().getInt("search.history-size", 15));
023    }
024
025    /**
026     * Adds strings to the model.
027     * <p>
028     * Strings are added only until the max. history size is reached.
029     *
030     * @param strings the strings to add
031     */
032    public void addAllStrings(List<String> strings) {
033        strings.forEach(s -> addElement(s));
034    }
035
036    /**
037     * Gets all items in the history as a list of strings.
038     *
039     * @return the items in the history
040     */
041    public List<String> asStringList() {
042        List<String> list = new ArrayList<>(getSize());
043        this.forEach(item -> list.add(item));
044        return list;
045    }
046
047    /**
048     * Gets a preference loader and saver for this model.
049     *
050     * @return the instance
051     */
052    public Preferences prefs() {
053        return prefs(x -> x, x -> x);
054    }
055}