source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/HistoryComboBox.java @ 12841

Last change on this file since 12841 was 12841, checked in by bastiK, 5 weeks ago

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.util.List;
5
6import javax.swing.text.JTextComponent;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
10
11/**
12 * An {@link AutoCompletingComboBox} which keeps a history
13 */
14public class HistoryComboBox extends AutoCompletingComboBox {
15    private final ComboBoxHistory model;
16
17    /**
18     * The default size of the search history.
19     */
20    public static final int DEFAULT_SEARCH_HISTORY_SIZE = 15;
21
22    /**
23     * Constructs a new {@code HistoryComboBox}.
24     */
25    public HistoryComboBox() {
26        int maxsize = Main.pref.getInt("search.history-size", DEFAULT_SEARCH_HISTORY_SIZE);
27        model = new ComboBoxHistory(maxsize);
28        setModel(model);
29        setEditable(true);
30    }
31
32    /**
33     * Returns the text contained in this component
34     * @return the text
35     * @see JTextComponent#getText()
36     */
37    public String getText() {
38        return getEditorComponent().getText();
39    }
40
41    /**
42     * Sets the text of this component to the specified text
43     * @param value the text to set
44     * @see JTextComponent#setText(java.lang.String)
45     */
46    public void setText(String value) {
47        setAutocompleteEnabled(false);
48        getEditorComponent().setText(value);
49        setAutocompleteEnabled(true);
50    }
51
52    /**
53     * Adds or moves the current element to the top of the history
54     * @see ComboBoxHistory#addElement(java.lang.String)
55     */
56    public void addCurrentItemToHistory() {
57        model.addElement((String) getEditor().getItem());
58    }
59
60    /**
61     * Sets the elements of the ComboBox to the given items
62     * @param history the items to set
63     * @see ComboBoxHistory#setItemsAsString(java.util.List)
64     */
65    public void setHistory(List<String> history) {
66        model.setItemsAsString(history);
67    }
68
69    /**
70     * Returns the items as strings
71     * @return the items as strings
72     * @see ComboBoxHistory#asStringList()
73     */
74    public List<String> getHistory() {
75        return model.asStringList();
76    }
77}
Note: See TracBrowser for help on using the repository browser.