JAL-2476 ability to drag and drop score matrix file to desktop, simplify
[jalview.git] / src / jalview / io / FileLoader.java
index 4f83ab1..e41403c 100755 (executable)
@@ -35,6 +35,7 @@ import jalview.gui.AlignViewport;
 import jalview.gui.Desktop;
 import jalview.gui.Jalview2XML;
 import jalview.gui.JvOptionPane;
+import jalview.gui.JvSwingUtils;
 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
 import jalview.schemes.ColourSchemeI;
 import jalview.structure.StructureSelectionManager;
@@ -91,18 +92,12 @@ public class FileLoader implements Runnable
           DataSourceType protocol, FileFormatI format)
   {
     this.viewport = viewport;
-    LoadFile(file, protocol, format);
-  }
-
-  public void LoadFile(String file, DataSourceType protocol,
-          FileFormatI format)
-  {
     this.file = file;
     this.protocol = protocol;
     this.format = format;
-
+    
     final Thread loader = new Thread(this);
-
+    
     SwingUtilities.invokeLater(new Runnable()
     {
       @Override
@@ -114,17 +109,6 @@ public class FileLoader implements Runnable
   }
 
   /**
-   * Load a (file, protocol) source of unknown type
-   * 
-   * @param file
-   * @param protocol
-   */
-  public void LoadFile(String file, DataSourceType protocol)
-  {
-    LoadFile(file, protocol, null);
-  }
-
-  /**
    * Load alignment from (file, protocol) and wait till loaded
    * 
    * @param file
@@ -155,24 +139,6 @@ public class FileLoader implements Runnable
   }
 
   /**
-   * Load alignment from FileParse source of type format and wait till loaded
-   * 
-   * @param source
-   * @param format
-   * @return alignFrame constructed from file contents
-   */
-  public AlignFrame LoadFileWaitTillLoaded(AlignmentFileReaderI source,
-          FileFormatI format)
-  {
-    this.source = source;
-
-    file = source.getInFile();
-    protocol = source.getDataSourceType();
-    this.format = format;
-    return _LoadFileWaitTillLoaded();
-  }
-
-  /**
    * start thread and wait until finished, then return the alignFrame that's
    * (hopefully) been read.
    * 
@@ -245,7 +211,7 @@ public class FileLoader implements Runnable
   @Override
   public void run()
   {
-    String title = protocol == DataSourceType.PASTE ? "Copied From Clipboard"
+    final String title = protocol == DataSourceType.PASTE ? "Copied From Clipboard"
             : file;
     Runtime rt = Runtime.getRuntime();
     try
@@ -291,10 +257,8 @@ public class FileLoader implements Runnable
       if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
       {
         System.gc();
-        memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
-        // memory
-        // before
-        // load
+        // free memory before loading file
+        memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory());
       }
       loadtime = -System.currentTimeMillis();
       AlignmentI al = null;
@@ -334,7 +298,22 @@ public class FileLoader implements Runnable
           error = ex.getMessage();
         }
 
-        if ((al != null) && (al.getHeight() > 0) && al.hasValidSequence())
+        if (!format.hasSequenceData())
+        {
+          javax.swing.SwingUtilities.invokeLater(new Runnable()
+          {
+            @Override
+            public void run()
+            {
+              String msg = MessageManager.formatMessage("label.successfully_loaded_file_type", format.getName(), title);
+              msg = JvSwingUtils.wrapTooltip(true, msg);
+              JvOptionPane.showInternalMessageDialog(Desktop.desktop, msg,
+                      "", JvOptionPane.INFORMATION_MESSAGE);
+            }
+          });
+        }
+        else if ((al != null) && (al.getHeight() > 0)
+                && al.hasValidSequence())
         {
           // construct and register dataset sequences
           for (SequenceI sq : al.getSequences())
@@ -347,8 +326,6 @@ public class FileLoader implements Runnable
             {
               for (PDBEntry pdbe : sq.getAllPDBEntries())
               {
-                // register PDB entries with desktop's structure selection
-                // manager
                 StructureSelectionManager.getStructureSelectionManager(
                         Desktop.instance).registerPDBEntry(pdbe);
               }
@@ -368,71 +345,7 @@ public class FileLoader implements Runnable
           }
           else
           {
-            // otherwise construct the alignFrame
-
-            if (source instanceof ComplexAlignFile)
-            {
-              ColumnSelection colSel = ((ComplexAlignFile) source)
-                      .getColumnSelection();
-              SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
-                      .getHiddenSequences();
-              String colourSchemeName = ((ComplexAlignFile) source)
-                      .getGlobalColourScheme();
-              FeaturesDisplayedI fd = ((ComplexAlignFile) source)
-                      .getDisplayedFeatures();
-              alignFrame = new AlignFrame(al, hiddenSeqs, colSel,
-                      AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
-              alignFrame.getViewport().setFeaturesDisplayed(fd);
-              alignFrame.getViewport().setShowSequenceFeatures(
-                      ((ComplexAlignFile) source).isShowSeqFeatures());
-              ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
-                      colourSchemeName, al);
-              if (cs != null)
-              {
-                alignFrame.changeColour(cs);
-              }
-            }
-            else
-            {
-              alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
-                      AlignFrame.DEFAULT_HEIGHT);
-              if (source instanceof FeaturesSourceI)
-              {
-                alignFrame.getViewport().setShowSequenceFeatures(true);
-              }
-            }
-            // add metadata and update ui
-            if (!(protocol == DataSourceType.PASTE))
-            {
-              alignFrame.setFileName(file, format);
-            }
-            if (proxyColourScheme != null)
-            {
-              alignFrame.getViewport()
-                      .applyFeaturesStyle(proxyColourScheme);
-            }
-            alignFrame.statusBar.setText(MessageManager.formatMessage(
-                    "label.successfully_loaded_file",
-                    new String[] { title }));
-
-            if (raiseGUI)
-            {
-              // add the window to the GUI
-              // note - this actually should happen regardless of raiseGUI
-              // 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,
-                      AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
-            }
-
-            try
-            {
-              alignFrame.setMaximum(jalview.bin.Cache.getDefault(
-                      "SHOW_FULLSCREEN", false));
-            } catch (java.beans.PropertyVetoException ex)
-            {
-            }
+            addAlignFrame(al, title);
           }
         }
         else
@@ -449,7 +362,8 @@ public class FileLoader implements Runnable
                   + "\n" + error;
           // TODO: refactor FileLoader to be independent of Desktop / Applet GUI
           // bits ?
-          if (raiseGUI && Desktop.desktop != null)
+          if (format.hasSequenceData() && raiseGUI
+                  && Desktop.desktop != null)
           {
             javax.swing.SwingUtilities.invokeLater(new Runnable()
             {
@@ -556,18 +470,81 @@ public class FileLoader implements Runnable
 
   }
 
-  /*
-   * (non-Javadoc)
+  /**
+   * Helper method that adds the alignment as a new frame on the Desktop
    * 
-   * @see java.lang.Object#finalize()
+   * @param al
+   * @param title
    */
-  @Override
-  protected void finalize() throws Throwable
+  protected void addAlignFrame(AlignmentI al, String title)
   {
-    source = null;
-    alignFrame = null;
-    viewport = null;
-    super.finalize();
+    // otherwise construct the alignFrame
+
+    if (source instanceof ComplexAlignFile)
+    {
+      ColumnSelection colSel = ((ComplexAlignFile) source)
+              .getColumnSelection();
+      SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
+              .getHiddenSequences();
+      String colourSchemeName = ((ComplexAlignFile) source)
+              .getGlobalColourScheme();
+      FeaturesDisplayedI fd = ((ComplexAlignFile) source)
+              .getDisplayedFeatures();
+      alignFrame = new AlignFrame(al, hiddenSeqs, colSel,
+              AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+      alignFrame.getViewport().setFeaturesDisplayed(fd);
+      alignFrame.getViewport().setShowSequenceFeatures(
+              ((ComplexAlignFile) source).isShowSeqFeatures());
+      ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
+              colourSchemeName, al);
+      if (cs != null)
+      {
+        alignFrame.changeColour(cs);
+      }
+    }
+    else
+    {
+      alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
+              AlignFrame.DEFAULT_HEIGHT);
+      if (source instanceof FeaturesSourceI)
+      {
+        alignFrame.getViewport().setShowSequenceFeatures(true);
+      }
+    }
+    // add metadata and update ui
+    if (!(protocol == DataSourceType.PASTE))
+    {
+      alignFrame.setFileName(file, format);
+    }
+    FeatureSettingsModelI proxyColourScheme = source
+            .getFeatureColourScheme();
+    if (proxyColourScheme != null)
+    {
+      alignFrame.getViewport()
+              .applyFeaturesStyle(proxyColourScheme);
+    }
+    alignFrame.statusBar.setText(MessageManager.formatMessage(
+            "label.successfully_loaded_file",
+            new String[] { title }));
+
+    if (raiseGUI)
+    {
+      // add the window to the GUI
+      // note - this actually should happen regardless of raiseGUI
+      // 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,
+              AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+    }
+
+    try
+    {
+      alignFrame.setMaximum(jalview.bin.Cache.getDefault(
+              "SHOW_FULLSCREEN", false));
+    } catch (java.beans.PropertyVetoException ex)
+    {
+    }
   }
 
 }