X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2Fcache%2FAppCache.java;fp=src%2Fjalview%2Fio%2Fcache%2FAppCache.java;h=92013415c2bf5ae5e094fcf2a04386942d59a4ca;hb=0ce95d9c8581b19c1d636f67f67dd670e41f1ca9;hp=0000000000000000000000000000000000000000;hpb=b3ff575126349c79f25dcba8526ed8828d668db1;p=jalview.git diff --git a/src/jalview/io/cache/AppCache.java b/src/jalview/io/cache/AppCache.java new file mode 100644 index 0000000..9201341 --- /dev/null +++ b/src/jalview/io/cache/AppCache.java @@ -0,0 +1,89 @@ +package jalview.io.cache; + + +import java.util.Hashtable; +import java.util.LinkedHashSet; +import java.util.Set; + +import javax.swing.JComboBox; +import javax.swing.JComponent; + +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) + { + JComboBox cacheComboBox = cacheable.getCacheComboBox(); + String cacheKey = cacheable.getCacheKey(); + JComponent nextFocusableComponent = cacheable.getNextFocusableElement(); + String userInput = cacheComboBox.getEditor().getItem() == null ? "" + : cacheComboBox.getEditor().getItem().toString().trim(); + + if (userInput != null && !userInput.isEmpty()) + { + LinkedHashSet foundCache = getAllCachedItemsFor(cacheKey); + foundCache.add(userInput); + cacheItems.put(cacheKey, foundCache); + } + + String lastSearch = userInput; + nextFocusableComponent.requestFocusInWindow(); + 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(""); + } + } + +}