Merge branch 'develop' into trialMerge
[jalview.git] / src / jalview / gui / SequenceFetcher.java
index fca130b..8e1d549 100755 (executable)
@@ -44,6 +44,7 @@ import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -116,8 +117,6 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   private static Thread initingThread = null;
 
-  int debounceTrap = 0;
-
   public JTextArea getTextArea()
   {
     return textArea;
@@ -169,9 +168,8 @@ public class SequenceFetcher extends JPanel implements Runnable
     if (sfetch == null
             || dasRegistry != Cache.getDasSourceRegistry()
             || lastDasSourceRegistry != (Cache.getDasSourceRegistry()
-                    .getDasRegistryURL() + Cache
-                    .getDasSourceRegistry().getLocalSourceString())
-                    .hashCode())
+                    .getDasRegistryURL() + Cache.getDasSourceRegistry()
+                    .getLocalSourceString()).hashCode())
     {
       _initingFetcher = true;
       initingThread = Thread.currentThread();
@@ -203,8 +201,19 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   private IProgressIndicator progressIndicator;
 
+  private volatile boolean _isConstructing = false;
+
+  private List<AlignFrame> newAlframes = null;
+
   public SequenceFetcher(IProgressIndicator guiIndic)
   {
+    this(guiIndic, null, null);
+  }
+
+  public SequenceFetcher(IProgressIndicator guiIndic,
+          final String selectedDb, final String queryString)
+  {
+    this._isConstructing = true;
     this.progressIndicator = guiIndic;
     final SequenceFetcher us = this;
     // launch initialiser thread
@@ -216,7 +225,8 @@ public class SequenceFetcher extends JPanel implements Runnable
       {
         if (getSequenceFetcherSingleton(progressIndicator) != null)
         {
-          us.initGui(progressIndicator);
+          us.initGui(progressIndicator, selectedDb, queryString);
+          us._isConstructing = false;
         }
         else
         {
@@ -225,14 +235,14 @@ public class SequenceFetcher extends JPanel implements Runnable
             @Override
             public void run()
             {
-              JOptionPane
+              JvOptionPane
                       .showInternalMessageDialog(
                               Desktop.desktop,
                               MessageManager
                                       .getString("warn.couldnt_create_sequence_fetcher_client"),
                               MessageManager
                                       .getString("label.couldnt_create_sequence_fetcher"),
-                              JOptionPane.ERROR_MESSAGE);
+                              JvOptionPane.ERROR_MESSAGE);
             }
           });
 
@@ -243,6 +253,32 @@ public class SequenceFetcher extends JPanel implements Runnable
     sf.start();
   }
 
+  /**
+   * blocking call which creates a new sequence fetcher panel, configures it and
+   * presses the OK button with the given database and query.
+   * 
+   * @param database
+   * @param query
+   */
+  public static List<AlignFrame> fetchAndShow(String database, String query)
+  {
+    final SequenceFetcher sf = new SequenceFetcher(Desktop.instance,
+            database, query);
+    while (sf._isConstructing)
+    {
+      try
+      {
+        Thread.sleep(50);
+      } catch (Exception q)
+      {
+        return Collections.emptyList();
+      }
+    }
+    sf.newAlframes = new ArrayList<AlignFrame>();
+    sf.run();
+    return sf.newAlframes;
+  }
+
   private class DatabaseAuthority extends DefaultMutableTreeNode
   {
 
@@ -254,11 +290,57 @@ public class SequenceFetcher extends JPanel implements Runnable
   };
 
   /**
+   * initialise the database and query for this fetcher panel
+   * 
+   * @param selectedDb
+   *          - string that should correspond to a sequence fetcher
+   * @param queryString
+   *          - string that will be entered in the query dialog
+   * @return true if UI was configured with valid database and query string
+   */
+  protected boolean setInitialQuery(String selectedDb, String queryString)
+  {
+    if (selectedDb == null || selectedDb.trim().length() == 0)
+    {
+      return false;
+    }
+    try
+    {
+      List<DbSourceProxy> sp = sfetch.getSourceProxy(selectedDb);
+      for (DbSourceProxy sourcep : sp)
+      {
+        if (sourcep.getTier() == 0)
+        {
+          database.selection = Arrays
+                  .asList(new DbSourceProxy[] { sourcep });
+          break;
+        }
+      }
+      if (database.selection == null || database.selection.size() == 0)
+      {
+        System.err.println("Ignoring fetch parameter db='" + selectedDb
+                + "'");
+        return false;
+      }
+      textArea.setText(queryString);
+    } catch (Exception q)
+    {
+      System.err.println("Ignoring fetch parameter db='" + selectedDb
+              + "' and query='" + queryString + "'");
+      return false;
+    }
+    return true;
+  }
+
+  /**
    * called by thread spawned by constructor
    * 
    * @param guiWindow
+   * @param queryString
+   * @param selectedDb
    */
