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

Last change on this file since 6380 was 6380, checked in by Don-vip, 11 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 3.0 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
12public class ComboBoxHistory extends DefaultComboBoxModel implements Iterable<AutoCompletionListItem> {
13
14 private int maxSize = 10;
15
16 private List<HistoryChangedListener> listeners = new ArrayList<HistoryChangedListener>();
17
18 public ComboBoxHistory(int size) {
19 maxSize = size;
20 }
21
22 /**
23 * Adds or moves an element to the top of the history
24 */
25 @Override
26 public void addElement(Object o) {
27 if (o instanceof String) {
28 o = new AutoCompletionListItem((String) o);
29 }
30
31 String newEntry = ((AutoCompletionListItem)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 = ((AutoCompletionListItem) 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 (AutoCompletionListItem)getElementAt(position);
78 }
79
80 };
81 }
82
83 public void setItemsAsString(List<String> items) {
84 removeAllElements();
85 for (int i = items.size()-1; i>=0; i--) {
86 addElement(new AutoCompletionListItem(items.get(i)));
87 }
88 }
89
90 public List<String> asStringList() {
91 List<String> list = new ArrayList<String>(maxSize);
92 for (AutoCompletionListItem item : this) {
93 list.add(item.getValue());
94 }
95 return list;
96 }
97
98 public void addHistoryChangedListener(HistoryChangedListener l) {
99 listeners.add(l);
100 }
101
102 public void removeHistoryChangedListener(HistoryChangedListener l) {
103 listeners.remove(l);
104 }
105
106 private void fireHistoryChanged() {
107 for (HistoryChangedListener l : listeners) {
108 l.historyChanged(asStringList());
109 }
110 }
111}
Note: See TracBrowser for help on using the repository browser.