| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.model.swing; |
| 5 | |
|
| 6 | |
import java.util.AbstractList; |
| 7 | |
import java.util.ArrayList; |
| 8 | |
import java.util.Collections; |
| 9 | |
import java.util.HashSet; |
| 10 | |
import java.util.List; |
| 11 | |
import java.util.Set; |
| 12 | |
|
| 13 | |
import org.homeunix.thecave.buddi.model.Document; |
| 14 | |
import org.homeunix.thecave.buddi.model.Transaction; |
| 15 | |
|
| 16 | |
import ca.digitalcave.moss.application.document.DocumentChangeEvent; |
| 17 | |
import ca.digitalcave.moss.application.document.DocumentChangeListener; |
| 18 | |
|
| 19 | 1 | public class DescriptionList extends AbstractList<String> { |
| 20 | |
private static final long serialVersionUID = 0; |
| 21 | |
|
| 22 | |
private Document model; |
| 23 | |
private final DocumentChangeListener listener; |
| 24 | |
private final List<String> descriptionList; |
| 25 | 1165 | private final Set<String> descriptions = new HashSet<String>(); |
| 26 | |
|
| 27 | 1165 | public DescriptionList(Document model) { |
| 28 | 1165 | this.model = model; |
| 29 | 1165 | this.descriptionList = new ArrayList<String>(); |
| 30 | 1165 | updateAutoCompleteList(); |
| 31 | |
|
| 32 | 1165 | listener = new DocumentChangeListener(){ |
| 33 | |
public void documentChange(DocumentChangeEvent event) { |
| 34 | 1 | updateAutoCompleteList(); |
| 35 | 1 | } |
| 36 | |
}; |
| 37 | 1165 | model.addDocumentChangeListener(listener); |
| 38 | 1165 | } |
| 39 | |
|
| 40 | |
@Override |
| 41 | |
public String get(int index) { |
| 42 | 0 | return descriptionList.get(index); |
| 43 | |
} |
| 44 | |
|
| 45 | |
@Override |
| 46 | |
public int size() { |
| 47 | 2426 | return descriptionList.size(); |
| 48 | |
} |
| 49 | |
|
| 50 | |
private void updateAutoCompleteList(){ |
| 51 | 1166 | descriptions.clear(); |
| 52 | 1166 | for (Transaction t : model.getTransactions()) { |
| 53 | 0 | descriptions.add(t.getDescription()); |
| 54 | |
} |
| 55 | |
|
| 56 | 1166 | descriptionList.clear(); |
| 57 | 1166 | descriptionList.addAll(descriptions); |
| 58 | 1166 | Collections.sort(descriptionList); |
| 59 | 1166 | } |
| 60 | |
} |