basic implementation of profile display and todos
[jalview.git] / src / jalview / gui / Desktop.java
index f9689db..1b24694 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.*;
@@ -380,6 +387,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   {
     Transferable t = evt.getTransferable();
     java.util.List files = null;
+    java.util.List protocols = null;
 
     try
     {
@@ -398,6 +406,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
         evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
         String data = (String) t.getTransferData(uriListFlavor);
         files = new java.util.ArrayList(1);
+        protocols = new java.util.ArrayList(1);
         for (java.util.StringTokenizer st = new java.util.StringTokenizer(
                 data, "\r\n"); st.hasMoreTokens();)
         {
@@ -407,10 +416,19 @@ public class Desktop extends jalview.jbgui.GDesktop implements
             // the line is a comment (as per the RFC 2483)
             continue;
           }
-
           java.net.URI uri = new java.net.URI(s);
-          java.io.File file = new java.io.File(uri);
-          files.add(file);
+          if (uri.getScheme().toLowerCase().startsWith("http"))
+          {
+            protocols.add(FormatAdapter.URL);
+            files.add(uri.toString());
+          }
+          else
+          {
+            // otherwise preserve old behaviour: catch all for file objects
+            java.io.File file = new java.io.File(uri);
+            protocols.add(FormatAdapter.FILE);
+            files.add(file.toString());
+          }
         }
       }
     } catch (Exception e)
@@ -424,7 +442,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
         for (int i = 0; i < files.size(); i++)
         {
           String file = files.get(i).toString();
-          String protocol = FormatAdapter.FILE;
+          String protocol = (protocols==null) ? FormatAdapter.FILE : (String) protocols.get(i);
           String format = null;
 
           if (file.endsWith(".jar"))
@@ -827,8 +845,12 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
       java.io.File choice = chooser.getSelectedFile();
+      JProgressBar progpanel = addProgressPanel("Saving jalview project "+choice.getName());
       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
+      // TODO catch and handle errors for savestate
       new Jalview2XML().SaveState(choice);
+      removeProgressPanel(progpanel);
+
     }
   }
 
@@ -852,9 +874,11 @@ public class Desktop extends jalview.jbgui.GDesktop implements
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
       String choice = chooser.getSelectedFile().getAbsolutePath();
+      setProgressBar("loading jalview project "+chooser.getSelectedFile().getName(),choice.hashCode());
       jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
               .getSelectedFile().getParent());
       new Jalview2XML().LoadJalviewAlign(choice);
+      setProgressBar(null,choice.hashCode());
     }
   }
 
@@ -940,6 +964,12 @@ public class Desktop extends jalview.jbgui.GDesktop implements
   public static AlignmentPanel[] getAlignmentPanels(String viewId)
   {
     int count = 0;
+    if (Desktop.desktop==null)
+    {
+      // no frames created and in headless mode
+      // TODO: verify that frames are recoverable when in headless mode
+      return null;
+    }
     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
     ArrayList aps = new ArrayList();
     for (int t = 0; t < frames.length; t++)
@@ -1125,6 +1155,49 @@ 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)
   {
     if (v_client != null)
@@ -1135,17 +1208,20 @@ public class Desktop extends jalview.jbgui.GDesktop implements
       return false;
     }
 
+    setProgressBar("Importing VAMSAS session from "+file.getName(),file.hashCode());
     try
     {
-      v_client = new jalview.gui.VamsasApplication(this, file);
+      v_client = new jalview.gui.VamsasApplication(this, file,null);
     } catch (Exception ex)
     {
+      setProgressBar("Importing VAMSAS session from "+file.getName(),file.hashCode());
       jalview.bin.Cache.log.error(
               "New vamsas session from existing session file failed:", ex);
       return false;
     }
     setupVamsasConnectedGui();
     v_client.initial_update(); // TODO: thread ?
+    setProgressBar("Importing VAMSAS session from "+file.getName(),file.hashCode());
     return v_client.inSession();
   }
   public boolean joinVamsasSession(String mysesid) {
@@ -1306,6 +1382,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
       if (value == JalviewFileChooser.APPROVE_OPTION)
       {
         java.io.File choice = chooser.getSelectedFile();
+        JProgressBar progpanel = addProgressPanel("Saving VAMSAS Document to "+choice.getName());
         jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice.getParent());
         String warnmsg = null;
         String warnttl = null;
@@ -1327,6 +1404,7 @@ public class Desktop extends jalview.jbgui.GDesktop implements
                   + choice, ex);
 
         }
+        removeProgressPanel(progpanel);
         if (warnmsg != null)
         {
           JOptionPane.showInternalMessageDialog(Desktop.desktop,