JAL-3026 major update 1/2
[jalview.git] / src / jalview / gui / AlignFrame.java
index e4a3f8c..fb952b7 100644 (file)
@@ -26,7 +26,7 @@ import jalview.analysis.CrossRef;
 import jalview.analysis.Dna;
 import jalview.analysis.ParseProperties;
 import jalview.analysis.SequenceIdMatcher;
-import jalview.api.AlignExportSettingI;
+import jalview.api.AlignExportSettingsI;
 import jalview.api.AlignViewControllerGuiI;
 import jalview.api.AlignViewControllerI;
 import jalview.api.AlignViewportI;
@@ -45,6 +45,7 @@ import jalview.commands.RemoveGapColCommand;
 import jalview.commands.RemoveGapsCommand;
 import jalview.commands.SlideSequencesCommand;
 import jalview.commands.TrimRegionCommand;
+import jalview.datamodel.AlignExportSettingsAdapter;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
@@ -54,7 +55,6 @@ import jalview.datamodel.AlignmentOrder;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.HiddenColumns;
-import jalview.datamodel.HiddenSequences;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SeqCigar;
 import jalview.datamodel.Sequence;
@@ -87,6 +87,7 @@ import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.ResidueColourScheme;
 import jalview.schemes.TCoffeeColourScheme;
+import jalview.util.ImageMaker.TYPE;
 import jalview.util.MessageManager;
 import jalview.util.dialogrunner.RunResponse;
 import jalview.viewmodel.AlignmentViewport;
@@ -99,6 +100,7 @@ import jalview.ws.jws2.jabaws2.Jws2Instance;
 import jalview.ws.seqfetcher.DbSourceProxy;
 
 import java.awt.BorderLayout;
+import java.awt.Color;
 import java.awt.Component;
 import java.awt.Rectangle;
 import java.awt.Toolkit;
@@ -137,12 +139,14 @@ import java.util.List;
 import java.util.Vector;
 
 import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JComponent;
 import javax.swing.JEditorPane;
-import javax.swing.JFileChooser;
 import javax.swing.JInternalFrame;
+import javax.swing.JLabel;
 import javax.swing.JLayeredPane;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
+import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.SwingUtilities;
 
@@ -182,6 +186,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    */
   String fileName = null;
 
+  File fileObject;
+
   /**
    * Creates a new AlignFrame object with specific width and height.
    * 
@@ -335,6 +341,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
    */
@@ -395,7 +423,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
        * 
        */
       {
-      addServiceListeners();
+        addServiceListeners();
       }
       setGUINucleotide();
     }
@@ -515,6 +543,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
    */
@@ -1053,11 +1092,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())
@@ -1102,7 +1153,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     if (fileName == null || (currentFileFormat == null)
             || fileName.startsWith("http"))
     {
-      saveAs_actionPerformed(null);
+      saveAs_actionPerformed();
     }
     else
     {
@@ -1111,226 +1162,235 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * DOCUMENT ME!
-   * 
-   * @param e
-   *          DOCUMENT ME!
+   * Saves the alignment to a file with a name chosen by the user, if necessary
+   * warning if a file would be overwritten
    */
   @Override
-  public void saveAs_actionPerformed(ActionEvent e)
+  public void saveAs_actionPerformed()
   {
-    // TODO: JAL-3048 JalviewFileChooser - Save option
-
     String format = currentFileFormat == null ? null
             : currentFileFormat.getName();
-    final JalviewFileChooser chooser = JalviewFileChooser
+    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"));
 
-    chooser.response(new RunResponse(JalviewFileChooser.APPROVE_OPTION)
-    {
-      @Override
-      public void run()
-        {
-        currentFileFormat = chooser.getSelectedFormat();
-        while (currentFileFormat == null)
-        {
-          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);
-        }
+    int value = chooser.showSaveDialog(this);
 
-        fileName = chooser.getSelectedFile().getPath();
+    if (value != JalviewFileChooser.APPROVE_OPTION)
+    {
+      return;
+    }
+    currentFileFormat = chooser.getSelectedFormat();
+    // todo is this (2005) test now obsolete - value is never null?
+    while (currentFileFormat == null)
+    {
+      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)
+      {
+        return;
+      }
+    }
 
-        Cache.setProperty("DEFAULT_FILE_FORMAT",
-                currentFileFormat.getName());
+    fileName = chooser.getSelectedFile().getPath();
 
-        Cache.setProperty("LAST_DIRECTORY", fileName);
-        saveAlignment(fileName, currentFileFormat);
-      }
-    }).showSaveDialog(this);
+    Cache.setProperty("DEFAULT_FILE_FORMAT", currentFileFormat.getName());
+    Cache.setProperty("LAST_DIRECTORY", fileName);
+    saveAlignment(fileName, currentFileFormat);
   }
 
