JAL-3451 setting dimensions for AlignFrame if embedded
[jalview.git] / src / jalview / io / FileLoader.java
index 4b2f797..1fa6957 100755 (executable)
@@ -33,21 +33,21 @@ import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
 import jalview.gui.Desktop;
-import jalview.gui.Jalview2XML;
 import jalview.gui.JvOptionPane;
 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
+import jalview.project.Jalview2XML;
 import jalview.schemes.ColourSchemeI;
-import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 import jalview.ws.utils.UrlDownloadClient;
 
+import java.awt.Dimension;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.StringTokenizer;
 import java.util.Vector;
@@ -56,16 +56,35 @@ import javax.swing.SwingUtilities;
 
 public class FileLoader implements Runnable
 {
+  private File selectedFile;
+
   String file;
 
   DataSourceType protocol;
 
   FileFormatI format;
 
-  AlignmentFileReaderI source = null; // alternative specification of where data
-                                      // comes
+  AlignmentFileReaderI source; // alternative specification of where data
+                               // comes from
 
-  // from
+  /**
+   * It is critical that all these fields are set, as this instance is reused.
+   * 
+   * @param source
+   * @param file
+   * @param inFile
+   * @param dataSourceType
+   * @param format
+   */
+  private void setFileFields(AlignmentFileReaderI source, File file,
+          String inFile, DataSourceType dataSourceType, FileFormatI format)
+  {
+    this.source = source;
+    this.file = inFile;
+    this.selectedFile = file;
+    this.protocol = dataSourceType;
+    this.format = format;
+  }
 
   AlignViewport viewport;
 
@@ -77,8 +96,6 @@ public class FileLoader implements Runnable
 
   boolean raiseGUI = true;
 
-  private File selectedFile;
-
   /**
    * default constructor always raised errors in GUI dialog boxes
    */
@@ -98,7 +115,7 @@ public class FileLoader implements Runnable
     this.raiseGUI = raiseGUI;
   }
 
-  public void LoadFile(AlignViewport viewport, Object file,
+  public void loadFile(AlignViewport viewport, Object file,
           DataSourceType protocol, FileFormatI format)
   {
     this.viewport = viewport;
@@ -106,10 +123,10 @@ public class FileLoader implements Runnable
       this.selectedFile = (File) file;
       file = selectedFile.getPath();
     }
-    LoadFile(file.toString(), protocol, format);
+    loadFile(file.toString(), protocol, format);
   }
 
