JAL-1064 JAL-671 - add tree based database dialog as button in sequence fetcher
authorjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 20 Apr 2012 15:57:37 +0000 (16:57 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 20 Apr 2012 15:57:37 +0000 (16:57 +0100)
src/jalview/gui/SequenceFetcher.java

index 260950f..5ea5768 100755 (executable)
@@ -23,6 +23,9 @@ import java.awt.*;
 import java.awt.event.*;
 
 import javax.swing.*;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.MutableTreeNode;
+import javax.swing.tree.TreeModel;
 
 import jalview.datamodel.*;
 import jalview.io.*;
@@ -44,10 +47,10 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   final String noDbSelected = "-- Select Database --";
 
-  Hashtable sources = new Hashtable();
-
   private static jalview.ws.SequenceFetcher sfetch = null;
 
+  private static long lastDasSourceRegistry = -3;
+
   private static DasSourceRegistryI dasRegistry = null;
 
   private static boolean _initingFetcher = false;
@@ -94,7 +97,11 @@ public class SequenceFetcher extends JPanel implements Runnable
       }
     }
     if (sfetch == null
-            || dasRegistry != jalview.bin.Cache.getDasSourceRegistry())
+            || dasRegistry != jalview.bin.Cache.getDasSourceRegistry()
+            || lastDasSourceRegistry != (jalview.bin.Cache
+                    .getDasSourceRegistry().getDasRegistryURL() + jalview.bin.Cache
+                    .getDasSourceRegistry().getLocalSourceString())
+                    .hashCode())
     {
       _initingFetcher = true;
       initingThread = Thread.currentThread();
@@ -108,13 +115,15 @@ public class SequenceFetcher extends JPanel implements Runnable
       }
       dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
       dasRegistry.refreshSources();
-      
+
       jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
       if (guiWindow != null)
       {
         guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
                 Thread.currentThread().hashCode());
       }
+      lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
+              .getLocalSourceString()).hashCode();
       sfetch = sf;
       _initingFetcher = false;
       initingThread = null;
@@ -158,6 +167,16 @@ public class SequenceFetcher extends JPanel implements Runnable
     sf.start();
   }
 
+  private class DatabaseAuthority extends DefaultMutableTreeNode
+  {
+
+  };
+
+  private class DatabaseSource extends DefaultMutableTreeNode
+  {
+
+  };
+
   /**
    * called by thread spawned by constructor
    * 
@@ -170,29 +189,7 @@ public class SequenceFetcher extends JPanel implements Runnable
     {
       alignFrame = (AlignFrame) guiWindow;
     }
-
-    database.addItem(noDbSelected);
-    /*
-     * Dynamically generated database list will need a translation function from
-     * internal source to externally distinct names. UNIPROT and UP_NAME are
-     * identical DB sources, and should be collapsed.
-     */
-
-    String dbs[] = sfetch.getOrderedSupportedSources();
-    for (int i = 0; i < dbs.length; i++)
-    {
-      if (!sources.containsValue(dbs[i]))
-      {
-        String name = sfetch.getSourceProxy(dbs[i]).getDbName();
-        // duplicate source names are thrown away, here.
-        if (!sources.containsKey(name))
-        {
-          database.addItem(name);
-        }
-        // overwrite with latest version of the retriever for this source
-        sources.put(name, dbs[i]);
-      }
-    }
+    database = new JDatabaseTree(sfetch);
     try
     {
       jbInit();
@@ -284,7 +281,8 @@ public class SequenceFetcher extends JPanel implements Runnable
     jPanel1.add(close);
     jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
     jPanel2.setLayout(borderLayout3);
-
+    databaseButt = database.getDatabaseSelectorButton();
+    databaseButt.setFont(JvSwingUtils.getLabelFont());
     database.addActionListener(new ActionListener()
     {
 
@@ -293,9 +291,11 @@ public class SequenceFetcher extends JPanel implements Runnable
         DbSourceProxy db = null;
         try
         {
-          db = sfetch.getSourceProxy((String) sources.get(database
-                  .getSelectedItem()));
-          String eq = db.getTestQuery();
+          databaseButt.setText(database.getSelectedItem()
+                  + (database.getSelectedSources().size() > 1 ? " (and "
+                          + database.getSelectedSources().size()
+                          + " others)" : ""));
+          String eq = database.getExampleQueries();
           dbeg.setText("Example query: " + eq);
           replacePunctuation.setEnabled(!(eq != null && eq.indexOf(",") > -1));
         } catch (Exception ex)
@@ -307,7 +307,7 @@ public class SequenceFetcher extends JPanel implements Runnable
       }
     });
     dbeg.setText("");
-    jPanel2.add(database, java.awt.BorderLayout.NORTH);
+    jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
     jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
     JPanel jPanel2a = new JPanel(new BorderLayout());
     jPanel2a.add(jLabel1, java.awt.BorderLayout.NORTH);
