JAL-1551 formatting
[jalview.git] / src / jalview / gui / AlignFrame.java
index 26c5548..6911d86 100644 (file)
@@ -88,6 +88,7 @@ import jalview.schemes.ColourSchemes;
 import jalview.schemes.ResidueColourScheme;
 import jalview.schemes.TCoffeeColourScheme;
 import jalview.util.MessageManager;
+import jalview.util.dialogrunner.RunResponse;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.ViewportRanges;
 import jalview.ws.DBRefFetcher;
@@ -181,6 +182,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    */
   String fileName = null;
 
+  File fileObject;
+
   /**
    * Creates a new AlignFrame object with specific width and height.
    * 
@@ -334,6 +337,28 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
+   * BH 2018
+   * 
+   * @return true if we have any features
+   */
+  @Override
+  protected boolean haveAlignmentFeatures()
+  {
+    AlignmentI alignment = getViewport().getAlignment();
+
+    for (int i = 0; i < alignment.getHeight(); i++)
+    {
+      SequenceI seq = alignment.getSequenceAt(i);
+      for (String group : seq.getFeatures().getFeatureGroups(true))
+      {
+        if (group != null)
+          return true;
+      }
+    }
+    return false;
+  }
+
+  /**
    * initalise the alignframe from the underlying viewport data and the
    * configurations
    */
@@ -394,7 +419,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
        * 
        */
       {
-      addServiceListeners();
+        addServiceListeners();
       }
       setGUINucleotide();
     }
@@ -514,6 +539,17 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
+   * JavaScript will have this, maybe others. More dependable than a file name
+   * and maintains a reference to the actual bytes loaded.
+   * 
+   * @param file
+   */
+  public void setFileObject(File file)
+  {
+    this.fileObject = file;
+  }
+
+  /**
    * Add a KeyListener with handlers for various KeyPressed and KeyReleased
    * events
    */
@@ -1052,11 +1088,23 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         Rectangle bounds = this.getBounds();
 
         FileLoader loader = new FileLoader();
-        DataSourceType protocol = fileName.startsWith("http:")
-                ? DataSourceType.URL
-                : DataSourceType.FILE;
-        AlignFrame newframe = loader.LoadFileWaitTillLoaded(fileName,
-                protocol, currentFileFormat);
+
+        AlignFrame newframe = null;
+
+        if (fileObject == null)
+        {
+
+          DataSourceType protocol = (fileName.startsWith("http:")
+                  ? DataSourceType.URL
+                  : DataSourceType.FILE);
+          newframe = loader.LoadFileWaitTillLoaded(fileName, protocol,
+                  currentFileFormat);
+        }
+        else
+        {
+          newframe = loader.LoadFileWaitTillLoaded(fileObject,
+                  DataSourceType.FILE, currentFileFormat);
+        }
 
         newframe.setBounds(bounds);
         if (featureSettings != null && featureSettings.isShowing())
@@ -1120,42 +1168,41 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   {
     String format = currentFileFormat == null ? null
             : currentFileFormat.getName();
-    JalviewFileChooser chooser = JalviewFileChooser
+    final JalviewFileChooser chooser = JalviewFileChooser
             .forWrite(Cache.getProperty("LAST_DIRECTORY"), format);
-
+    final AlignFrame us = this;
     chooser.setFileView(new JalviewFileView());
     chooser.setDialogTitle(
             MessageManager.getString("label.save_alignment_to_file"));
     chooser.setToolTipText(MessageManager.getString("action.save"));
 
-    int value = chooser.showSaveDialog(this);
-
-
-    if (value == JalviewFileChooser.APPROVE_OPTION)
+    chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION)
     {
-      currentFileFormat = chooser.getSelectedFormat();
-      while (currentFileFormat == null)
+      @Override
+      public void run()
       {
-        JvOptionPane.showInternalMessageDialog(Desktop.desktop,
-                MessageManager.getString(
-                        "label.select_file_format_before_saving"),
-                MessageManager.getString("label.file_format_not_specified"),
-                JvOptionPane.WARNING_MESSAGE);
         currentFileFormat = chooser.getSelectedFormat();
-        value = chooser.showSaveDialog(this);
-        if (value != JalviewFileChooser.APPROVE_OPTION)
+        while (currentFileFormat == null)
         {
-          return;
+          JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+                  MessageManager.getString(
+                          "label.select_file_format_before_saving"),
+                  MessageManager
+                          .getString("label.file_format_not_specified"),
+                  JvOptionPane.WARNING_MESSAGE);
+          currentFileFormat = chooser.getSelectedFormat();
+          chooser.showSaveDialog(us);
         }
-      }
 
-      fileName = chooser.getSelectedFile().getPath();
+        fileName = chooser.getSelectedFile().getPath();
 
-      Cache.setProperty("DEFAULT_FILE_FORMAT", currentFileFormat.getName());
+        Cache.setProperty("DEFAULT_FILE_FORMAT",
+                currentFileFormat.getName());
 
