X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fio%2Fcache%2FJvCacheableInputBox.java;h=beef3e77a2df3a8b7e5d6e96ad348290ba68f0a1;hb=739d51a0ee7f834923715bab90fa13b2263749e9;hp=3eb193e1bba4cba0770aacb35b20a8a39b9886c9;hpb=53e3890428beabca3cdb44b90ad8e1c294d58768;p=jalview.git
diff --git a/src/jalview/io/cache/JvCacheableInputBox.java b/src/jalview/io/cache/JvCacheableInputBox.java
index 3eb193e..beef3e7 100644
--- a/src/jalview/io/cache/JvCacheableInputBox.java
+++ b/src/jalview/io/cache/JvCacheableInputBox.java
@@ -1,103 +1,156 @@
+/*
+ * 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 .
+ * 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;
import java.util.Set;
import javax.swing.JComboBox;
-import javax.swing.JLabel;
import javax.swing.JMenuItem;
-import javax.swing.JPanel;
import javax.swing.JPopupMenu;
-import javax.swing.JTextField;
import javax.swing.SwingUtilities;
-import javax.swing.text.AttributeSet;
-import javax.swing.text.BadLocationException;
-import javax.swing.text.PlainDocument;
public class JvCacheableInputBox extends JComboBox
{
private static final long serialVersionUID = 5774610435079326695L;
+ private static final int LEFT_BOARDER_WIDTH = 16;
+
private String cacheKey;
private AppCache appCache;
- JPanel pnlDefaultCache = new JPanel();
-
- JLabel lblDefaultCacheSize = new JLabel();
-
- 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;
- final static String SPACE = " ";
+ /**
+ * @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();
}
-
/**
- * Initialise this cache's pop-up menu
+ * Method for initialising cache items for a given cache key and populating the
+ * in-memory cache with persisted cache items
+ *
+ * @param cacheKey
*/
- private void initCachePopupMenu()
+ private void initCache(String cacheKey)
{
- add(popup);
- setComponentPopupMenu(popup);
- pnlDefaultCache.setBackground(Color.WHITE);
- txtDefaultCacheSize.setPreferredSize(new Dimension(45, 20));
- lblDefaultCacheSize.setText(SPACE
- + MessageManager.getString("label.default_cache_size"));
- // Force input to accept only Integer entries up to length - INPUT_LIMIT
- txtDefaultCacheSize.setDocument(new PlainDocument()
+ // obtain persisted cache items from properties file as a delimited string
+ String delimitedCacheStr = Cache.getProperty(cacheKey);
+ if (delimitedCacheStr == null || delimitedCacheStr.isEmpty())
{
- private static final long serialVersionUID = 1L;
+ return;
+ }
+ // convert delimited cache items to a list of strings
+ List persistedCacheItems = Arrays
+ .asList(delimitedCacheStr.split(AppCache.CACHE_DELIMITER));
- @Override
- public void insertString(int offs, String str, AttributeSet a)
- throws BadLocationException
- {
- if (getLength() + str.length() <= INPUT_LIMIT && isInteger(str))
- {
- super.insertString(offs, str, a);
- }
- }
- });
- txtDefaultCacheSize.setText(appCache.getCacheLmit(cacheKey));
- pnlDefaultCache.add(lblDefaultCacheSize);
- pnlDefaultCache.add(txtDefaultCacheSize);
- popup.insert(pnlDefaultCache, 0);
+ 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);
+ }
- menuItemClearCache.setText(MessageManager
- .getString("action.clear_cached_items"));
+ /**
+ * Initialise this cache's pop-up menu
+ */
+ private void initCachePopupMenu()
+ {
+ menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12));
+ menuItemClearCache
+ .setText(MessageManager.getString("action.clear_cached_items"));
menuItemClearCache.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
- System.out.println(">>>>> Clear cache items");
+ // System.out.println(">>>>> Clear cache items");
setSelectedItem("");
appCache.deleteCacheItems(cacheKey);
updateCache();
@@ -105,6 +158,8 @@ public class JvCacheableInputBox extends JComboBox
});
popup.add(menuItemClearCache);
+ setComponentPopupMenu(popup);
+ add(popup);
}
/**
@@ -113,7 +168,7 @@ public class JvCacheableInputBox extends JComboBox
* @param text
* @return
*/
- private boolean isInteger(String text)
+ static boolean isInteger(String text)
{
try
{
@@ -135,8 +190,7 @@ public class JvCacheableInputBox extends JComboBox
@Override
public void run()
{
- int cacheLimit = appCache.updateCacheLimit(cacheKey,
- txtDefaultCacheSize.getText());
+ int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey));
String userInput = getUserInput();
if (userInput != null && !userInput.isEmpty())
{
@@ -155,7 +209,7 @@ public class JvCacheableInputBox extends JComboBox
removeAllItems();
}
Set cacheItems = appCache.getAllCachedItemsFor(cacheKey);
- List reversedCacheItems = new ArrayList();
+ List reversedCacheItems = new ArrayList<>();
reversedCacheItems.addAll(cacheItems);
cacheItems = null;
Collections.reverse(reversedCacheItems);
@@ -197,7 +251,6 @@ public class JvCacheableInputBox extends JComboBox
});
}
-
/**
* This method should be called to persist the in-memory cache when this
* components parent frame is closed / exited
@@ -205,7 +258,6 @@ public class JvCacheableInputBox extends JComboBox
public void persistCache()
{
appCache.persistCache(cacheKey);
- appCache.updateCacheLimit(cacheKey, txtDefaultCacheSize.getText());
}
/**
@@ -215,8 +267,8 @@ public class JvCacheableInputBox extends JComboBox
*/
public String getUserInput()
{
- return getEditor().getItem() == null ? "" : getEditor().getItem()
- .toString().trim();
+ return getEditor().getItem() == null ? ""
+ : getEditor().getItem().toString().trim();
}
}