source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/ComboBoxHistory.java@ 9484

Last change on this file since 9484 was 9484, checked in by simon04, 8 years ago

Refactoring: add JosmComboBox.getEditorComponent, Javadoc

  • Property svn:eol-style set to native
File size: 3.6 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[2218]2package org.openstreetmap.josm.gui.widgets;
3
4import java.util.ArrayList;
5import java.util.Iterator;
6import java.util.List;
7
8import javax.swing.DefaultComboBoxModel;
9
[3351]10import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
[2218]11
[9484]12/**
13 * A data model for {@link HistoryComboBox}
14 */
15class ComboBoxHistory extends DefaultComboBoxModel<AutoCompletionListItem> implements Iterable<AutoCompletionListItem> {
[3215]16
[9078]17 private final int maxSize;
[2218]18
[9078]19 private final transient List<HistoryChangedListener> listeners = new ArrayList<>();
[2219]20
[9484]21 /**
22 * Constructs a {@code ComboBoxHistory} keeping track of {@code maxSize} items
23 * @param size the history size
24 */
25 ComboBoxHistory(int size) {
[2218]26 maxSize = size;
27 }
28
[9484]29 /**
30 * Adds or moves an element to the top of the history
31 * @param s the element to add
32 */
[7015]33 public void addElement(String s) {
34 addElement(new AutoCompletionListItem(s));
35 }
[7509]36
[2218]37 /**
38 * Adds or moves an element to the top of the history
[9484]39 * @param o the element to add
[2218]40 */
41 @Override
[7015]42 public void addElement(AutoCompletionListItem o) {
[7021]43 String newEntry = o.getValue();
[3215]44
[2218]45 // if history contains this object already, delete it,
46 // so that it looks like a move to the top
47 for (int i = 0; i < getSize(); i++) {
[7021]48 String oldEntry = getElementAt(i).getValue();
[8510]49 if (oldEntry.equals(newEntry)) {
[2218]50 removeElementAt(i);
51 }
52 }
53
54 // insert element at the top
55 insertElementAt(o, 0);
56
57 // remove an element, if the history gets too large
[8510]58 if (getSize() > maxSize) {
[2218]59 removeElementAt(getSize()-1);
60 }
61
62 // set selected item to the one just added
63 setSelectedItem(o);
[2219]64
65 fireHistoryChanged();
[2218]66 }
67
[6084]68 @Override
[3215]69 public Iterator<AutoCompletionListItem> iterator() {
70 return new Iterator<AutoCompletionListItem>() {
[2218]71
72 private int position = -1;
73
[6084]74 @Override
[2218]75 public void remove() {
76 removeElementAt(position);
77 }
78
[6084]79 @Override
[2218]80 public boolean hasNext() {
[8510]81 if (position < getSize()-1 && getSize() > 0)
[2218]82 return true;
83 return false;
84 }
85
[6084]86 @Override
[3215]87 public AutoCompletionListItem next() {
[2218]88 position++;
[7021]89 return getElementAt(position);
[2218]90 }
91 };
92 }
93
[9484]94 /**
95 * {@link javax.swing.DefaultComboBoxModel#removeAllElements() Removes all items}
96 * and {@link ComboBoxHistory#addElement(String) adds} the given items.
97 * @param items the items to set
98 */
[3215]99 public void setItemsAsString(List<String> items) {
[2218]100 removeAllElements();
[8510]101 for (int i = items.size()-1; i >= 0; i--) {
[9484]102 addElement(items.get(i));
[2218]103 }
104 }
105
[9484]106 /**
107 * Returns the {@link AutoCompletionListItem} items as strings
108 * @return a list of strings
109 */
[3215]110 public List<String> asStringList() {
[7005]111 List<String> list = new ArrayList<>(maxSize);
[3215]112 for (AutoCompletionListItem item : this) {
113 list.add(item.getValue());
[2218]114 }
115 return list;
116 }
[2219]117
118 public void addHistoryChangedListener(HistoryChangedListener l) {
119 listeners.add(l);
120 }
121
122 public void removeHistoryChangedListener(HistoryChangedListener l) {
123 listeners.remove(l);
124 }
125
126 private void fireHistoryChanged() {
127 for (HistoryChangedListener l : listeners) {
[3215]128 l.historyChanged(asStringList());
[2219]129 }
130 }
[2218]131}
Note: See TracBrowser for help on using the repository browser.