JAL-3048 revised AlignExportSettings dialog with response action
[jalview.git] / src / jalview / gui / AlignFrame.java
index 145653a..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,7 +45,7 @@ import jalview.commands.RemoveGapColCommand;
 import jalview.commands.RemoveGapsCommand;
 import jalview.commands.SlideSequencesCommand;
 import jalview.commands.TrimRegionCommand;
-import jalview.datamodel.AlignExportSettingBeanI;
+import jalview.datamodel.AlignExportSettingsAdapter;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
@@ -1180,37 +1180,39 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
     int value = chooser.showSaveDialog(this);
 
-    if (value == JalviewFileChooser.APPROVE_OPTION)
+    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();
-      while (currentFileFormat == null)
+      value = chooser.showSaveDialog(this);
+      if (value != JalviewFileChooser.APPROVE_OPTION)
       {
-        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;
-        }
+        return;
       }
+    }
 
-      fileName = chooser.getSelectedFile().getPath();
+    fileName = chooser.getSelectedFile().getPath();
 
-      Cache.setProperty("DEFAULT_FILE_FORMAT", currentFileFormat.getName());
-      Cache.setProperty("LAST_DIRECTORY", fileName);
-      saveAlignment(fileName, currentFileFormat);
-    }
+    Cache.setProperty("DEFAULT_FILE_FORMAT", currentFileFormat.getName());
+    Cache.setProperty("LAST_DIRECTORY", fileName);
+    saveAlignment(fileName, currentFileFormat);
   }
 
-  private boolean lastSaveSuccessful = false;
+  boolean lastSaveSuccessful = false;
 
-  private FileFormatI lastFormatSaved;
+  FileFormatI lastFormatSaved;
 
-  private String lastFilenameSaved;
+  String lastFilenameSaved;
 
   /**
    * Raise a dialog or status message for the last call to saveAlignment.
@@ -1239,113 +1241,126 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     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;
-    AlignFrame us = this;
+
     if (FileFormat.Jalview.equals(format))
     {
       String shortName = title;
-
-      if (shortName.indexOf(java.io.File.separatorChar) > -1)
+      if (shortName.indexOf(File.separatorChar) > -1)
       {
         shortName = shortName.substring(
-                shortName.lastIndexOf(java.io.File.separatorChar) + 1);
+                shortName.lastIndexOf(File.separatorChar) + 1);
       }
-
       lastSaveSuccessful = new Jalview2XML().saveAlignment(this, file,
               shortName);
+      return;
     }
-    else
+
+    AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
+    RunResponse cancelAction = new RunResponse(JvOptionPane.CANCEL_OPTION)
     {
-      AlignExportSettingI settings = getAlignmentForExport(format,
-              alignPanel, null);
-      settings.addActionListener(new ActionListener()
+      @Override
+      public void run()
       {
-        @Override
-        public void actionPerformed(ActionEvent e)
+        lastSaveSuccessful = false;
+      }
+    };
+    RunResponse outputAction = new RunResponse(JvOptionPane.OK_OPTION)
+    {
+      @Override
+      public void run()
+      {
+        // 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)
         {
-
-          if (settings.isCancelled())
+          lastSaveSuccessful = false;
+        }
+        else
+        {
+          try
           {
-            lastSaveSuccessful = false;
-            return;
-          }
-          AlignmentExportData exportData = settings.getAlignExportData();
-          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)
+            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;
-          }
-          else
-          {
-            try
-            {
-              PrintWriter out = new PrintWriter(new FileWriter(file));
-
-              out.print(output);
-              out.close();
-              us.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();
-            }
+            ex.printStackTrace();
           }
         }
-      });
+      }
+    };
 
-      settings.doShowSettings();
+    /*
+     * show dialog with export options if applicable; else just do it
+     */
+    if (AlignExportOptions.isNeeded(viewport, format))
+    {
+      AlignExportOptions choices = new AlignExportOptions(
+              alignPanel.getAlignViewport(), format, options);
+      choices.setResponseAction(outputAction);
+      choices.setResponseAction(cancelAction);
+      choices.showDialog();
+    }
+    else
+    {
+      outputAction.run();
     }
   }
 
   /**
-   * 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());
-    AlignExportSettingI settings = getAlignmentForExport(fileFormat,
-            alignPanel, null);
-    settings.addActionListener(new ActionListener()
+            .forName(fileFormatName);
+    AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
+    RunResponse outputAction = new RunResponse(JvOptionPane.OK_OPTION)
     {
       @Override
-      public void actionPerformed(ActionEvent e)
+      public void run()
       {
-        if (settings.isCancelled())
-        {
-          return;
-        }
-        AlignmentExportData exportData = settings.getAlignExportData();
+        // 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, settings)
+          cap.setText(new FormatAdapter(alignPanel, options)
                   .formatSequences(format, exportData.getAlignment(),
                           exportData.getOmitHidden(),
                           exportData.getStartEndPostions(),
@@ -1359,19 +1374,23 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                   oom);
           cap.dispose();
         }
-        // TODO Auto-generated method stub
-
       }
-    });
-    settings.doShowSettings();
-  }
+    };
 
-  public static AlignExportSettingI getAlignmentForExport(
-          FileFormatI format, AlignmentPanel alignPanel,
-          AlignExportSettingBeanI exportSettings)
-  {
-
-    return new AlignExportSettings(alignPanel, format, exportSettings);
+    /*
+     * show dialog with export options if applicable; else just do it
+     */
+    if (AlignExportOptions.isNeeded(viewport, fileFormat))
+    {
+      AlignExportOptions choices = new AlignExportOptions(
+              alignPanel.getAlignViewport(), fileFormat, options);
+      choices.setResponseAction(outputAction);
+      choices.showDialog();
+    }
+    else
+    {
+      outputAction.run();
+    }
   }
 
   /**