1 package jalview.io.cache;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.LinkedHashSet;
9 import javax.swing.JComboBox;
10 import javax.swing.SwingUtilities;
12 public class JvCacheableInputBox<E> extends JComboBox<String>
15 private static final long serialVersionUID = 5774610435079326695L;
17 private String cacheKey;
19 private AppCache appCache;
21 public JvCacheableInputBox(String cacheKey)
24 this.cacheKey = cacheKey;
26 setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
27 appCache = AppCache.getInstance();
28 appCache.initCache(cacheKey);
34 * Answers true if input text is an integer
39 private boolean isInteger(String text)
43 Integer.parseInt(text);
45 } catch (NumberFormatException e)
52 * Method called to update the cache with the last user input
54 public void updateCache()
56 SwingUtilities.invokeLater(new Runnable()
61 String userInput = getUserInput();
62 if (userInput != null && !userInput.isEmpty())
64 LinkedHashSet<String> foundCache = appCache
65 .getAllCachedItemsFor(cacheKey);
66 // remove old cache item so as to place current input at the top of
68 foundCache.remove(userInput);
69 foundCache.add(userInput);
70 appCache.putCache(cacheKey, foundCache);
73 String lastSearch = userInput;
74 if (getItemCount() > 0)
78 Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
79 List<String> reversedCacheItems = new ArrayList<String>();
80 reversedCacheItems.addAll(cacheItems);
82 Collections.reverse(reversedCacheItems);
83 if (lastSearch.isEmpty())
87 if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
89 for (String cacheItem : reversedCacheItems)
94 setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
100 public String getUserInput()
102 return getEditor().getItem() == null ? "" : getEditor().getItem()