[2512] | 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 |
|
---|
[8510] | 8 | import org.openstreetmap.josm.Main;
|
---|
[3210] | 9 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
|
---|
| 10 |
|
---|
[9484] | 11 | /**
|
---|
| 12 | * An {@link AutoCompletingComboBox} which keeps a history
|
---|
| 13 | */
|
---|
[3210] | 14 | public class HistoryComboBox extends AutoCompletingComboBox {
|
---|
[9078] | 15 | private final ComboBoxHistory model;
|
---|
[2512] | 16 |
|
---|
[12304] | 17 | /**
|
---|
| 18 | * The default size of the search history.
|
---|
| 19 | */
|
---|
[4018] | 20 | public static final int DEFAULT_SEARCH_HISTORY_SIZE = 15;
|
---|
| 21 |
|
---|
[7015] | 22 | /**
|
---|
| 23 | * Constructs a new {@code HistoryComboBox}.
|
---|
| 24 | */
|
---|
[2512] | 25 | public HistoryComboBox() {
|
---|
[12841] | 26 | int maxsize = Main.pref.getInt("search.history-size", DEFAULT_SEARCH_HISTORY_SIZE);
|
---|
[10179] | 27 | model = new ComboBoxHistory(maxsize);
|
---|
| 28 | setModel(model);
|
---|
[2512] | 29 | setEditable(true);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[9484] | 32 | /**
|
---|
| 33 | * Returns the text contained in this component
|
---|
| 34 | * @return the text
|
---|
| 35 | * @see JTextComponent#getText()
|
---|
| 36 | */
|
---|
[2512] | 37 | public String getText() {
|
---|
[9484] | 38 | return getEditorComponent().getText();
|
---|
[2512] | 39 | }
|
---|
| 40 |
|
---|
[9484] | 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 | */
|
---|
[2512] | 46 | public void setText(String value) {
|
---|
| 47 | setAutocompleteEnabled(false);
|
---|
[9484] | 48 | getEditorComponent().setText(value);
|
---|
[2512] | 49 | setAutocompleteEnabled(true);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[9484] | 52 | /**
|
---|
| 53 | * Adds or moves the current element to the top of the history
|
---|
| 54 | * @see ComboBoxHistory#addElement(java.lang.String)
|
---|
| 55 | */
|
---|
[2512] | 56 | public void addCurrentItemToHistory() {
|
---|
[8510] | 57 | model.addElement((String) getEditor().getItem());
|
---|
[2512] | 58 | }
|
---|
| 59 |
|
---|
[9484] | 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 | */
|
---|
[2512] | 65 | public void setHistory(List<String> history) {
|
---|
[3215] | 66 | model.setItemsAsString(history);
|
---|
[2512] | 67 | }
|
---|
| 68 |
|
---|
[9484] | 69 | /**
|
---|
| 70 | * Returns the items as strings
|
---|
| 71 | * @return the items as strings
|
---|
| 72 | * @see ComboBoxHistory#asStringList()
|
---|
| 73 | */
|
---|
[2512] | 74 | public List<String> getHistory() {
|
---|
[3215] | 75 | return model.asStringList();
|
---|
[2512] | 76 | }
|
---|
| 77 | }
|
---|