use OOMwarning to warn user when out of Memory occurs
[jalview.git] / src / jalview / gui / SequenceFetcher.java
index 3a07bd1..8c63f9c 100755 (executable)
@@ -1,17 +1,17 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
+ * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * 
  * This program 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 2
  * of the License, or (at your option) any later version.
- *
+ * 
  * This program 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 this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
@@ -58,7 +58,37 @@ public class SequenceFetcher extends JPanel implements Runnable
   private static jalview.ws.SequenceFetcher sfetch = null;
 
   private static String dasRegistry = null;
+  /**
+   * Blocking method that initialises and returns the shared instance of the SequenceFetcher client 
+   * @param guiWindow - where the initialisation delay message should be shown
+   * @return the singleton instance of the sequence fetcher client
+   */
+  public static jalview.ws.SequenceFetcher getSequenceFetcherSingleton(final IProgressIndicator guiWindow) {
+    if (sfetch == null
+            || dasRegistry != DasSourceBrowser.getDasRegistryURL())
+    {
+      /**
+       * give a visual indication that sequence fetcher construction is
+       * occuring
+       */
+      if (guiWindow != null)
+      {
+        guiWindow.setProgressBar(
+                "Initialising Sequence Database Fetchers", Thread.currentThread()
+                        .hashCode());
+      }
+      dasRegistry = DasSourceBrowser.getDasRegistryURL();
+      jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
+      if (guiWindow != null)
+      {
+        guiWindow.setProgressBar(
+                "Initialising Sequence Database Fetchers", Thread.currentThread().hashCode());
+      }
+      sfetch = sf;
 
+    }
+    return sfetch;
+  }
   public SequenceFetcher(IProgressIndicator guiIndic)
   {
     final IProgressIndicator guiWindow = guiIndic;
@@ -69,31 +99,22 @@ public class SequenceFetcher extends JPanel implements Runnable
 
       public void run()
       {
-        if (sfetch == null
-                || dasRegistry != DasSourceBrowser.getDasRegistryURL())
+        if (getSequenceFetcherSingleton(guiWindow)!=null)
         {
-          /**
-           * give a visual indication that sequence fetcher construction is
-           * occuring
-           */
-          if (guiWindow != null)
-          {
-            guiWindow.setProgressBar(
-                    "Initialising Sequence Database Fetchers", this
-                            .hashCode());
-          }
-          dasRegistry = DasSourceBrowser.getDasRegistryURL();
-          jalview.ws.SequenceFetcher sf = new jalview.ws.SequenceFetcher();
-          if (guiWindow != null)
+          us.initGui(guiWindow);
+        } else {
+          javax.swing.SwingUtilities.invokeLater(new Runnable()
           {
-            guiWindow.setProgressBar(
-                    "Initialising Sequence Database Fetchers", this
-                            .hashCode());
-          }
-          sfetch = sf;
-
+            public void run()
+            {
+              JOptionPane.showInternalMessageDialog(Desktop.desktop, 
+                      "Could not create the sequence fetcher client. Check error logs for details.",
+                      "Couldn't create SequenceFetcher", JOptionPane.ERROR_MESSAGE);
+            }
+          });
+          
+          // raise warning dialog
         }
-        us.initGui(guiWindow);
       }
     });
     sf.start();
@@ -146,11 +167,11 @@ public class SequenceFetcher extends JPanel implements Runnable
     frame.setContentPane(this);
     if (new jalview.util.Platform().isAMac())
     {
-      Desktop.addInternalFrame(frame, getFrameTitle(), 400, 200);
+      Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
     }
     else
     {
-      Desktop.addInternalFrame(frame, getFrameTitle(), 400, 180);
+      Desktop.addInternalFrame(frame, getFrameTitle(), 400, 140);
     }
   }
 
@@ -374,6 +395,7 @@ public class SequenceFetcher extends JPanel implements Runnable
       e.printStackTrace();
     } catch (OutOfMemoryError e)
     {
+      // resets dialog box - so we don't use OOMwarning here.
       showErrorMessage("Out of Memory when retrieving "
               + textArea.getText()
               + " from "
@@ -386,11 +408,12 @@ public class SequenceFetcher extends JPanel implements Runnable
               + " from " + database.getSelectedItem());
       e.printStackTrace();
     }
-    guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
     if (aresult != null)
     {
       parseResult(aresult, null, null);
     }
+    // only remove visual delay after we finished parsing.
+    guiWindow.setProgressBar(null, Thread.currentThread().hashCode());
     resetDialog();
   }