summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/converterGUI/SearchBoxList.java
blob: 10ef589c010260d394ebf5ea250ff84f473e3cba (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
 * Copyright (C) 2019 Adrien Hopkins
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.unitConverter.converterGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.function.Predicate;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * @author Adrien Hopkins
 * @since 2019-04-13
 * @since v0.2.0
 */
final class SearchBoxList extends JPanel {
	
	/**
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	private static final long serialVersionUID = 6226930279415983433L;
	
	/**
	 * The text to place in an empty search box.
	 * 
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	private static final String EMPTY_TEXT = "Search...";
	
	/**
	 * The color to use for an empty foreground.
	 * 
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	private static final Color EMPTY_FOREGROUND = new Color(192, 192, 192);
	
	// the components
	private final Collection<String> itemsToFilter;
	private final DelegateListModel<String> listModel;
	private final JTextField searchBox;
	private final JList<String> searchItems;
	
	private boolean searchBoxEmpty = true;
	
	// I need to do this because, for some reason, Swing is auto-focusing my
	// search box without triggering a focus
	// event.
	private boolean searchBoxFocused = false;
	
	private Predicate<String> customSearchFilter = o -> true;
	private final Comparator<String> defaultOrdering;
	private final boolean caseSensitive;
	
	/**
	 * Creates the {@code SearchBoxList}.
	 * 
	 * @param itemsToFilter items to put in the list
	 * @since 2019-04-14
	 */
	public SearchBoxList(final Collection<String> itemsToFilter) {
		this(itemsToFilter, null, false);
	}
	
	/**
	 * Creates the {@code SearchBoxList}.
	 * 
	 * @param itemsToFilter   items to put in the list
	 * @param defaultOrdering default ordering of items after filtration
	 *                        (null=Comparable)
	 * @param caseSensitive   whether or not the filtration is case-sensitive
	 * 
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	public SearchBoxList(final Collection<String> itemsToFilter,
			final Comparator<String> defaultOrdering,
			final boolean caseSensitive) {
		super(new BorderLayout(), true);
		this.itemsToFilter = itemsToFilter;
		this.defaultOrdering = defaultOrdering;
		this.caseSensitive = caseSensitive;
		
		// create the components
		this.listModel = new DelegateListModel<>(new ArrayList<>(itemsToFilter));
		this.searchItems = new JList<>(this.listModel);
		
		this.searchBox = new JTextField(EMPTY_TEXT);
		this.searchBox.setForeground(EMPTY_FOREGROUND);
		
		// add them to the panel
		this.add(this.searchBox, BorderLayout.PAGE_START);
		this.add(new JScrollPane(this.searchItems), BorderLayout.CENTER);
		
		// set up the search box
		this.searchBox.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(final FocusEvent e) {
				SearchBoxList.this.searchBoxFocusGained(e);
			}
			
			@Override
			public void focusLost(final FocusEvent e) {
				SearchBoxList.this.searchBoxFocusLost(e);
			}
		});
		
		this.searchBox.addCaretListener(e -> this.searchBoxTextChanged());
		this.searchBoxEmpty = true;
	}
	
	/**
	 * Adds an additional filter for searching.
	 * 
	 * @param filter filter to add.
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	public void addSearchFilter(final Predicate<String> filter) {
		this.customSearchFilter = this.customSearchFilter.and(filter);
	}
	
	/**
	 * Resets the search filter.
	 * 
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	public void clearSearchFilters() {
		this.customSearchFilter = o -> true;
	}
	
	/**
	 * @return this component's search box component
	 * @since 2019-04-14
	 * @since v0.2.0
	 */
	public final JTextField getSearchBox() {
		return this.searchBox;
	}
	
	/**
	 * @param searchText text to search for
	 * @return a filter that filters out that text, based on this list's case
	 *         sensitive setting
	 * @since 2019-04-14
	 * @since v0.2.0
	 */
	private Predicate<String> getSearchFilter(final String searchText) {
		if (this.caseSensitive)
			return string -> string.contains(searchText);
		else
			return string -> string.toLowerCase()
					.contains(searchText.toLowerCase());
	}
	
	/**
	 * @return this component's list component
	 * @since 2019-04-14
	 * @since v0.2.0
	 */
	public final JList<String> getSearchList() {
		return this.searchItems;
	}
	
	/**
	 * @return index selected in item list
	 * @since 2019-04-14
	 * @since v0.2.0
	 */
	public int getSelectedIndex() {
		return this.searchItems.getSelectedIndex();
	}
	
	/**
	 * @return value selected in item list
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	public String getSelectedValue() {
		return this.searchItems.getSelectedValue();
	}
	
	/**
	 * Re-applies the filters.
	 * 
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	public void reapplyFilter() {
		final String searchText = this.searchBoxEmpty ? ""
				: this.searchBox.getText();
		final FilterComparator comparator = new FilterComparator(searchText,
				this.defaultOrdering, this.caseSensitive);
		final Predicate<String> searchFilter = this.getSearchFilter(searchText);
		
		this.listModel.clear();
		this.itemsToFilter.forEach(string -> {
			if (searchFilter.test(string)) {
				this.listModel.add(string);
			}
		});
		
		// applies the custom filters
		this.listModel.removeIf(this.customSearchFilter.negate());
		
		// sorts the remaining items
		this.listModel.sort(comparator);
	}
	
	/**
	 * Runs whenever the search box gains focus.
	 * 
	 * @param e focus event
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	private void searchBoxFocusGained(final FocusEvent e) {
		this.searchBoxFocused = true;
		if (this.searchBoxEmpty) {
			this.searchBox.setText("");
			this.searchBox.setForeground(Color.BLACK);
		}
	}
	
	/**
	 * Runs whenever the search box loses focus.
	 * 
	 * @param e focus event
	 * @since 2019-04-13
	 * @since v0.2.0
	 */
	private void searchBoxFocusLost(final FocusEvent e) {
		this.searchBoxFocused = false;
		if (this.searchBoxEmpty) {
			this.searchBox.setText(EMPTY_TEXT);
			this.searchBox.setForeground(EMPTY_FOREGROUND);
		}
	}
	
	/**
	 * Runs whenever the text in the search box is changed.
	 * <p>
	 * Reapplies the search filter, and custom filters.
	 * </p>
	 * 
	 * @since 2019-04-14
	 * @since v0.2.0
	 */
	private void searchBoxTextChanged() {
		if (this.searchBoxFocused) {
			this.searchBoxEmpty = this.searchBox.getText().equals("");
		}
		final String searchText = this.searchBoxEmpty ? ""
				: this.searchBox.getText();
		final FilterComparator comparator = new FilterComparator(searchText,
				this.defaultOrdering, this.caseSensitive);
		final Predicate<String> searchFilter = this.getSearchFilter(searchText);
		
		// initialize list with items that match the filter then sort
		this.listModel.clear();
		this.itemsToFilter.forEach(string -> {
			if (searchFilter.test(string)) {
				this.listModel.add(string);
			}
		});
		
		// applies the custom filters
		this.listModel.removeIf(this.customSearchFilter.negate());
		
		// sorts the remaining items
		this.listModel.sort(comparator);
	}
	
	/**
	 * Manually updates the search box's item list.
	 * 
	 * @since 2020-08-27
	 */
	public void updateList() {
		this.searchBoxTextChanged();
	}
}