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

Last change on this file since 17318 was 12859, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - deprecate non-GUI AutoCompletion* classes from gui.tagging.ac. Offer better replacements in new data.tagging.ac package

  • Property svn:eol-style set to native
File size: 3.1 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;
7import java.util.NoSuchElementException;
8
9import javax.swing.DefaultComboBoxModel;
10
11import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
12
13/**
14 * A data model for {@link HistoryComboBox}
15 */
16class 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}
Note: See TracBrowser for help on using the repository browser.