-  public boolean saveAlignment(String file, FileFormatI format)
-  {
-    boolean success = true;
+  boolean lastSaveSuccessful = false;
 
-    if (FileFormat.Jalview.equals(format))
-    {
-      String shortName = title;
+  FileFormatI lastFormatSaved;
 
-      if (shortName.indexOf(java.io.File.separatorChar) > -1)
-      {
-        shortName = shortName.substring(
-                shortName.lastIndexOf(java.io.File.separatorChar) + 1);
-      }
+  String lastFilenameSaved;
+
+  /**
+   * Raise a dialog or status message for the last call to saveAlignment.
+   *
+   * @return true if last call to saveAlignment(file, format) was successful.
+   */
+  public boolean isSaveAlignmentSuccessful()
+  {
 
-      success = new Jalview2XML().saveAlignment(this, file, shortName);
+    if (!lastSaveSuccessful)
+    {
+      JvOptionPane.showInternalMessageDialog(this, MessageManager
+              .formatMessage("label.couldnt_save_file", new Object[]
+              { lastFilenameSaved }),
+              MessageManager.getString("label.error_saving_file"),
+              JvOptionPane.WARNING_MESSAGE);
+    }
+    else
+    {
 
       statusBar.setText(MessageManager.formatMessage(
               "label.successfully_saved_to_file_in_format", new Object[]
-              { fileName, format }));
+              { lastFilenameSaved, lastFormatSaved }));
 
     }
-    else
+    return lastSaveSuccessful;
+  }
+
+  /**
+   * Saves the alignment to the specified file path, in the specified format,
+   * which may be an alignment format, or Jalview project format. If the
+   * alignment has hidden regions, or the format is one capable of including
+   * non-sequence data (features, annotations, groups), then the user may be
+   * prompted to specify what to include in the output.
+   * 
+   * @param file
+   * @param format
+   */
+  public void saveAlignment(String file, FileFormatI format)
+  {
+    lastSaveSuccessful = false;
+    lastFilenameSaved = file;
+    lastFormatSaved = format;
+
+    if (FileFormat.Jalview.equals(format))
     {
-      AlignmentExportData exportData = getAlignmentForExport(format,
-              viewport, null);
-      if (exportData.getSettings().isCancelled())
+      String shortName = title;
+      if (shortName.indexOf(File.separatorChar) > -1)
       {
-        return false;
+        shortName = shortName.substring(
+                shortName.lastIndexOf(File.separatorChar) + 1);
       }
-      FormatAdapter f = new FormatAdapter(alignPanel,
-              exportData.getSettings());
-      String output = f.formatSequences(format, exportData.getAlignment(), // class
-                                                                           // cast
-                                                                           // exceptions
-                                                                           // will
-              // occur in the distant future
-              exportData.getOmitHidden(), exportData.getStartEndPostions(),
-              f.getCacheSuffixDefault(format),
-              viewport.getAlignment().getHiddenColumns());
-
-      if (output == null)
+      lastSaveSuccessful = new Jalview2XML().saveAlignment(this, file,
+              shortName);
+      return;
+    }
+
+    AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
+    RunResponse cancelAction = new RunResponse(JvOptionPane.CANCEL_OPTION)
+    {
+      @Override
+      public void run()
       {
-        success = false;
+        lastSaveSuccessful = false;
       }
-      else
+    };
+    RunResponse outputAction = new RunResponse(JvOptionPane.OK_OPTION)
+    {
+      @Override
+      public void run()
       {
-        try
+        // todo defer this to inside formatSequences (or later)
+        AlignmentExportData exportData = viewport
+                .getAlignExportData(options);
+        String output = new FormatAdapter(alignPanel, options)
+                .formatSequences(format, exportData.getAlignment(),
+                        exportData.getOmitHidden(),
+                        exportData.getStartEndPostions(),
+                        viewport.getAlignment().getHiddenColumns());
+        if (output == null)
         {
-          PrintWriter out = new PrintWriter(new FileWriter(file));
-
-          out.print(output);
-          out.close();
-          this.setTitle(file);
-          statusBar.setText(MessageManager.formatMessage(
-                  "label.successfully_saved_to_file_in_format", new Object[]
-                  { fileName, format.getName() }));
-        } catch (Exception ex)
+          lastSaveSuccessful = false;
+        }
+        else
         {
-          success = false;
-          ex.printStackTrace();
+          try
+          {
+            PrintWriter out = new PrintWriter(new FileWriter(file));
+            out.print(output);
+            out.close();
+            AlignFrame.this.setTitle(file);
+            statusBar.setText(MessageManager.formatMessage(
+                    "label.successfully_saved_to_file_in_format",
+                    new Object[]
+                    { fileName, format.getName() }));
+          } catch (Exception ex)
+          {
+            lastSaveSuccessful = false;
+            ex.printStackTrace();
+          }
         }
       }
-    }
+    };
 
