JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / gui / SequenceFetcher.java
index 94c4f66..9f92c42 100755 (executable)
@@ -1,45 +1,99 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
  *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.gui;
 
-import java.util.*;
-import java.util.List;
-
-import java.awt.*;
-import java.awt.event.*;
-
-import javax.swing.*;
-import javax.swing.tree.DefaultMutableTreeNode;
-
-import com.stevesoft.pat.Regex;
-
-import jalview.datamodel.*;
-import jalview.io.*;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.DBRefSource;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
+import jalview.io.FormatAdapter;
+import jalview.io.IdentifyFile;
 import jalview.util.DBRefUtils;
 import jalview.util.MessageManager;
 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
 import jalview.ws.seqfetcher.DbSourceProxy;
+
 import java.awt.BorderLayout;
+import java.awt.Font;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JInternalFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.SwingConstants;
+import javax.swing.tree.DefaultMutableTreeNode;
+
+import com.stevesoft.pat.Regex;
 
 public class SequenceFetcher extends JPanel implements Runnable
 {
-  // ASequenceFetcher sfetch;
+  JLabel dbeg = new JLabel();
+
+  JDatabaseTree database;
+
+  JButton databaseButt;
+
+  JLabel jLabel1 = new JLabel();
+
+  JCheckBox replacePunctuation = new JCheckBox();
+
+  JButton ok = new JButton();
+
+  JButton clear = new JButton();
+
+  JButton example = new JButton();
+
+  JButton close = new JButton();
+
+  JPanel jPanel1 = new JPanel();
+
+  JTextArea textArea = new JTextArea();
+
+  JScrollPane jScrollPane1 = new JScrollPane();
+
+  JPanel jPanel2 = new JPanel();
+
+  JPanel jPanel3 = new JPanel();
+
+  JPanel jPanel4 = new JPanel();
+
+  BorderLayout borderLayout1 = new BorderLayout();
+
+  BorderLayout borderLayout2 = new BorderLayout();
+
+  BorderLayout borderLayout3 = new BorderLayout();
+
   JInternalFrame frame;
 
   IProgressIndicator guiWindow;
@@ -60,6 +114,8 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   private static Thread initingThread = null;
 
+  int debounceTrap = 0;
+
   /**
    * Blocking method that initialises and returns the shared instance of the
    * SequenceFetcher client
@@ -75,9 +131,11 @@ public class SequenceFetcher extends JPanel implements Runnable
     {
       if (guiWindow != null)
       {
-        guiWindow.setProgressBar(
-                "Waiting for Sequence Database Fetchers to initialise",
-                Thread.currentThread().hashCode());
+        guiWindow
+                .setProgressBar(
+                        MessageManager
+                                .getString("status.waiting_sequence_database_fetchers_init"),
+                        Thread.currentThread().hashCode());
       }
       // initting happening on another thread - so wait around to see if it
       // finishes.
@@ -94,9 +152,11 @@ public class SequenceFetcher extends JPanel implements Runnable
       }
       if (guiWindow != null)
       {
-        guiWindow.setProgressBar(
-                "Waiting for Sequence Database Fetchers to initialise",
-                Thread.currentThread().hashCode());
+        guiWindow
+                .setProgressBar(
+                        MessageManager
+                                .getString("status.waiting_sequence_database_fetchers_init"),
+                        Thread.currentThread().hashCode());
       }
     }
     if (sfetch == null
@@ -113,7 +173,8 @@ public class SequenceFetcher extends JPanel implements Runnable
        */
       if (guiWindow != null)
       {
-        guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
+        guiWindow.setProgressBar(MessageManager
+                .getString("status.init_sequence_database_fetchers"),
                 Thread.currentThread().hashCode());
       }
       dasRegistry = jalview.bin.Cache.getDasSourceRegistry();
@@ -122,8 +183,7 @@ public class SequenceFetcher extends JPanel implements Runnable
       jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
       if (guiWindow != null)
       {
-        guiWindow.setProgressBar("Initialising Sequence Database Fetchers",
-                Thread.currentThread().hashCode());
+        guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
       }
       lastDasSourceRegistry = (dasRegistry.getDasRegistryURL() + dasRegistry
               .getLocalSourceString()).hashCode();
@@ -134,9 +194,11 @@ public class SequenceFetcher extends JPanel implements Runnable
     return sfetch;
   }
 