-  public void LoadFile(String file, DataSourceType protocol,
+  public void loadFile(String file, DataSourceType protocol,
           FileFormatI format)
   {
     this.file = file;
@@ -136,7 +153,7 @@ public class FileLoader implements Runnable
    */
   public void LoadFile(String file, DataSourceType protocol)
   {
-    LoadFile(file, protocol, null);
+    loadFile(file, protocol, null);
   }
 
   /**
@@ -149,7 +166,7 @@ public class FileLoader implements Runnable
   public AlignFrame LoadFileWaitTillLoaded(String file,
           DataSourceType sourceType)
   {
-    return LoadFileWaitTillLoaded(file, sourceType, null);
+    return loadFileWaitTillLoaded(file, sourceType, null);
   }
 
   /**
@@ -160,13 +177,11 @@ public class FileLoader implements Runnable
    * @param format
    * @return alignFrame constructed from file contents
    */
-  public AlignFrame LoadFileWaitTillLoaded(String file,
+  public AlignFrame loadFileWaitTillLoaded(String file,
           DataSourceType sourceType, FileFormatI format)
   {
-    this.file = file;
-    this.protocol = sourceType;
-    this.format = format;
-    return _LoadFileWaitTillLoaded();
+    setFileFields(null, null, file, sourceType, format);
+    return _loadFileWaitTillLoaded();
   }
 
   /**
@@ -177,14 +192,11 @@ public class FileLoader implements Runnable
    * @param format
    * @return alignFrame constructed from file contents
    */
-  public AlignFrame LoadFileWaitTillLoaded(File file,
+  public AlignFrame loadFileWaitTillLoaded(File file,
           DataSourceType sourceType, FileFormatI format)
   {
-    this.selectedFile = file;
-    this.file = file.getPath();
-    this.protocol = sourceType;
-    this.format = format;
-    return _LoadFileWaitTillLoaded();
+    setFileFields(null, file, null, sourceType, format);
+    return _loadFileWaitTillLoaded();
   }
 
   /**
@@ -194,15 +206,12 @@ public class FileLoader implements Runnable
    * @param format
    * @return alignFrame constructed from file contents
    */
-  public AlignFrame LoadFileWaitTillLoaded(AlignmentFileReaderI source,
+  public AlignFrame loadFileWaitTillLoaded(AlignmentFileReaderI source,
           FileFormatI format)
   {
-    this.source = source;
-
-    file = source.getInFile();
-    protocol = source.getDataSourceType();
-    this.format = format;
-    return _LoadFileWaitTillLoaded();
+    setFileFields(source, null, source.getInFile(),
+            source.getDataSourceType(), format);
+    return _loadFileWaitTillLoaded();
   }
 
   /**
@@ -211,7 +220,7 @@ public class FileLoader implements Runnable
    * 
    * @return
    */
-  protected AlignFrame _LoadFileWaitTillLoaded()
+  private AlignFrame _loadFileWaitTillLoaded()
   {
     this.run();
     return alignFrame;
@@ -278,9 +287,9 @@ public class FileLoader implements Runnable
     Runtime rt = Runtime.getRuntime();
     try
     {
-      if (Desktop.instance != null)
+      if (Desktop.getInstance() != null)
       {
-        Desktop.instance.startLoading(file);
+        Desktop.getInstance().startLoading(file);
       }
       if (format == null)
       {
@@ -302,12 +311,12 @@ public class FileLoader implements Runnable
 
       if (format == null)
       {
-        Desktop.instance.stopLoading();
+        Desktop.getInstance().stopLoading();
         System.err.println("The input file \"" + file
                 + "\" has null or unidentifiable data content!");
         if (!Jalview.isHeadlessMode())
         {
-          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+          JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
                   MessageManager.getString("label.couldnt_read_data")
                           + " in " + file + "\n"
                           + AppletFormatAdapter.getSupportedFormats(),
@@ -318,7 +327,7 @@ public class FileLoader implements Runnable
       }
       // TODO: cache any stream datasources as a temporary file (eg. PDBs
       // retrieved via URL)
-      if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
+      if (Desktop.getDesktopPane() != null && Desktop.getDesktopPane().isShowMemoryUsage())
       {
         System.gc();
         memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
@@ -338,7 +347,11 @@ public class FileLoader implements Runnable
                   "IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
           // We read the data anyway - it might make sense.
         }
-        alignFrame = new Jalview2XML(raiseGUI).loadJalviewAlign(file);
+        // BH 2018 switch to File object here instead of filename
+        Platform.timeCheck(null, Platform.TIME_MARK);
+        alignFrame = new Jalview2XML(raiseGUI).loadJalviewAlign(selectedFile == null ? file : selectedFile);
+        Platform.timeCheck("JVP loaded", Platform.TIME_MARK);
+
       }
       else
       {
@@ -360,15 +373,15 @@ public class FileLoader implements Runnable
             if (downloadStructureFile)
             {
               String structExt = format.getExtensions().split(",")[0];
-              String urlLeafName = file.substring(
-                      file.lastIndexOf(
-                              System.getProperty("file.separator")),
+              int pt = file.lastIndexOf(file.indexOf('/') >= 0 ? "/"
+                      : System.getProperty("file.separator"));
+              String urlLeafName = file.substring(pt,
                       file.lastIndexOf("."));
               String tempStructureFileStr = createNamedJvTempFile(
                       urlLeafName, structExt);
               
               // BH - switching to File object here so as to hold
-              // ._bytes array directly
+              // .秘bytes array directly
               File tempFile = new File(tempStructureFileStr);
               UrlDownloadClient.download(file, tempFile);
               
@@ -409,8 +422,7 @@ public class FileLoader implements Runnable
               {
                 // register PDB entries with desktop's structure selection
                 // manager
-                StructureSelectionManager
-                        .getStructureSelectionManager(Desktop.instance)
+                Desktop.getStructureSelectionManager()
                         .registerPDBEntry(pdbe);
               }
             }
@@ -473,7 +485,7 @@ public class FileLoader implements Runnable
               alignFrame.getViewport()
                       .applyFeaturesStyle(proxyColourScheme);
             }
-            alignFrame.statusBar.setText(MessageManager.formatMessage(
+            alignFrame.setStatus(MessageManager.formatMessage(
                     "label.successfully_loaded_file", new String[]
                     { title }));
 
@@ -484,8 +496,13 @@ public class FileLoader implements Runnable
               // status in Jalview 3
               // TODO: define 'virtual desktop' for benefit of headless scripts
               // that perform queries to find the 'current working alignment'
-              Desktop.addInternalFrame(alignFrame, title,
+              
+              
+              Dimension dim = Platform.getDimIfEmbedded(alignFrame,
                       AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+              alignFrame.setSize(dim);
+              Desktop.addInternalFrame(alignFrame, title, dim.width,
+                      dim.height);
             }
 
             try
@@ -499,23 +516,23 @@ public class FileLoader implements Runnable
         }
         else
         {
-          if (Desktop.instance != null)
+          if (Desktop.getInstance() != null)
           {
-            Desktop.instance.stopLoading();
+            Desktop.getInstance().stopLoading();
           }
 
           final String errorMessage = MessageManager.getString(
                   "label.couldnt_load_file") + " " + title + "\n" + error;
           // TODO: refactor FileLoader to be independent of Desktop / Applet GUI
           // bits ?
-          if (raiseGUI && Desktop.desktop != null)
+          if (raiseGUI && Desktop.getDesktopPane() != null)
           {
             javax.swing.SwingUtilities.invokeLater(new Runnable()
             {
               @Override
               public void run()
               {
-                JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+                JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
                         errorMessage,
                         MessageManager
                                 .getString("label.error_loading_file"),
@@ -543,7 +560,7 @@ public class FileLoader implements Runnable
           @Override
           public void run()
           {
-            JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+            JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
                     MessageManager.formatMessage(
                             "label.problems_opening_file", new String[]
                             { file }),
@@ -565,7 +582,7 @@ public class FileLoader implements Runnable
           @Override
           public void run()
           {
-            JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+            JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
                     MessageManager.formatMessage(
                             "warn.out_of_memory_loading_file", new String[]
                             { file }),
@@ -587,7 +604,7 @@ public class FileLoader implements Runnable
     // memory
     // after
     // load
-    if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
+    if (Desktop.getDesktopPane() != null && Desktop.getDesktopPane().isShowMemoryUsage())
     {
       if (alignFrame != null)
       {
@@ -609,9 +626,9 @@ public class FileLoader implements Runnable
       }
     }
     // remove the visual delay indicator
-    if (Desktop.instance != null)
+    if (Desktop.getInstance() != null)
     {
-      Desktop.instance.stopLoading();
+      Desktop.getInstance().stopLoading();
     }
 
   }
@@ -645,14 +662,16 @@ public class FileLoader implements Runnable
    * @return
    * @throws FileNotFoundException 
    */
-  @SuppressWarnings("unused")
-  public static BufferedReader getBuffereReader(Object file) throws FileNotFoundException {
+  public static BufferedReader getBufferedReader(Object file) throws FileNotFoundException {
     if (file instanceof String)
+    {
       return new BufferedReader(new FileReader((String) file));
-    
-    byte[] bytes = /** @j2sNative file._bytes || */ null;
+    }
+    byte[] bytes = Platform.getFileBytes((File) file);
     if (bytes != null)
+    {
       return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes)));
+    }
     return  new BufferedReader(new FileReader((File) file));
   }