-    if (!success)
-    {
-      JvOptionPane.showInternalMessageDialog(this, MessageManager
-              .formatMessage("label.couldnt_save_file", new Object[]
-              { fileName }),
-              MessageManager.getString("label.error_saving_file"),
-              JvOptionPane.WARNING_MESSAGE);
-    }
-
-    return success;
-  }
-
-  private void warningMessage(String warning, String title)
-  {
-    if (new jalview.util.Platform().isHeadless())
+    /*
+     * show dialog with export options if applicable; else just do it
+     */
+    if (AlignExportOptions.isNeeded(viewport, format))
     {
-      System.err.println("Warning: " + title + "\nWarning: " + warning);
-
+      AlignExportOptions choices = new AlignExportOptions(
+              alignPanel.getAlignViewport(), format, options);
+      choices.setResponseAction(outputAction);
+      choices.setResponseAction(cancelAction);
+      choices.showDialog();
     }
     else
     {
-      JvOptionPane.showInternalMessageDialog(this, warning, title,
-              JvOptionPane.WARNING_MESSAGE);
+      outputAction.run();
     }
-    return;
   }
 
   /**
-   * DOCUMENT ME!
+   * Outputs the alignment to textbox in the requested format, if necessary
+   * first prompting the user for whether to include hidden regions or
+   * non-sequence data
    * 
-   * @param e
-   *          DOCUMENT ME!
+   * @param fileFormatName
    */
   @Override