+  private IProgressIndicator progressIndicator;
+
   public SequenceFetcher(IProgressIndicator guiIndic)
   {
-    final IProgressIndicator guiWindow = guiIndic;
+    this.progressIndicator = guiIndic;
     final SequenceFetcher us = this;
     // launch initialiser thread
     Thread sf = new Thread(new Runnable()
@@ -145,9 +207,9 @@ public class SequenceFetcher extends JPanel implements Runnable
       @Override
       public void run()
       {
-        if (getSequenceFetcherSingleton(guiWindow) != null)
+        if (getSequenceFetcherSingleton(progressIndicator) != null)
         {
-          us.initGui(guiWindow);
+          us.initGui(progressIndicator);
         }
         else
         {
@@ -159,8 +221,10 @@ public class SequenceFetcher extends JPanel implements Runnable
               JOptionPane
                       .showInternalMessageDialog(
                               Desktop.desktop,
-                              "Could not create the sequence fetcher client. Check error logs for details.",
-                              "Couldn't create SequenceFetcher",
+                              MessageManager
+                                      .getString("warn.couldnt_create_sequence_fetcher_client"),
+                              MessageManager
+                                      .getString("label.couldnt_create_sequence_fetcher"),
                               JOptionPane.ERROR_MESSAGE);
             }
           });
@@ -217,8 +281,9 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   private String getFrameTitle()
   {
-    return ((alignFrame == null) ? "New " : "Additional ")
-            + "Sequence Fetcher";
+    return ((alignFrame == null) ? MessageManager
+            .getString("label.new_sequence_fetcher") : MessageManager
+            .getString("label.additional_sequence_fetcher"));
   }
 
   private void jbInit() throws Exception
@@ -229,13 +294,15 @@ public class SequenceFetcher extends JPanel implements Runnable
     dbeg.setFont(new java.awt.Font("Verdana", Font.BOLD, 11));
     jLabel1.setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
     jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
-    jLabel1.setText("Separate multiple accession ids with semi colon \";\"");
+    jLabel1.setText(MessageManager
+            .getString("label.separate_multiple_accession_ids"));
 
     replacePunctuation.setHorizontalAlignment(SwingConstants.CENTER);
     replacePunctuation
             .setFont(new java.awt.Font("Verdana", Font.ITALIC, 11));
-    replacePunctuation.setText("Replace commas with semi-colons");
-    ok.setText("OK");
+    replacePunctuation.setText(MessageManager
+            .getString("label.replace_commas_semicolons"));
+    ok.setText(MessageManager.getString("action.ok"));
     ok.addActionListener(new ActionListener()
     {
       @Override
@@ -244,7 +311,7 @@ public class SequenceFetcher extends JPanel implements Runnable
         ok_actionPerformed();
       }
     });
-    clear.setText("Clear");
+    clear.setText(MessageManager.getString("action.clear"));
     clear.addActionListener(new ActionListener()
     {
       @Override
@@ -254,7 +321,7 @@ public class SequenceFetcher extends JPanel implements Runnable
       }
     });
 
-    example.setText("Example");
+    example.setText(MessageManager.getString("label.example"));
     example.addActionListener(new ActionListener()
     {
       @Override
@@ -263,7 +330,7 @@ public class SequenceFetcher extends JPanel implements Runnable
         example_actionPerformed();
       }
     });
-    close.setText("Close");
+    close.setText(MessageManager.getString("action.close"));
     close.addActionListener(new ActionListener()
     {
       @Override
@@ -280,7 +347,9 @@ public class SequenceFetcher extends JPanel implements Runnable
       public void keyPressed(KeyEvent e)
       {
         if (e.getKeyCode() == KeyEvent.VK_ENTER)
+        {
           ok_actionPerformed();
+        }
       }
     });
     jPanel3.setLayout(borderLayout1);
