1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package info.rolandkrueger.roklib.ui.swing.rapidsuggest;
26
27 import info.rolandkrueger.roklib.util.TernarySearchTreeMap;
28
29 import java.util.Collection;
30 import java.util.Iterator;
31 import java.util.Map;
32
33 import javax.swing.DefaultComboBoxModel;
34
35
36
37
38
39
40
41
42
43
44
45 public class SuggestionComboBoxModel extends DefaultComboBoxModel
46 {
47 private TernarySearchTreeMap tstMap;
48 private int selectedItemIndex = - 1;
49
50 public SuggestionComboBoxModel (Collection items)
51 {
52 tstMap = new TernarySearchTreeMap ();
53 for (Iterator it = items.iterator (); it.hasNext ();)
54 {
55 Object e = it.next ();
56 tstMap.put (e.toString ().toLowerCase (), e);
57 }
58 }
59
60 public SuggestionComboBoxModel (Object[] items)
61 {
62 tstMap = new TernarySearchTreeMap ();
63 for (int i = 0; i < items.length; i++)
64 {
65 tstMap.put (items[i].toString ().toLowerCase (), items[i]);
66 }
67 }
68
69 public SuggestionComboBoxModel ()
70 {
71 tstMap = new TernarySearchTreeMap ();
72 }
73
74 public SuggestionComboBoxModel (TernarySearchTreeMap map)
75 {
76 tstMap = new TernarySearchTreeMap ();
77 Map.Entry entry;
78 for (Iterator it = map.entrySet ().iterator (); it.hasNext ();)
79 {
80 entry = (Map.Entry) it.next ();
81 tstMap.put (entry.getKey ().toString ().toLowerCase (), entry.getValue ());
82 }
83 }
84
85 public String getSuggestion (String prefix)
86 {
87
88 String match;
89 Iterator it = tstMap.getPrefixSubtreeIterator (prefix.toLowerCase ());
90 if ((it != null) && (it.hasNext ()))
91 match = ((Map.Entry) it.next ()).getValue ().toString ();
92 else
93 match = null;
94 setSelectedItem (match);
95 return match;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 }
122
123 public boolean contains (Object o, boolean caseSensitive)
124 {
125 if (caseSensitive)
126 return tstMap.containsKey (o);
127 else
128 {
129 Map.Entry entry;
130 for (Iterator it = tstMap.entrySet ().iterator (); it.hasNext ();)
131 {
132 entry = (Map.Entry) it.next ();
133 if (o.toString ().equalsIgnoreCase (entry.getKey ().toString ())) return true;
134 }
135 }
136 return false;
137 }
138
139 public void setSelectedItem (Object anObject)
140 {
141 if (anObject == null)
142 {
143 selectedItemIndex = - 1;
144 return;
145 }
146 selectedItemIndex = tstMap.indexOf (anObject.toString ().toLowerCase ());
147 }
148
149 public Object getSelectedItem ()
150 {
151 if (selectedItemIndex == - 1)
152 return null;
153 else
154 return getElementAt (selectedItemIndex);
155 }
156
157
158
159
160
161
162 public void removeElementAt (int index)
163 {
164
165
166 }
167
168
169
170
171
172
173 public void addElement (Object obj)
174 {
175 tstMap.put (obj.toString ().toLowerCase (), obj);
176 }
177
178
179
180
181
182
183 public void removeElement (Object obj)
184 {
185 tstMap.remove (obj);
186 }
187
188
189
190
191
192
193
194 public void insertElementAt (Object obj, int index)
195 {
196 addElement (obj);
197 }
198
199
200
201
202
203
204 public int getSize ()
205 {
206 return tstMap.size ();
207 }
208
209 public Object getElementAt (int index)
210 {
211
212
213
214
215
216
217 return tstMap.getValueAt (index);
218 }
219
220
221
222
223
224
225
226
227
228
229
230 }