-      Cache.setProperty("LAST_DIRECTORY", fileName);
-      saveAlignment(fileName, currentFileFormat);
-    }
+        Cache.setProperty("LAST_DIRECTORY", fileName);
+        saveAlignment(fileName, currentFileFormat);
+      }
+    }).showSaveDialog(this);
   }
 
   public boolean saveAlignment(String file, FileFormatI format)
@@ -1234,21 +1281,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     return success;
   }
 
-  private void warningMessage(String warning, String title)
-  {
-    if (new jalview.util.Platform().isHeadless())
-    {
-      System.err.println("Warning: " + title + "\nWarning: " + warning);
-
-    }
-    else
-    {
-      JvOptionPane.showInternalMessageDialog(this, warning, title,
-              JvOptionPane.WARNING_MESSAGE);
-    }
-    return;
-  }
-
   /**
    * DOCUMENT ME!
    * 
@@ -1422,40 +1454,27 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public void associatedData_actionPerformed(ActionEvent e)
   {
-    // Pick the tree file
-    JalviewFileChooser chooser = new JalviewFileChooser(
+    final JalviewFileChooser chooser = new JalviewFileChooser(
             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
     chooser.setFileView(new JalviewFileView());
     chooser.setDialogTitle(
             MessageManager.getString("label.load_jalview_annotations"));
     chooser.setToolTipText(
             MessageManager.getString("label.load_jalview_annotations"));
+    chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION)
+    {
 
-    Desktop.getDesktop().dialogData = new Object[] { "SelectedFile",
-        new Runnable()
-        {
-
-          @Override
-          public void run()
-          {
-            Object[] data = Desktop.getDesktop().dialogData;
-            int value = ((Integer) data[0]).intValue();
-
-            if (value == JFileChooser.APPROVE_OPTION)
-            {
-              JalviewFileChooser chooser = (JalviewFileChooser) data[2];
-              String choice = chooser.getSelectedFile().getPath();
-              jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
-              loadJalviewDataFile(choice, null, null, null);
-            }
-          }
-
-        }, chooser };
-
-    chooser.showOpenDialog(null);
-
+      @Override
+      public void run()
+      {
+        String choice = chooser.getSelectedFile().getPath();
+        jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
+        loadJalviewDataFile(chooser.getSelectedFile(), null, null, null);
+      }
 
+    });
 
+    chooser.openDialog(this);
   }
 
   /**
@@ -3913,33 +3932,38 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     chooser.setToolTipText(
             MessageManager.getString("label.load_tree_file"));
 
-    int value = chooser.showOpenDialog(null);
-
-    if (value == JalviewFileChooser.APPROVE_OPTION)
+    chooser.response(new jalview.util.dialogrunner.RunResponse(
+            JalviewFileChooser.APPROVE_OPTION)
     {
-      String filePath = chooser.getSelectedFile().getPath();
-      Cache.setProperty("LAST_DIRECTORY", filePath);
-      NewickFile fin = null;
-      try
-      {
-        fin = new NewickFile(filePath, DataSourceType.FILE);
-        viewport.setCurrentTree(showNewickTree(fin, filePath).getTree());
-      } catch (Exception ex)
-      {
-        JvOptionPane.showMessageDialog(Desktop.desktop, ex.getMessage(),
-                MessageManager.getString("label.problem_reading_tree_file"),
-                JvOptionPane.WARNING_MESSAGE);
-        ex.printStackTrace();
-      }
-      if (fin != null && fin.hasWarningMessage())
+      @Override
+      public void run()
       {
-        JvOptionPane.showMessageDialog(Desktop.desktop,
-                fin.getWarningMessage(),
-                MessageManager
-                        .getString("label.possible_problem_with_tree_file"),
-                JvOptionPane.WARNING_MESSAGE);
+        String filePath = chooser.getSelectedFile().getPath();
+        Cache.setProperty("LAST_DIRECTORY", filePath);
+        NewickFile fin = null;
+        try
+        {
+          fin = new NewickFile(new FileParse(chooser.getSelectedFile(),
+                  DataSourceType.FILE));
+          viewport.setCurrentTree(showNewickTree(fin, filePath).getTree());
+        } catch (Exception ex)
+        {
+          JvOptionPane.showMessageDialog(Desktop.desktop, ex.getMessage(),
+                  MessageManager
+                          .getString("label.problem_reading_tree_file"),
+                  JvOptionPane.WARNING_MESSAGE);
+          ex.printStackTrace();
+        }
+        if (fin != null && fin.hasWarningMessage())
+        {
+          JvOptionPane.showMessageDialog(Desktop.desktop,
+                  fin.getWarningMessage(),
+                  MessageManager.getString(
+                          "label.possible_problem_with_tree_file"),
+                  JvOptionPane.WARNING_MESSAGE);
+        }
       }
-    }
+    }).openDialog(this);
   }
 
   public TreePanel showNewickTree(NewickFile nf, String treeTitle)
@@ -4359,13 +4383,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * Try to load a features file onto the alignment.
    * 
    * @param file
-   *          contents or path to retrieve file
+   *          contents or path to retrieve file or a File object
    * @param sourceType
    *          access mode of file (see jalview.io.AlignFile)
    * @return true if features file was parsed correctly.
    */
