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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/* Copyright (c) 2008, Henrik Niehaus
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * 3. Neither the name of the project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.openstreetmap.josm.gui.widgets;
29
30import java.util.ArrayList;
31import java.util.Iterator;
32import java.util.List;
33
34import javax.swing.DefaultComboBoxModel;
35
36import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
37
38public class ComboBoxHistory extends DefaultComboBoxModel implements Iterable<AutoCompletionListItem> {
39
40 private int maxSize = 10;
41
42 private List<HistoryChangedListener> listeners = new ArrayList<HistoryChangedListener>();
43
44 public ComboBoxHistory(int size) {
45 maxSize = size;
46 }
47
48 /**
49 * Adds or moves an element to the top of the history
50 */
51 @Override
52 public void addElement(Object o) {
53 if (o instanceof String) {
54 o = new AutoCompletionListItem((String) o);
55 }
56
57 String newEntry = ((AutoCompletionListItem)o).getValue();
58
59 // if history contains this object already, delete it,
60 // so that it looks like a move to the top
61 for (int i = 0; i < getSize(); i++) {
62 String oldEntry = ((AutoCompletionListItem) getElementAt(i)).getValue();
63 if(oldEntry.equals(newEntry)) {
64 removeElementAt(i);
65 }
66 }
67
68 // insert element at the top
69 insertElementAt(o, 0);
70
71 // remove an element, if the history gets too large
72 if(getSize()> maxSize) {
73 removeElementAt(getSize()-1);
74 }
75
76 // set selected item to the one just added
77 setSelectedItem(o);
78
79 fireHistoryChanged();
80 }
81
82 @Override
83 public Iterator<AutoCompletionListItem> iterator() {
84 return new Iterator<AutoCompletionListItem>() {
85
86 private int position = -1;
87
88 @Override
89 public void remove() {
90 removeElementAt(position);
91 }
92
93 @Override
94 public boolean hasNext() {
95 if(position < getSize()-1 && getSize()>0)
96 return true;
97 return false;
98 }
99
100 @Override
101 public AutoCompletionListItem next() {
102 position++;
103 return (AutoCompletionListItem)getElementAt(position);
104 }
105
106 };
107 }
108
109 public void setItemsAsString(List<String> items) {
110 removeAllElements();
111 for (int i = items.size()-1; i>=0; i--) {
112 addElement(new AutoCompletionListItem(items.get(i)));
113 }
114 }
115
116 public List<String> asStringList() {
117 List<String> list = new ArrayList<String>(maxSize);
118 for (AutoCompletionListItem item : this) {
119 list.add(item.getValue());
120 }
121 return list;
122 }
123
124 public void addHistoryChangedListener(HistoryChangedListener l) {
125 listeners.add(l);
126 }
127
128 public void removeHistoryChangedListener(HistoryChangedListener l) {
129 listeners.remove(l);
130 }
131
132 private void fireHistoryChanged() {
133 for (HistoryChangedListener l : listeners) {
134 l.historyChanged(asStringList());
135 }
136 }
137}
Note: See TracBrowser for help on using the repository browser.