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