JAL-1648 made further abstraction and createda new interface CacheBox in order to...
[jalview.git] / src / jalview / io / cache / AppCache.java
1 package jalview.io.cache;
2
3
4 import java.util.Hashtable;
5 import java.util.LinkedHashSet;
6 import java.util.Set;
7
8 public class AppCache implements AppCacheI
9 {
10   private static AppCache instance = null;
11
12   private Hashtable<String, LinkedHashSet<String>> cacheItems;
13
14   private AppCache()
15   {
16     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
17   }
18
19   @Override
20   public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
21   {
22     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
23     if (foundCache == null)
24     {
25       foundCache = new LinkedHashSet<String>();
26       cacheItems.put(cacheKey, foundCache);
27     }
28     return foundCache;
29   }
30
31
32   public static AppCache getInstance()
33   {
34     if (instance == null)
35     {
36       instance = new AppCache();
37     }
38     return instance;
39   }
40
41   @Override
42   public void updateCache(Cacheable cacheable)
43   {
44     CacheBoxI<String> cacheComboBox = cacheable.getCacheComboBox();
45     String cacheKey = cacheable.getCacheKey();
46     cacheComboBox.looseFocus();
47     String userInput = cacheComboBox.getUserInput();
48
49     if (userInput != null && !userInput.isEmpty())
50     {
51       LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheKey);
52       foundCache.add(userInput);
53       cacheItems.put(cacheKey, foundCache);
54     }
55
56     String lastSearch = userInput;
57     if (cacheComboBox.getItemCount() > 0)
58     {
59       cacheComboBox.removeAllItems();
60     }
61
62     Set<String> cacheItems = getAllCachedItemsFor(cacheKey);
63     if (cacheItems != null && !cacheItems.isEmpty())
64     {
65       for (String cacheItem : cacheItems)
66       {
67         cacheComboBox.addItem(cacheItem);
68       }
69     }
70
71     if (!lastSearch.isEmpty())
72     {
73       cacheComboBox.setSelectedItem(lastSearch);
74       cacheComboBox.requestFocus();
75     }
76     else
77     {
78       cacheable.init();
79       cacheComboBox.addItem("");
80       cacheComboBox.setSelectedItem("");
81     }
82   }
83
84 }