adds protype size to JTextArea hack for nonexistent SwingJS editable
[jalview.git] / src / jalview / io / cache / JvCacheableInputBox.java
index 5e79393..934ba9e 100644 (file)
 package jalview.io.cache;
 
 import jalview.bin.Cache;
-import jalview.bin.Jalview;
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 
+import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.FocusListener;
@@ -76,6 +77,10 @@ public class JvCacheableInputBox<E>
 
   volatile boolean enterWasPressed = false;
 
+private String prototypeDisplayValue;
+
+private boolean isJS;
+
   /**
    * @return flag indicating if the most recent keypress was enter
    */
@@ -92,9 +97,18 @@ public class JvCacheableInputBox<E>
   public JvCacheableInputBox(String newCacheKey)
   {
     super();
-    if (Jalview.isJS())
+    isJS = true;//Platform.isJS();
+    this.prototypeDisplayValue = 
+            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
+    if (isJS)
     {
-      textField = new JTextField();
+      textField = new JTextField() {
+         public Dimension getPreferredSize() {
+                 return super.getPreferredSize();
+//                FontMetrics fm = getFontMetrics(getFont());
+//                     return new Dimension(fm.stringWidth(prototypeDisplayValue), fm.getHeight());
+         }
+      };
       return;
     }
 
@@ -114,8 +128,7 @@ public class JvCacheableInputBox<E>
         // let event bubble up
       }
     });
-    comboBox.setPrototypeDisplayValue(
-            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
+    comboBox.setPrototypeDisplayValue(prototypeDisplayValue);
     appCache = AppCache.getInstance();
     initCachePopupMenu();
     initCache(newCacheKey);
@@ -202,7 +215,7 @@ public class JvCacheableInputBox<E>
    */
   public void updateCache()
   {
-    if (Jalview.isJS())
+    if (isJS)
     {
       return;
     }
@@ -278,7 +291,7 @@ public class JvCacheableInputBox<E>
    */
   public void persistCache()
   {
-    if (!Jalview.isJS())
+    if (!isJS)
     {
       appCache.persistCache(cacheKey);
     }
@@ -291,7 +304,7 @@ public class JvCacheableInputBox<E>
    */
   public String getUserInput()
   {
-    if (Jalview.isJS())
+    if (isJS)
     {
       return textField.getText().trim();
     }
@@ -301,12 +314,16 @@ public class JvCacheableInputBox<E>
 
   public JComponent getComponent()
   {
-    return Jalview.isJS() ? textField : comboBox;
+    return isJS ? textField : comboBox;
   }
 
   public void addActionListener(ActionListener actionListener)
   {
-    if (!Jalview.isJS())
+    if (isJS)
+    {
+      textField.addActionListener(actionListener);
+    }
+    else
     {
       comboBox.addActionListener(actionListener);
     }
@@ -314,7 +331,7 @@ public class JvCacheableInputBox<E>
 
   public void addDocumentListener(DocumentListener listener)
   {
-    if (!Jalview.isJS())
+    if (!isJS)
     {
       ((JTextComponent) comboBox.getEditor().getEditorComponent())
               .getDocument().addDocumentListener(listener);
@@ -323,7 +340,11 @@ public class JvCacheableInputBox<E>
 
   public void addFocusListener(FocusListener focusListener)
   {
-    if (!Jalview.isJS())
+    if (isJS)
+    {
+      textField.addFocusListener(focusListener);
+    }
+    else
     {
       comboBox.addFocusListener(focusListener);
     }
@@ -331,7 +352,7 @@ public class JvCacheableInputBox<E>
 
   public void addKeyListener(KeyListener kl)
   {
-    if (!Jalview.isJS())
+    if (!isJS)
     {
       comboBox.getEditor().getEditorComponent().addKeyListener(kl);
     }
@@ -339,7 +360,7 @@ public class JvCacheableInputBox<E>
 
   public void setEditable(boolean b)
   {
-    if (!Jalview.isJS())
+    if (!isJS)
     {
       comboBox.setEditable(b);
     }
@@ -347,15 +368,16 @@ public class JvCacheableInputBox<E>
 
   public void setPrototypeDisplayValue(String string)
   {
-    if (!Jalview.isJS())
+       this.prototypeDisplayValue = string;
+    if (!isJS)
     {
       comboBox.setPrototypeDisplayValue(string);
-    }
+    } 
   }
 
   public void setSelectedItem(String userInput)
   {
-    if (!Jalview.isJS())
+    if (!isJS)
     {
       comboBox.setSelectedItem(userInput);
     }
@@ -363,7 +385,7 @@ public class JvCacheableInputBox<E>
 
   public boolean isPopupVisible()
   {
-    if (!Jalview.isJS())
+    if (!isJS)
     {
       return comboBox.isPopupVisible();
     }
@@ -372,11 +394,19 @@ public class JvCacheableInputBox<E>
 
   public void addCaretListener(CaretListener caretListener)
   {
-    if (!Jalview.isJS())
+    if (!isJS)
     {
       ((JTextComponent) comboBox.getEditor().getEditorComponent())
               .addCaretListener(caretListener);
     }
   }
 
+  public void addItem(String item)
+  {
+    if (!isJS)
+    {
+      comboBox.addItem(item);
+    }
+  }
+
 }