JAL-3446 JavaScript interface
[jalview.git] / src / jalview / util / Platform.java
index 8f8350f..b1b6697 100644 (file)
@@ -33,8 +33,12 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.net.URL;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.Date;
 import java.util.Locale;
@@ -804,17 +808,17 @@ public class Platform
   {
     if (isJS)
     {
-      jsutil.setAppletAttribute("app", j);
+      jsutil.setAppClass(j);
     }
   }
 
   /**
    *
-   * If this frame ia embedded in a web page, return a known type.
+   * If this frame is embedded in a web page, return a known type.
    * 
    * @param frame
    *          a JFrame or JInternalFrame
-   * @param type
+   * @param type "name", "node", "init", "dim", or any DOM attribute, such as "id"
    * @return null if frame is not embedded.
    */
   public static Object getEmbeddedAttribute(Component frame, String type)
@@ -930,11 +934,12 @@ public class Platform
    */
   public static String getAppID(String frameType)
   {
+    
     String id = Jalview.getInstance().j2sAppletID;
     if (id == null)
     {
-      Jalview.getInstance().j2sAppletID = id = (String) jsutil
-              .getAppletAttribute("_id");
+      Jalview.getInstance().j2sAppletID = id = (isJS ? (String) jsutil
+              .getAppletAttribute("_id") : "jalview");
     }
     return id + (frameType == null ? "" : "-" + frameType);
   }
@@ -949,7 +954,54 @@ public class Platform
    */
   public static Locale getLocaleOrNone(Locale loc)
   {
-    return (isJS && loc.getCountry() == "en" ? new Locale("") : loc);
+    return (isJS && loc.getLanguage() == "en" ? new Locale("") : loc);
+  }
+
+  /**
+   * From UrlDownloadClient; trivial in JavaScript; painful in Java.
+   * 
+   * @param urlstring
+   * @param outfile
+   * @throws IOException
+   */
+  public static void download(String urlstring, String outfile)
+          throws IOException
+  {
+    Path temp = null;
+    try (InputStream is = new URL(urlstring).openStream())
+    {
+      if (isJS)
+      { // so much easier!
+        streamToFile(is, new File(outfile));
+        return;
+      }
+      temp = Files.createTempFile(".jalview_", ".tmp");
+      try (FileOutputStream fos = new FileOutputStream(temp.toString());
+              ReadableByteChannel rbc = Channels.newChannel(is))
+      {
+        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
+        // copy tempfile to outfile once our download completes
+        // incase something goes wrong
+        Files.copy(temp, Paths.get(outfile),
+                StandardCopyOption.REPLACE_EXISTING);
+      }
+    } catch (IOException e)
+    {
+      throw e;
+    } finally
+    {
+      try
+      {
+        if (temp != null)
+        {
+          Files.deleteIfExists(temp);
+        }
+      } catch (IOException e)
+      {
+        System.out.println("Exception while deleting download temp file: "
+                + e.getMessage());
+      }
+    }
   }
 
 }