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

Last change on this file since 2218 was 2218, checked in by Gubaer, 16 years ago

see #3371: Fatal freeze trying to enter a changeset comment

File size: 3.6 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
37public class ComboBoxHistory extends DefaultComboBoxModel implements Iterable<String> {
38
39 private int maxSize = 10;
40
41 public ComboBoxHistory(int size) {
42 maxSize = size;
43 }
44
45 /**
46 * Adds or moves an element to the top of the history
47 */
48 @Override
49 public void addElement(Object o) {
50 String newEntry = (String)o;
51
52 // if history contains this object already, delete it,
53 // so that it looks like a move to the top
54 for (int i = 0; i < getSize(); i++) {
55 String oldEntry = (String) getElementAt(i);
56 if(oldEntry.equals(newEntry)) {
57 removeElementAt(i);
58 }
59 }
60
61 // insert element at the top
62 insertElementAt(o, 0);
63
64 // remove an element, if the history gets too large
65 if(getSize()> maxSize) {
66 removeElementAt(getSize()-1);
67 }
68
69 // set selected item to the one just added
70 setSelectedItem(o);
71 }
72
73 public Iterator<String> iterator() {
74 return new Iterator<String>() {
75
76 private int position = -1;
77
78 public void remove() {
79 removeElementAt(position);
80 }
81
82 public boolean hasNext() {
83 if(position < getSize()-1 && getSize()>0)
84 return true;
85 return false;
86 }
87
88 public String next() {
89 position++;
90 return getElementAt(position).toString();
91 }
92
93 };
94 }
95
96 public void setItems(List<String> items) {
97 removeAllElements();
98 Collections.reverse(items);
99 for (String item : items) {
100 addElement(item);
101 }
102 Collections.reverse(items);
103 }
104
105 public List<String> asList() {
106 List<String> list = new ArrayList<String>(maxSize);
107 for (String item : this) {
108 list.add(item);
109 }
110 return list;
111 }
112}
Note: See TracBrowser for help on using the repository browser.