1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompComboBoxModel;
|
---|
8 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A data model for the {@link HistoryComboBox}.
|
---|
12 | * <p>
|
---|
13 | * This model is an {@link AutoCompComboBoxModel} specialized in {@code String}s. It offers
|
---|
14 | * convenience functions to serialize to and from the JOSM preferences.
|
---|
15 | *
|
---|
16 | * @since 18173
|
---|
17 | */
|
---|
18 | public class HistoryComboBoxModel extends AutoCompComboBoxModel<String> {
|
---|
19 |
|
---|
20 | HistoryComboBoxModel() {
|
---|
21 | // The user's preference for max. number of items in histories.
|
---|
22 | setSize(Config.getPref().getInt("search.history-size", 15));
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Adds strings to the model.
|
---|
27 | * <p>
|
---|
28 | * Strings are added only until the max. history size is reached.
|
---|
29 | *
|
---|
30 | * @param strings the strings to add
|
---|
31 | */
|
---|
32 | public void addAllStrings(List<String> strings) {
|
---|
33 | strings.forEach(s -> addElement(s));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Gets all items in the history as a list of strings.
|
---|
38 | *
|
---|
39 | * @return the items in the history
|
---|
40 | */
|
---|
41 | public List<String> asStringList() {
|
---|
42 | List<String> list = new ArrayList<>(getSize());
|
---|
43 | this.forEach(item -> list.add(item));
|
---|
44 | return list;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Gets a preference loader and saver for this model.
|
---|
49 | *
|
---|
50 | * @return the instance
|
---|
51 | */
|
---|
52 | public Preferences prefs() {
|
---|
53 | return prefs(x -> x, x -> x);
|
---|
54 | }
|
---|
55 | }
|
---|