JAL-2154 jalview.gui.SequenceFetcher.fetchAndShow(db,query) entry point for programma...
authorJim Procter <jprocter@issues.jalview.org>
Thu, 11 Aug 2016 20:32:56 +0000 (21:32 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 11 Aug 2016 20:32:56 +0000 (21:32 +0100)
src/jalview/gui/SequenceFetcher.java

index 0f3e2f4..e259913 100755 (executable)
@@ -43,6 +43,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;
 
@@ -201,9 +202,19 @@ public class SequenceFetcher extends JPanel implements Runnable
   }
 
   private IProgressIndicator progressIndicator;
+  private 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
@@ -215,7 +226,8 @@ public class SequenceFetcher extends JPanel implements Runnable
       {
         if (getSequenceFetcherSingleton(progressIndicator) != null)
         {
-          us.initGui(progressIndicator);
+          us.initGui(progressIndicator, selectedDb, queryString);
+          us._isConstructing=false;
         }
         else
         {
@@ -241,6 +253,26 @@ 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.EMPTY_LIST;
+      }
+    }
+    sf.newAlframes = new ArrayList<AlignFrame>();
+    sf.run();
+    return sf.newAlframes;
+  }
 
   private class DatabaseAuthority extends DefaultMutableTreeNode
   {
@@ -251,13 +283,51 @@ 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);
+      if (sp == null || sp.size() != 1)
+      {
+        System.err.println("Ignoring fetch parameter db='" + selectedDb
+                + "'");
+        return false;
+      }
+      database.selection = sp;
+      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)
@@ -268,7 +338,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();
@@ -938,7 +1017,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);