001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.widgets; 003 004import java.util.ArrayList; 005import java.util.Iterator; 006import java.util.List; 007 008import javax.swing.DefaultComboBoxModel; 009 010import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem; 011 012public class ComboBoxHistory extends DefaultComboBoxModel implements Iterable<AutoCompletionListItem> { 013 014 private int maxSize = 10; 015 016 private List<HistoryChangedListener> listeners = new ArrayList<HistoryChangedListener>(); 017 018 public ComboBoxHistory(int size) { 019 maxSize = size; 020 } 021 022 /** 023 * Adds or moves an element to the top of the history 024 */ 025 @Override 026 public void addElement(Object o) { 027 if (o instanceof String) { 028 o = new AutoCompletionListItem((String) o); 029 } 030 031 String newEntry = ((AutoCompletionListItem)o).getValue(); 032 033 // if history contains this object already, delete it, 034 // so that it looks like a move to the top 035 for (int i = 0; i < getSize(); i++) { 036 String oldEntry = ((AutoCompletionListItem) getElementAt(i)).getValue(); 037 if(oldEntry.equals(newEntry)) { 038 removeElementAt(i); 039 } 040 } 041 042 // insert element at the top 043 insertElementAt(o, 0); 044 045 // remove an element, if the history gets too large 046 if(getSize()> maxSize) { 047 removeElementAt(getSize()-1); 048 } 049 050 // set selected item to the one just added 051 setSelectedItem(o); 052 053 fireHistoryChanged(); 054 } 055 056 @Override 057 public Iterator<AutoCompletionListItem> iterator() { 058 return new Iterator<AutoCompletionListItem>() { 059 060 private int position = -1; 061 062 @Override 063 public void remove() { 064 removeElementAt(position); 065 } 066 067 @Override 068 public boolean hasNext() { 069 if(position < getSize()-1 && getSize()>0) 070 return true; 071 return false; 072 } 073 074 @Override 075 public AutoCompletionListItem next() { 076 position++; 077 return (AutoCompletionListItem)getElementAt(position); 078 } 079 080 }; 081 } 082 083 public void setItemsAsString(List<String> items) { 084 removeAllElements(); 085 for (int i = items.size()-1; i>=0; i--) { 086 addElement(new AutoCompletionListItem(items.get(i))); 087 } 088 } 089 090 public List<String> asStringList() { 091 List<String> list = new ArrayList<String>(maxSize); 092 for (AutoCompletionListItem item : this) { 093 list.add(item.getValue()); 094 } 095 return list; 096 } 097 098 public void addHistoryChangedListener(HistoryChangedListener l) { 099 listeners.add(l); 100 } 101 102 public void removeHistoryChangedListener(HistoryChangedListener l) { 103 listeners.remove(l); 104 } 105 106 private void fireHistoryChanged() { 107 for (HistoryChangedListener l : listeners) { 108 l.historyChanged(asStringList()); 109 } 110 } 111}