*/
package jalview.io.cache;
-import jalview.bin.Cache;
-import jalview.util.MessageManager;
-
-import java.awt.Color;
import java.awt.Dimension;
+import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.awt.event.FocusListener;
+import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
-import javax.swing.BorderFactory;
import javax.swing.JComboBox;
-import javax.swing.JLabel;
+import javax.swing.JComponent;
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<E> extends JComboBox<String>
-{
-
- private static final long serialVersionUID = 5774610435079326695L;
+import javax.swing.event.CaretListener;
+import javax.swing.event.DocumentListener;
+import javax.swing.text.JTextComponent;
- private static final int INPUT_LIMIT = 2;
-
- private static final int LEFT_BOARDER_WIDTH = 16;
+import jalview.bin.Cache;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
- private String cacheKey;
+/**
+ * A class that provides an editable combobox with a memory of previous entries
+ * that may be persisted
+ *
+ * @author tcofoegbu
+ *
+ * @param <E>
+ */
+/*
+ * (temporary?) patches to wrap a JTextField instead when running as Javascript
+ */
+public class JvCacheableInputBox<E>
+{
+ protected JComboBox<String> comboBox; // used for Jalview
- private AppCache appCache;
+ protected JTextField textField; // used for JalviewJS
- private JPanel pnlDefaultCache = new JPanel();
+ protected JTextComponent textComponent; // used for both
- private JLabel lblDefaultCacheSize = new JLabel();
+ protected String cacheKey;
- private JTextField txtDefaultCacheSize = new JTextField();
+ 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
*/
return enterWasPressed;
}
- public JvCacheableInputBox(String newCacheKey)
+ /**
+ * Constructor given the key to cached values, and the (approximate) length in
+ * characters of the input field
+ *
+ * @param newCacheKey
+ * @param length
+ */
+ public JvCacheableInputBox(String newCacheKey, int length)
{
- super();
- this.cacheKey = newCacheKey;
- setEditable(true);
- addKeyListener(new KeyListener()
+ // super();
+ cacheKey = newCacheKey;
+ prototypeDisplayValue = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
+ if (length > 0)
{
-
- @Override
- public void keyTyped(KeyEvent e)
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++)
{
- enterWasPressed = false;
- if (e.getKeyCode() == KeyEvent.VK_ENTER)
- {
- enterWasPressed = true;
- }
- // let event bubble up
+ sb.append("X");
}
-
- @Override
- public void keyReleased(KeyEvent e)
- {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void keyPressed(KeyEvent e)
+ setPrototypeDisplayValue(sb.toString());
+ }
+ boolean useTextField = Platform.isJS();
+ // BH 2019.03 only switch for JavaScript here
+ // SwingJS TODO implement editable combo box
+ if (useTextField)
+ {
+ appCache = null;
+ textComponent = textField = new JTextField();
+ FontMetrics fm = textField.getFontMetrics(textField.getFont());
+ textField.setPreferredSize(new Dimension(
+ fm.stringWidth(prototypeDisplayValue), fm.getHeight() + 4));
+ // {
+ // @Override
+ // public Dimension getPreferredSize() {
+ // return super.getPreferredSize();
+ //// FontMetrics fm = getFontMetrics(getFont());
+ //// return new Dimension(fm.stringWidth(prototypeDisplayValue),
+ // fm.getHeight());
+ // }
+ // };
+ }
+ else
+ {
+ appCache = AppCache.getInstance();
+ comboBox = new JComboBox<>();
+ textComponent = (JTextComponent) comboBox.getEditor()
+ .getEditorComponent();
+ comboBox.setEditable(true);
+ comboBox.addKeyListener(new KeyAdapter()
{
- // TODO Auto-generated method stub
-
- }
- });
- setPrototypeDisplayValue(
- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
- appCache = AppCache.getInstance();
- initCachePopupMenu();
- initCache(newCacheKey);
- updateCache();
+ @Override
+ public void keyTyped(KeyEvent e)
+ {
+ enterWasPressed = false;
+ if (e.getKeyCode() == KeyEvent.VK_ENTER)
+ {
+ enterWasPressed = true;
+ }
+ // let event bubble up
+ }
+ });
+ comboBox.setPrototypeDisplayValue(prototypeDisplayValue);
+ initCachePopupMenu();
+ initCache(newCacheKey);
+ updateCache();
+ }
}
/**
*/
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())
.getAllCachedItemsFor(cacheKey);
if (foundCacheItems == null)
{
- foundCacheItems = new LinkedHashSet<String>();
+ foundCacheItems = new LinkedHashSet<>();
}
// populate memory cache
for (String cacheItem : persistedCacheItems)
*/
private void initCachePopupMenu()
{
- pnlDefaultCache.setBackground(Color.WHITE);
- // 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()
+ if (appCache == null)
{
- 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.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);
+ return;
+ }
menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12));
- pnlDefaultCache.add(txtDefaultCacheSize);
menuItemClearCache
.setText(MessageManager.getString("action.clear_cached_items"));
menuItemClearCache.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
{
- // System.out.println(">>>>> Clear cache items");
+ // jalview.bin.Console.outPrintln(">>>>> Clear cache items");
setSelectedItem("");
appCache.deleteCacheItems(cacheKey);
updateCache();
}
});
- popup.insert(pnlDefaultCache, 0);
popup.add(menuItemClearCache);
- setComponentPopupMenu(popup);
- add(popup);
- }
-
- private void closePopup()
- {
- popup.setVisible(false);
- popup.transferFocus();
+ comboBox.setComponentPopupMenu(popup);
+ comboBox.add(popup);
}
/**
*/
public void updateCache()
{
+ if (appCache == null)
+ {
+ return;
+ }
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
- int userLimit = txtDefaultCacheSize.getText().trim().isEmpty()
- ? Integer.valueOf(AppCache.DEFAULT_LIMIT)
- : Integer.valueOf(txtDefaultCacheSize.getText());
- int cacheLimit = appCache.updateCacheLimit(cacheKey, userLimit);
+ int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey));
String userInput = getUserInput();
if (userInput != null && !userInput.isEmpty())
{
}
String lastSearch = userInput;
- if (getItemCount() > 0)
+ if (comboBox.getItemCount() > 0)
{
- removeAllItems();
+ comboBox.removeAllItems();
}
Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
- List<String> reversedCacheItems = new ArrayList<String>();
+ List<String> reversedCacheItems = new ArrayList<>();
reversedCacheItems.addAll(cacheItems);
cacheItems = null;
Collections.reverse(reversedCacheItems);
if (lastSearch.isEmpty())
{
- addItem("");
+ comboBox.addItem("");
}
if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
}
else
{
- addItem(cacheItem);
+ comboBox.addItem(cacheItem);
}
}
else
{
- addItem(cacheItem);
+ comboBox.addItem(cacheItem);
}
}
appCache.putCache(cacheKey, foundCache);
*/
public void persistCache()
{
+ if (appCache == null)
+ {
+ return;
+ }
appCache.persistCache(cacheKey);
- int userLimit = txtDefaultCacheSize.getText().trim().isEmpty()
- ? Integer.valueOf(AppCache.DEFAULT_LIMIT)
- : Integer.valueOf(txtDefaultCacheSize.getText());
- appCache.updateCacheLimit(cacheKey, userLimit);
}
/**
- * Method to obtain input text from the cache box
+ * Returns the trimmed text in the input field
*
* @return
*/
public String getUserInput()
{
- return getEditor().getItem() == null ? ""
- : getEditor().getItem().toString().trim();
+ if (comboBox == null)
+ {
+ return textField.getText().trim();
+ }
+ Object item = comboBox.getEditor().getItem();
+ return item == null ? "" : item.toString().trim();
+ }
+
+ public JComponent getComponent()
+ {
+ return (comboBox == null ? textField : comboBox);
+ }
+
+ public void addActionListener(ActionListener actionListener)
+ {
+ if (comboBox == null)
+ {
+ textField.addActionListener(actionListener);
+ }
+ else
+ {
+ comboBox.addActionListener(actionListener);
+ }
+ }
+
+ public void addDocumentListener(DocumentListener listener)
+ {
+ textComponent.getDocument().addDocumentListener(listener);
+ }
+
+ public void addFocusListener(FocusListener focusListener)
+ {
+ textComponent.addFocusListener(focusListener);
+ }
+
+ public void addKeyListener(KeyListener kl)
+ {
+ textComponent.addKeyListener(kl);
+ }
+
+ public void addCaretListener(CaretListener caretListener)
+ {
+ textComponent.addCaretListener(caretListener);
+ }
+
+ public void setEditable(boolean b)
+ {
+ if (comboBox != null)
+ {
+ comboBox.setEditable(b);
+ }
+ }
+
+ public void setPrototypeDisplayValue(String string)
+ {
+ prototypeDisplayValue = string;
+ if (comboBox != null)
+ {
+ comboBox.setPrototypeDisplayValue(string);
+ }
+ }
+
+ public void setSelectedItem(String userInput)
+ {
+ if (comboBox != null)
+ {
+ comboBox.setSelectedItem(userInput);
+ }
+ }
+
+ public boolean isPopupVisible()
+ {
+ return (comboBox != null && comboBox.isPopupVisible());
+ }
+
+ public void addItem(String item)
+ {
+ if (comboBox != null)
+ {
+ comboBox.addItem(item);
+ }
}
}