1 package jalview.io.cache;
4 import java.util.Hashtable;
5 import java.util.LinkedHashSet;
8 import javax.swing.JComboBox;
9 import javax.swing.JComponent;
11 public class AppCache implements AppCacheI
13 private static AppCache instance = null;
15 private Hashtable<String, LinkedHashSet<String>> cacheItems;
19 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
23 public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
25 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
26 if (foundCache == null)
28 foundCache = new LinkedHashSet<String>();
29 cacheItems.put(cacheKey, foundCache);
35 public static AppCache getInstance()
39 instance = new AppCache();
45 public void updateCache(Cacheable cacheable)
47 JComboBox<String> cacheComboBox = cacheable.getCacheComboBox();
48 String cacheKey = cacheable.getCacheKey();
49 JComponent nextFocusableComponent = cacheable.getNextFocusableElement();
50 String userInput = cacheComboBox.getEditor().getItem() == null ? ""
51 : cacheComboBox.getEditor().getItem().toString().trim();
53 if (userInput != null && !userInput.isEmpty())
55 LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheKey);
56 foundCache.add(userInput);
57 cacheItems.put(cacheKey, foundCache);
60 String lastSearch = userInput;
61 nextFocusableComponent.requestFocusInWindow();
62 if (cacheComboBox.getItemCount() > 0)
64 cacheComboBox.removeAllItems();
67 Set<String> cacheItems = getAllCachedItemsFor(cacheKey);
68 if (cacheItems != null && !cacheItems.isEmpty())
70 for (String cacheItem : cacheItems)
72 cacheComboBox.addItem(cacheItem);
76 if (!lastSearch.isEmpty())
78 cacheComboBox.setSelectedItem(lastSearch);
79 cacheComboBox.requestFocus();
84 cacheComboBox.addItem("");
85 cacheComboBox.setSelectedItem("");