-  protected void outputText_actionPerformed(ActionEvent e)
+  protected void outputText_actionPerformed(String fileFormatName)
   {
     FileFormatI fileFormat = FileFormats.getInstance()
-            .forName(e.getActionCommand());
-    AlignmentExportData exportData = getAlignmentForExport(fileFormat,
-            viewport, null);
-    if (exportData.getSettings().isCancelled())
-    {
-      return;
-    }
-    CutAndPasteTransfer cap = new CutAndPasteTransfer();
-    cap.setForInput(null);
-    try
-    {
-      FileFormatI format = fileFormat;
-      cap.setText(new FormatAdapter(alignPanel, exportData.getSettings())
-              .formatSequences(format, exportData.getAlignment(),
-                      exportData.getOmitHidden(),
-                      exportData.getStartEndPostions(),
-                      viewport.getAlignment().getHiddenColumns()));
-      Desktop.addInternalFrame(cap, MessageManager
-              .formatMessage("label.alignment_output_command", new Object[]
-              { e.getActionCommand() }), 600, 500);
-    } catch (OutOfMemoryError oom)
+            .forName(fileFormatName);
+    AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
+    RunResponse outputAction = new RunResponse(JvOptionPane.OK_OPTION)
     {
-      new OOMWarning("Outputting alignment as " + e.getActionCommand(),
-              oom);
-      cap.dispose();
-    }
-
-  }
-
-  public static AlignmentExportData getAlignmentForExport(
-          FileFormatI format, AlignViewportI viewport,
-          AlignExportSettingI exportSettings)
-  {
-    AlignmentI alignmentToExport = null;
-    AlignExportSettingI settings = exportSettings;
-    String[] omitHidden = null;
-
-    HiddenSequences hiddenSeqs = viewport.getAlignment()
-            .getHiddenSequences();
-
-    alignmentToExport = viewport.getAlignment();
-
-    boolean hasHiddenSeqs = hiddenSeqs.getSize() > 0;
-    if (settings == null)
-    {
-      settings = new AlignExportSettings(hasHiddenSeqs,
-              viewport.hasHiddenColumns(), format);
-    }
-    // settings.isExportAnnotations();
-
-    if (viewport.hasHiddenColumns() && !settings.isExportHiddenColumns())
-    {
-      omitHidden = viewport.getViewAsString(false,
-              settings.isExportHiddenSequences());
-    }
+      @Override
+      public void run()
+      {
+        // todo defer this to inside formatSequences (or later)
+        AlignmentExportData exportData = viewport
+                .getAlignExportData(options);
+        CutAndPasteTransfer cap = new CutAndPasteTransfer();
+        cap.setForInput(null);
+        try
+        {
+          FileFormatI format = fileFormat;
+          cap.setText(new FormatAdapter(alignPanel, options)
+                  .formatSequences(format, exportData.getAlignment(),
+                          exportData.getOmitHidden(),
+                          exportData.getStartEndPostions(),
+                          viewport.getAlignment().getHiddenColumns()));
+          Desktop.addInternalFrame(cap, MessageManager.formatMessage(
+                  "label.alignment_output_command", new Object[]
+                  { fileFormat.getName() }), 600, 500);
+        } catch (OutOfMemoryError oom)
+        {
+          new OOMWarning("Outputting alignment as " + fileFormat.getName(),
+                  oom);
+          cap.dispose();
+        }
+      }
+    };
 
-    int[] alignmentStartEnd = new int[2];
-    if (hasHiddenSeqs && settings.isExportHiddenSequences())
+    /*
+     * show dialog with export options if applicable; else just do it
+     */
+    if (AlignExportOptions.isNeeded(viewport, fileFormat))
     {
-      alignmentToExport = hiddenSeqs.getFullAlignment();
+      AlignExportOptions choices = new AlignExportOptions(
+              alignPanel.getAlignViewport(), fileFormat, options);
+      choices.setResponseAction(outputAction);
+      choices.showDialog();
     }
     else
     {
-      alignmentToExport = viewport.getAlignment();
+      outputAction.run();
     }
-    alignmentStartEnd = viewport.getAlignment().getHiddenColumns()
-            .getVisibleStartAndEndIndex(alignmentToExport.getWidth());
-    AlignmentExportData ed = new AlignmentExportData(alignmentToExport,
-            omitHidden, alignmentStartEnd, settings);
-    return ed;
   }
 
   /**
@@ -1359,33 +1419,39 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   }
 
   /**
-   * DOCUMENT ME!
+   * Creates a PNG image of the alignment and writes it to the given file. If
+   * the file is null, the user is prompted to choose a file.
    * 
-   * @param e
-   *          DOCUMENT ME!
+   * @param f
    */
   @Override
   public void createPNG(File f)
   {
-    alignPanel.makePNG(f);
+    alignPanel.makeAlignmentImage(TYPE.PNG, f);
   }
 
   /**
-   * DOCUMENT ME!
+   * Creates an EPS image of the alignment and writes it to the given file. If
+   * the file is null, the user is prompted to choose a file.
    * 
-   * @param e
-   *          DOCUMENT ME!
+   * @param f
    */
   @Override
   public void createEPS(File f)
   {
-    alignPanel.makeEPS(f);
+    alignPanel.makeAlignmentImage(TYPE.EPS, f);
   }
 
