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

Last change on this file since 4848 was 3351, checked in by jttt, 14 years ago

Fixed #5173 memory leak in search dialog - almost every ExtendedDialog wasn't disposed after close - changed to dispose dialogs automatically

  • Property svn:eol-style set to native
File size: 4.4 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 public Iterator<AutoCompletionListItem> iterator() {
83 return new Iterator<AutoCompletionListItem>() {
84
85 private int position = -1;
86
87 public void remove() {
88 removeElementAt(position);
89 }
90
91 public boolean hasNext() {
92 if(position < getSize()-1 && getSize()>0)
93 return true;
94 return false;
95 }
96
97 public AutoCompletionListItem next() {
98 position++;
99 return (AutoCompletionListItem)getElementAt(position);
100 }
101
102 };
103 }
104
105 public void setItemsAsString(List<String> items) {
106 removeAllElements();
107 for (int i = items.size()-1; i>=0; i--) {
108 addElement(new AutoCompletionListItem(items.get(i)));
109 }
110 }
111
112 public List<String> asStringList() {
113 List<String> list = new ArrayList<String>(maxSize);
114 for (AutoCompletionListItem item : this) {
115 list.add(item.getValue());
116 }
117 return list;
118 }
119
120 public void addHistoryChangedListener(HistoryChangedListener l) {
121 listeners.add(l);
122 }
123
124 public void removeHistoryChangedListener(HistoryChangedListener l) {
125 listeners.remove(l);
126 }
127
128 private void fireHistoryChanged() {
129 for (HistoryChangedListener l : listeners) {
130 l.historyChanged(asStringList());
131 }
132 }
133}
Note: See TracBrowser for help on using the repository browser.