source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionList.java@ 12042

Last change on this file since 12042 was 10137, checked in by Don-vip, 8 years ago

sonar, javadoc

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.ac;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10
11import javax.swing.JTable;
12import javax.swing.table.AbstractTableModel;
13
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15
16/**
17 * AutoCompletionList manages a list of {@link AutoCompletionListItem}s.
18 *
19 * The list is sorted, items with higher priority first, then according to lexicographic order
20 * on the value of the {@link AutoCompletionListItem}.
21 *
22 * AutoCompletionList maintains two views on the list of {@link AutoCompletionListItem}s.
23 * <ol>
24 * <li>the bare, unfiltered view which includes all items</li>
25 * <li>a filtered view, which includes only items which match a current filter expression</li>
26 * </ol>
27 *
28 * AutoCompletionList is an {@link AbstractTableModel} which serves the list of filtered
29 * items to a {@link JTable}.
30 * @since 1762
31 */
32public class AutoCompletionList extends AbstractTableModel {
33
34 /** the bare list of AutoCompletionItems */
35 private final transient List<AutoCompletionListItem> list;
36 /** the filtered list of AutoCompletionItems */
37 private final transient ArrayList<AutoCompletionListItem> filtered;
38 /** the filter expression */
39 private String filter;
40 /** map from value to priority */
41 private final transient Map<String, AutoCompletionListItem> valutToItemMap;
42
43 /**
44 * constructor
45 */
46 public AutoCompletionList() {
47 list = new ArrayList<>();
48 filtered = new ArrayList<>();
49 valutToItemMap = new HashMap<>();
50 }
51
52 /**
53 * applies a filter expression to the list of {@link AutoCompletionListItem}s.
54 *
55 * The matching criterion is a case insensitive substring match.
56 *
57 * @param filter the filter expression; must not be null
58 *
59 * @throws IllegalArgumentException if filter is null
60 */
61 public void applyFilter(String filter) {
62 CheckParameterUtil.ensureParameterNotNull(filter, "filter");
63 this.filter = filter;
64 filter();
65 }
66
67 /**
68 * clears the current filter
69 */
70 public void clearFilter() {
71 filter = null;
72 filter();
73 }
74
75 /**
76 * @return the current filter expression; null, if no filter expression is set
77 */
78 public String getFilter() {
79 return filter;
80 }
81
82 /**
83 * adds an AutoCompletionListItem to the list. Only adds the item if it
84 * is not null and if not in the list yet.
85 *
86 * @param item the item
87 */
88 public void add(AutoCompletionListItem item) {
89 if (item == null)
90 return;
91 appendOrUpdatePriority(item);
92 sort();
93 filter();
94 }
95
96 /**
97 * adds another AutoCompletionList to this list. An item is only
98 * added it is not null and if it does not exist in the list yet.
99 *
100 * @param other another auto completion list; must not be null
101 * @throws IllegalArgumentException if other is null
102 */
103 public void add(AutoCompletionList other) {
104 CheckParameterUtil.ensureParameterNotNull(other, "other");
105 for (AutoCompletionListItem item : other.list) {
106 appendOrUpdatePriority(item);
107 }
108 sort();
109 filter();
110 }
111
112 /**
113 * adds a list of AutoCompletionListItem to this list. Only items which
114 * are not null and which do not exist yet in the list are added.
115 *
116 * @param other a list of AutoCompletionListItem; must not be null
117 * @throws IllegalArgumentException if other is null
118 */
119 public void add(List<AutoCompletionListItem> other) {
120 CheckParameterUtil.ensureParameterNotNull(other, "other");
121 for (AutoCompletionListItem toadd : other) {
122 appendOrUpdatePriority(toadd);
123 }
124 sort();
125 filter();
126 }
127
128 /**
129 * adds a list of strings to this list. Only strings which
130 * are not null and which do not exist yet in the list are added.
131 *
132 * @param values a list of strings to add
133 * @param priority the priority to use
134 */
135 public void add(Collection<String> values, AutoCompletionItemPriority priority) {
136 if (values == null)
137 return;
138 for (String value : values) {
139 if (value == null) {
140 continue;
141 }
142 AutoCompletionListItem item = new AutoCompletionListItem(value, priority);
143 appendOrUpdatePriority(item);
144
145 }
146 sort();
147 filter();
148 }
149
150 /**
151 * Adds values that have been entered by the user.
152 * @param values values that have been entered by the user
153 */
154 public void addUserInput(Collection<String> values) {
155 if (values == null)
156 return;
157 int i = 0;
158 for (String value : values) {
159 if (value != null) {
160 appendOrUpdatePriority(
161 new AutoCompletionListItem(value, new AutoCompletionItemPriority(false, false, false, i++)));
162 }
163 }
164 sort();
165 filter();
166 }
167
168 protected void appendOrUpdatePriority(AutoCompletionListItem toAdd) {
169 AutoCompletionListItem item = valutToItemMap.get(toAdd.getValue());
170 if (item == null) {
171 // new item does not exist yet. Add it to the list
172 list.add(toAdd);
173 valutToItemMap.put(toAdd.getValue(), toAdd);
174 } else {
175 item.setPriority(item.getPriority().mergeWith(toAdd.getPriority()));
176 }
177 }
178
179 /**
180 * checks whether a specific item is already in the list. Matches for the
181 * the value <strong>and</strong> the priority of the item
182 *
183 * @param item the item to check
184 * @return true, if item is in the list; false, otherwise
185 */
186 public boolean contains(AutoCompletionListItem item) {
187 if (item == null)
188 return false;
189 return list.contains(item);
190 }
191
192 /**
193 * checks whether an item with the given value is already in the list. Ignores
194 * priority of the items.
195 *
196 * @param value the value of an auto completion item
197 * @return true, if value is in the list; false, otherwise
198 */
199 public boolean contains(String value) {
200 if (value == null)
201 return false;
202 for (AutoCompletionListItem item: list) {
203 if (item.getValue().equals(value))
204 return true;
205 }
206 return false;
207 }
208
209 /**
210 * removes the auto completion item with key <code>key</code>
211 * @param key the key;
212 */
213 public void remove(String key) {
214 if (key == null)
215 return;
216 for (int i = 0; i < list.size(); i++) {
217 AutoCompletionListItem item = list.get(i);
218 if (item.getValue().equals(key)) {
219 list.remove(i);
220 return;
221 }
222 }
223 }
224
225 /**
226 * sorts the list
227 */
228 protected void sort() {
229 Collections.sort(list);
230 }
231
232 protected void filter() {
233 filtered.clear();
234 if (filter == null) {
235 // Collections.copy throws an exception "Source does not fit in dest"
236 filtered.ensureCapacity(list.size());
237 for (AutoCompletionListItem item: list) {
238 filtered.add(item);
239 }
240 return;
241 }
242
243 // apply the pattern to list of possible values. If it matches, add the
244 // value to the list of filtered values
245 //
246 for (AutoCompletionListItem item : list) {
247 if (item.getValue().startsWith(filter)) {
248 filtered.add(item);
249 }
250 }
251 fireTableDataChanged();
252 }
253
254 /**
255 * replies the number of filtered items
256 *
257 * @return the number of filtered items
258 */
259 public int getFilteredSize() {
260 return this.filtered.size();
261 }
262
263 /**
264 * replies the idx-th item from the list of filtered items
265 * @param idx the index; must be in the range 0 &lt;= idx &lt; {@link #getFilteredSize()}
266 * @return the item
267 *
268 * @throws IndexOutOfBoundsException if idx is out of bounds
269 */
270 public AutoCompletionListItem getFilteredItem(int idx) {
271 if (idx < 0 || idx >= getFilteredSize())
272 throw new IndexOutOfBoundsException("idx out of bounds. idx=" + idx);
273 return filtered.get(idx);
274 }
275
276 List<AutoCompletionListItem> getList() {
277 return list;
278 }
279
280 List<AutoCompletionListItem> getUnmodifiableList() {
281 return Collections.unmodifiableList(list);
282 }
283
284 /**
285 * removes all elements from the auto completion list
286 *
287 */
288 public void clear() {
289 valutToItemMap.clear();
290 list.clear();
291 fireTableDataChanged();
292 }
293
294 @Override
295 public int getColumnCount() {
296 return 1;
297 }
298
299 @Override
300 public int getRowCount() {
301
302 return list == null ? 0 : getFilteredSize();
303 }
304
305 @Override
306 public Object getValueAt(int rowIndex, int columnIndex) {
307 return list == null ? null : getFilteredItem(rowIndex);
308 }
309}
Note: See TracBrowser for help on using the repository browser.