@@ -295,37 +364,24 @@ public class SequenceFetcher extends JPanel implements Runnable
     databaseButt.setFont(JvSwingUtils.getLabelFont());
     database.addActionListener(new ActionListener()
     {
-
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        try
+        debounceTrap++;
+        String currentSelection = database.getSelectedItem();
+        if (!currentSelection.equalsIgnoreCase("pdb"))
         {
-          databaseButt.setText(database.getSelectedItem()
-                  + (database.getSelectedSources().size() > 1 ? " (and "
-                          + database.getSelectedSources().size()
-                          + " others)" : ""));
-          String eq = database.getExampleQueries();
-          dbeg.setText("Example query: " + eq);
-          boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
-          for (DbSourceProxy dbs : database.getSelectedSources())
-          {
-            if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
-            {
-              enablePunct = false;
-              break;
-            }
-          }
-          replacePunctuation.setEnabled(enablePunct);
-
-        } catch (Exception ex)
+          otherSourceAction();
+        }
+        if (currentSelection.equalsIgnoreCase("pdb")
+                && (database.action == KeyEvent.VK_ENTER || ((debounceTrap % 2) == 0)))
         {
-          dbeg.setText("");
-          replacePunctuation.setEnabled(true);
+          pdbSourceAction();
         }
-        jPanel2.repaint();
+        database.action = -1;
       }
     });
+
     dbeg.setText("");
     jPanel2.add(databaseButt, java.awt.BorderLayout.NORTH);
     jPanel2.add(dbeg, java.awt.BorderLayout.CENTER);
@@ -342,6 +398,43 @@ public class SequenceFetcher extends JPanel implements Runnable
 
   }
 
+  private void pdbSourceAction()
+  {
+    databaseButt.setText(database.getSelectedItem());
+    new PDBSearchPanel(this);
+    frame.dispose();
+  }
+
+  private void otherSourceAction()
+  {
+    try
+    {
+      databaseButt.setText(database.getSelectedItem()
+              + (database.getSelectedSources().size() > 1 ? " (and "
+                      + database.getSelectedSources().size() + " others)"
+                      : ""));
+      String eq = database.getExampleQueries();
+      dbeg.setText(MessageManager.formatMessage(
+              "label.example_query_param", new String[] { eq }));
+      boolean enablePunct = !(eq != null && eq.indexOf(",") > -1);
+      for (DbSourceProxy dbs : database.getSelectedSources())
+      {
+        if (dbs instanceof jalview.ws.dbsources.das.datamodel.DasSequenceSource)
+        {
+          enablePunct = false;
+          break;
+        }
+      }
+      replacePunctuation.setEnabled(enablePunct);
+
+    } catch (Exception ex)
+    {
+      dbeg.setText("");
+      replacePunctuation.setEnabled(true);
+    }
+    jPanel2.repaint();
+  }
+
   protected void example_actionPerformed()
   {
     DbSourceProxy db = null;
@@ -360,42 +453,6 @@ public class SequenceFetcher extends JPanel implements Runnable
     jPanel3.repaint();
   }
 
