X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2Fcache%2FJvCacheableInputBox.java;h=eedf09a47bc75e23255c34b7caf826893ab85295;hb=6703c43876aef57616388139c30b05ef53440b07;hp=d2ad8dec9eae776c12855c950892e7cd58353104;hpb=7c6dc8ce96f205c9330592ac18d5e9e6ef9407e0;p=jalview.git diff --git a/src/jalview/io/cache/JvCacheableInputBox.java b/src/jalview/io/cache/JvCacheableInputBox.java index d2ad8de..eedf09a 100644 --- a/src/jalview/io/cache/JvCacheableInputBox.java +++ b/src/jalview/io/cache/JvCacheableInputBox.java @@ -21,8 +21,8 @@ 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; @@ -60,15 +60,15 @@ import javax.swing.text.JTextComponent; */ public class JvCacheableInputBox { - private JComboBox comboBox; // used for Jalview + protected JComboBox 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(); @@ -76,6 +76,8 @@ public class JvCacheableInputBox volatile boolean enterWasPressed = false; + private String prototypeDisplayValue; + /** * @return flag indicating if the most recent keypress was enter */ @@ -85,51 +87,82 @@ public class JvCacheableInputBox } /** - * Constructor + * 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) + public JvCacheableInputBox(String newCacheKey, int length) { - super(); - if (Jalview.isJS()) + // super(); + cacheKey = newCacheKey; + prototypeDisplayValue = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; + if (length > 0) { - textField = new JTextField(); - return; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) + { + sb.append("X"); + } + setPrototypeDisplayValue(sb.toString()); } - - this.cacheKey = newCacheKey; - comboBox = new JComboBox(); - comboBox.setEditable(true); - comboBox.addKeyListener(new KeyAdapter() + boolean useTextField = Platform.isJS(); + // BH 2019.03 only switch for JavaScript here + // SwingJS TODO implement editable combo box + if (useTextField) { - @Override - public void keyTyped(KeyEvent e) + 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()); + // } + // }; + } + else + { + 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(); - initCachePopupMenu(); - initCache(newCacheKey); - updateCache(); + }); + 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()) @@ -159,6 +192,10 @@ public class JvCacheableInputBox */ private void initCachePopupMenu() { + if (appCache == null) + { + return; + } menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12)); menuItemClearCache .setText(MessageManager.getString("action.clear_cached_items")); @@ -202,7 +239,7 @@ public class JvCacheableInputBox */ public void updateCache() { - if (Jalview.isJS()) + if (appCache == null) { return; } @@ -278,10 +315,11 @@ public class JvCacheableInputBox */ public void persistCache() { - if (!Jalview.isJS()) + if (appCache == null) { - appCache.persistCache(cacheKey); + return; } + appCache.persistCache(cacheKey); } /** @@ -291,7 +329,7 @@ public class JvCacheableInputBox */ public String getUserInput() { - if (Jalview.isJS()) + if (comboBox == null) { return textField.getText().trim(); } @@ -301,12 +339,16 @@ public class JvCacheableInputBox 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); } @@ -314,32 +356,27 @@ public class JvCacheableInputBox 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); } @@ -347,7 +384,8 @@ public class JvCacheableInputBox public void setPrototypeDisplayValue(String string) { - if (!Jalview.isJS()) + prototypeDisplayValue = string; + if (comboBox != null) { comboBox.setPrototypeDisplayValue(string); } @@ -355,7 +393,7 @@ public class JvCacheableInputBox public void setSelectedItem(String userInput) { - if (!Jalview.isJS()) + if (comboBox != null) { comboBox.setSelectedItem(userInput); } @@ -363,25 +401,12 @@ public class JvCacheableInputBox public boolean isPopupVisible() { - if (!Jalview.isJS()) - { - return comboBox.isPopupVisible(); - } - return false; - } - - public void addCaretListener(CaretListener caretListener) - { - if (!Jalview.isJS()) - { - ((JTextComponent) comboBox.getEditor().getEditorComponent()) - .addCaretListener(caretListener); - } + return (comboBox != null && comboBox.isPopupVisible()); } public void addItem(String item) { - if (!Jalview.isJS()) + if (comboBox != null) { comboBox.addItem(item); }