Recently loaded files, urls, append sequences to Alignment
[jalview.git] / src / jalview / io / FileLoader.java
index a3ef237..1785e89 100755 (executable)
@@ -22,22 +22,43 @@ 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.datamodel.*;\r
 import jalview.gui.Desktop;\r
 import jalview.datamodel.SequenceI;\r
+import java.util.Vector;\r
+import java.util.StringTokenizer;\r
 \r
 public class FileLoader\r
 {\r
+  String file;\r
+  String protocol;\r
+  String format;\r
+  AlignmentI alignment;\r
+\r
+  public void LoadFile(AlignmentI alignment, String file, String protocol, String format)\r
+  {\r
+    this.alignment = alignment;\r
+    LoadFile(file, protocol, format);\r
+  }\r
+\r
   public void LoadFile(String file, String protocol, 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
+    LoadingThread loader = new LoadingThread();\r
     loader.start();\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
+    LoadingThread loader = new LoadingThread();\r
     loader.start();\r
     while (loader.isAlive())\r
     {\r
@@ -53,71 +74,120 @@ public class FileLoader
   }\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
+    String historyItems = jalview.bin.Cache.getProperty(type);\r
 \r
-    if (format.equalsIgnoreCase("Jalview"))\r
+    StringTokenizer st;\r
+\r
+    if (historyItems != null)\r
     {\r
-      af = new Jalview2XML().LoadJalviewAlign(file);\r
+      st = new StringTokenizer(historyItems, "\t");\r
+\r
+      while (st.hasMoreTokens())\r
+      {\r
+        recent.addElement(st.nextElement().toString().trim());\r
+      }\r
+    }\r
+\r
+    if (recent.contains(file))\r
+    {\r
+      recent.remove(file);\r
+    }\r
+\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
-    else\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
+\r
+  class LoadingThread\r
+      extends Thread\r
+  {\r
+\r
+    AlignFrame af;\r
+\r
+\r
+    public void run()\r
     {\r
-      String errorMessage = AppletFormatAdapter.SUPPORTED_FORMATS;\r
+      SequenceI[] sequences = null;\r
 \r
-      if (FormatAdapter.formats.contains(format))\r
+      if (format.equalsIgnoreCase("Jalview"))\r
       {\r
-        try{\r
-          sequences = new FormatAdapter().readFile(file, protocol, format);\r
-        }catch(java.io.IOException ex)\r
-        {\r
-          errorMessage = ex.getMessage();\r
-        }\r
+        af = new Jalview2XML().LoadJalviewAlign(file);\r
       }\r
-\r
-      if ( (sequences != null) && (sequences.length > 0))\r
+      else\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
+        String errorMessage = AppletFormatAdapter.SUPPORTED_FORMATS;\r
 \r
+        if (FormatAdapter.formats.contains(format))\r
+        {\r
+          try\r
+          {\r
+            sequences = new FormatAdapter().readFile(file, protocol, format);\r
+          }\r
+          catch (java.io.IOException ex)\r
+          {\r
+            errorMessage = ex.getMessage();\r
+          }\r
+        }\r
 \r
-        try\r
+        if ( (sequences != null) && (sequences.length > 0))\r
         {\r
-          af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));\r
+          if(alignment!=null)\r
+          {\r
+            for(int i=0; i<sequences.length; i++)\r
+              alignment.addSequence(sequences[i]);\r
+          }\r
+          else\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
+            }\r
+            catch (java.beans.PropertyVetoException ex)\r
+            {\r
+            }\r
+          }\r
+\r
         }\r
-        catch (Exception ex)\r
+        else\r
         {\r
+          JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
+                                                "Couldn't load file " + file +\r
+                                                "\n"\r
+                                                + errorMessage,\r
+                                                "Error loading file",\r
+                                                JOptionPane.WARNING_MESSAGE);\r
         }\r
       }\r
-      else\r
+\r
+      if (af != null)\r
       {\r
-        JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
-                                              "Couldn't load file "+file+"\n"\r
-                                              +errorMessage,\r
-                                              "Error loading file",\r
-                                              JOptionPane.WARNING_MESSAGE);\r
+        updateRecentlyOpened();\r
       }\r
     }\r
   }\r
-}\r
 \r
 }\r