Any character non aa or nucleotide is a space
[jalview.git] / src / jalview / io / FileLoader.java
index 26aff08..34e5886 100755 (executable)
@@ -1,6 +1,6 @@
 /*\r
 * Jalview - A Sequence Alignment Editor and Viewer\r
-* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+* Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
 *\r
 * This program is free software; you can redistribute it and/or\r
 * modify it under the terms of the GNU General Public License\r
@@ -22,23 +22,57 @@ package jalview.io;
 import jalview.gui.AlignFrame;\r
 import jalview.gui.Jalview2XML;\r
 import javax.swing.JOptionPane;\r
-import jalview.datamodel.Alignment;\r
-import jalview.gui.Desktop;\r
-import jalview.datamodel.SequenceI;\r
 \r
-public class FileLoader\r
+import jalview.gui.*;\r
+import jalview.datamodel.*;\r
+import java.util.Vector;\r
+import java.util.StringTokenizer;\r
+import javax.swing.SwingUtilities;\r
+\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
+    this.viewport = viewport;\r
+    LoadFile(file, protocol, format);\r
+  }\r
+\r
+\r
+\r
   public void LoadFile(String file, String protocol, String format)\r
   {\r
-    LoadingThread loader = new LoadingThread(file, protocol, format);\r
-    loader.start();\r
+    this.file = file;\r
+    this.protocol = protocol;\r
+    this.format = format;\r
+\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
-    LoadingThread loader = new LoadingThread(file, protocol, format);\r
+    this.file = file;\r
+    this.protocol = protocol;\r
+    this.format = format;\r
+\r
+    Thread loader = new Thread(this);\r
     loader.start();\r
+\r
     while (loader.isAlive())\r
     {\r
       try\r
@@ -49,69 +83,162 @@ public class FileLoader
       {}\r
     }\r
 \r
-    return loader.af;\r
+    return alignFrame;\r
   }\r
 \r
 \r
-  class LoadingThread\r
-    extends Thread\r
-{\r
-  String file;\r
-  String protocol;\r
-  String format;\r
-  AlignFrame af;\r
 \r
-  public LoadingThread(String file, String protocol, String format)\r
+  public void updateRecentlyOpened()\r
   {\r
-    this.file = file;\r
-    this.protocol = protocol;\r
-    this.format = format;\r
-  }\r
+    Vector recent = new Vector();\r
 \r
-  public void run()\r
-  {\r
-    SequenceI[] sequences = null;\r
+    String type = protocol.equals(FormatAdapter.FILE)\r
+        ? "RECENT_FILE" : "RECENT_URL";\r
 \r
-    if (format.equalsIgnoreCase("Jalview"))\r
-    {\r
-      af = Jalview2XML.LoadJalviewAlign(file);\r
-    }\r
-    else\r
+    String historyItems = jalview.bin.Cache.getProperty(type);\r
+\r
+    StringTokenizer st;\r
+\r
+    if (historyItems != null)\r
     {\r
-      if (FormatAdapter.formats.contains(format))\r
+      st = new StringTokenizer(historyItems, "\t");\r
+\r
+      while (st.hasMoreTokens())\r
       {\r
-        sequences = new FormatAdapter().readFile(file, protocol, format);\r
+        recent.addElement(st.nextElement().toString().trim());\r
       }\r
+    }\r
 \r
-      if ( (sequences != null) && (sequences.length > 0))\r
-      {\r
-        af = new AlignFrame(new Alignment(sequences));\r
-        af.currentFileFormat = format;\r
-        af.statusBar.setText("Successfully loaded file " + file);\r
+    if (recent.contains(file))\r
+    {\r
+      recent.remove(file);\r
+    }\r
 \r
-        Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
-                           AlignFrame.NEW_WINDOW_HEIGHT);\r
+    StringBuffer newHistory = new StringBuffer(file);\r
+    for (int i = 0; i < recent.size() && i < 10; i++)\r
+    {\r
+      newHistory.append("\t");\r
+      newHistory.append(recent.elementAt(i));\r
+    }\r
+\r
+    jalview.bin.Cache.setProperty(type, newHistory.toString());\r
+\r
+    if(type.equals(FormatAdapter.FILE))\r
+      jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);\r
+  }\r
+\r
+    public void run()\r
+    {\r
+      try{\r
+        if (Desktop.instance != null)\r
+          Desktop.instance.startLoading(file);\r
 \r
+        Alignment al = null;\r
 \r
-        try\r
+        if (format.equalsIgnoreCase("Jalview"))\r
         {\r
-          af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));\r
+          alignFrame = new Jalview2XML().LoadJalviewAlign(file);\r
         }\r
-        catch (Exception ex)\r
+        else\r
         {\r
+          String error = AppletFormatAdapter.SUPPORTED_FORMATS;\r
+\r
+          if (FormatAdapter.isValidFormat(format))\r
+          {\r
+            try\r
+            {\r
+              al = new FormatAdapter().readFile(file, protocol, format);\r
+            }\r
+            catch (java.io.IOException ex)\r
+            {\r
+              error = ex.getMessage();\r
+            }\r
+          }\r
+\r
+          if ( (al != null) && (al.getHeight() > 0))\r
+          {\r
+            if (viewport != null)\r
+            {\r
+              for (int i = 0; i < al.getHeight(); i++)\r
+              {\r
+                viewport.getAlignment().addSequence(al.getSequenceAt(i));\r
+              }\r
+              viewport.firePropertyChange("alignment", null,\r
+                                          viewport.getAlignment().getSequences());\r
+\r
+            }\r
+            else\r
+            {\r
+              alignFrame = new AlignFrame(al,\r
+                                           AlignFrame.DEFAULT_WIDTH,\r
+                                           AlignFrame.DEFAULT_HEIGHT);\r
+\r
+              alignFrame.statusBar.setText("Successfully loaded file " + file);\r
+              alignFrame.setFileName(file, format);\r
+\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
+          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
+         updateRecentlyOpened();\r
+\r
       }\r
-      else\r
+      catch (OutOfMemoryError er)\r
       {\r
-        JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
-                                              "Couldn't open file.\n" +\r
-                                              "Formats currently supported are\n" +\r
-                                              "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter!\r
-                                              , "Error loading file",\r
-                                              JOptionPane.WARNING_MESSAGE);\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
+     System.gc();\r
+     if (Desktop.instance != null)\r
+        Desktop.instance.stopLoading();\r
+\r
     }\r
-  }\r
-}\r
 \r
 }\r