retrieve and init vamsas session from vdoc at URL
[jalview.git] / src / jalview / gui / Desktop.java
index f9689db..484bcde 100755 (executable)
@@ -24,8 +24,15 @@ import java.awt.*;
 import java.awt.datatransfer.*;
 import java.awt.dnd.*;
 import java.awt.event.*;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.lang.reflect.Constructor;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.channels.ReadableByteChannel;
 import java.util.*;
 
 import javax.swing.*;
@@ -1125,8 +1132,52 @@ public class Desktop extends jalview.jbgui.GDesktop implements
    * @param file 
    * @return true if import was a success and a session was started.
    */
+  public boolean vamsasImport(URL url)
+  {
+    // TODO: create progress bar
+    if (v_client != null)
+    {
+      
+      jalview.bin.Cache.log
+      .error("Implementation error - load session from a running session is not supported.");
+      return false;     
+    }
+    
+    try
+      {
+      // copy the URL content to a temporary local file
+      // TODO: be a bit cleverer here with nio (?!)
+      File file = File.createTempFile("vdocfromurl", ".vdj");
+      FileOutputStream fos = new FileOutputStream(file);
+      BufferedInputStream bis = new BufferedInputStream(url.openStream());
+      byte[] buffer = new byte[2048];
+      int ln;
+      while ((ln = bis.read(buffer))>-1)
+      {
+        fos.write(buffer,0,ln);
+      }
+      bis.close();
+      fos.close();
+      v_client = new jalview.gui.VamsasApplication(this, file, url.toExternalForm());
+      } catch (Exception ex)
+      {
+        jalview.bin.Cache.log.error(
+                "Failed to create new vamsas session from contents of URL "+url,ex);
+        return false;
+      }
+      setupVamsasConnectedGui();
+      v_client.initial_update(); // TODO: thread ?
+      return v_client.inSession();
+    }
+
+  /**
+   * import file into a new vamsas session (uses jalview.gui.VamsasApplication) 
+   * @param file 
+   * @return true if import was a success and a session was started.
+   */
   public boolean vamsasImport(File file)
   {
+    // TODO: create progress bar
     if (v_client != null)
     {
 
@@ -1137,7 +1188,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
 
     try
     {
-      v_client = new jalview.gui.VamsasApplication(this, file);
+      v_client = new jalview.gui.VamsasApplication(this, file,null);
     } catch (Exception ex)
     {
       jalview.bin.Cache.log.error(