001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.widgets; 003 004import java.util.List; 005 006import javax.swing.text.JTextComponent; 007 008import org.openstreetmap.josm.Main; 009import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox; 010 011/** 012 * An {@link AutoCompletingComboBox} which keeps a history 013 */ 014public class HistoryComboBox extends AutoCompletingComboBox { 015 private final ComboBoxHistory model; 016 017 public static final int DEFAULT_SEARCH_HISTORY_SIZE = 15; 018 019 /** 020 * Constructs a new {@code HistoryComboBox}. 021 */ 022 public HistoryComboBox() { 023 int maxsize = Main.pref.getInteger("search.history-size", DEFAULT_SEARCH_HISTORY_SIZE); 024 setModel(model = new ComboBoxHistory(maxsize)); 025 setEditable(true); 026 } 027 028 /** 029 * Returns the text contained in this component 030 * @return the text 031 * @see JTextComponent#getText() 032 */ 033 public String getText() { 034 return getEditorComponent().getText(); 035 } 036 037 /** 038 * Sets the text of this component to the specified text 039 * @param value the text to set 040 * @see JTextComponent#setText(java.lang.String) 041 */ 042 public void setText(String value) { 043 setAutocompleteEnabled(false); 044 getEditorComponent().setText(value); 045 setAutocompleteEnabled(true); 046 } 047 048 /** 049 * Adds or moves the current element to the top of the history 050 * @see ComboBoxHistory#addElement(java.lang.String) 051 */ 052 public void addCurrentItemToHistory() { 053 model.addElement((String) getEditor().getItem()); 054 } 055 056 /** 057 * Sets the elements of the ComboBox to the given items 058 * @param history the items to set 059 * @see ComboBoxHistory#setItemsAsString(java.util.List) 060 */ 061 public void setHistory(List<String> history) { 062 model.setItemsAsString(history); 063 } 064 065 /** 066 * Returns the items as strings 067 * @return the items as strings 068 * @see ComboBoxHistory#asStringList() 069 */ 070 public List<String> getHistory() { 071 return model.asStringList(); 072 } 073}