-  JLabel dbeg = new JLabel();
-
-  JDatabaseTree database;
-
-  JButton databaseButt;
-
-  JLabel jLabel1 = new JLabel();
-
-  JCheckBox replacePunctuation = new JCheckBox();
-
-  JButton ok = new JButton();
-
-  JButton clear = new JButton();
-
-  JButton example = new JButton();
-
-  JButton close = new JButton();
-
-  JPanel jPanel1 = new JPanel();
-
-  JTextArea textArea = new JTextArea();
-
-  JScrollPane jScrollPane1 = new JScrollPane();
-
-  JPanel jPanel2 = new JPanel();
-
-  JPanel jPanel3 = new JPanel();
-
-  JPanel jPanel4 = new JPanel();
-
-  BorderLayout borderLayout1 = new BorderLayout();
-
-  BorderLayout borderLayout2 = new BorderLayout();
-
-  BorderLayout borderLayout3 = new BorderLayout();
-
   public void close_actionPerformed(ActionEvent e)
   {
     try
@@ -488,9 +545,13 @@ public class SequenceFetcher extends JPanel implements Runnable
       try
       {
         // update status
-        guiWindow.setProgressBar("Fetching " + nqueries
-                + " sequence queries from " + proxy.getDbName(), Thread
-                .currentThread().hashCode());
+        guiWindow
+                .setProgressBar(MessageManager.formatMessage(
+                        "status.fetching_sequence_queries_from",
+                        new String[] {
+                            Integer.valueOf(nqueries).toString(),
+                            proxy.getDbName() }), Thread.currentThread()
+                        .hashCode());
         isAliSource = proxy.isA(DBRefSource.ALIGNMENTDB);
         if (proxy.getAccessionSeparator() == null)
         {
@@ -687,11 +748,15 @@ public class SequenceFetcher extends JPanel implements Runnable
           presultTitle.add(titl);
         }
       }
-      guiWindow.setProgressBar("Finished querying", Thread.currentThread()
+      guiWindow.setProgressBar(MessageManager
+              .getString("status.finshed_querying"), Thread.currentThread()
               .hashCode());
     }
-    guiWindow.setProgressBar((presult.size() > 0) ? "Parsing results."
-            : "Processing ..", Thread.currentThread().hashCode());
+    guiWindow.setProgressBar(
+            (presult.size() > 0) ? MessageManager
+                    .getString("status.parsing_results") : MessageManager
+                    .getString("status.processing"), Thread.currentThread()
+                    .hashCode());
     // process results
     while (presult.size() > 0)
     {
@@ -726,7 +791,7 @@ public class SequenceFetcher extends JPanel implements Runnable
   AlignmentI parseResult(String result, String title)
   {
     String format = new IdentifyFile().Identify(result, "Paste");
-    Alignment sequences = null;
+    AlignmentI sequences = null;
     if (FormatAdapter.isValidFormat(format))
     {
       sequences = null;
@@ -768,6 +833,10 @@ public class SequenceFetcher extends JPanel implements Runnable
 
     if (al != null && al.getHeight() > 0)
     {
+      if (title == null)
+      {
+        title = getDefaultRetrievalTitle();
+      }
       if (alignFrame == null)
       {
         AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
@@ -780,17 +849,13 @@ public class SequenceFetcher extends JPanel implements Runnable
           // Alignments?
         }
 
-        if (title == null)
-        {
-          title = getDefaultRetrievalTitle();
-        }
         SequenceFeature[] sfs = null;
         List<SequenceI> alsqs;
         synchronized (alsqs = al.getSequences())
         {
           for (SequenceI sq : alsqs)
           {
-            if ((sfs = (sq).getDatasetSequence().getSequenceFeatures()) != null)
+            if ((sfs = sq.getSequenceFeatures()) != null)
             {
               if (sfs.length > 0)
               {
@@ -804,7 +869,8 @@ public class SequenceFetcher extends JPanel implements Runnable
         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
                 AlignFrame.DEFAULT_HEIGHT);
 
-        af.statusBar.setText("Successfully pasted alignment file");
+        af.statusBar.setText(MessageManager
+                .getString("label.successfully_pasted_alignment_file"));
 
         try
         {
@@ -816,21 +882,7 @@ public class SequenceFetcher extends JPanel implements Runnable
       }
       else
       {
-        for (int i = 0; i < al.getHeight(); i++)
-        {
-          alignFrame.viewport.getAlignment().addSequence(
-                  al.getSequenceAt(i)); // this
-          // also
-          // creates
-          // dataset
-          // sequence
-          // entries
-        }
-        alignFrame.viewport.setEndSeq(alignFrame.viewport.getAlignment()
-                .getHeight());
-        alignFrame.viewport.getAlignment().getWidth();
-        alignFrame.viewport.firePropertyChange("alignment", null,
-                alignFrame.viewport.getAlignment().getSequences());
+        alignFrame.viewport.addAlignment(al, title);
       }
     }
     return al;
@@ -845,8 +897,19 @@ public class SequenceFetcher extends JPanel implements Runnable
       public void run()
       {
         JOptionPane.showInternalMessageDialog(Desktop.desktop, error,
-                MessageManager.getString("label.error_retrieving_data"), JOptionPane.WARNING_MESSAGE);
+                MessageManager.getString("label.error_retrieving_data"),
+                JOptionPane.WARNING_MESSAGE);
       }
     });
   }
+
+  public IProgressIndicator getProgressIndicator()
+  {
+    return progressIndicator;
+  }
+
+  public void setProgressIndicator(IProgressIndicator progressIndicator)
+  {
+    this.progressIndicator = progressIndicator;
+  }
 }