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