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

Last change on this file since 13173 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

  • Property svn:eol-style set to native
File size: 7.1 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.Set;
8
9import javax.swing.JTable;
10import javax.swing.table.AbstractTableModel;
11
12import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
13import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority;
14import org.openstreetmap.josm.data.tagging.ac.AutoCompletionSet;
15import org.openstreetmap.josm.tools.CheckParameterUtil;
16
17/**
18 * AutoCompletionList manages a graphical list of {@link AutoCompletionItem}s.
19 *
20 * The list is sorted, items with higher priority first, then according to lexicographic order
21 * on the value of the {@link AutoCompletionItem}.
22 *
23 * AutoCompletionList maintains two views on the list of {@link AutoCompletionItem}s.
24 * <ol>
25 * <li>the bare, unfiltered view which includes all items</li>
26 * <li>a filtered view, which includes only items which match a current filter expression</li>
27 * </ol>
28 *
29 * AutoCompletionList is an {@link AbstractTableModel} which serves the list of filtered
30 * items to a {@link JTable}.
31 * @since 1762
32 */
33public class AutoCompletionList extends AbstractTableModel {
34
35 /** the bare list of AutoCompletionItems */
36 private final transient AutoCompletionSet list;
37 /** the filtered list of AutoCompletionItems */
38 private final transient ArrayList<AutoCompletionItem> filtered;
39 /** the filter expression */
40 private String filter;
41
42 /**
43 * constructor
44 */
45 public AutoCompletionList() {
46 list = new AutoCompletionSet();
47 filtered = new ArrayList<>();
48 }
49
50 /**
51 * applies a filter expression to the list of {@link AutoCompletionItem}s.
52 *
53 * The matching criterion is a case insensitive substring match.
54 *
55 * @param filter the filter expression; must not be null
56 *
57 * @throws IllegalArgumentException if filter is null
58 */
59 public void applyFilter(String filter) {
60 CheckParameterUtil.ensureParameterNotNull(filter, "filter");
61 this.filter = filter;
62 filter();
63 }
64
65 /**
66 * clears the current filter
67 */
68 public void clearFilter() {
69 filter = null;
70 filter();
71 }
72
73 /**
74 * @return the current filter expression; null, if no filter expression is set
75 */
76 public String getFilter() {
77 return filter;
78 }
79
80 /**
81 * adds an {@link AutoCompletionItem} to the list. Only adds the item if it
82 * is not null and if not in the list yet.
83 *
84 * @param item the item
85 * @since 12859
86 */
87 public void add(AutoCompletionItem item) {
88 if (item != null && list.add(item)) {
89 filter();
90 }
91 }
92
93 /**
94 * adds another {@link AutoCompletionList} to this list. An item is only
95 * added it is not null and if it does not exist in the list yet.
96 *
97 * @param other another auto completion list; must not be null
98 * @throws IllegalArgumentException if other is null
99 */
100 public void add(AutoCompletionList other) {
101 CheckParameterUtil.ensureParameterNotNull(other, "other");
102 add(other.list);
103 }
104
105 /**
106 * adds a colleciton of {@link AutoCompletionItem} to this list. An item is only
107 * added it is not null and if it does not exist in the list yet.
108 *
109 * @param collection auto completion collection; must not be null
110 * @throws IllegalArgumentException if other is null
111 * @since 12859
112 */
113 public void add(Collection<AutoCompletionItem> collection) {
114 CheckParameterUtil.ensureParameterNotNull(collection, "collection");
115 if (list.addAll(collection)) {
116 filter();
117 }
118 }
119
120 /**
121 * adds a list of strings to this list. Only strings which
122 * are not null and which do not exist yet in the list are added.
123 *
124 * @param values a list of strings to add
125 * @param priority the priority to use
126 * @since 12859
127 */
128 public void add(Collection<String> values, AutoCompletionPriority priority) {
129 if (values != null && list.addAll(values, priority)) {
130 filter();
131 }
132 }
133
134 /**
135 * Adds values that have been entered by the user.
136 * @param values values that have been entered by the user
137 */
138 public void addUserInput(Collection<String> values) {
139 if (values != null && list.addUserInput(values)) {
140 filter();
141 }
142 }
143
144 /**
145 * checks whether a specific item is already in the list. Matches for the
146 * the value <strong>and</strong> the priority of the item
147 *
148 * @param item the item to check
149 * @return true, if item is in the list; false, otherwise
150 * @since 12859
151 */
152 public boolean contains(AutoCompletionItem item) {
153 return list.contains(item);
154 }
155
156 /**
157 * checks whether an item with the given value is already in the list. Ignores
158 * priority of the items.
159 *
160 * @param value the value of an auto completion item
161 * @return true, if value is in the list; false, otherwise
162 */
163 public boolean contains(String value) {
164 return list.contains(value);
165 }
166
167 /**
168 * removes the auto completion item with key <code>key</code>
169 * @param key the key
170 */
171 public void remove(String key) {
172 if (key != null) {
173 list.remove(key);
174 }
175 }
176
177 protected void filter() {
178 filtered.clear();
179 if (filter == null) {
180 // Collections.copy throws an exception "Source does not fit in dest"
181 filtered.ensureCapacity(list.size());
182 filtered.addAll(list);
183 return;
184 }
185
186 // apply the pattern to list of possible values. If it matches, add the
187 // value to the list of filtered values
188 //
189 list.stream().filter(e -> e.getValue().startsWith(filter)).forEach(filtered::add);
190 fireTableDataChanged();
191 }
192
193 /**
194 * replies the number of filtered items
195 *
196 * @return the number of filtered items
197 */
198 public int getFilteredSize() {
199 return filtered.size();
200 }
201
202 /**
203 * replies the idx-th item from the list of filtered items
204 * @param idx the index; must be in the range 0 &lt;= idx &lt; {@link #getFilteredSize()}
205 * @return the item
206 *
207 * @throws IndexOutOfBoundsException if idx is out of bounds
208 * @since 12859
209 */
210 public AutoCompletionItem getFilteredItemAt(int idx) {
211 return filtered.get(idx);
212 }
213
214 AutoCompletionSet getSet() {
215 return list;
216 }
217
218 Set<AutoCompletionItem> getUnmodifiableSet() {
219 return Collections.unmodifiableSet(list);
220 }
221
222 /**
223 * removes all elements from the auto completion list
224 */
225 public void clear() {
226 list.clear();
227 fireTableDataChanged();
228 }
229
230 @Override
231 public int getColumnCount() {
232 return 1;
233 }
234
235 @Override
236 public int getRowCount() {
237 return list == null ? 0 : getFilteredSize();
238 }
239
240 @Override
241 public Object getValueAt(int rowIndex, int columnIndex) {
242 return list == null ? null : getFilteredItemAt(rowIndex);
243 }
244}
Note: See TracBrowser for help on using the repository browser.