@@ -327,9 +327,7 @@ public class SequenceFetcher extends JPanel implements Runnable
     DbSourceProxy db = null;
     try
     {
-      db = sfetch.getSourceProxy((String) sources.get(database
-              .getSelectedItem()));
-      textArea.setText(db.getTestQuery());
+      textArea.setText(database.getExampleQueries());
     } catch (Exception ex)
     {
     }
@@ -344,7 +342,9 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   JLabel dbeg = new JLabel();
 
-  JComboBox database = new JComboBox();
+  JDatabaseTree database;
+
+  JButton databaseButt;
 
   JLabel jLabel1 = new JLabel();
 
@@ -408,7 +408,7 @@ public class SequenceFetcher extends JPanel implements Runnable
   public void run()
   {
     String error = "";
-    if (database.getSelectedItem().equals(noDbSelected))
+    if (!database.hasSelection())
     {
       error += "Please select the source database\n";
     }
@@ -438,18 +438,16 @@ public class SequenceFetcher extends JPanel implements Runnable
       resetDialog();
       return;
     }
-    ArrayList<String> aresultq=new ArrayList<String>();
+    ArrayList<String> aresultq = new ArrayList<String>();
     ArrayList<AlignmentI> aresult = new ArrayList<AlignmentI>();
-    Object source = database.getSelectedItem();
+    DbSourceProxy proxy = database.getSelectedSources().get(0);
     Enumeration en = new StringTokenizer(textArea.getText(), ";");
-    boolean isAliSource=false;
+    boolean isAliSource = false;
     try
     {
       guiWindow.setProgressBar(
-              "Fetching Sequences from " + database.getSelectedItem(),
-              Thread.currentThread().hashCode());
-      DbSourceProxy proxy = sfetch.getSourceProxy((String) sources
-              .get(source));
+              "Fetching Sequences from " + proxy.getDbName(), Thread
+                      .currentThread().hashCode());
       isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
       if (proxy.getAccessionSeparator() == null)
       {
@@ -478,7 +476,7 @@ public class SequenceFetcher extends JPanel implements Runnable
             } catch (OutOfMemoryError oome)
             {
               new OOMWarning("fetching " + item + " from "
-                      + database.getSelectedItem(), oome, this);
+                      + proxy.getDbName(), oome, this);
             }
             if (indres != null)
             {
@@ -488,7 +486,7 @@ public class SequenceFetcher extends JPanel implements Runnable
           } catch (Exception e)
           {
             jalview.bin.Cache.log.info("Error retrieving " + item
-                    + " from " + source, e);
+                    + " from " + proxy.getDbName(), e);
           }
         }
       }
@@ -539,24 +537,35 @@ public class SequenceFetcher extends JPanel implements Runnable
               + " from " + database.getSelectedItem());
       e.printStackTrace();
     }
-    if (aresult != null && aresult.size()>0)
+    if (aresult != null && aresult.size() > 0)
     {
-      AlignmentI ar=null;
-      if (isAliSource) {
+      AlignmentI ar = null;
+      if (isAliSource)
+      {
         // new window for each result
-        while (aresult.size()>0)
+        while (aresult.size() > 0)
         {
-          parseResult(aresult.remove(0), aresultq.remove(0)+" "+getDefaultRetrievalTitle(), null);
+          parseResult(aresult.remove(0), aresultq.remove(0) + " "
+                  + getDefaultRetrievalTitle(), null);
         }
-      } else {
+      }
+      else
+      {
         // concatenate all results in one window
-        while (aresult.size()>0)
+        while (aresult.size() > 0)
         {
-          if (ar==null) { ar = aresult.remove(0);}
-          else { ar.append(aresult.remove(0)); };
+          if (ar == null)
+          {
+            ar = aresult.remove(0);
+          }
+          else
+          {
+            ar.append(aresult.remove(0));
+          }
+          ;
         }
         parseResult(ar, null, null);
-      } 
+      }
     }
     // only remove visual delay after we finished parsing.
     guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
@@ -737,11 +746,14 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   /**
    * 
-   * @return a standard title for any results retrieved using the currently selected source and settings
+   * @return a standard title for any results retrieved using the currently
+   *         selected source and settings
    */
-  public String getDefaultRetrievalTitle() {
+  public String getDefaultRetrievalTitle()
+  {
     return "Retrieved from " + database.getSelectedItem();
   }
+
   AlignmentI parseResult(AlignmentI al, String title,
           String currentFileFormat)
   {
@@ -796,7 +808,8 @@ public class SequenceFetcher extends JPanel implements Runnable
       {
         for (int i = 0; i < al.getHeight(); i++)
         {
-          alignFrame.viewport.getAlignment().addSequence(al.getSequenceAt(i)); // this
+          alignFrame.viewport.getAlignment().addSequence(
+                  al.getSequenceAt(i)); // this
           // also
           // creates
           // dataset