JAL-3060 tidy Cancel dialog action handler code
[jalview.git] / src / jalview / gui / AlignExportSettings.java
index 49bb64a..fbe12d8 100644 (file)
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.gui;
 
 import jalview.api.AlignExportSettingI;
+import jalview.api.AlignViewportI;
 import jalview.bin.Jalview;
+import jalview.datamodel.AlignExportSettingBeanI;
+import jalview.datamodel.AlignmentExportData;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.HiddenSequences;
+import jalview.io.FileFormatI;
 import jalview.jbgui.GAlignExportSettings;
+import jalview.util.MessageManager;
+import jalview.util.dialogrunner.RunResponse;
 
 import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 
-import javax.swing.JDialog;
-import javax.swing.JInternalFrame;
 import javax.swing.JOptionPane;
 
 @SuppressWarnings("serial")
-public class AlignExportSettings extends GAlignExportSettings implements
-        AlignExportSettingI
+public class AlignExportSettings extends GAlignExportSettings
+        implements AlignExportSettingI
 {
-  protected JInternalFrame frame;
-
   boolean cancelled = false;
 
-  JDialog dialog;
+  JvOptionPane dialog;
+
+  AlignmentPanel alignPanel;
+  
+  /**
+   * settings provided at construction override the GUI 
+   */
+  AlignExportSettingBeanI override=null;
 
-  public AlignExportSettings(boolean hasHiddenSeq, boolean hasHiddenCols,
-          String alignFileFormat)
+  public AlignExportSettings(AlignmentPanel alignmentPanel,
+          FileFormatI format, AlignExportSettingBeanI exportSettings)
+  {
+    super(alignmentPanel.getAlignViewport().hasHiddenRows(),
+            alignmentPanel.getAlignViewport().hasHiddenColumns(), format);
+    this.alignPanel = alignmentPanel;
+    override = exportSettings;
+  }     
+
+  /**
+   * @param viewport
+   * @return a bean with data for export
+   */
+  @Override
+  public AlignmentExportData getAlignExportData()
   {
-    super(hasHiddenSeq, hasHiddenCols, alignFileFormat);
-    if (!Jalview.isHeadlessMode() && isShowDialog())
+    // settings.isExportAnnotations();
+
+    AlignmentI alignmentToExport = null;
+    String[] omitHidden = null;
+    AlignViewportI viewport = alignPanel.getAlignViewport();
+
+    alignmentToExport = viewport.getAlignment();
+
+    if (viewport.hasHiddenColumns() && !isExportHiddenColumns())
     {
-      JOptionPane pane = new JOptionPane(null, JOptionPane.DEFAULT_OPTION,
-              JOptionPane.DEFAULT_OPTION, null, new Object[]
-              { this });
-      dialog = pane.createDialog(Desktop.desktop, "Export Settings");
-      dialog.pack();
-      dialog.setVisible(true);
-      dialog.setContentPane(this);
-      dialog.validate();
+      omitHidden = viewport.getViewAsString(false,
+              isExportHiddenSequences());
     }
-  }
 
+    int[] alignmentStartEnd = new int[2];
+    if (viewport.hasHiddenRows() && isExportHiddenSequences())
+    {
+      alignmentToExport = viewport.getAlignment().getHiddenSequences()
+              .getFullAlignment();
+    }
+    else
+    {
+      alignmentToExport = viewport.getAlignment();
+    }
+    alignmentStartEnd = viewport.getAlignment().getHiddenColumns()
+            .getVisibleStartAndEndIndex(alignmentToExport.getWidth());
+    AlignmentExportData ed = new AlignmentExportData(alignmentToExport,
+            omitHidden, alignmentStartEnd, this);
+    return ed;
+  }
 
+  @Override
   public void ok_actionPerformed(ActionEvent e)
   {
     cancelled = false;
-    dialog.setVisible(false);
   }
 
+  @Override
   public void cancel_actionPerformed(ActionEvent e)
   {
     cancelled = true;
-    dialog.setVisible(false);
   }
 
   @Override
   public boolean isExportHiddenSequences()
   {
-    return chkHiddenSeqs.isSelected();
+    return override!=null ? override.isExportHiddenSequences() : chkHiddenSeqs.isSelected();
   }
 
   @Override
   public boolean isExportHiddenColumns()
   {
-    return chkHiddenCols.isSelected();
+    return override!=null ? override.isExportHiddenColumns() : chkHiddenCols.isSelected();
   }
 
   @Override
   public boolean isExportAnnotations()
   {
-    return chkExportAnnots.isSelected();
+    return override!=null ? override.isExportAnnotations() : chkExportAnnots.isSelected();
   }
 
   @Override
   public boolean isExportFeatures()
   {
-    return chkExportFeats.isSelected();
+    return override!=null ? override.isExportFeatures() : chkExportFeats.isSelected();
   }
 
   @Override
   public boolean isExportGroups()
   {
-    return chkExportGrps.isSelected();
+    return override!=null ? override.isExportGroups() : chkExportGrps.isSelected();
   }
 
+  @Override
   public boolean isCancelled()
   {
-    return cancelled;
+    return override!=null ? override.isCancelled() : cancelled;
+  }
+
+  ActionListener afterShownAction = null;
+
+  @Override
+  public void addActionListener(ActionListener actionListener)
+  {
+    afterShownAction = actionListener;
+  }
+
+  @Override
+  public void doShowSettings()
+  {
+    if (!Jalview.isHeadlessMode() && override==null && isShowDialog())
+    {
+
+      dialog = JvOptionPane.newOptionDialog(alignPanel);
+
+      dialog.response(new RunResponse(JvOptionPane.OK_OPTION)
+      {
+        public void run()
+        {
+          ok_actionPerformed(null);
+          afterShownAction.actionPerformed(null);
+        }
+      }).defaultResponse(new Runnable()
+      {
+        @Override
+        public void run()
+        {
+          cancelled=true;
+          afterShownAction.actionPerformed(null);
+        }
+      });
+      String ok = MessageManager.getString("action.ok"),
+              cancel = MessageManager.getString("action.cancel");
+      dialog.showInternalDialog(this,
+              MessageManager.getString("label.export_settings"),
+              JOptionPane.DEFAULT_OPTION, JvOptionPane.DEFAULT_OPTION, null,
+              new Object[]
+              { ok, cancel }, ok);
+    }
+    else
+    {
+      afterShownAction.actionPerformed(null);
+    }
   }
 
 }