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 | /**
|
---|
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 | }
|
---|