OutOfMemoryError test
authoramwaterhouse <Andrew Waterhouse>
Thu, 5 Oct 2006 16:22:07 +0000 (16:22 +0000)
committeramwaterhouse <Andrew Waterhouse>
Thu, 5 Oct 2006 16:22:07 +0000 (16:22 +0000)
src/jalview/io/FileLoader.java

index dc18cb7..7fa4fda 100755 (executable)
@@ -27,13 +27,15 @@ import jalview.gui.*;
 import jalview.datamodel.*;\r
 import java.util.Vector;\r
 import java.util.StringTokenizer;\r
+import javax.swing.SwingUtilities;\r
 \r
-public class FileLoader\r
+public class FileLoader implements Runnable\r
 {\r
   String file;\r
   String protocol;\r
   String format;\r
   AlignViewport viewport;\r
+  AlignFrame alignFrame;\r
 \r
   public void LoadFile(AlignViewport viewport, String file, String protocol, String format)\r
   {\r
@@ -41,16 +43,26 @@ public class FileLoader
     LoadFile(file, protocol, format);\r
   }\r
 \r
+\r
+\r
   public void LoadFile(String file, String protocol, String format)\r
   {\r
     this.file = file;\r
     this.protocol = protocol;\r
     this.format = format;\r
 \r
-    LoadingThread loader = new LoadingThread();\r
-    loader.start();\r
+    final Thread loader = new Thread(this);\r
+\r
+    SwingUtilities.invokeLater(new Runnable()\r
+    {\r
+      public void run()\r
+      {\r
+        loader.start();\r
+      }\r
+    });\r
   }\r
 \r
+\r
   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,\r
                                                   String format)\r
   {\r
@@ -58,8 +70,9 @@ public class FileLoader
     this.protocol = protocol;\r
     this.format = format;\r
 \r
-    LoadingThread loader = new LoadingThread();\r
+    Thread loader = new Thread(this);\r
     loader.start();\r
+\r
     while (loader.isAlive())\r
     {\r
       try\r
@@ -70,7 +83,7 @@ public class FileLoader
       {}\r
     }\r
 \r
-    return loader.af;\r
+    return alignFrame;\r
   }\r
 \r
 \r
@@ -114,98 +127,120 @@ public class FileLoader
       jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);\r
   }\r
 \r
