package jalview.io.cache; import java.util.Hashtable; import java.util.LinkedHashSet; import java.util.Set; public class AppCache implements AppCacheI { private static AppCache instance = null; private Hashtable> cacheItems; private AppCache() { cacheItems = new Hashtable>(); } @Override public LinkedHashSet getAllCachedItemsFor(String cacheKey) { LinkedHashSet foundCache = cacheItems.get(cacheKey); if (foundCache == null) { foundCache = new LinkedHashSet(); cacheItems.put(cacheKey, foundCache); } return foundCache; } public static AppCache getInstance() { if (instance == null) { instance = new AppCache(); } return instance; } @Override public void updateCache(Cacheable cacheable) { CacheBoxI cacheComboBox = cacheable.getCacheComboBox(); String cacheKey = cacheable.getCacheKey(); cacheComboBox.looseFocus(); String userInput = cacheComboBox.getUserInput(); if (userInput != null && !userInput.isEmpty()) { LinkedHashSet foundCache = getAllCachedItemsFor(cacheKey); foundCache.add(userInput); cacheItems.put(cacheKey, foundCache); } String lastSearch = userInput; if (cacheComboBox.getItemCount() > 0) { cacheComboBox.removeAllItems(); } Set cacheItems = getAllCachedItemsFor(cacheKey); if (cacheItems != null && !cacheItems.isEmpty()) { for (String cacheItem : cacheItems) { cacheComboBox.addItem(cacheItem); } } if (!lastSearch.isEmpty()) { cacheComboBox.setSelectedItem(lastSearch); cacheComboBox.requestFocus(); } else { cacheable.init(); cacheComboBox.addItem(""); cacheComboBox.setSelectedItem(""); } } }