JAL-3210 Barebones gradle/buildship/eclipse. See README
[jalview.git] / src / jalview / io / FileLoader.java
index c3afe1f..95f85e6 100755 (executable)
@@ -37,6 +37,7 @@ 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;
@@ -55,35 +56,16 @@ import javax.swing.SwingUtilities;
 
 public class FileLoader implements Runnable
 {
-  private File selectedFile;
-
   String file;
 
   DataSourceType protocol;
 
   FileFormatI format;
 
-  AlignmentFileReaderI source; // alternative specification of where data
-                               // comes from
+  AlignmentFileReaderI source = null; // alternative specification of where data
+                                      // comes
 
-  /**
-   * 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;
-  }
+  // from
 
   AlignViewport viewport;
 
@@ -95,6 +77,8 @@ public class FileLoader implements Runnable
 
   boolean raiseGUI = true;
 
+  private File selectedFile;
+
   /**
    * default constructor always raised errors in GUI dialog boxes
    */
@@ -114,7 +98,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;
@@ -122,10 +106,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;
@@ -152,7 +136,7 @@ public class FileLoader implements Runnable
    */
   public void LoadFile(String file, DataSourceType protocol)
   {
-    loadFile(file, protocol, null);
+    LoadFile(file, protocol, null);
   }
 
   /**
@@ -165,7 +149,7 @@ public class FileLoader implements Runnable
   public AlignFrame LoadFileWaitTillLoaded(String file,
           DataSourceType sourceType)
   {
-    return loadFileWaitTillLoaded(file, sourceType, null);
+    return LoadFileWaitTillLoaded(file, sourceType, null);
   }
 
   /**
@@ -176,11 +160,13 @@ 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)
   {
-    setFileFields(null, null, file, sourceType, format);
-    return _loadFileWaitTillLoaded();
+    this.file = file;
+    this.protocol = sourceType;
+    this.format = format;
+    return _LoadFileWaitTillLoaded();
   }
 
   /**
@@ -191,11 +177,14 @@ 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)
   {
-    setFileFields(null, file, null, sourceType, format);
-    return _loadFileWaitTillLoaded();
+    this.selectedFile = file;
+    this.file = file.getPath();
+    this.protocol = sourceType;
+    this.format = format;
+    return _LoadFileWaitTillLoaded();
   }
 
   /**
@@ -205,12 +194,15 @@ 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)
   {
-    setFileFields(source, null, source.getInFile(),
-            source.getDataSourceType(), format);
-    return _loadFileWaitTillLoaded();
+    this.source = source;
+
+    file = source.getInFile();
+    protocol = source.getDataSourceType();
+    this.format = format;
+    return _LoadFileWaitTillLoaded();
   }
 
   /**
@@ -219,7 +211,7 @@ public class FileLoader implements Runnable
    * 
    * @return
    */
-  private AlignFrame _loadFileWaitTillLoaded()
+  protected AlignFrame _LoadFileWaitTillLoaded()
   {
     this.run();
     return alignFrame;
@@ -286,9 +278,9 @@ public class FileLoader implements Runnable
     Runtime rt = Runtime.getRuntime();
     try
     {
-      if (Desktop.getInstance() != null)
+      if (Desktop.instance != null)
       {
-        Desktop.getInstance().startLoading(file);
+        Desktop.instance.startLoading(file);
       }
       if (format == null)
       {
@@ -310,12 +302,12 @@ public class FileLoader implements Runnable
 
       if (format == null)
       {
-        Desktop.getInstance().stopLoading();
+        Desktop.instance.stopLoading();
         System.err.println("The input file \"" + file
                 + "\" has null or unidentifiable data content!");
         if (!Jalview.isHeadlessMode())
         {
-          JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
+          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
                   MessageManager.getString("label.couldnt_read_data")
                           + " in " + file + "\n"
                           + AppletFormatAdapter.getSupportedFormats(),
@@ -326,7 +318,7 @@ public class FileLoader implements Runnable
       }
       // TODO: cache any stream datasources as a temporary file (eg. PDBs
       // retrieved via URL)
-      if (Desktop.getDesktopPane() != null && Desktop.getDesktopPane().isShowMemoryUsage())
+      if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
       {
         System.gc();
         memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
@@ -347,10 +339,7 @@ public class FileLoader implements Runnable
           // We read the data anyway - it might make sense.
         }
         // 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
       {
@@ -372,15 +361,15 @@ public class FileLoader implements Runnable
             if (downloadStructureFile)
             {
               String structExt = format.getExtensions().split(",")[0];
-              int pt = file.lastIndexOf(file.indexOf('/') >= 0 ? "/"
-                      : System.getProperty("file.separator"));
-              String urlLeafName = file.substring(pt,
+              String urlLeafName = file.substring(
+                      file.lastIndexOf(
+                              System.getProperty("file.separator")),
                       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);
               
@@ -421,7 +410,8 @@ public class FileLoader implements Runnable
               {
                 // register PDB entries with desktop's structure selection
                 // manager
-                Desktop.getInstance().getStructureSelectionManager()
+                StructureSelectionManager
+                        .getStructureSelectionManager(Desktop.instance)
                         .registerPDBEntry(pdbe);
               }
             }
@@ -510,23 +500,23 @@ public class FileLoader implements Runnable
         }
         else
         {
-          if (Desktop.getInstance() != null)
+          if (Desktop.instance != null)
           {
-            Desktop.getInstance().stopLoading();
+            Desktop.instance.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.getDesktopPane() != null)
+          if (raiseGUI && Desktop.desktop != null)
           {
             javax.swing.SwingUtilities.invokeLater(new Runnable()
             {
               @Override
               public void run()
               {
-                JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
+                JvOptionPane.showInternalMessageDialog(Desktop.desktop,
                         errorMessage,
                         MessageManager
                                 .getString("label.error_loading_file"),
@@ -554,7 +544,7 @@ public class FileLoader implements Runnable
           @Override
           public void run()
           {
-            JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop,
                     MessageManager.formatMessage(
                             "label.problems_opening_file", new String[]
                             { file }),
@@ -576,7 +566,7 @@ public class FileLoader implements Runnable
           @Override
           public void run()
           {
-            JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
+            JvOptionPane.showInternalMessageDialog(Desktop.desktop,
                     MessageManager.formatMessage(
                             "warn.out_of_memory_loading_file", new String[]
                             { file }),
@@ -598,7 +588,7 @@ public class FileLoader implements Runnable
     // memory
     // after
     // load
-    if (Desktop.getDesktopPane() != null && Desktop.getDesktopPane().isShowMemoryUsage())
+    if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
     {
       if (alignFrame != null)
       {
@@ -620,9 +610,9 @@ public class FileLoader implements Runnable
       }
     }
     // remove the visual delay indicator
-    if (Desktop.getInstance() != null)
+    if (Desktop.instance != null)
     {
-      Desktop.getInstance().stopLoading();
+      Desktop.instance.stopLoading();
     }
 
   }