-  private void initGui(IProgressIndicator guiWindow)
+  private void initGui(IProgressIndicator guiWindow, String selectedDb,
+          String queryString)
   {
     this.guiWindow = guiWindow;
     if (guiWindow instanceof AlignFrame)
@@ -269,6 +351,16 @@ public class SequenceFetcher extends JPanel implements Runnable
     try
     {
       jbInit();
+      /*
+       * configure the UI with any query parameters we were called with
+       */
+      if (!setInitialQuery(selectedDb, queryString))
+      {
+        /*
+         * none provided, so show the database chooser
+         */
+        database.waitForInput();
+      }
     } catch (Exception ex)
     {
       ex.printStackTrace();
@@ -365,7 +457,6 @@ public class SequenceFetcher extends JPanel implements Runnable
     jPanel1.add(example);
     jPanel1.add(clear);
     jPanel1.add(close);
-    jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
     jPanel2.setLayout(borderLayout3);
     databaseButt = /*database.getDatabaseSelectorButton();
                    final JButton viewdbs =*/new JButton(
@@ -386,7 +477,6 @@ public class SequenceFetcher extends JPanel implements Runnable
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        debounceTrap++;
         String currentSelection = database.getSelectedItem();
         if (currentSelection == null)
         {
@@ -395,13 +485,11 @@ public class SequenceFetcher extends JPanel implements Runnable
 
         showPanel();
 
-        if (currentSelection.equalsIgnoreCase("pdb")
-                && (database.action == KeyEvent.VK_ENTER || ((debounceTrap % 2) == 0)))
+        if ("pdb".equalsIgnoreCase(currentSelection))
         {
           pdbSourceAction();
         }
-        else if (currentSelection.equalsIgnoreCase("uniprot")
-                && (database.action == KeyEvent.VK_ENTER || ((debounceTrap % 2) == 0)))
+        else if ("uniprot".equalsIgnoreCase(currentSelection))
         {
           uniprotSourceAction();
         }
@@ -426,11 +514,6 @@ public class SequenceFetcher extends JPanel implements Runnable
     this.add(jPanel3, java.awt.BorderLayout.CENTER);
     this.add(jPanel2, java.awt.BorderLayout.NORTH);
     jScrollPane1.getViewport().add(textArea);
-
-    /*
-     * open the database tree
-     */
-    database.waitForInput();
   }
 
   private void pdbSourceAction()
@@ -446,6 +529,7 @@ public class SequenceFetcher extends JPanel implements Runnable
     new UniprotFTSPanel(this);
     frame.dispose();
   }
+
   private void otherSourceAction()
   {
     try
@@ -835,8 +919,8 @@ public class SequenceFetcher extends JPanel implements Runnable
     } catch (Exception e)
     {
       Cache.log.info(
-              "Error retrieving " + accession
-              + " from " + proxy.getDbName(), e);
+              "Error retrieving " + accession + " from "
+                      + proxy.getDbName(), e);
     }
     return success;
   }
@@ -943,7 +1027,10 @@ public class SequenceFetcher extends JPanel implements Runnable
         {
           af.hideFeatureColumns(SequenceOntologyI.EXON, false);
         }
-
+        if (newAlframes != null)
+        {
+          newAlframes.add(af);
+        }
         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
                 AlignFrame.DEFAULT_HEIGHT);
 
@@ -952,8 +1039,7 @@ public class SequenceFetcher extends JPanel implements Runnable
 
         try
         {
-          af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN",
-                  false));
+          af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
         } catch (Exception ex)
         {
         }
@@ -974,9 +1060,9 @@ public class SequenceFetcher extends JPanel implements Runnable
       @Override
       public void run()
       {
-        JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
+        JvOptionPane.showInternalMessageDialog(Desktop.desktop, error,
                 MessageManager.getString("label.error_retrieving_data"),
-                JOptionPane.WARNING_MESSAGE);
+                JvOptionPane.WARNING_MESSAGE);
       }
     });
   }