+  /**
+   * Creates an SVG image of the alignment and writes it to the given file. If
+   * the file is null, the user is prompted to choose a file.
+   * 
+   * @param f
+   */
   @Override
   public void createSVG(File f)
   {
-    alignPanel.makeSVG(f);
+    alignPanel.makeAlignmentImage(TYPE.SVG, f);
   }
 
   @Override
@@ -1424,39 +1490,27 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public void associatedData_actionPerformed(ActionEvent e)
   {
-    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);
   }
 
   /**
@@ -3246,15 +3300,37 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public void alignmentProperties()
   {
-    JEditorPane editPane = new JEditorPane("text/html", "");
-    editPane.setEditable(false);
+    JComponent pane;
     StringBuffer contents = new AlignmentProperties(viewport.getAlignment())
+
             .formatAsHtml();
-    editPane.setText(
-            MessageManager.formatMessage("label.html_content", new Object[]
-            { contents.toString() }));
+    String content = MessageManager.formatMessage("label.html_content",
+            new Object[]
+            { contents.toString() });
+    contents = null;
+
+    if (/** @j2sNative true || */
+    false)
+    {
+      JLabel textLabel = new JLabel();
+      textLabel.setText(content);
+      textLabel.setBackground(Color.WHITE);
+      
+      pane = new JPanel(new BorderLayout());
+      ((JPanel) pane).setOpaque(true);
+      ((JPanel) pane).add(textLabel, BorderLayout.CENTER);
+    }
+    else
+    {
+      JEditorPane editPane = new JEditorPane("text/html", "");
+      editPane.setEditable(false);
+      editPane.setText(content);
+      pane = editPane;
+    }
+
     JInternalFrame frame = new JInternalFrame();
-    frame.getContentPane().add(new JScrollPane(editPane));
+
+    frame.getContentPane().add(new JScrollPane(pane));
 
     Desktop.addInternalFrame(frame, MessageManager
             .formatMessage("label.alignment_properties", new Object[]
@@ -3905,7 +3981,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   protected void loadTreeMenuItem_actionPerformed(ActionEvent e)
   {
-    // TODO: JAL-3048 JalviewFileChooser
     // Pick the tree file
     JalviewFileChooser chooser = new JalviewFileChooser(
             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
@@ -3915,40 +3990,38 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     chooser.setToolTipText(
             MessageManager.getString("label.load_tree_file"));
 
-    chooser.response(
-            new jalview.util.dialogrunner.RunResponse(JalviewFileChooser.APPROVE_OPTION)
-            {
-              @Override
-              public void run()
-              {
-                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);
+    chooser.response(new jalview.util.dialogrunner.RunResponse(
+            JalviewFileChooser.APPROVE_OPTION)
+    {
+      @Override
+      public void run()
+      {
+        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)
@@ -4375,7 +4448,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    */
   public boolean parseFeaturesFile(Object file, DataSourceType sourceType)
   {
-    // BH 2018 
+    // BH 2018
     return avc.parseFeaturesFile(file, sourceType,
             Cache.getDefault("RELAXEDSEQIDMATCHING", false));
 
@@ -4422,8 +4495,7 @@ 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<Object> files = new ArrayList<>();
     List<DataSourceType> protocols = new ArrayList<>();
@@ -4456,14 +4528,17 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
             ArrayList<Object> filesnotmatched = new ArrayList<>();
             for (int i = 0; i < files.size(); i++)
             {
-              // BH 2018 
+              // BH 2018
               Object file = files.get(i);
               String fileName = file.toString();
               String pdbfn = "";
-              DataSourceType protocol = (file instanceof File ? DataSourceType.FILE : FormatAdapter.checkProtocol(fileName));
+              DataSourceType protocol = (file instanceof File
+                      ? DataSourceType.FILE
+                      : FormatAdapter.checkProtocol(fileName));
               if (protocol == DataSourceType.FILE)
               {
-                File fl = (file instanceof File ? (File) file : new File(fileName));
+                File fl = (file instanceof File ? (File) file
+                        : new File(fileName));
                 pdbfn = fl.getName();
               }
               else if (protocol == DataSourceType.URL)
@@ -4512,7 +4587,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(