-\r
-  class LoadingThread\r
-      extends Thread\r
-  {\r
-\r
-    AlignFrame af;\r
-\r
-\r
-\r
     public void run()\r
     {\r
-      if (Desktop.instance != null)\r
-        Desktop.instance.startLoading(file);\r
+      try{\r
+        if (Desktop.instance != null)\r
+          Desktop.instance.startLoading(file);\r
 \r
-      SequenceI[] sequences = null;\r
+        SequenceI[] sequences = null;\r
 \r
-      if (format.equalsIgnoreCase("Jalview"))\r
-      {\r
-        af = new Jalview2XML().LoadJalviewAlign(file);\r
-      }\r
-      else\r
-      {\r
-        String error = AppletFormatAdapter.SUPPORTED_FORMATS;\r
-\r
-        if (FormatAdapter.isValidFormat(format))\r
+        if (format.equalsIgnoreCase("Jalview"))\r
         {\r
-          try\r
-          {\r
-            sequences = new FormatAdapter().readFile(file, protocol, format);\r
-          }\r
-          catch (java.io.IOException ex)\r
-          {\r
-            error = ex.getMessage();\r
-          }\r
+          alignFrame = new Jalview2XML().LoadJalviewAlign(file);\r
         }\r
-\r
-        if ( (sequences != null) && (sequences.length > 0))\r
+        else\r
         {\r
-          if(viewport!=null)\r
-          {\r
-            for(int i=0; i<sequences.length; i++)\r
-              viewport.getAlignment().addSequence(sequences[i]);\r
+          String error = AppletFormatAdapter.SUPPORTED_FORMATS;\r
 \r
-              viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());\r
-          }\r
-          else\r
+          if (FormatAdapter.isValidFormat(format))\r
           {\r
-            af = new AlignFrame(new Alignment(sequences));\r
-            af.currentFileFormat = format;\r
-            af.statusBar.setText("Successfully loaded file " + file);\r
-\r
-            Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
-                                     AlignFrame.NEW_WINDOW_HEIGHT);\r
-\r
             try\r
             {\r
-              af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));\r
+              sequences = new FormatAdapter().readFile(file, protocol, format);\r
             }\r
-            catch (java.beans.PropertyVetoException ex)\r
+            catch (java.io.IOException ex)\r
             {\r
+              error = ex.getMessage();\r
             }\r
           }\r
-        }\r
-        else\r
-        {\r
-          if (Desktop.instance != null)\r
-            Desktop.instance.stopLoading();\r
-\r
-          final String errorMessage = "Couldn't load file "+file+"\n"+error;\r
 \r
-          javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+          if ( (sequences != null) && (sequences.length > 0))\r
           {\r
-            public void run()\r
+            if (viewport != null)\r
             {\r
-              JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
-                                                    errorMessage,\r
-                                                    "Error loading file",\r
-                                                    JOptionPane.WARNING_MESSAGE);\r
+              for (int i = 0; i < sequences.length; i++)\r
+              {\r
+                viewport.getAlignment().addSequence(sequences[i]);\r
+              }\r
+              viewport.firePropertyChange("alignment", null,\r
+                                          viewport.getAlignment().getSequences());\r
+\r
+            }\r
+            else\r
+            {\r
+              Alignment al = new Alignment(sequences);\r
+\r
+              alignFrame = new AlignFrame(al,\r
+                                           AlignFrame.DEFAULT_WIDTH,\r
+                                           AlignFrame.DEFAULT_HEIGHT);\r
+              alignFrame.currentFileFormat = format;\r
+              alignFrame.statusBar.setText("Successfully loaded file " + file);\r
+\r
+              Desktop.addInternalFrame(alignFrame, file,\r
+                                       AlignFrame.DEFAULT_WIDTH,\r
+                                       AlignFrame.DEFAULT_HEIGHT);\r
+\r
+              try\r
+              {\r
+               alignFrame.setMaximum(jalview.bin.Cache.getDefault(\r
+                   "SHOW_FULLSCREEN", false));\r
+              }\r
+              catch (java.beans.PropertyVetoException ex)\r
+              {\r
+              }\r
             }\r
-          });\r
+          }\r
+          else\r
+          {\r
+            if (Desktop.instance != null)\r
+              Desktop.instance.stopLoading();\r
+\r
+            final String errorMessage = "Couldn't load file " + file + "\n" +\r
+                error;\r
+\r
+            javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+            {\r
+              public void run()\r
+              {\r
+                JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
+                    errorMessage,\r
+                    "Error loading file",\r
+                    JOptionPane.WARNING_MESSAGE);\r
+              }\r
+            });\r
+          }\r
         }\r
-      }\r
 \r
-      if (af != null)\r
+        if (alignFrame != null)\r
+        {\r
+         updateRecentlyOpened();\r
+        }\r
+      }\r
+      catch (OutOfMemoryError er)\r
       {\r
-        updateRecentlyOpened();\r
+\r
+        er.printStackTrace();\r
+        alignFrame = null;\r
+\r
+        javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+        {\r
+          public void run()\r
+          {\r
+            javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
+                "Out of memory loading file "+file+"!!"\r
+                +\r
+                "\nSee help files for increasing Java Virtual Machine memory."\r
+                , "Out of memory",\r
+                javax.swing.JOptionPane.WARNING_MESSAGE);\r
+          }\r
+        });\r
       }\r
 \r
-      if (Desktop.instance != null)\r
+     System.gc();\r
+     if (Desktop.instance != null)\r
         Desktop.instance.stopLoading();\r
 \r
     }\r
-  }\r
 \r
 }\r