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
Line 
1// License: GPL. For details, see LICENSE file.
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
10import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
11
12/**
13 * A data model for {@link HistoryComboBox}
14 */
15class ComboBoxHistory extends DefaultComboBoxModel<AutoCompletionListItem> implements Iterable<AutoCompletionListItem> {
16
17 private final int maxSize;
18
19 private final transient List<HistoryChangedListener> listeners = new ArrayList<>();
20
21 /**
22 * Constructs a {@code ComboBoxHistory} keeping track of {@code maxSize} items
23 * @param size the history size
24 */
25 ComboBoxHistory(int size) {
26 maxSize = size;
27 }
28
29 /**
30 * Adds or moves an element to the top of the history
31 * @param s the element to add
32 */
33 public void addElement(String s) {
34 addElement(new AutoCompletionListItem(s));
35 }
36
37 /**
38 * Adds or moves an element to the top of the history
39 * @param o the element to add
40 */
41 @Override
42 public void addElement(AutoCompletionListItem o) {
43 String newEntry = o.getValue();
44
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++) {
48 String oldEntry = getElementAt(i).getValue();
49 if (oldEntry.equals(newEntry)) {
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
58 if (getSize() > maxSize) {
59 removeElementAt(getSize()-1);
60 }
61
62 // set selected item to the one just added
63 setSelectedItem(o);
64
65 fireHistoryChanged();
66 }
67
68 @Override
69 public Iterator<AutoCompletionListItem> iterator() {
70 return new Iterator<AutoCompletionListItem>() {
71
72 private int position = -1;
73
74 @Override
75 public void remove() {
76 removeElementAt(position);
77 }
78
79 @Override
80 public boolean hasNext() {
81 if (position < getSize()-1 && getSize() > 0)
82 return true;
83 return false;
84 }
85
86 @Override
87 public AutoCompletionListItem next() {
88 position++;
89 return getElementAt(position);
90 }
91 };
92 }
93
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 */
99 public void setItemsAsString(List<String> items) {
100 removeAllElements();
101 for (int i = items.size()-1; i >= 0; i--) {
102 addElement(items.get(i));
103 }
104 }
105
106 /**
107 * Returns the {@link AutoCompletionListItem} items as strings
108 * @return a list of strings
109 */
110 public List<String> asStringList() {
111 List<String> list = new ArrayList<>(maxSize);
112 for (AutoCompletionListItem item : this) {
113 list.add(item.getValue());
114 }
115 return list;
116 }
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) {
128 l.historyChanged(asStringList());
129 }
130 }
131}
Note: See TracBrowser for help on using the repository browser.