1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.io.cache; |
22 |
|
|
23 |
|
import java.awt.Dimension; |
24 |
|
import java.awt.FontMetrics; |
25 |
|
import java.awt.event.ActionEvent; |
26 |
|
import java.awt.event.ActionListener; |
27 |
|
import java.awt.event.FocusListener; |
28 |
|
import java.awt.event.KeyAdapter; |
29 |
|
import java.awt.event.KeyEvent; |
30 |
|
import java.awt.event.KeyListener; |
31 |
|
import java.util.ArrayList; |
32 |
|
import java.util.Arrays; |
33 |
|
import java.util.Collections; |
34 |
|
import java.util.LinkedHashSet; |
35 |
|
import java.util.List; |
36 |
|
import java.util.Set; |
37 |
|
|
38 |
|
import javax.swing.JComboBox; |
39 |
|
import javax.swing.JComponent; |
40 |
|
import javax.swing.JMenuItem; |
41 |
|
import javax.swing.JPopupMenu; |
42 |
|
import javax.swing.JTextField; |
43 |
|
import javax.swing.SwingUtilities; |
44 |
|
import javax.swing.event.CaretListener; |
45 |
|
import javax.swing.event.DocumentListener; |
46 |
|
import javax.swing.text.JTextComponent; |
47 |
|
|
48 |
|
import jalview.bin.Cache; |
49 |
|
import jalview.util.MessageManager; |
50 |
|
import jalview.util.Platform; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@author |
57 |
|
|
58 |
|
@param |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
|
|
| 52.2% |
Uncovered Elements: 88 (184) |
Complexity: 51 |
Complexity Density: 0.46 |
|
63 |
|
public class JvCacheableInputBox<E> |
64 |
|
{ |
65 |
|
protected JComboBox<String> comboBox; |
66 |
|
|
67 |
|
protected JTextField textField; |
68 |
|
|
69 |
|
protected JTextComponent textComponent; |
70 |
|
|
71 |
|
protected String cacheKey; |
72 |
|
|
73 |
|
protected AppCache appCache; |
74 |
|
|
75 |
|
private JPopupMenu popup = new JPopupMenu(); |
76 |
|
|
77 |
|
private JMenuItem menuItemClearCache = new JMenuItem(); |
78 |
|
|
79 |
|
volatile boolean enterWasPressed = false; |
80 |
|
|
81 |
|
private String prototypeDisplayValue; |
82 |
|
|
83 |
|
|
84 |
|
@return |
85 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
86 |
0 |
public boolean wasEnterPressed()... |
87 |
|
{ |
88 |
0 |
return enterWasPressed; |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@param |
96 |
|
@param |
97 |
|
|
|
|
| 78.6% |
Uncovered Elements: 6 (28) |
Complexity: 4 |
Complexity Density: 0.18 |
|
98 |
5 |
public JvCacheableInputBox(String newCacheKey, int length)... |
99 |
|
{ |
100 |
|
|
101 |
5 |
cacheKey = newCacheKey; |
102 |
5 |
prototypeDisplayValue = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; |
103 |
5 |
if (length > 0) |
104 |
|
{ |
105 |
5 |
StringBuilder sb = new StringBuilder(); |
106 |
161 |
for (int i = 0; i < length; i++) |
107 |
|
{ |
108 |
156 |
sb.append("X"); |
109 |
|
} |
110 |
5 |
setPrototypeDisplayValue(sb.toString()); |
111 |
|
} |
112 |
5 |
boolean useTextField = Platform.isJS(); |
113 |
|
|
114 |
|
|
115 |
5 |
if (useTextField) |
116 |
|
{ |
117 |
0 |
appCache = null; |
118 |
0 |
textComponent = textField = new JTextField(); |
119 |
0 |
FontMetrics fm = textField.getFontMetrics(textField.getFont()); |
120 |
0 |
textField.setPreferredSize(new Dimension( |
121 |
|
fm.stringWidth(prototypeDisplayValue), fm.getHeight() + 4)); |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
} |
132 |
|
else |
133 |
|
{ |
134 |
5 |
appCache = AppCache.getInstance(); |
135 |
5 |
comboBox = new JComboBox<>(); |
136 |
5 |
textComponent = (JTextComponent) comboBox.getEditor() |
137 |
|
.getEditorComponent(); |
138 |
5 |
comboBox.setEditable(true); |
139 |
5 |
comboBox.addKeyListener(new KeyAdapter() |
140 |
|
{ |
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
141 |
0 |
@Override... |
142 |
|
public void keyTyped(KeyEvent e) |
143 |
|
{ |
144 |
0 |
enterWasPressed = false; |
145 |
0 |
if (e.getKeyCode() == KeyEvent.VK_ENTER) |
146 |
|
{ |
147 |
0 |
enterWasPressed = true; |
148 |
|
} |
149 |
|
|
150 |
|
} |
151 |
|
}); |
152 |
5 |
comboBox.setPrototypeDisplayValue(prototypeDisplayValue); |
153 |
5 |
initCachePopupMenu(); |
154 |
5 |
initCache(newCacheKey); |
155 |
5 |
updateCache(); |
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
|
|
|
| 33.3% |
Uncovered Elements: 12 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
165 |
5 |
private void initCache(String cacheKey)... |
166 |
|
{ |
167 |
5 |
if (appCache == null) |
168 |
|
{ |
169 |
0 |
return; |
170 |
|
} |
171 |
|
|
172 |
5 |
String delimitedCacheStr = Cache.getProperty(cacheKey); |
173 |
5 |
if (delimitedCacheStr == null || delimitedCacheStr.isEmpty()) |
174 |
|
{ |
175 |
5 |
return; |
176 |
|
} |
177 |
|
|
178 |
0 |
List<String> persistedCacheItems = Arrays |
179 |
|
.asList(delimitedCacheStr.split(AppCache.CACHE_DELIMITER)); |
180 |
|
|
181 |
0 |
LinkedHashSet<String> foundCacheItems = appCache |
182 |
|
.getAllCachedItemsFor(cacheKey); |
183 |
0 |
if (foundCacheItems == null) |
184 |
|
{ |
185 |
0 |
foundCacheItems = new LinkedHashSet<>(); |
186 |
|
} |
187 |
|
|
188 |
0 |
for (String cacheItem : persistedCacheItems) |
189 |
|
{ |
190 |
0 |
foundCacheItems.add(cacheItem); |
191 |
|
} |
192 |
0 |
appCache.putCache(cacheKey, foundCacheItems); |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
198 |
5 |
private void initCachePopupMenu()... |
199 |
|
{ |
200 |
5 |
if (appCache == null) |
201 |
|
{ |
202 |
0 |
return; |
203 |
|
} |
204 |
5 |
menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12)); |
205 |
5 |
menuItemClearCache |
206 |
|
.setText(MessageManager.getString("action.clear_cached_items")); |
207 |
5 |
menuItemClearCache.addActionListener(new ActionListener() |
208 |
|
{ |
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
209 |
0 |
@Override... |
210 |
|
public void actionPerformed(ActionEvent e) |
211 |
|
{ |
212 |
|
|
213 |
0 |
setSelectedItem(""); |
214 |
0 |
appCache.deleteCacheItems(cacheKey); |
215 |
0 |
updateCache(); |
216 |
|
} |
217 |
|
}); |
218 |
|
|
219 |
5 |
popup.add(menuItemClearCache); |
220 |
5 |
comboBox.setComponentPopupMenu(popup); |
221 |
5 |
comboBox.add(popup); |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
@param |
228 |
|
@return |
229 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
230 |
0 |
static boolean isInteger(String text)... |
231 |
|
{ |
232 |
0 |
try |
233 |
|
{ |
234 |
0 |
Integer.parseInt(text); |
235 |
0 |
return true; |
236 |
|
} catch (NumberFormatException e) |
237 |
|
{ |
238 |
0 |
return false; |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
245 |
8 |
public void updateCache()... |
246 |
|
{ |
247 |
8 |
if (appCache == null) |
248 |
|
{ |
249 |
0 |
return; |
250 |
|
} |
251 |
8 |
SwingUtilities.invokeLater(new Runnable() |
252 |
|
{ |
|
|
| 46.7% |
Uncovered Elements: 24 (45) |
Complexity: 10 |
Complexity Density: 0.32 |
|
253 |
7 |
@Override... |
254 |
|
public void run() |
255 |
|
{ |
256 |
7 |
int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey)); |
257 |
7 |
String userInput = getUserInput(); |
258 |
7 |
if (userInput != null && !userInput.isEmpty()) |
259 |
|
{ |
260 |
0 |
LinkedHashSet<String> foundCache = appCache |
261 |
|
.getAllCachedItemsFor(cacheKey); |
262 |
|
|
263 |
|
|
264 |
0 |
foundCache.remove(userInput); |
265 |
0 |
foundCache.add(userInput); |
266 |
0 |
appCache.putCache(cacheKey, foundCache); |
267 |
|
} |
268 |
|
|
269 |
7 |
String lastSearch = userInput; |
270 |
7 |
if (comboBox.getItemCount() > 0) |
271 |
|
{ |
272 |
2 |
comboBox.removeAllItems(); |
273 |
|
} |
274 |
7 |
Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey); |
275 |
7 |
List<String> reversedCacheItems = new ArrayList<>(); |
276 |
7 |
reversedCacheItems.addAll(cacheItems); |
277 |
7 |
cacheItems = null; |
278 |
7 |
Collections.reverse(reversedCacheItems); |
279 |
7 |
if (lastSearch.isEmpty()) |
280 |
|
{ |
281 |
7 |
comboBox.addItem(""); |
282 |
|
} |
283 |
|
|
284 |
7 |
if (reversedCacheItems != null && !reversedCacheItems.isEmpty()) |
285 |
|
{ |
286 |
0 |
LinkedHashSet<String> foundCache = appCache |
287 |
|
.getAllCachedItemsFor(cacheKey); |
288 |
0 |
boolean prune = reversedCacheItems.size() > cacheLimit; |
289 |
0 |
int count = 1; |
290 |
0 |
boolean limitExceeded = false; |
291 |
0 |
for (String cacheItem : reversedCacheItems) |
292 |
|
{ |
293 |
0 |
limitExceeded = (count++ > cacheLimit); |
294 |
0 |
if (prune) |
295 |
|
{ |
296 |
0 |
if (limitExceeded) |
297 |
|
{ |
298 |
0 |
foundCache.remove(cacheItem); |
299 |
|
} |
300 |
|
else |
301 |
|
{ |
302 |
0 |
comboBox.addItem(cacheItem); |
303 |
|
} |
304 |
|
} |
305 |
|
else |
306 |
|
{ |
307 |
0 |
comboBox.addItem(cacheItem); |
308 |
|
} |
309 |
|
} |
310 |
0 |
appCache.putCache(cacheKey, foundCache); |
311 |
|
} |
312 |
7 |
setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch); |
313 |
|
} |
314 |
|
}); |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
321 |
2 |
public void persistCache()... |
322 |
|
{ |
323 |
2 |
if (appCache == null) |
324 |
|
{ |
325 |
0 |
return; |
326 |
|
} |
327 |
2 |
appCache.persistCache(cacheKey); |
328 |
|
} |
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
@return |
334 |
|
|
|
|
| 62.5% |
Uncovered Elements: 3 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
335 |
9 |
public String getUserInput()... |
336 |
|
{ |
337 |
9 |
if (comboBox == null) |
338 |
|
{ |
339 |
0 |
return textField.getText().trim(); |
340 |
|
} |
341 |
9 |
Object item = comboBox.getEditor().getItem(); |
342 |
9 |
return item == null ? "" : item.toString().trim(); |
343 |
|
} |
344 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
345 |
10 |
public JComponent getComponent()... |
346 |
|
{ |
347 |
10 |
return (comboBox == null ? textField : comboBox); |
348 |
|
} |
349 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
350 |
2 |
public void addActionListener(ActionListener actionListener)... |
351 |
|
{ |
352 |
2 |
if (comboBox == null) |
353 |
|
{ |
354 |
0 |
textField.addActionListener(actionListener); |
355 |
|
} |
356 |
|
else |
357 |
|
{ |
358 |
2 |
comboBox.addActionListener(actionListener); |
359 |
|
} |
360 |
|
} |
361 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
362 |
2 |
public void addDocumentListener(DocumentListener listener)... |
363 |
|
{ |
364 |
2 |
textComponent.getDocument().addDocumentListener(listener); |
365 |
|
} |
366 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
367 |
4 |
public void addFocusListener(FocusListener focusListener)... |
368 |
|
{ |
369 |
4 |
textComponent.addFocusListener(focusListener); |
370 |
|
} |
371 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
372 |
4 |
public void addKeyListener(KeyListener kl)... |
373 |
|
{ |
374 |
4 |
textComponent.addKeyListener(kl); |
375 |
|
} |
376 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
377 |
0 |
public void addCaretListener(CaretListener caretListener)... |
378 |
|
{ |
379 |
0 |
textComponent.addCaretListener(caretListener); |
380 |
|
} |
381 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
382 |
0 |
public void setEditable(boolean b)... |
383 |
|
{ |
384 |
0 |
if (comboBox != null) |
385 |
|
{ |
386 |
0 |
comboBox.setEditable(b); |
387 |
|
} |
388 |
|
} |
389 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
390 |
5 |
public void setPrototypeDisplayValue(String string)... |
391 |
|
{ |
392 |
5 |
prototypeDisplayValue = string; |
393 |
5 |
if (comboBox != null) |
394 |
|
{ |
395 |
0 |
comboBox.setPrototypeDisplayValue(string); |
396 |
|
} |
397 |
|
} |
398 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
399 |
8 |
public void setSelectedItem(String userInput)... |
400 |
|
{ |
401 |
8 |
if (comboBox != null) |
402 |
|
{ |
403 |
8 |
comboBox.setSelectedItem(userInput); |
404 |
|
} |
405 |
|
} |
406 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
407 |
0 |
public boolean isPopupVisible()... |
408 |
|
{ |
409 |
0 |
return (comboBox != null && comboBox.isPopupVisible()); |
410 |
|
} |
411 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
412 |
0 |
public void addItem(String item)... |
413 |
|
{ |
414 |
0 |
if (comboBox != null) |
415 |
|
{ |
416 |
0 |
comboBox.addItem(item); |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
|
} |