JAL-3885 Made the database JComboBox have StringPair entries to allow a display value...
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 29 Sep 2021 19:51:22 +0000 (20:51 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 29 Sep 2021 19:51:22 +0000 (20:51 +0100)
src/jalview/gui/SequenceFetcher.java

index caecbba..54aefc4 100755 (executable)
  */
 package jalview.gui;
 
-import jalview.api.FeatureSettingsModelI;
-import jalview.bin.Cache;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.DBRefEntry;
-import jalview.datamodel.SequenceI;
-import jalview.fts.core.GFTSPanel;
-import jalview.fts.service.pdb.PDBFTSPanel;
-import jalview.fts.service.threedbeacons.TDBeaconsFTSPanel;
-import jalview.fts.service.uniprot.UniprotFTSPanel;
-import jalview.io.FileFormatI;
-import jalview.io.gff.SequenceOntologyI;
-import jalview.util.DBRefUtils;
-import jalview.util.MessageManager;
-import jalview.util.Platform;
-import jalview.ws.seqfetcher.DbSourceProxy;
-
 import java.awt.BorderLayout;
 import java.awt.Font;
 import java.awt.event.ActionEvent;
@@ -58,6 +42,22 @@ import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
 import javax.swing.SwingConstants;
 
+import jalview.api.FeatureSettingsModelI;
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.SequenceI;
+import jalview.fts.core.GFTSPanel;
+import jalview.fts.service.pdb.PDBFTSPanel;
+import jalview.fts.service.threedbeacons.TDBeaconsFTSPanel;
+import jalview.fts.service.uniprot.UniprotFTSPanel;
+import jalview.io.FileFormatI;
+import jalview.io.gff.SequenceOntologyI;
+import jalview.util.DBRefUtils;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+import jalview.ws.seqfetcher.DbSourceProxy;
+
 /**
  * A panel where the use may choose a database source, and enter one or more
  * accessions, to retrieve entries from the database.
@@ -67,11 +67,50 @@ import javax.swing.SwingConstants;
  */
 public class SequenceFetcher extends JPanel implements Runnable
 {
+  private class StringPair
+  {
+    private String key;
+
+    private String display;
+
+    public StringPair(String s1, String s2)
+    {
+      key = s1;
+      display = s2;
+    }
+
+    public StringPair(String s)
+    {
+      this(s, s);
+    }
+
+    public String getKey()
+    {
+      return key;
+    }
+
+    public String getDisplay()
+    {
+      return display;
+    }
+
+    @Override
+    public String toString()
+    {
+      return display;
+    }
+
+    public boolean equals(StringPair other)
+    {
+      return other.key == this.key;
+    }
+  }
+
   private static jalview.ws.SequenceFetcher sfetch = null;
 
   JLabel exampleAccession;
 
-  JComboBox<String> database;
+  JComboBox<StringPair> database;
 
   JCheckBox replacePunctuation;
 
@@ -146,8 +185,8 @@ public class SequenceFetcher extends JPanel implements Runnable
 
     frame = new JInternalFrame();
     frame.setContentPane(this);
-    Desktop.addInternalFrame(frame, getFrameTitle(), true, 400, 
-               Platform.isAMacAndNotJS() ? 240 : 180);
+    Desktop.addInternalFrame(frame, getFrameTitle(), true, 400,
+            Platform.isAMacAndNotJS() ? 240 : 180);
   }
 
   private String getFrameTitle()
@@ -164,15 +203,25 @@ public class SequenceFetcher extends JPanel implements Runnable
 
     database = new JComboBox<>();
     database.setFont(JvSwingUtils.getLabelFont());
-    database.setPrototypeDisplayValue("ENSEMBLGENOMES   ");
+    StringPair instructionItem = new StringPair(
+            MessageManager.getString("action.select_ddbb"));
+    database.setPrototypeDisplayValue(instructionItem);
     String[] sources = new jalview.ws.SequenceFetcher().getSupportedDb();
     Arrays.sort(sources, String.CASE_INSENSITIVE_ORDER);
-    database.addItem(MessageManager.getString("action.select_ddbb"));
+    database.addItem(instructionItem);
     for (String source : sources)
     {
-      database.addItem(source);
+      List<DbSourceProxy> slist = sfetch.getSourceProxy(source);
+      if (slist.size() == 1 && slist.get(0) != null)
+      {
+        database.addItem(new StringPair(source, slist.get(0).getDbName()));
+      }
+      else
+      {
+        database.addItem(new StringPair(source));
+      }
     }
-    database.setSelectedItem(selectedDb);
+    setDatabaseSelectedItem(selectedDb);
     if (database.getSelectedIndex() == -1)
     {
       database.setSelectedIndex(0);
@@ -183,7 +232,8 @@ public class SequenceFetcher extends JPanel implements Runnable
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        String currentSelection = (String) database.getSelectedItem();
+        String currentSelection = ((StringPair) database.getSelectedItem())
+                .getKey();
         updateExampleQuery(currentSelection);
 
         if ("pdb".equalsIgnoreCase(currentSelection))
@@ -310,6 +360,19 @@ public class SequenceFetcher extends JPanel implements Runnable
     this.add(databasePanel, BorderLayout.NORTH);
   }
 
+  private void setDatabaseSelectedItem(String db)
+  {
+    for (int i = 0; i < database.getItemCount(); i++)
+    {
+      StringPair sp = database.getItemAt(i);
+      if (sp != null && db != null && db.equals(sp.getKey()))
+      {
+        database.setSelectedIndex(i);
+        return;
+      }
+    }
+  }
+
   /**
    * Answers a semi-colon-delimited string with the example query or queries for
    * the selected database
@@ -381,7 +444,8 @@ public class SequenceFetcher extends JPanel implements Runnable
    */
   protected void example_actionPerformed()
   {
-    String eq = getExampleQueries((String) database.getSelectedItem());
+    String eq = getExampleQueries(
+            ((StringPair) database.getSelectedItem()).getKey());
     textArea.setText(eq);
     repaint();
   }
@@ -430,9 +494,9 @@ public class SequenceFetcher extends JPanel implements Runnable
       text = text.replace(",", ";");
     }
     text = text.replaceAll("(\\s|[; ])+", ";");
