1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Iterator;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import javax.swing.DefaultComboBoxModel;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
|
---|
11 |
|
---|
12 | public class ComboBoxHistory extends DefaultComboBoxModel<AutoCompletionListItem> implements Iterable<AutoCompletionListItem> {
|
---|
13 |
|
---|
14 | private int maxSize = 10;
|
---|
15 |
|
---|
16 | private transient List<HistoryChangedListener> listeners = new ArrayList<>();
|
---|
17 |
|
---|
18 | public ComboBoxHistory(int size) {
|
---|
19 | maxSize = size;
|
---|
20 | }
|
---|
21 |
|
---|
22 | public void addElement(String s) {
|
---|
23 | addElement(new AutoCompletionListItem(s));
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Adds or moves an element to the top of the history
|
---|
28 | */
|
---|
29 | @Override
|
---|
30 | public void addElement(AutoCompletionListItem o) {
|
---|
31 | String newEntry = o.getValue();
|
---|
32 |
|
---|
33 | // if history contains this object already, delete it,
|
---|
34 | // so that it looks like a move to the top
|
---|
35 | for (int i = 0; i < getSize(); i++) {
|
---|
36 | String oldEntry = getElementAt(i).getValue();
|
---|
37 | if (oldEntry.equals(newEntry)) {
|
---|
38 | removeElementAt(i);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | // insert element at the top
|
---|
43 | insertElementAt(o, 0);
|
---|
44 |
|
---|
45 | // remove an element, if the history gets too large
|
---|
46 | if (getSize() > maxSize) {
|
---|
47 | removeElementAt(getSize()-1);
|
---|
48 | }
|
---|
49 |
|
---|
50 | // set selected item to the one just added
|
---|
51 | setSelectedItem(o);
|
---|
52 |
|
---|
53 | fireHistoryChanged();
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public Iterator<AutoCompletionListItem> iterator() {
|
---|
58 | return new Iterator<AutoCompletionListItem>() {
|
---|
59 |
|
---|
60 | private int position = -1;
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void remove() {
|
---|
64 | removeElementAt(position);
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public boolean hasNext() {
|
---|
69 | if (position < getSize()-1 && getSize() > 0)
|
---|
70 | return true;
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public AutoCompletionListItem next() {
|
---|
76 | position++;
|
---|
77 | return getElementAt(position);
|
---|
78 | }
|
---|
79 | };
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void setItemsAsString(List<String> items) {
|
---|
83 | removeAllElements();
|
---|
84 | for (int i = items.size()-1; i >= 0; i--) {
|
---|
85 | addElement(new AutoCompletionListItem(items.get(i)));
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public List<String> asStringList() {
|
---|
90 | List<String> list = new ArrayList<>(maxSize);
|
---|
91 | for (AutoCompletionListItem item : this) {
|
---|
92 | list.add(item.getValue());
|
---|
93 | }
|
---|
94 | return list;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void addHistoryChangedListener(HistoryChangedListener l) {
|
---|
98 | listeners.add(l);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void removeHistoryChangedListener(HistoryChangedListener l) {
|
---|
102 | listeners.remove(l);
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void fireHistoryChanged() {
|
---|
106 | for (HistoryChangedListener l : listeners) {
|
---|
107 | l.historyChanged(asStringList());
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|