clean-up of isJS business for (temporarily) noneditable JS JComboBox
[jalview.git] / src / jalview / io / cache / JvCacheableInputBox.java
index cf21c62..9ec3a9f 100644 (file)
@@ -60,15 +60,15 @@ import javax.swing.text.JTextComponent;
  */
 public class JvCacheableInputBox<E>
 {
-  private JComboBox<String> comboBox; // used for Jalview
+  protected JComboBox<String> 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<E>
 
   volatile boolean enterWasPressed = false;
 
+  private String prototypeDisplayValue;
+
   /**
    * @return flag indicating if the most recent keypress was enter
    */
@@ -91,45 +93,65 @@ public class JvCacheableInputBox<E>
    */
   public JvCacheableInputBox(String newCacheKey)
   {
-    super();
-    if (Platform.isJS())
+    // 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)
     {
-      textField = new JTextField();
-      return;
+      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());
+      // }
+      // };
     }
-
-    this.cacheKey = newCacheKey;
-    comboBox = new JComboBox<String>();
-    comboBox.setEditable(true);
-    comboBox.addKeyListener(new KeyAdapter()
+    else
     {
-      @Override
-      public void keyTyped(KeyEvent e)
+      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();
+      });
+      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 +181,10 @@ public class JvCacheableInputBox<E>
    */
   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 +228,7 @@ public class JvCacheableInputBox<E>
    */
   public void updateCache()
   {
-    if (Platform.isJS())
+    if (appCache == null)
     {
       return;
     }
@@ -278,10 +304,11 @@ public class JvCacheableInputBox<E>
    */
   public void persistCache()
   {
-    if (!Platform.isJS())
+    if (appCache == null)
     {
-      appCache.persistCache(cacheKey);
+      return;
     }
+    appCache.persistCache(cacheKey);
   }
 
   /**
@@ -291,7 +318,7 @@ public class JvCacheableInputBox<E>
    */
   public String getUserInput()
   {
-    if (Platform.isJS())
+    if (comboBox == null)
     {
       return textField.getText().trim();
     }
@@ -301,12 +328,12 @@ public class JvCacheableInputBox<E>
 
   public JComponent getComponent()
   {
-    return Platform.isJS() ? textField : comboBox;
+    return (comboBox == null ? textField : comboBox);
   }
 
   public void addActionListener(ActionListener actionListener)
   {
-    if (Platform.isJS())
+    if (comboBox == null)
     {
       textField.addActionListener(actionListener);
     }
@@ -318,36 +345,27 @@ public class JvCacheableInputBox<E>
 
   public void addDocumentListener(DocumentListener listener)
   {
-    if (!Platform.isJS())
-    {
-      ((JTextComponent) comboBox.getEditor().getEditorComponent())
-              .getDocument().addDocumentListener(listener);
-    }
+    textComponent.getDocument().addDocumentListener(listener);
   }
 
   public void addFocusListener(FocusListener focusListener)
   {
-    if (Platform.isJS())
-    {
-      textField.addFocusListener(focusListener);
-    }
-    else
-    {
-      comboBox.addFocusListener(focusListener);
-    }
+    textComponent.addFocusListener(focusListener);
   }
 
   public void addKeyListener(KeyListener kl)
   {
-    if (!Platform.isJS())
-    {
-      comboBox.getEditor().getEditorComponent().addKeyListener(kl);
-    }
+    textComponent.addKeyListener(kl);
+  }
+
+  public void addCaretListener(CaretListener caretListener)
+  {
+    textComponent.addCaretListener(caretListener);
   }
 
   public void setEditable(boolean b)
   {
-    if (!Platform.isJS())
+    if (comboBox != null)
     {
       comboBox.setEditable(b);
     }
@@ -355,7 +373,8 @@ public class JvCacheableInputBox<E>
 
   public void setPrototypeDisplayValue(String string)
   {
-    if (!Platform.isJS())
+    prototypeDisplayValue = string;
+    if (comboBox != null)
     {
       comboBox.setPrototypeDisplayValue(string);
     }
@@ -363,7 +382,7 @@ public class JvCacheableInputBox<E>
 
   public void setSelectedItem(String userInput)
   {
-    if (!Platform.isJS())
+    if (comboBox != null)
     {
       comboBox.setSelectedItem(userInput);
     }
@@ -371,25 +390,12 @@ public class JvCacheableInputBox<E>
 
   public boolean isPopupVisible()
   {
-    if (!Platform.isJS())
-    {
-      return comboBox.isPopupVisible();
-    }
-    return false;
-  }
-
-  public void addCaretListener(CaretListener caretListener)
-  {
-    if (!Platform.isJS())
-    {
-      ((JTextComponent) comboBox.getEditor().getEditorComponent())
-              .addCaretListener(caretListener);
-    }
+    return (comboBox != null && comboBox.isPopupVisible());
   }
 
   public void addItem(String item)
   {
-    if (!Platform.isJS())
+    if (comboBox != null)
     {
       comboBox.addItem(item);
     }