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