X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fio%2Fcache%2FJvCacheableInputBox.java;h=444670bb9eb428e4f15ff24d308e0472f1f84519;hb=5a9eb84110b75bca3de6489501c17a499de98054;hp=6101bdd5fd8e0c1fe9844a432ade7d2c522503c0;hpb=3b41d15a9ed265cc0dab6b9032aeccfdb507140f;p=jalview.git diff --git a/src/jalview/io/cache/JvCacheableInputBox.java b/src/jalview/io/cache/JvCacheableInputBox.java index 6101bdd..444670b 100644 --- a/src/jalview/io/cache/JvCacheableInputBox.java +++ b/src/jalview/io/cache/JvCacheableInputBox.java @@ -1,12 +1,15 @@ package jalview.io.cache; +import jalview.bin.Cache; import jalview.util.MessageManager; import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; @@ -29,35 +32,67 @@ public class JvCacheableInputBox extends JComboBox private static final long serialVersionUID = 5774610435079326695L; - private String cacheKey; + private static final int INPUT_LIMIT = 2; - private AppCache appCache; + private static final int LEFT_BOARDER_WIDTH = 16; - JPanel pnlDefaultCache = new JPanel(); + private String cacheKey; - JLabel lblDefaultCacheSize = new JLabel(); + private AppCache appCache; - JTextField txtDefaultCacheSize = new JTextField(); + private JPanel pnlDefaultCache = new JPanel(); - JPopupMenu popup = new JPopupMenu(); + private JLabel lblDefaultCacheSize = new JLabel(); - JMenuItem menuItemClearCache = new JMenuItem(); + private JTextField txtDefaultCacheSize = new JTextField(); - final static int INPUT_LIMIT = 2; + private JPopupMenu popup = new JPopupMenu(); + private JMenuItem menuItemClearCache = new JMenuItem(); - public JvCacheableInputBox(String cacheKey) + public JvCacheableInputBox(String newCacheKey) { super(); - this.cacheKey = cacheKey; + this.cacheKey = newCacheKey; setEditable(true); setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); appCache = AppCache.getInstance(); initCachePopupMenu(); - appCache.initCache(cacheKey); + initCache(newCacheKey); updateCache(); } + /** + * Method for initialising cache items for a given cache key and populating + * the in-memory cache with persisted cache items + * + * @param cacheKey + */ + private void initCache(String cacheKey) + { + // obtain persisted cache items from properties file as a delimited string + String delimitedCacheStr = Cache.getProperty(cacheKey); + if (delimitedCacheStr == null || delimitedCacheStr.isEmpty()) + { + return; + } + // convert delimited cache items to a list of strings + List persistedCacheItems = Arrays.asList(delimitedCacheStr + .split(AppCache.CACHE_DELIMITER)); + + LinkedHashSet foundCacheItems = appCache + .getAllCachedItemsFor(cacheKey); + if (foundCacheItems == null) + { + foundCacheItems = new LinkedHashSet(); + } + // populate memory cache + for (String cacheItem : persistedCacheItems) + { + foundCacheItems.add(cacheItem); + } + appCache.putCache(cacheKey, foundCacheItems); + } /** * Initialise this cache's pop-up menu @@ -65,10 +100,14 @@ public class JvCacheableInputBox extends JComboBox private void initCachePopupMenu() { pnlDefaultCache.setBackground(Color.WHITE); - pnlDefaultCache.setBorder(BorderFactory.createEmptyBorder(0, 19, 0, 0)); + // pad panel so as to align with other menu items + pnlDefaultCache.setBorder(BorderFactory.createEmptyBorder(0, + LEFT_BOARDER_WIDTH, 0, 0)); txtDefaultCacheSize.setPreferredSize(new Dimension(45, 20)); + txtDefaultCacheSize.setFont(new java.awt.Font("Verdana", 0, 12)); lblDefaultCacheSize.setText(MessageManager .getString("label.default_cache_size")); + lblDefaultCacheSize.setFont(new java.awt.Font("Verdana", 0, 12)); // Force input to accept only Integer entries up to length - INPUT_LIMIT txtDefaultCacheSize.setDocument(new PlainDocument() { @@ -84,8 +123,23 @@ public class JvCacheableInputBox extends JComboBox } } }); - txtDefaultCacheSize.setText(appCache.getCacheLmit(cacheKey)); + txtDefaultCacheSize.addKeyListener(new java.awt.event.KeyAdapter() + { + @Override + public void keyPressed(KeyEvent e) + { + if (e.getKeyCode() == KeyEvent.VK_ENTER) + { + e.consume(); + updateCache(); + closePopup(); + } + } + }); + + txtDefaultCacheSize.setText(appCache.getCacheLimit(cacheKey)); pnlDefaultCache.add(lblDefaultCacheSize); + menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12)); pnlDefaultCache.add(txtDefaultCacheSize); menuItemClearCache.setText(MessageManager .getString("action.clear_cached_items")); @@ -94,7 +148,7 @@ public class JvCacheableInputBox extends JComboBox @Override public void actionPerformed(ActionEvent e) { - System.out.println(">>>>> Clear cache items"); + // System.out.println(">>>>> Clear cache items"); setSelectedItem(""); appCache.deleteCacheItems(cacheKey); updateCache(); @@ -107,13 +161,19 @@ public class JvCacheableInputBox extends JComboBox add(popup); } + private void closePopup() + { + popup.setVisible(false); + popup.transferFocus(); + } + /** * Answers true if input text is an integer * * @param text * @return */ - private boolean isInteger(String text) + static boolean isInteger(String text) { try { @@ -135,8 +195,10 @@ public class JvCacheableInputBox extends JComboBox @Override public void run() { - int cacheLimit = appCache.updateCacheLimit(cacheKey, - txtDefaultCacheSize.getText()); + int userLimit = txtDefaultCacheSize.getText().trim().isEmpty() ? Integer + .valueOf(AppCache.DEFAULT_LIMIT) : Integer + .valueOf(txtDefaultCacheSize.getText()); + int cacheLimit = appCache.updateCacheLimit(cacheKey, userLimit); String userInput = getUserInput(); if (userInput != null && !userInput.isEmpty()) { @@ -205,7 +267,10 @@ public class JvCacheableInputBox extends JComboBox public void persistCache() { appCache.persistCache(cacheKey); - appCache.updateCacheLimit(cacheKey, txtDefaultCacheSize.getText()); + int userLimit = txtDefaultCacheSize.getText().trim().isEmpty() ? Integer + .valueOf(AppCache.DEFAULT_LIMIT) : Integer + .valueOf(txtDefaultCacheSize.getText()); + appCache.updateCacheLimit(cacheKey, userLimit); } /**