package jalview.io.cache;
import jalview.bin.Cache;
-import jalview.bin.Jalview;
import jalview.util.MessageManager;
+import jalview.util.Platform;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
*/
public class JvCacheableInputBox<E>
{
- private JComboBox<String> comboBox; // used for Jalview
+ protected JComboBox<String> comboBox; // used for Jalview
- private JTextField textField; // used for JalviewJS
+ protected JTextField textField; // used for JalviewJS
- private static final long serialVersionUID = 5774610435079326695L;
+ protected JTextComponent textComponent; // used for both
- private String cacheKey;
+ protected String cacheKey;
- private AppCache appCache;
+ protected AppCache appCache;
private JPopupMenu popup = new JPopupMenu();
volatile boolean enterWasPressed = false;
+ private String prototypeDisplayValue;
+
/**
* @return flag indicating if the most recent keypress was enter
*/
*/
public JvCacheableInputBox(String newCacheKey)
{
- super();
- if (Jalview.isJS())
+ // super();
+ cacheKey = newCacheKey;
+ prototypeDisplayValue = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
+ boolean useTextField = Platform.isJS();
+ // BH 2019.03 only switch for JavaScript here
+ // SwingJS TODO implement editable combo box
+ if (useTextField)
{
- textField = new JTextField();
- return;
+ appCache = null;
+ textComponent = textField = new JTextField();
+ // {
+ // @Override
+ // public Dimension getPreferredSize() {
+ // return super.getPreferredSize();
+ //// FontMetrics fm = getFontMetrics(getFont());
+ //// return new Dimension(fm.stringWidth(prototypeDisplayValue),
+ // fm.getHeight());
+ // }
+ // };
}
-
- this.cacheKey = newCacheKey;
- comboBox = new JComboBox<String>();
- comboBox.setEditable(true);
- comboBox.addKeyListener(new KeyAdapter()
+ else
{
- @Override
- public void keyTyped(KeyEvent e)
+ appCache = AppCache.getInstance();
+ comboBox = new JComboBox<>();
+ textComponent = (JTextComponent) comboBox.getEditor()
+ .getEditorComponent();
+ comboBox.setEditable(true);
+ comboBox.addKeyListener(new KeyAdapter()
{
- enterWasPressed = false;
- if (e.getKeyCode() == KeyEvent.VK_ENTER)
+ @Override
+ public void keyTyped(KeyEvent e)
{
- enterWasPressed = true;
+ enterWasPressed = false;
+ if (e.getKeyCode() == KeyEvent.VK_ENTER)
+ {
+ enterWasPressed = true;
+ }
+ // let event bubble up
}
- // let event bubble up
- }
- });
- comboBox.setPrototypeDisplayValue(
- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
- appCache = AppCache.getInstance();
+ });
+ comboBox.setPrototypeDisplayValue(prototypeDisplayValue);
+ }
initCachePopupMenu();
initCache(newCacheKey);
updateCache();
}
/**
- * Method for initialising cache items for a given cache key and populating the
- * in-memory cache with persisted cache items
+ * 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)
{
+ if (appCache == null)
+ {
+ return;
+ }
// obtain persisted cache items from properties file as a delimited string
String delimitedCacheStr = Cache.getProperty(cacheKey);
if (delimitedCacheStr == null || delimitedCacheStr.isEmpty())
*/
private void initCachePopupMenu()
{
+ if (appCache == null)
+ {
+ return;
+ }
menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12));
menuItemClearCache
.setText(MessageManager.getString("action.clear_cached_items"));
*/
public void updateCache()
{
- if (Jalview.isJS())
+ if (appCache == null)
{
return;
}
@Override
public void run()
{
- int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey));
- String userInput = getUserInput();
- if (userInput != null && !userInput.isEmpty())
- {
- LinkedHashSet<String> 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);
- }
+ updateCacheNow();
+ }
+ });
+ }
- String lastSearch = userInput;
- if (comboBox.getItemCount() > 0)
- {
- comboBox.removeAllItems();
- }
- Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
- List<String> reversedCacheItems = new ArrayList<>();
- reversedCacheItems.addAll(cacheItems);
- cacheItems = null;
- Collections.reverse(reversedCacheItems);
- if (lastSearch.isEmpty())
- {
- comboBox.addItem("");
- }
+ /**
+ * Updates the cached values to include the last user input as the most
+ * recently added (without duplication), and rebuilds the dropdown list with
+ * this as the first entry. If the maximum configured cache size would be
+ * exceeded, the oldest entry is removed.
+ */
+ void updateCacheNow()
+ {
+ int cacheLimit = Integer.valueOf(appCache.getCacheLimit(cacheKey));
+ String userInput = getUserInput();
+ if (userInput != null && !userInput.isEmpty())
+ {
+ LinkedHashSet<String> 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);
+ }
- if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
+ String lastSearch = userInput;
+ if (comboBox.getItemCount() > 0)
+ {
+ comboBox.removeAllItems();
+ }
+ Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
+ List<String> reversedCacheItems = new ArrayList<>();
+ reversedCacheItems.addAll(cacheItems);
+ cacheItems = null;
+ Collections.reverse(reversedCacheItems);
+ if (lastSearch.isEmpty())
+ {
+ comboBox.addItem("");
+ }
+
+ if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
+ {
+ LinkedHashSet<String> foundCache = appCache
+ .getAllCachedItemsFor(cacheKey);
+ boolean prune = reversedCacheItems.size() > cacheLimit;
+ int count = 1;
+ boolean limitExceeded = false;
+ for (String cacheItem : reversedCacheItems)
+ {
+ limitExceeded = (count++ > cacheLimit);
+ if (prune)
{
- LinkedHashSet<String> foundCache = appCache
- .getAllCachedItemsFor(cacheKey);
- boolean prune = reversedCacheItems.size() > cacheLimit;
- int count = 1;
- boolean limitExceeded = false;
- for (String cacheItem : reversedCacheItems)
+ if (limitExceeded)
+ {
+ foundCache.remove(cacheItem);
+ }
+ else
{
- limitExceeded = (count++ > cacheLimit);
- if (prune)
- {
- if (limitExceeded)
- {
- foundCache.remove(cacheItem);
- }
- else
- {
- comboBox.addItem(cacheItem);
- }
- }
- else
- {
- comboBox.addItem(cacheItem);
- }
+ comboBox.addItem(cacheItem);
}
- appCache.putCache(cacheKey, foundCache);
}
- setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
+ else
+ {
+ comboBox.addItem(cacheItem);
+ }
}
- });
+ appCache.putCache(cacheKey, foundCache);
+ }
+ setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
+
}
/**
*/
public void persistCache()
{
- if (!Jalview.isJS())
+ if (appCache == null)
{
- appCache.persistCache(cacheKey);
+ return;
}
+ appCache.persistCache(cacheKey);
}
/**
*/
public String getUserInput()
{
- if (Jalview.isJS())
+ if (comboBox == null)
{
return textField.getText().trim();
}
public JComponent getComponent()
{
- return Jalview.isJS() ? textField : comboBox;
+ return (comboBox == null ? textField : comboBox);
}
public void addActionListener(ActionListener actionListener)
{
- if (!Jalview.isJS())
+ if (comboBox == null)
+ {
+ textField.addActionListener(actionListener);
+ }
+ else
{
comboBox.addActionListener(actionListener);
}
public void addDocumentListener(DocumentListener listener)
{
- if (!Jalview.isJS())
- {
- ((JTextComponent) comboBox.getEditor().getEditorComponent())
- .getDocument().addDocumentListener(listener);
- }
+ textComponent.getDocument().addDocumentListener(listener);
}
public void addFocusListener(FocusListener focusListener)
{
- if (!Jalview.isJS())
- {
- comboBox.addFocusListener(focusListener);
- }
+ textComponent.addFocusListener(focusListener);
}
public void addKeyListener(KeyListener kl)
{
- if (!Jalview.isJS())
- {
- comboBox.getEditor().getEditorComponent().addKeyListener(kl);
- }
+ textComponent.addKeyListener(kl);
+ }
+
+ public void addCaretListener(CaretListener caretListener)
+ {
+ textComponent.addCaretListener(caretListener);
}
public void setEditable(boolean b)
{
- if (!Jalview.isJS())
+ if (comboBox != null)
{
comboBox.setEditable(b);
}
public void setPrototypeDisplayValue(String string)
{
- if (!Jalview.isJS())
+ prototypeDisplayValue = string;
+ if (comboBox != null)
{
comboBox.setPrototypeDisplayValue(string);
}
public void setSelectedItem(String userInput)
{
- if (!Jalview.isJS())
+ if (comboBox != null)
{
comboBox.setSelectedItem(userInput);
}
public boolean isPopupVisible()
{
- if (!Jalview.isJS())
- {
- return comboBox.isPopupVisible();
- }
- return false;
+ return (comboBox != null && comboBox.isPopupVisible());
}
- public void addCaretListener(CaretListener caretListener)
+ public void addItem(String item)
{
- if (!Jalview.isJS())
+ if (comboBox != null)
{
- ((JTextComponent) comboBox.getEditor().getEditorComponent())
- .addCaretListener(caretListener);
+ comboBox.addItem(item);
}
}