package jalview.io.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.util.ArrayList; 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 String cacheKey; private AppCache appCache; JPanel pnlDefaultCache = new JPanel(); JLabel lblDefaultCacheSize = new JLabel(); JTextField txtDefaultCacheSize = new JTextField(); JPopupMenu popup = new JPopupMenu(); JMenuItem menuItemClearCache = new JMenuItem(); final static int INPUT_LIMIT = 2; final static String SPACE = " "; public JvCacheableInputBox(String cacheKey) { super(); this.cacheKey = cacheKey; setEditable(true); setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); appCache = AppCache.getInstance(); initCachePopupMenu(); appCache.initCache(cacheKey); updateCache(); } /** * Initialise this cache's pop-up menu */ private void initCachePopupMenu() { 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() { private static final long serialVersionUID = 1L; @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); menuItemClearCache.setText(MessageManager .getString("action.clear_cached_items")); menuItemClearCache.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(">>>>> Clear cache items"); setSelectedItem(""); appCache.deleteCacheItems(cacheKey); updateCache(); } }); popup.add(menuItemClearCache); } /** * Answers true if input text is an integer * * @param text * @return */ private boolean isInteger(String text) { try { Integer.parseInt(text); return true; } catch (NumberFormatException e) { return false; } } /** * Method called to update the cache with the last user input */ public void updateCache() { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { int cacheLimit = appCache.updateCacheLimit(cacheKey, txtDefaultCacheSize.getText()); String userInput = getUserInput(); if (userInput != null && !userInput.isEmpty()) { LinkedHashSet foundCache = appCache .getAllCachedItemsFor(cacheKey); // remove old cache item so as to place current input at the top of // the result foundCache.remove(userInput); foundCache.add(userInput); appCache.putCache(cacheKey, foundCache); } String lastSearch = userInput; if (getItemCount() > 0) { removeAllItems(); } Set cacheItems = appCache.getAllCachedItemsFor(cacheKey); List reversedCacheItems = new ArrayList(); reversedCacheItems.addAll(cacheItems); cacheItems = null; Collections.reverse(reversedCacheItems); if (lastSearch.isEmpty()) { addItem(""); } if (reversedCacheItems != null && !reversedCacheItems.isEmpty()) { LinkedHashSet foundCache = appCache .getAllCachedItemsFor(cacheKey); boolean prune = reversedCacheItems.size() > cacheLimit; int count = 1; boolean limitExceeded = false; for (String cacheItem : reversedCacheItems) { limitExceeded = (count++ > cacheLimit); if (prune) { if (limitExceeded) { foundCache.remove(cacheItem); } else { addItem(cacheItem); } } else { addItem(cacheItem); } } appCache.putCache(cacheKey, foundCache); } setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch); } }); } /** * This method should be called to persist the in-memory cache when this * components parent frame is closed / exited */ public void persistCache() { appCache.persistCache(cacheKey); appCache.updateCacheLimit(cacheKey, txtDefaultCacheSize.getText()); } /** * Method to obtain input text from the cache box * * @return */ public String getUserInput() { return getEditor().getItem() == null ? "" : getEditor().getItem() .toString().trim(); } }