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