X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2Fcache%2FJvCacheableInputBox.java;h=5e7939335c85f85059dc5e13070264fa86b8d9ba;hb=4ac42096ed80f98b1fc62459095238a0373667b7;hp=beef3e77a2df3a8b7e5d6e96ad348290ba68f0a1;hpb=3bab14077f339c128bdbebcd49d8e764b25aa2dd;p=jalview.git diff --git a/src/jalview/io/cache/JvCacheableInputBox.java b/src/jalview/io/cache/JvCacheableInputBox.java index beef3e7..5e79393 100644 --- a/src/jalview/io/cache/JvCacheableInputBox.java +++ b/src/jalview/io/cache/JvCacheableInputBox.java @@ -21,10 +21,13 @@ package jalview.io.cache; import jalview.bin.Cache; +import jalview.bin.Jalview; import jalview.util.MessageManager; 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; @@ -35,16 +38,33 @@ import java.util.List; import java.util.Set; import javax.swing.JComboBox; +import javax.swing.JComponent; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; +import javax.swing.JTextField; import javax.swing.SwingUtilities; +import javax.swing.event.CaretListener; +import javax.swing.event.DocumentListener; +import javax.swing.text.JTextComponent; -public class JvCacheableInputBox extends JComboBox +/** + * A class that provides an editable combobox with a memory of previous entries + * that may be persisted + * + * @author tcofoegbu + * + * @param + */ +/* + * (temporary?) patches to wrap a JTextField instead when running as Javascript + */ +public class JvCacheableInputBox { + private JComboBox comboBox; // used for Jalview - private static final long serialVersionUID = 5774610435079326695L; + private JTextField textField; // used for JalviewJS - private static final int LEFT_BOARDER_WIDTH = 16; + private static final long serialVersionUID = 5774610435079326695L; private String cacheKey; @@ -64,14 +84,25 @@ public class JvCacheableInputBox extends JComboBox return enterWasPressed; } + /** + * Constructor + * + * @param newCacheKey + */ public JvCacheableInputBox(String newCacheKey) { super(); - this.cacheKey = newCacheKey; - setEditable(true); - addKeyListener(new KeyListener() + if (Jalview.isJS()) { + textField = new JTextField(); + return; + } + this.cacheKey = newCacheKey; + comboBox = new JComboBox(); + comboBox.setEditable(true); + comboBox.addKeyListener(new KeyAdapter() + { @Override public void keyTyped(KeyEvent e) { @@ -82,22 +113,8 @@ public class JvCacheableInputBox extends JComboBox } // 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( + comboBox.setPrototypeDisplayValue( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); appCache = AppCache.getInstance(); initCachePopupMenu(); @@ -158,8 +175,8 @@ public class JvCacheableInputBox extends JComboBox }); popup.add(menuItemClearCache); - setComponentPopupMenu(popup); - add(popup); + comboBox.setComponentPopupMenu(popup); + comboBox.add(popup); } /** @@ -185,6 +202,10 @@ public class JvCacheableInputBox extends JComboBox */ public void updateCache() { + if (Jalview.isJS()) + { + return; + } SwingUtilities.invokeLater(new Runnable() { @Override @@ -204,9 +225,9 @@ public class JvCacheableInputBox extends JComboBox } String lastSearch = userInput; - if (getItemCount() > 0) + if (comboBox.getItemCount() > 0) { - removeAllItems(); + comboBox.removeAllItems(); } Set cacheItems = appCache.getAllCachedItemsFor(cacheKey); List reversedCacheItems = new ArrayList<>(); @@ -215,7 +236,7 @@ public class JvCacheableInputBox extends JComboBox Collections.reverse(reversedCacheItems); if (lastSearch.isEmpty()) { - addItem(""); + comboBox.addItem(""); } if (reversedCacheItems != null && !reversedCacheItems.isEmpty()) @@ -236,12 +257,12 @@ public class JvCacheableInputBox extends JComboBox } else { - addItem(cacheItem); + comboBox.addItem(cacheItem); } } else { - addItem(cacheItem); + comboBox.addItem(cacheItem); } } appCache.putCache(cacheKey, foundCache); @@ -257,18 +278,105 @@ public class JvCacheableInputBox extends JComboBox */ public void persistCache() { - appCache.persistCache(cacheKey); + if (!Jalview.isJS()) + { + appCache.persistCache(cacheKey); + } } /** - * 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 (Jalview.isJS()) + { + return textField.getText().trim(); + } + Object item = comboBox.getEditor().getItem(); + return item == null ? "" : item.toString().trim(); + } + + public JComponent getComponent() + { + return Jalview.isJS() ? textField : comboBox; + } + + public void addActionListener(ActionListener actionListener) + { + if (!Jalview.isJS()) + { + comboBox.addActionListener(actionListener); + } + } + + public void addDocumentListener(DocumentListener listener) + { + if (!Jalview.isJS()) + { + ((JTextComponent) comboBox.getEditor().getEditorComponent()) + .getDocument().addDocumentListener(listener); + } + } + + public void addFocusListener(FocusListener focusListener) + { + if (!Jalview.isJS()) + { + comboBox.addFocusListener(focusListener); + } + } + + public void addKeyListener(KeyListener kl) + { + if (!Jalview.isJS()) + { + comboBox.getEditor().getEditorComponent().addKeyListener(kl); + } + } + + public void setEditable(boolean b) + { + if (!Jalview.isJS()) + { + comboBox.setEditable(b); + } + } + + public void setPrototypeDisplayValue(String string) + { + if (!Jalview.isJS()) + { + comboBox.setPrototypeDisplayValue(string); + } + } + + public void setSelectedItem(String userInput) + { + if (!Jalview.isJS()) + { + comboBox.setSelectedItem(userInput); + } + } + + 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); + } } }