Merge branch 'releases/Release_2_10_3_Branch'
[jalview.git] / src / jalview / io / cache / JvCacheableInputBox.java
index 2cf1678..a837512 100644 (file)
@@ -1,12 +1,36 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 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.awt.event.KeyListener;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -29,35 +53,106 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
 
   private static final long serialVersionUID = 5774610435079326695L;
 
+  private static final int INPUT_LIMIT = 2;
+
+  private static final int LEFT_BOARDER_WIDTH = 16;
+
   private String cacheKey;
 
   private AppCache appCache;
 
-  JPanel pnlDefaultCache = new JPanel();
+  private JPanel pnlDefaultCache = new JPanel();
 
-  JLabel lblDefaultCacheSize = new JLabel();
+  private JLabel lblDefaultCacheSize = new JLabel();
 
-  JTextField txtDefaultCacheSize = new JTextField();
+  private JTextField txtDefaultCacheSize = new JTextField();
 
-  JPopupMenu popup = new JPopupMenu();
+  private JPopupMenu popup = new JPopupMenu();
 
-  JMenuItem menuItemClearCache = new JMenuItem();
+  private JMenuItem menuItemClearCache = new JMenuItem();
 
-  final static int INPUT_LIMIT = 2;
+  volatile boolean enterWasPressed = false;
 
+  /**
+   * @return flag indicating if the most recent keypress was enter
+   */
+  public boolean wasEnterPressed()
+  {
+    return enterWasPressed;
+  }
 
-  public JvCacheableInputBox(String cacheKey)
+  public JvCacheableInputBox(String newCacheKey)
   {
     super();
-    this.cacheKey = cacheKey;
+    this.cacheKey = newCacheKey;
     setEditable(true);
-    setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
+    addKeyListener(new KeyListener()
+    {
+
+      @Override
+      public void keyTyped(KeyEvent e)
+      {
+        enterWasPressed = false;
+        if (e.getKeyCode() == KeyEvent.VK_ENTER)
+        {
+          enterWasPressed = true;
+        }
+        // let event bubble up
+      }
+
+      @Override
+      public void keyReleased(KeyEvent e)
+      {
+        // TODO Auto-generated method stub
+
+      }
+
+      @Override
+      public void keyPressed(KeyEvent e)
+      {
+        // TODO Auto-generated method stub
+
+      }
+    });
+    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<String> persistedCacheItems = Arrays
+            .asList(delimitedCacheStr.split(AppCache.CACHE_DELIMITER));
+
+    LinkedHashSet<String> foundCacheItems = appCache
+            .getAllCachedItemsFor(cacheKey);
+    if (foundCacheItems == null)
+    {
+      foundCacheItems = new LinkedHashSet<String>();
+    }
+    // populate memory cache
+    for (String cacheItem : persistedCacheItems)
+    {
+      foundCacheItems.add(cacheItem);
+    }
+    appCache.putCache(cacheKey, foundCacheItems);
+  }
 
   /**
    * Initialise this cache's pop-up menu
@@ -66,10 +161,13 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
   {
     pnlDefaultCache.setBackground(Color.WHITE);
     // pad panel so as to align with other menu items
-    pnlDefaultCache.setBorder(BorderFactory.createEmptyBorder(0, 19, 0, 0));
+    pnlDefaultCache.setBorder(
+            BorderFactory.createEmptyBorder(0, LEFT_BOARDER_WIDTH, 0, 0));
     txtDefaultCacheSize.setPreferredSize(new Dimension(45, 20));
-    lblDefaultCacheSize.setText(MessageManager
-            .getString("label.default_cache_size"));
+    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()
     {
@@ -85,11 +183,26 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
         }
       }
     });
-    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"));
+    menuItemClearCache
+            .setText(MessageManager.getString("action.clear_cached_items"));
     menuItemClearCache.addActionListener(new ActionListener()
     {
       @Override
@@ -108,6 +221,12 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
     add(popup);
   }
 
+  private void closePopup()
+  {
+    popup.setVisible(false);
+    popup.transferFocus();
+  }
+
   /**
    * Answers true if input text is an integer
    * 
@@ -136,8 +255,10 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
       @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())
         {
@@ -198,7 +319,6 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
     });
   }
 
-
   /**
    * This method should be called to persist the in-memory cache when this
    * components parent frame is closed / exited
@@ -206,7 +326,10 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
   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);
   }
 
   /**
@@ -216,8 +339,8 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
    */
   public String getUserInput()
   {
-    return getEditor().getItem() == null ? "" : getEditor().getItem()
-            .toString().trim();
+    return getEditor().getItem() == null ? ""
+            : getEditor().getItem().toString().trim();
   }
 
 }