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

Last change on this file since 3215 was 3215, checked in by bastiK, 15 years ago

fixed #4979 Exception when opening the download window; fixed #2221 - Unexcepted Exception when typing into "Change values?" box with an input method on Mac OS X

  • 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.Collections;
32import java.util.Iterator;
33import java.util.List;
34
35import javax.swing.DefaultComboBoxModel;
36
37import org.openstreetmap.josm.gui.tagging.ac.*;
38
39public class ComboBoxHistory extends DefaultComboBoxModel implements Iterable<AutoCompletionListItem> {
40
41 private int maxSize = 10;
42
43 private List<HistoryChangedListener> listeners = new ArrayList<HistoryChangedListener>();
44
45 public ComboBoxHistory(int size) {
46 maxSize = size;
47 }
48
49 /**
50 * Adds or moves an element to the top of the history
51 */
52 @Override
53 public void addElement(Object o) {
54 if (o instanceof String) {
55 o = new AutoCompletionListItem((String) o);
56 }
57
58 String newEntry = ((AutoCompletionListItem)o).getValue();
59
60 // if history contains this object already, delete it,
61 // so that it looks like a move to the top
62 for (int i = 0; i < getSize(); i++) {
63 String oldEntry = ((AutoCompletionListItem) getElementAt(i)).getValue();
64 if(oldEntry.equals(newEntry)) {
65 removeElementAt(i);
66 }
67 }
68
69 // insert element at the top
70 insertElementAt(o, 0);
71
72 // remove an element, if the history gets too large
73 if(getSize()> maxSize) {
74 removeElementAt(getSize()-1);
75 }
76
77 // set selected item to the one just added
78 setSelectedItem(o);
79
80 fireHistoryChanged();
81 }
82
83 public Iterator<AutoCompletionListItem> iterator() {
84 return new Iterator<AutoCompletionListItem>() {
85
86 private int position = -1;
87
88 public void remove() {
89 removeElementAt(position);
90 }
91
92 public boolean hasNext() {
93 if(position < getSize()-1 && getSize()>0)
94 return true;
95 return false;
96 }
97
98 public AutoCompletionListItem next() {
99 position++;
100 return (AutoCompletionListItem)getElementAt(position);
101 }
102
103 };
104 }
105
106 public void setItemsAsString(List<String> items) {
107 removeAllElements();
108 for (int i = items.size()-1; i>=0; i--) {
109 addElement(new AutoCompletionListItem(items.get(i)));
110 }
111 }
112
113 public List<String> asStringList() {
114 List<String> list = new ArrayList<String>(maxSize);
115 for (AutoCompletionListItem item : this) {
116 list.add(item.getValue());
117 }
118 return list;
119 }
120
121 public void addHistoryChangedListener(HistoryChangedListener l) {
122 listeners.add(l);
123 }
124
125 public void removeHistoryChangedListener(HistoryChangedListener l) {
126 listeners.remove(l);
127 }
128
129 private void fireHistoryChanged() {
130 for (HistoryChangedListener l : listeners) {
131 l.historyChanged(asStringList());
132 }
133 }
134}
Note: See TracBrowser for help on using the repository browser.