JAL-3038 workaround to replace editable combobox with text field in JS
[jalview.git] / src / jalview / io / cache / JvCacheableInputBox.java
index beef3e7..5e79393 100644 (file)
 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<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;
 
@@ -64,14 +84,25 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
     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)
       {
@@ -82,22 +113,8 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
         }
         // 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<E> extends JComboBox<String>
     });
 
     popup.add(menuItemClearCache);
-    setComponentPopupMenu(popup);
-    add(popup);
+    comboBox.setComponentPopupMenu(popup);
+    comboBox.add(popup);
   }
 
   /**
@@ -185,6 +202,10 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
    */
   public void updateCache()
   {
+    if (Jalview.isJS())
+    {
+      return;
+    }
     SwingUtilities.invokeLater(new Runnable()
     {
       @Override
@@ -204,9 +225,9 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
         }
 
         String lastSearch = userInput;
-        if (getItemCount() > 0)
+        if (comboBox.getItemCount() > 0)
         {
-          removeAllItems();
+          comboBox.removeAllItems();
         }
         Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
         List<String> reversedCacheItems = new ArrayList<>();
@@ -215,7 +236,7 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
         Collections.reverse(reversedCacheItems);
         if (lastSearch.isEmpty())
         {
-          addItem("");
+          comboBox.addItem("");
         }
 
         if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
@@ -236,12 +257,12 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
               }
               else
               {
-                addItem(cacheItem);
+                comboBox.addItem(cacheItem);
               }
             }
             else
             {
-              addItem(cacheItem);
+              comboBox.addItem(cacheItem);
             }
           }
           appCache.putCache(cacheKey, foundCache);
@@ -257,18 +278,105 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
    */
   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);
+    }
   }
 
 }