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 model = new ComboBoxHistory(maxsize); 025 setModel(model); 026 setEditable(true); 027 } 028 029 /** 030 * Returns the text contained in this component 031 * @return the text 032 * @see JTextComponent#getText() 033 */ 034 public String getText() { 035 return getEditorComponent().getText(); 036 } 037 038 /** 039 * Sets the text of this component to the specified text 040 * @param value the text to set 041 * @see JTextComponent#setText(java.lang.String) 042 */ 043 public void setText(String value) { 044 setAutocompleteEnabled(false); 045 getEditorComponent().setText(value); 046 setAutocompleteEnabled(true); 047 } 048 049 /** 050 * Adds or moves the current element to the top of the history 051 * @see ComboBoxHistory#addElement(java.lang.String) 052 */ 053 public void addCurrentItemToHistory() { 054 model.addElement((String) getEditor().getItem()); 055 } 056 057 /** 058 * Sets the elements of the ComboBox to the given items 059 * @param history the items to set 060 * @see ComboBoxHistory#setItemsAsString(java.util.List) 061 */ 062 public void setHistory(List<String> history) { 063 model.setItemsAsString(history); 064 } 065 066 /** 067 * Returns the items as strings 068 * @return the items as strings 069 * @see ComboBoxHistory#asStringList() 070 */ 071 public List<String> getHistory() { 072 return model.asStringList(); 073 } 074}