JAL-3048 check for headless
[jalview.git] / src / jalview / gui / AlignFrame.java
index b18d33c..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;
@@ -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;
 
@@ -1149,7 +1153,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     if (fileName == null || (currentFileFormat == null)
             || fileName.startsWith("http"))
     {
-      saveAs_actionPerformed(null);
+      saveAs_actionPerformed();
     }
     else
     {
@@ -1158,58 +1162,57 @@ 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()
   {
     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)
+    int value = chooser.showSaveDialog(this);
+
+    if (value != JalviewFileChooser.APPROVE_OPTION)
     {
-      @Override
-      public void run()
+      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)
       {
-        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);
-        }
-
-        fileName = chooser.getSelectedFile().getPath();
+        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);
   }
 
-  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.
@@ -1238,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(),
@@ -1358,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();
+    }
   }
 
   /**
@@ -1399,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
@@ -3274,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[]