Merge branch 'develop' into feature/JAL-3390hideUnmappedStructure
[jalview.git] / src / jalview / io / cache / JvCacheableInputBox.java
index beef3e7..9ec3a9f 100644 (file)
@@ -22,9 +22,12 @@ package jalview.io.cache;
 
 import jalview.bin.Cache;
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 
 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,20 +38,37 @@ 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>
 {
+  protected JComboBox<String> comboBox; // used for Jalview
 
-  private static final long serialVersionUID = 5774610435079326695L;
+  protected JTextField textField; // used for JalviewJS
 
-  private static final int LEFT_BOARDER_WIDTH = 16;
+  protected JTextComponent textComponent; // used for both
 
-  private String cacheKey;
+  protected String cacheKey;
 
-  private AppCache appCache;
+  protected AppCache appCache;
 
   private JPopupMenu popup = new JPopupMenu();
 
@@ -56,6 +76,8 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
 
   volatile boolean enterWasPressed = false;
 
+  private String prototypeDisplayValue;
+
   /**
    * @return flag indicating if the most recent keypress was enter
    */
@@ -64,55 +86,72 @@ 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()
+    // super();
+    cacheKey = newCacheKey;
+    prototypeDisplayValue = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
+    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
-      }
-
-      @Override
-      public void keyReleased(KeyEvent e)
-      {
-        // TODO Auto-generated method stub
-
-      }
-
-      @Override
-      public void keyPressed(KeyEvent e)
-      {
-        // TODO Auto-generated method stub
-
-      }
-    });
-    setPrototypeDisplayValue(
-            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
-    appCache = AppCache.getInstance();
+      });
+      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())
@@ -142,6 +181,10 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
    */
   private void initCachePopupMenu()
   {
+    if (appCache == null)
+    {
+      return;
+    }
     menuItemClearCache.setFont(new java.awt.Font("Verdana", 0, 12));
     menuItemClearCache
             .setText(MessageManager.getString("action.clear_cached_items"));
@@ -158,8 +201,8 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
     });
 
     popup.add(menuItemClearCache);
-    setComponentPopupMenu(popup);
-    add(popup);
+    comboBox.setComponentPopupMenu(popup);
+    comboBox.add(popup);
   }
 
   /**
@@ -185,6 +228,10 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
    */
   public void updateCache()
   {
+    if (appCache == null)
+    {
+      return;
+    }
     SwingUtilities.invokeLater(new Runnable()
     {
       @Override
@@ -204,9 +251,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 +262,7 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
         Collections.reverse(reversedCacheItems);
         if (lastSearch.isEmpty())
         {
-          addItem("");
+          comboBox.addItem("");
         }
 
         if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
@@ -236,12 +283,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 +304,101 @@ public class JvCacheableInputBox<E> extends JComboBox<String>
    */
   public void persistCache()
   {
+    if (appCache == null)
+    {
+      return;
+    }
     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 (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);
+    }
   }
 
 }