From 7b10124f09af29607ea0150726ad5d63da09fdaf Mon Sep 17 00:00:00 2001 From: tcofoegbu Date: Mon, 3 Apr 2017 16:21:57 +0100 Subject: [PATCH] JAL-1648 Added improvement to persist cache items to properties file beyound a Jalview session --- src/jalview/fts/core/GFTSPanel.java | 12 +++-- src/jalview/fts/service/pdb/PDBFTSPanel.java | 6 +-- .../fts/service/uniprot/UniprotFTSPanel.java | 6 +-- src/jalview/gui/Finder.java | 19 +++++++- src/jalview/io/cache/AppCache.java | 49 ++++++++++++++++++-- src/jalview/io/cache/AppCacheI.java | 6 ++- src/jalview/io/cache/Cacheable.java | 9 ++-- src/jalview/jbgui/GFinder.java | 25 +++++----- 8 files changed, 100 insertions(+), 32 deletions(-) diff --git a/src/jalview/fts/core/GFTSPanel.java b/src/jalview/fts/core/GFTSPanel.java index 5425f5d..1c20d1d 100644 --- a/src/jalview/fts/core/GFTSPanel.java +++ b/src/jalview/fts/core/GFTSPanel.java @@ -656,6 +656,7 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI, Cacheable getTempUserPrefs().put("FTSPanel.x", mainFrame.getX()); getTempUserPrefs().put("FTSPanel.y", mainFrame.getY()); mainFrame.dispose(); + AppCache.getInstance().persistCache(this); } public class DeferredTextInputListener implements DocumentListener @@ -993,19 +994,20 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI, Cacheable mainFrame.setTitle(getFTSFrameTitle()); } - - protected void fireCache() + @Override + public void initCache() { - AppCache.getInstance().updateCache(this); + AppCache.getInstance().initCache(this); } @Override - public void init() + public void updateCache() { - // reset(); + AppCache.getInstance().updateCache(this); } + @Override public String getCacheKey() { diff --git a/src/jalview/fts/service/pdb/PDBFTSPanel.java b/src/jalview/fts/service/pdb/PDBFTSPanel.java index 5a747be..23ff8f3 100644 --- a/src/jalview/fts/service/pdb/PDBFTSPanel.java +++ b/src/jalview/fts/service/pdb/PDBFTSPanel.java @@ -42,7 +42,7 @@ public class PDBFTSPanel extends GFTSPanel private static Map tempUserPrefs = new HashMap(); - private static final String PDB_FTS_CACHE_KEY = "PDB_FTS_CACHE_KEY"; + private static final String PDB_FTS_CACHE_KEY = "CACHE.PDB_FTS"; public PDBFTSPanel(SequenceFetcher seqFetcher) { @@ -51,7 +51,7 @@ public class PDBFTSPanel extends GFTSPanel this.seqFetcher = seqFetcher; this.progressIndicator = (seqFetcher == null) ? null : seqFetcher .getProgressIndicator(); - fireCache(); + initCache(); } @Override @@ -144,7 +144,7 @@ public class PDBFTSPanel extends GFTSPanel refreshPaginatorState(); updateSummaryTableSelections(); } - fireCache(); + updateCache(); } }.start(); } diff --git a/src/jalview/fts/service/uniprot/UniprotFTSPanel.java b/src/jalview/fts/service/uniprot/UniprotFTSPanel.java index eba0f88..88442de 100644 --- a/src/jalview/fts/service/uniprot/UniprotFTSPanel.java +++ b/src/jalview/fts/service/uniprot/UniprotFTSPanel.java @@ -43,7 +43,7 @@ public class UniprotFTSPanel extends GFTSPanel private static Map tempUserPrefs = new HashMap(); - private static final String UNIPROT_FTS_CACHE_KEY = "UNIPROT_FTS_CACHE_KEY"; + private static final String UNIPROT_FTS_CACHE_KEY = "CACHE.UNIPROT_FTS"; public UniprotFTSPanel(SequenceFetcher seqFetcher) { @@ -53,7 +53,7 @@ public class UniprotFTSPanel extends GFTSPanel this.seqFetcher = seqFetcher; this.progressIndicator = (seqFetcher == null) ? null : seqFetcher .getProgressIndicator(); - fireCache(); + initCache(); } @Override @@ -145,7 +145,7 @@ public class UniprotFTSPanel extends GFTSPanel refreshPaginatorState(); updateSummaryTableSelections(); } - fireCache(); + updateCache(); } }.start(); diff --git a/src/jalview/gui/Finder.java b/src/jalview/gui/Finder.java index 63278ac..8eabadb 100755 --- a/src/jalview/gui/Finder.java +++ b/src/jalview/gui/Finder.java @@ -24,6 +24,7 @@ import jalview.datamodel.SearchResultMatchI; import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; +import jalview.io.cache.AppCache; import jalview.jbgui.GFinder; import jalview.util.MessageManager; import jalview.viewmodel.AlignmentViewport; @@ -39,6 +40,7 @@ import javax.swing.JComponent; import javax.swing.JInternalFrame; import javax.swing.JLayeredPane; import javax.swing.KeyStroke; +import javax.swing.event.InternalFrameEvent; /** * Performs the menu option for searching the alignment, for the next or all @@ -76,6 +78,7 @@ public class Finder extends GFinder { this(null, null); focusfixed = false; + initCache(); } /** @@ -93,6 +96,14 @@ public class Finder extends GFinder frame = new JInternalFrame(); frame.setContentPane(this); frame.setLayer(JLayeredPane.PALETTE_LAYER); + frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() + { + @Override + public void internalFrameClosing(InternalFrameEvent e) + { + closeAction(); + } + }); addEscapeHandler(); Desktop.addInternalFrame(frame, MessageManager.getString("label.find"), WIDTH, HEIGHT); @@ -321,7 +332,7 @@ public class Finder extends GFinder seqIndex = 0; } } - + updateCache(); } /** @@ -373,4 +384,10 @@ public class Finder extends GFinder } return error; } + + protected void closeAction() + { + frame.dispose(); + AppCache.getInstance().persistCache(this); + } } diff --git a/src/jalview/io/cache/AppCache.java b/src/jalview/io/cache/AppCache.java index 111a99b..26e1eda 100644 --- a/src/jalview/io/cache/AppCache.java +++ b/src/jalview/io/cache/AppCache.java @@ -1,8 +1,12 @@ package jalview.io.cache; +import jalview.bin.Cache; + +import java.util.Arrays; import java.util.Hashtable; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; public class AppCache implements AppCacheI @@ -11,14 +15,17 @@ public class AppCache implements AppCacheI private Hashtable> cacheItems; + private static final String CACHE_DELIMITER = ";"; + private AppCache() { cacheItems = new Hashtable>(); } @Override - public LinkedHashSet getAllCachedItemsFor(String cacheKey) + public LinkedHashSet getAllCachedItemsFor(Cacheable cacheable) { + String cacheKey = cacheable.getCacheKey(); LinkedHashSet foundCache = cacheItems.get(cacheKey); if (foundCache == null) { @@ -48,7 +55,7 @@ public class AppCache implements AppCacheI if (userInput != null && !userInput.isEmpty()) { - LinkedHashSet foundCache = getAllCachedItemsFor(cacheKey); + LinkedHashSet foundCache = getAllCachedItemsFor(cacheable); foundCache.add(userInput); cacheItems.put(cacheKey, foundCache); } @@ -59,7 +66,7 @@ public class AppCache implements AppCacheI cacheComboBox.removeAllItems(); } - Set cacheItems = getAllCachedItemsFor(cacheKey); + Set cacheItems = getAllCachedItemsFor(cacheable); if (cacheItems != null && !cacheItems.isEmpty()) { for (String cacheItem : cacheItems) @@ -75,10 +82,44 @@ public class AppCache implements AppCacheI } else { - cacheable.init(); + cacheable.initCache(); cacheComboBox.addItem(""); cacheComboBox.setSelectedItem(""); } } + @Override + public void initCache(Cacheable cacheable) + { + String separatedStr = Cache.getProperty(cacheable.getCacheKey()); + if (separatedStr == null || separatedStr.isEmpty()) + { + return; + } + + List persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER)); + String cacheKey = cacheable.getCacheKey(); + + LinkedHashSet foundCacheItems = cacheItems.get(cacheKey); + if (foundCacheItems == null) + { + foundCacheItems = new LinkedHashSet(); + } + + for (String cacheItem : persistedCacheItems) + { + foundCacheItems.add(cacheItem); + } + cacheItems.put(cacheKey, foundCacheItems); + updateCache(cacheable); + } + + @Override + public void persistCache(Cacheable cacheable) + { + String cacheKey = cacheable.getCacheKey(); + LinkedHashSet foundCache = getAllCachedItemsFor(cacheable); + String commaJoinedStr = String.join(CACHE_DELIMITER, foundCache); + Cache.setProperty(cacheKey, commaJoinedStr); + } } diff --git a/src/jalview/io/cache/AppCacheI.java b/src/jalview/io/cache/AppCacheI.java index 51d51c2..8d9e9f9 100644 --- a/src/jalview/io/cache/AppCacheI.java +++ b/src/jalview/io/cache/AppCacheI.java @@ -5,7 +5,11 @@ import java.util.Set; public interface AppCacheI { - public Set getAllCachedItemsFor(String cacheKey); + public void initCache(Cacheable cacheable); public void updateCache(Cacheable cacheable); + + public void persistCache(Cacheable cacheable); + + public Set getAllCachedItemsFor(Cacheable cacheable); } diff --git a/src/jalview/io/cache/Cacheable.java b/src/jalview/io/cache/Cacheable.java index 9aa0a0a..0287457 100644 --- a/src/jalview/io/cache/Cacheable.java +++ b/src/jalview/io/cache/Cacheable.java @@ -7,7 +7,12 @@ public interface Cacheable /** * method executed on cache initialisation */ - public void init(); + public void initCache(); + + /** + * method executed on cache update + */ + public void updateCache(); /** * Combo-box instance for the cache component @@ -23,6 +28,4 @@ public interface Cacheable * @return */ public String getCacheKey(); - - } diff --git a/src/jalview/jbgui/GFinder.java b/src/jalview/jbgui/GFinder.java index e5d760c..f676a49 100755 --- a/src/jalview/jbgui/GFinder.java +++ b/src/jalview/jbgui/GFinder.java @@ -82,7 +82,7 @@ public class GFinder extends JPanel implements Cacheable GridLayout optionsGridLayout = new GridLayout(); - private static final String FINDER_CACHE_KEY = "FINDER_CACHE_KEY"; + private static final String FINDER_CACHE_KEY = "CACHE.FINDER"; public GFinder() { @@ -190,7 +190,6 @@ public class GFinder extends JPanel implements Cacheable optionsPanel.add(searchDescription, null); jPanel4.add(optionsPanel, java.awt.BorderLayout.WEST); - fireCache(); } protected void findNext_actionPerformed(ActionEvent e) @@ -207,7 +206,6 @@ public class GFinder extends JPanel implements Cacheable { e.consume(); findNext_actionPerformed(null); - fireCache(); } } @@ -247,16 +245,7 @@ public class GFinder extends JPanel implements Cacheable } - protected void fireCache() - { - AppCache.getInstance().updateCache(this); - } - @Override - public void init() - { - // TODO Auto-generated method stub - } @Override public CacheBoxI getCacheComboBox() @@ -315,4 +304,16 @@ public class GFinder extends JPanel implements Cacheable return FINDER_CACHE_KEY; } + @Override + public void initCache() + { + AppCache.getInstance().initCache(this); + } + + @Override + public void updateCache() + { + AppCache.getInstance().updateCache(this); + } + } -- 1.7.10.2