-  public boolean parseFeaturesFile(String file, DataSourceType sourceType)
+  public boolean parseFeaturesFile(Object file, DataSourceType sourceType)
   {
+    // BH 2018
     return avc.parseFeaturesFile(file, sourceType,
             Cache.getDefault("RELAXEDSEQIDMATCHING", false));
 
@@ -4412,8 +4437,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     // Java's Transferable for native dnd
     evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
     Transferable t = evt.getTransferable();
+
     final AlignFrame thisaf = this;
-    final List<String> files = new ArrayList<>();
+    final List<Object> files = new ArrayList<>();
     List<DataSourceType> protocols = new ArrayList<>();
 
     try
@@ -4441,20 +4467,25 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
              * Object[] { String,SequenceI}
              */
             ArrayList<Object[]> filesmatched = new ArrayList<>();
-            ArrayList<String> filesnotmatched = new ArrayList<>();
+            ArrayList<Object> filesnotmatched = new ArrayList<>();
             for (int i = 0; i < files.size(); i++)
             {
-              String file = files.get(i).toString();
+              // BH 2018
+              Object file = files.get(i);
+              String fileName = file.toString();
               String pdbfn = "";
-              DataSourceType protocol = FormatAdapter.checkProtocol(file);
+              DataSourceType protocol = (file instanceof File
+                      ? DataSourceType.FILE
+                      : FormatAdapter.checkProtocol(fileName));
               if (protocol == DataSourceType.FILE)
               {
-                File fl = new File(file);
+                File fl = (file instanceof File ? (File) file
+                        : new File(fileName));
                 pdbfn = fl.getName();
               }
               else if (protocol == DataSourceType.URL)
               {
-                URL url = new URL(file);
+                URL url = new URL(fileName);
                 pdbfn = url.getFile();
               }
               if (pdbfn.length() > 0)
@@ -4476,7 +4507,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                 }
                 if (mtch != null)
                 {
-                  FileFormatI type = null;
+                  FileFormatI type;
                   try
                   {
                     type = new IdentifyFile().identify(file, protocol);
@@ -4498,7 +4529,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
             int assocfiles = 0;
             if (filesmatched.size() > 0)
             {
-              boolean autoAssociate = Cache.getDefault("AUTOASSOCIATE_PDBANDSEQS", false);
+              boolean autoAssociate = Cache
+                      .getDefault("AUTOASSOCIATE_PDBANDSEQS", false);
               if (!autoAssociate)
               {
                 String msg = MessageManager.formatMessage(
@@ -4545,7 +4577,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                  */
                 for (Object[] o : filesmatched)
                 {
-                  filesnotmatched.add((String) o[0]);
+                  filesnotmatched.add(o[0]);
                 }
               }
             }
@@ -4567,7 +4599,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
               {
                 return;
               }
-              for (String fn : filesnotmatched)
+              for (Object fn : filesnotmatched)
               {
                 loadJalviewDataFile(fn, null, null, null);
               }
@@ -4594,9 +4626,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * @param file
    *          either a filename or a URL string.
    */
-  public void loadJalviewDataFile(String file, DataSourceType sourceType,
+  public void loadJalviewDataFile(Object file, DataSourceType sourceType,
           FileFormatI format, SequenceI assocSeq)
   {
+    // BH 2018 was String file
     try
     {
       if (sourceType == null)
@@ -5632,16 +5665,22 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     chooser.setFileView(new JalviewFileView());
     chooser.setDialogTitle(MessageManager.getString("label.load_vcf_file"));
     chooser.setToolTipText(MessageManager.getString("label.load_vcf_file"));
+    final AlignFrame us = this;
+    chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION)
+    {
+      @Override
+      public void run()
+      {
 
-    int value = chooser.showOpenDialog(null);
+        {
+          String choice = chooser.getSelectedFile().getPath();
+          Cache.setProperty("LAST_DIRECTORY", choice);
+          SequenceI[] seqs = viewport.getAlignment().getSequencesArray();
+          new VCFLoader(choice).loadVCF(seqs, us);
+        }
 
-    if (value == JalviewFileChooser.APPROVE_OPTION)
-    {
-      String choice = chooser.getSelectedFile().getPath();
-      Cache.setProperty("LAST_DIRECTORY", choice);
-      SequenceI[] seqs = viewport.getAlignment().getSequencesArray();
-      new VCFLoader(choice).loadVCF(seqs, this);
-    }
+      };
+    }).openDialog(null);
 
   }