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;
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<E> extends JComboBox<String>
+/**
+ * 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>
{
+ private JComboBox<String> 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;
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<String>();
+ comboBox.setEditable(true);
+ comboBox.addKeyListener(new KeyAdapter()
+ {
@Override
public void keyTyped(KeyEvent e)
{
}
// 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();
});
popup.add(menuItemClearCache);
- setComponentPopupMenu(popup);
- add(popup);
+ comboBox.setComponentPopupMenu(popup);
+ comboBox.add(popup);
}
/**
*/
public void updateCache()
{
+ if (Jalview.isJS())
+ {
+ return;
+ }
SwingUtilities.invokeLater(new Runnable()
{
@Override
}
String lastSearch = userInput;
- if (getItemCount() > 0)
+ if (comboBox.getItemCount() > 0)
{
- removeAllItems();
+ comboBox.removeAllItems();
}
Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
List<String> reversedCacheItems = new ArrayList<>();
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()
{
- 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);
+ }
}
}