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 | import java.util.NoSuchElementException;
|
---|
8 |
|
---|
9 | import javax.swing.DefaultComboBoxModel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * A data model for {@link HistoryComboBox}
|
---|
15 | */
|
---|
16 | class ComboBoxHistory extends DefaultComboBoxModel<AutoCompletionItem> implements Iterable<AutoCompletionItem> {
|
---|
17 |
|
---|
18 | private final int maxSize;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constructs a {@code ComboBoxHistory} keeping track of {@code maxSize} items
|
---|
22 | * @param size the history size
|
---|
23 | */
|
---|
24 | ComboBoxHistory(int size) {
|
---|
25 | maxSize = size;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Adds or moves an element to the top of the history
|
---|
30 | * @param s the element to add
|
---|
31 | */
|
---|
32 | public void addElement(String s) {
|
---|
33 | addElement(new AutoCompletionItem(s));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Adds or moves an element to the top of the history
|
---|
38 | * @param o the element to add
|
---|
39 | */
|
---|
40 | @Override
|
---|
41 | public void addElement(AutoCompletionItem o) {
|
---|
42 | String newEntry = o.getValue();
|
---|
43 |
|
---|
44 | // if history contains this object already, delete it,
|
---|
45 | // so that it looks like a move to the top
|
---|
46 | for (int i = 0; i < getSize(); i++) {
|
---|
47 | String oldEntry = getElementAt(i).getValue();
|
---|
48 | if (oldEntry.equals(newEntry)) {
|
---|
49 | removeElementAt(i);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | // insert element at the top
|
---|
54 | insertElementAt(o, 0);
|
---|
55 |
|
---|
56 | // remove an element, if the history gets too large
|
---|
57 | if (getSize() > maxSize) {
|
---|
58 | removeElementAt(getSize()-1);
|
---|
59 | }
|
---|
60 |
|
---|
61 | // set selected item to the one just added
|
---|
62 | setSelectedItem(o);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public Iterator<AutoCompletionItem> iterator() {
|
---|
67 | return new Iterator<AutoCompletionItem>() {
|
---|
68 |
|
---|
69 | private int position = -1;
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public void remove() {
|
---|
73 | removeElementAt(position);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public boolean hasNext() {
|
---|
78 | return position < getSize()-1 && getSize() > 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public AutoCompletionItem next() {
|
---|
83 | if (!hasNext())
|
---|
84 | throw new NoSuchElementException();
|
---|
85 | position++;
|
---|
86 | return getElementAt(position);
|
---|
87 | }
|
---|
88 | };
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * {@link javax.swing.DefaultComboBoxModel#removeAllElements() Removes all items}
|
---|
93 | * and {@link ComboBoxHistory#addElement(String) adds} the given items.
|
---|
94 | * @param items the items to set
|
---|
95 | */
|
---|
96 | public void setItemsAsString(List<String> items) {
|
---|
97 | removeAllElements();
|
---|
98 | for (int i = items.size()-1; i >= 0; i--) {
|
---|
99 | addElement(items.get(i));
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Returns the {@link AutoCompletionItem} items as strings
|
---|
105 | * @return a list of strings
|
---|
106 | */
|
---|
107 | public List<String> asStringList() {
|
---|
108 | List<String> list = new ArrayList<>(maxSize);
|
---|
109 | for (AutoCompletionItem item : this) {
|
---|
110 | list.add(item.getValue());
|
---|
111 | }
|
---|
112 | return list;
|
---|
113 | }
|
---|
114 | }
|
---|