-    if (!t0.equals(text)) 
+    if (!t0.equals(text))
     {
-         textArea.setText(text);
+      textArea.setText(text);
     }
     if (text.isEmpty())
     {
@@ -477,8 +541,8 @@ public class SequenceFetcher extends JPanel implements Runnable
     List<String> presultTitle = new ArrayList<>();
     List<AlignmentI> presult = new ArrayList<>();
     List<AlignmentI> aresult = new ArrayList<>();
-    List<DbSourceProxy> sources = sfetch
-            .getSourceProxy((String) database.getSelectedItem());
+    List<DbSourceProxy> sources = sfetch.getSourceProxy(
+            ((StringPair) database.getSelectedItem()).getKey());
     Iterator<DbSourceProxy> proxies = sources.iterator();
     String[] qries = textArea.getText().trim().split(";");
     List<String> nextFetch = Arrays.asList(qries);
@@ -530,23 +594,25 @@ public class SequenceFetcher extends JPanel implements Runnable
       } catch (Exception e)
       {
         showErrorMessage("Error retrieving " + textArea.getText() + " from "
-                + database.getSelectedItem());
+                + ((StringPair) database.getSelectedItem()).getDisplay());
         // error
         // +="Couldn't retrieve sequences from "+database.getSelectedItem();
         System.err.println("Retrieval failed for source ='"
-                + database.getSelectedItem() + "' and query\n'"
-                + textArea.getText() + "'\n");
+                + ((StringPair) database.getSelectedItem()).getDisplay()
+                + "' and query\n'" + textArea.getText() + "'\n");
         e.printStackTrace();
       } catch (OutOfMemoryError e)
       {
         showErrorMessage("Out of Memory when retrieving "
-                + textArea.getText() + " from " + database.getSelectedItem()
+                + textArea.getText() + " from "
+                + ((StringPair) database.getSelectedItem()).getDisplay()
                 + "\nPlease see the Jalview FAQ for instructions for increasing the memory available to Jalview.\n");
         e.printStackTrace();
       } catch (Error e)
       {
         showErrorMessage("Serious Error retrieving " + textArea.getText()
-                + " from " + database.getSelectedItem());
+                + " from "
+                + ((StringPair) database.getSelectedItem()).getDisplay());
         e.printStackTrace();
       }
 
@@ -693,7 +759,8 @@ public class SequenceFetcher extends JPanel implements Runnable
     } catch (OutOfMemoryError oome)
     {
       new OOMWarning("fetching " + multiacc + " from "
-              + database.getSelectedItem(), oome, this);
+              + ((StringPair) database.getSelectedItem()).getDisplay(),
+              oome, this);
     }
   }
 
@@ -767,12 +834,12 @@ public class SequenceFetcher extends JPanel implements Runnable
 
     for (String q : queries)
     {
-       // BH 2019.01.25 dbr is never used.
-//      DBRefEntry dbr = new DBRefEntry();
-//      dbr.setSource(proxy.getDbSource());
-//      dbr.setVersion(null);
+      // BH 2019.01.25 dbr is never used.
+      // DBRefEntry dbr = new DBRefEntry();
+      // dbr.setSource(proxy.getDbSource());
+      // dbr.setVersion(null);
       String accId = proxy.getAccessionIdFromQuery(q);
-//      dbr.setAccessionId(accId);
+      // dbr.setAccessionId(accId);
       boolean rfound = false;
       for (int r = 0, nr = rs.length; r < nr; r++)
       {
@@ -801,15 +868,18 @@ public class SequenceFetcher extends JPanel implements Runnable
    */
   public String getDefaultRetrievalTitle()
   {
-    return "Retrieved from " + database.getSelectedItem();
+    return "Retrieved from "
+            + ((StringPair) database.getSelectedItem()).getDisplay();
   }
+
   /**
    * constructs an alignment frame given the data and metadata
+   * 
    * @param al
    * @param title
    * @param currentFileFormat
    * @param preferredFeatureColours
-   * @return the alignment 
+   * @return the alignment
    */
   public AlignmentI parseResult(AlignmentI al, String title,
           FileFormatI currentFileFormat,