JAL-3038 workaround to replace editable combobox with text field in JS
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 16 Aug 2018 14:07:35 +0000 (15:07 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 16 Aug 2018 14:07:35 +0000 (15:07 +0100)
src/jalview/fts/core/GFTSPanel.java
src/jalview/gui/AnnotationColumnChooser.java
src/jalview/gui/Finder.java
src/jalview/io/cache/JvCacheableInputBox.java
src/jalview/jbgui/GFinder.java

index b0ab8c6..38ea958 100644 (file)
@@ -73,7 +73,6 @@ import javax.swing.event.DocumentListener;
 import javax.swing.event.InternalFrameEvent;
 import javax.swing.table.DefaultTableModel;
 import javax.swing.table.TableColumn;
-import javax.swing.text.JTextComponent;
 
 /**
  * This class provides the swing GUI layout for FTS Panel and implements most of
@@ -274,7 +273,7 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI
           if (tabs != null
                   && tabs.getSelectedComponent() == ftsPanel)
           {
-            txt_search.requestFocusInWindow();
+            txt_search.getComponent().requestFocusInWindow();
           }
         }
       });
@@ -526,17 +525,16 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI
                     "label.separate_multiple_query_values", new Object[]
                     { getCmbSearchTarget().getSelectedItem().toString() });
           }
-          txt_search.setToolTipText(
+          txt_search.getComponent().setToolTipText(
                   JvSwingUtils.wrapTooltip(true, tooltipText));
           searchAction(true);
         }
       }
     });
 
-    txt_search.setFont(new java.awt.Font("Verdana", 0, 12));
+    txt_search.getComponent().setFont(new java.awt.Font("Verdana", 0, 12));
 
-    txt_search.getEditor().getEditorComponent()
-            .addKeyListener(new KeyAdapter()
+    txt_search.addKeyListener(new KeyAdapter()
             {
               @Override
               public void keyPressed(KeyEvent e)
@@ -576,8 +574,7 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI
                 }
               }
             }, false);
-    ((JTextComponent) txt_search.getEditor().getEditorComponent())
-            .getDocument().addDocumentListener(listener);
+    txt_search.addDocumentListener(listener);
 
     txt_search.addFocusListener(new FocusListener()
     {
@@ -629,7 +626,7 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI
           btn_ok.setEnabled(false);
           btn_next_page.setEnabled(false);
           btn_prev_page.setEnabled(false);
-          txt_search.setEnabled(false);
+          txt_search.getComponent().setEnabled(false);
           cmb_searchTarget.setEnabled(false);
           previousWantedFields = getFTSRestClient()
                   .getAllDefaultDisplayedFTSDataColumns()
@@ -640,7 +637,7 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI
           btn_back.setEnabled(true);
           btn_cancel.setEnabled(true);
           refreshPaginatorState();
-          txt_search.setEnabled(true);
+          txt_search.getComponent().setEnabled(true);
           cmb_searchTarget.setEnabled(true);
           if (wantedFieldsUpdated())
           {
@@ -666,7 +663,7 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI
 
     pnl_results.add(tabbedPane);
     pnl_inputs.add(cmb_searchTarget);
-    pnl_inputs.add(txt_search);
+    pnl_inputs.add(txt_search.getComponent());
     pnl_inputs.add(btn_autosearch);
     pnl_inputs.add(lbl_loading);
     pnl_inputs.add(lbl_warning);
@@ -805,11 +802,6 @@ public abstract class GFTSPanel extends JPanel implements GFTSPanelI
     return cmb_searchTarget;
   }
 
-  public JComboBox<String> getTxtSearch()
-  {
-    return txt_search;
-  }
-
   public JInternalFrame getMainFrame()
   {
     return mainFrame;
index 5adecad..5e12d2b 100644 (file)
@@ -731,10 +731,9 @@ public class AnnotationColumnChooser extends AnnotationRowFilter
               MessageManager.getString("label.search_filter")));
 
       searchBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXX");
-      searchBox.setToolTipText(
+      searchBox.getComponent().setToolTipText(
               MessageManager.getString("info.enter_search_text_here"));
-      searchBox.getEditor().getEditorComponent()
-              .addKeyListener(new java.awt.event.KeyAdapter()
+      searchBox.addKeyListener(new java.awt.event.KeyAdapter()
               {
                 @Override
                 public void keyPressed(KeyEvent e)
@@ -746,8 +745,7 @@ public class AnnotationColumnChooser extends AnnotationRowFilter
                   }
                 }
               });
-      searchBox.getEditor().getEditorComponent()
-              .addFocusListener(new FocusAdapter()
+      searchBox.addFocusListener(new FocusAdapter()
       {
         @Override
         public void focusLost(FocusEvent e)
@@ -777,7 +775,7 @@ public class AnnotationColumnChooser extends AnnotationRowFilter
       });
 
       syncState();
-      this.add(searchBox);
+      this.add(searchBox.getComponent());
       this.add(displayName);
       this.add(description);
     }
index 6361a86..0d35add 100755 (executable)
@@ -114,7 +114,7 @@ public class Finder extends GFinder
     Desktop.addInternalFrame(frame, MessageManager.getString("label.find"),
             MY_WIDTH, MY_HEIGHT);
     frame.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
-    searchBox.requestFocus();
+    searchBox.getComponent().requestFocus();
   }
 
   /**
@@ -217,7 +217,7 @@ public class Finder extends GFinder
     List<SequenceI> seqs = new ArrayList<SequenceI>();
     List<SequenceFeature> features = new ArrayList<SequenceFeature>();
 
-    String searchString = searchBox.getEditor().getItem().toString().trim();
+    String searchString = searchBox.getUserInput();
     String desc = "Search Results";
 
     /*
@@ -244,7 +244,7 @@ public class Finder extends GFinder
   {
     createFeatures.setEnabled(false);
 
-    String searchString = searchBox.getUserInput().trim();
+    String searchString = searchBox.getUserInput();
 
     if (isInvalidSearchString(searchString))
     {
@@ -304,7 +304,7 @@ public class Finder extends GFinder
       seqIndex = 0;
       JvOptionPane.showInternalMessageDialog(this,
               MessageManager.getString("label.finished_searching"), null,
-              JvOptionPane.INFORMATION_MESSAGE);
+              JvOptionPane.PLAIN_MESSAGE);
     }
     else
     {
@@ -325,7 +325,7 @@ public class Finder extends GFinder
         resIndex = -1;
         seqIndex = 0;
         JvOptionPane.showInternalMessageDialog(this, message, null,
-                JvOptionPane.INFORMATION_MESSAGE);
+                JvOptionPane.PLAIN_MESSAGE);
       }
     }
   }
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);
+    }
   }
 
 }
index 1ea4ab5..eb4b910 100755 (executable)
@@ -43,7 +43,6 @@ import javax.swing.SwingConstants;
 import javax.swing.SwingUtilities;
 import javax.swing.event.CaretEvent;
 import javax.swing.event.CaretListener;
-import javax.swing.text.JTextComponent;
 
 public class GFinder extends JPanel
 {
@@ -134,9 +133,9 @@ public class GFinder extends JPanel
         createFeatures_actionPerformed();
       }
     });
-    searchBox.setFont(new java.awt.Font("Verdana", Font.PLAIN, 12));
-    ((JTextComponent) searchBox.getEditor().getEditorComponent())
-            .addCaretListener(new CaretListener()
+    searchBox.getComponent()
+            .setFont(new java.awt.Font("Verdana", Font.PLAIN, 12));
+    searchBox.addCaretListener(new CaretListener()
             {
               @Override
               public void caretUpdate(CaretEvent e)
@@ -144,8 +143,7 @@ public class GFinder extends JPanel
                 textfield_caretUpdate(e);
               }
             });
-    searchBox.getEditor().getEditorComponent()
-            .addKeyListener(new java.awt.event.KeyAdapter()
+    searchBox.addKeyListener(new java.awt.event.KeyAdapter()
             {
               @Override
               public void keyPressed(KeyEvent e)
@@ -169,10 +167,10 @@ public class GFinder extends JPanel
     actionsPanel.add(createFeatures, null);
     this.add(jLabelFind, java.awt.BorderLayout.WEST);
     this.add(actionsPanel, java.awt.BorderLayout.EAST);
-    this.add(jPanel2, java.awt.BorderLayout.SOUTH);
+    // this.add(jPanel2, java.awt.BorderLayout.SOUTH);
     this.add(jPanel3, java.awt.BorderLayout.NORTH);
     this.add(jPanel4, java.awt.BorderLayout.CENTER);
-    jPanel4.add(searchBox, java.awt.BorderLayout.NORTH);
+    jPanel4.add(searchBox.getComponent(), java.awt.BorderLayout.NORTH);
 
     JPanel optionsPanel = new JPanel();