From: gmungoc Date: Fri, 20 Jul 2018 10:03:31 +0000 (+0100) Subject: JAL-3048 revised AlignExportSettings dialog with response action X-Git-Tag: Release_2_11_4_0~45^2~18^2~528 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=dd8112aefe2c3a08c51f1684d5cda16be938c74f;p=jalview.git JAL-3048 revised AlignExportSettings dialog with response action --- diff --git a/src/jalview/api/AlignExportSettingI.java b/src/jalview/api/AlignExportSettingI.java deleted file mode 100644 index 31d0940..0000000 --- a/src/jalview/api/AlignExportSettingI.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 . - * The Jalview Authors are detailed in the 'AUTHORS' file. - */ - -package jalview.api; - -import jalview.datamodel.AlignExportSettingBeanI; -import jalview.datamodel.AlignmentExportData; - -import java.awt.event.ActionListener; - -/** - * additional methods implemented by the export settings dialog - * - */ -public interface AlignExportSettingI extends AlignExportSettingBeanI -{ - - AlignmentExportData getAlignExportData(); - - /** - * the export data action to be performed - make sure you check isCancelled in this method first - * @param actionListener - */ - - public void addActionListener(ActionListener actionListener); - - /** - * show the dialog - */ - public void doShowSettings(); - -} diff --git a/src/jalview/api/AlignExportSettingsI.java b/src/jalview/api/AlignExportSettingsI.java new file mode 100644 index 0000000..e4ef584 --- /dev/null +++ b/src/jalview/api/AlignExportSettingsI.java @@ -0,0 +1,78 @@ +/* + * 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 . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ + +package jalview.api; + +/** + * An interface describing settings for including or excluding data when an + * alignment is output + * + */ +public interface AlignExportSettingsI +{ + /** + * Answers true if hidden sequences should be exported, false if not + * + * @return + */ + boolean isExportHiddenSequences(); + + /** + * Answers true if hidden columns should be exported, false if not + * + * @return + */ + boolean isExportHiddenColumns(); + + /** + * Answers true if Annotations should be exported. This is available for + * complex flat file exports like JSON, HTML, GFF. + * + * @return + */ + boolean isExportAnnotations(); + + /** + * Answers true if Sequence Features should be exported. This is available for + * complex flat file exports like JSON, HTML, GFF. + * + * @return + */ + boolean isExportFeatures(); + + /** + * Answers true if Sequence Groups should be exported. This is available for + * complex flat file exports like JSON, HTML, GFF. + * + * @return + */ + boolean isExportGroups(); + + void setExportHiddenSequences(boolean b); + + void setExportHiddenColumns(boolean b); + + void setExportAnnotations(boolean b); + + void setExportFeatures(boolean b); + + void setExportGroups(boolean b); +} diff --git a/src/jalview/api/AlignViewportI.java b/src/jalview/api/AlignViewportI.java index 931eba6..b976a3d 100644 --- a/src/jalview/api/AlignViewportI.java +++ b/src/jalview/api/AlignViewportI.java @@ -23,6 +23,7 @@ package jalview.api; import jalview.analysis.Conservation; import jalview.analysis.TreeModel; import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentExportData; import jalview.datamodel.AlignmentI; import jalview.datamodel.AlignmentView; import jalview.datamodel.ColumnSelection; @@ -487,7 +488,16 @@ public interface AlignViewportI extends ViewStyleI @Override void setProteinFontAsCdna(boolean b); - public abstract TreeModel getCurrentTree(); + TreeModel getCurrentTree(); - public abstract void setCurrentTree(TreeModel tree); + void setCurrentTree(TreeModel tree); + + /** + * Answers a data bean containing data for export as configured by the + * supplied options + * + * @param options + * @return + */ + AlignmentExportData getAlignExportData(AlignExportSettingsI options); } diff --git a/src/jalview/datamodel/AlignExportSettingBeanI.java b/src/jalview/datamodel/AlignExportSettingBeanI.java deleted file mode 100644 index 77b704e..0000000 --- a/src/jalview/datamodel/AlignExportSettingBeanI.java +++ /dev/null @@ -1,52 +0,0 @@ -package jalview.datamodel; - -/** - * * Abstract interface implemented by Alignment Export dialog to retrieve user - * configurations - * - * @author tcnofoegbu - * - */ -public interface AlignExportSettingBeanI -{ - - /** - * Checks if hidden sequences should be exported - * - * @return - */ - public boolean isExportHiddenSequences(); - - /** - * Checks if hidden columns shoulb be exported - * - * @return - */ - public boolean isExportHiddenColumns(); - - /** - * Checks if Annotations should be exported, note this is available for - * complex flat file exports like JSON, HTML, GFF - * - * @return - */ - public boolean isExportAnnotations(); - - /** - * Checks if SequenceFeatures should be exported, note this is available for - * complex flat file exports like JSON, HTML, GFF - * - * @return - */ - public boolean isExportFeatures(); - - /** - * Checks if SequenceGroups should be exported, note this is available for - * complex flat file exports like JSON, HTML, GFF - * - * @return - */ - public boolean isExportGroups(); - - public boolean isCancelled(); -} diff --git a/src/jalview/datamodel/AlignExportSettingsAdapter.java b/src/jalview/datamodel/AlignExportSettingsAdapter.java new file mode 100644 index 0000000..8ac561a --- /dev/null +++ b/src/jalview/datamodel/AlignExportSettingsAdapter.java @@ -0,0 +1,90 @@ +package jalview.datamodel; + +import jalview.api.AlignExportSettingsI; + +/** + * Export options that may be constructed as 'all' or 'none' (and further + * adjusted if wanted) + */ +public class AlignExportSettingsAdapter implements AlignExportSettingsI +{ + private boolean exportHiddenSeqs; + + private boolean exportHiddenCols; + + private boolean exportAnnotations; + + private boolean exportFeatures; + + private boolean exportGroups; + + /** + * Constructor sets all options to either true or false + * + * @param defaultOption + */ + public AlignExportSettingsAdapter(boolean defaultOption) + { + exportAnnotations = defaultOption; + exportFeatures = defaultOption; + exportGroups = defaultOption; + exportHiddenCols = defaultOption; + exportHiddenSeqs = defaultOption; + } + + @Override + public boolean isExportHiddenSequences() + { + return exportHiddenSeqs; + } + + @Override + public boolean isExportHiddenColumns() + { + return exportHiddenCols; + } + + @Override + public boolean isExportAnnotations() + { + return exportAnnotations; + } + + @Override + public boolean isExportFeatures() + { + return exportFeatures; + } + + @Override + public boolean isExportGroups() + { + return exportGroups; + } + + public void setExportHiddenSequences(boolean exportHiddenSeqs) + { + this.exportHiddenSeqs = exportHiddenSeqs; + } + + public void setExportHiddenColumns(boolean exportHiddenCols) + { + this.exportHiddenCols = exportHiddenCols; + } + + public void setExportAnnotations(boolean exportAnnotations) + { + this.exportAnnotations = exportAnnotations; + } + + public void setExportFeatures(boolean exportFeatures) + { + this.exportFeatures = exportFeatures; + } + + public void setExportGroups(boolean exportGroups) + { + this.exportGroups = exportGroups; + } + +} diff --git a/src/jalview/datamodel/AlignmentExportData.java b/src/jalview/datamodel/AlignmentExportData.java index 2393fe7..91ae53a 100644 --- a/src/jalview/datamodel/AlignmentExportData.java +++ b/src/jalview/datamodel/AlignmentExportData.java @@ -20,8 +20,6 @@ */ package jalview.datamodel; -import jalview.api.AlignExportSettingI; - public class AlignmentExportData { private AlignmentI alignment; @@ -30,15 +28,12 @@ public class AlignmentExportData private int[] startEnd; - private AlignExportSettingI settings; - public AlignmentExportData(AlignmentI align, String[] ommit, - int[] startEnd, AlignExportSettingI settings) + int[] startEnd) { this.alignment = align; this.omitHidden = ommit; this.startEnd = startEnd; - this.settings = settings; } public AlignmentI getAlignment() @@ -70,14 +65,4 @@ public class AlignmentExportData { this.startEnd = startEnd; } - - public AlignExportSettingI getSettings() - { - return settings; - } - - public void setSettings(AlignExportSettingI settings) - { - this.settings = settings; - } } diff --git a/src/jalview/gui/AlignExportOptions.java b/src/jalview/gui/AlignExportOptions.java new file mode 100644 index 0000000..5667744 --- /dev/null +++ b/src/jalview/gui/AlignExportOptions.java @@ -0,0 +1,261 @@ +/* + * 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 . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.gui; + +import jalview.api.AlignExportSettingsI; +import jalview.api.AlignViewportI; +import jalview.io.FileFormatI; +import jalview.util.MessageManager; +import jalview.util.dialogrunner.RunResponse; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; + +import javax.swing.JCheckBox; +import javax.swing.JPanel; + +/** + * A dialog that allows the user to specify whether to include hidden columns or + * sequences in an alignment export, and possibly features, annotations and + * groups, if applicable to the output file format + */ +@SuppressWarnings("serial") +public class AlignExportOptions extends JPanel +{ + protected JCheckBox chkHiddenSeqs = new JCheckBox(); + + protected JCheckBox chkHiddenCols = new JCheckBox(); + + protected JCheckBox chkExportAnnots = new JCheckBox(); + + protected JCheckBox chkExportFeats = new JCheckBox(); + + protected JCheckBox chkExportGrps = new JCheckBox(); + + protected AlignExportSettingsI settings; + + private boolean isComplexAlignFile; + + JvOptionPane dialog; + + /** + * A convenience method that answers true if this dialog should be shown - + * that is, if the alignment has any hidden rows or columns, or if the file + * format is one that can (optionally) represent annotations, features or + * groups data - else false + * + * @param viewport + * @param format + * @return + */ + public static boolean isNeeded(AlignViewportI viewport, + FileFormatI format) + { + if (viewport.hasHiddenColumns() || viewport.hasHiddenRows() + || format.isComplexAlignFile()) + { + return true; + } + return false; + } + + /** + * Constructor that passes in an initial set of export options. User choices + * in the dialog should update this object, and the same object + * should be used in any action handler set by calling + * setResponseAction. + * + * @param viewport + * @param format + * @param defaults + */ + public AlignExportOptions(AlignViewportI viewport, FileFormatI format, + AlignExportSettingsI defaults) + { + this.settings = defaults; + this.isComplexAlignFile = format.isComplexAlignFile(); + init(viewport.hasHiddenRows(), viewport.hasHiddenColumns()); + dialog = JvOptionPane.newOptionDialog(Desktop.desktop); + } + + /** + * Shows the dialog, and runs any registered response actions that correspond + * to user choices + */ + public void showDialog() + { + Object[] options = new Object[] { MessageManager.getString("action.ok"), + MessageManager.getString("action.cancel") }; + dialog.showInternalDialog(this, + MessageManager.getString("label.export_settings"), + JvOptionPane.OK_CANCEL_OPTION, JvOptionPane.PLAIN_MESSAGE, + null, options, MessageManager.getString("action.ok")); + } + + /** + * Registers a Runnable action to be performed for a particular user response + * in the dialog + * + * @param action + */ + public void setResponseAction(RunResponse action) + { + dialog.response(action); + } + + /** + * Selects/deselects all enabled and shown options on 'Check all' selected or + * deselected + * + * @param isSelected + */ + void checkAllAction(boolean isSelected) + { + boolean set = chkHiddenSeqs.isEnabled() && isSelected; + chkHiddenSeqs.setSelected(set); + settings.setExportHiddenSequences(set); + + set = chkHiddenCols.isEnabled() && isSelected; + chkHiddenCols.setSelected(set); + settings.setExportHiddenColumns(set); + + set = isComplexAlignFile && chkExportAnnots.isEnabled() && isSelected; + chkExportAnnots.setSelected(set); + settings.setExportAnnotations(set); + + set = isComplexAlignFile && chkExportFeats.isEnabled() && isSelected; + chkExportFeats.setSelected(set); + settings.setExportFeatures(set); + + set = isComplexAlignFile && chkExportGrps.isEnabled() && isSelected; + chkExportGrps.setSelected(set); + settings.setExportGroups(set); + } + + /** + * Initialises the components of the display + * + * @param hasHiddenSeq + * @param hasHiddenCols + */ + private void init(boolean hasHiddenSeq, boolean hasHiddenCols) + { + chkHiddenSeqs.setText( + MessageManager.getString("action.export_hidden_sequences")); + chkHiddenSeqs.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + settings.setExportHiddenSequences(chkHiddenSeqs.isSelected()); + } + }); + + chkHiddenCols.setText( + MessageManager.getString("action.export_hidden_columns")); + chkHiddenCols.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + settings.setExportHiddenColumns(chkHiddenCols.isSelected()); + } + }); + + chkExportAnnots + .setText(MessageManager.getString("action.export_annotations")); + chkExportAnnots.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + settings.setExportAnnotations(chkExportAnnots.isSelected()); + } + }); + + chkExportFeats + .setText(MessageManager.getString("action.export_features")); + chkExportFeats.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + settings.setExportFeatures(chkExportFeats.isSelected()); + } + }); + + chkExportGrps.setText(MessageManager.getString("action.export_groups")); + chkExportGrps.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + settings.setExportGroups(chkExportGrps.isSelected()); + } + }); + + JCheckBox chkAll = new JCheckBox( + MessageManager.getString("action.select_all")); + + JPanel hiddenRegionConfPanel = new JPanel(new BorderLayout()); + JPanel complexExportPanel = new JPanel(new BorderLayout()); + this.setLayout(new BorderLayout()); + + chkAll.addItemListener(new ItemListener() + { + @Override + public void itemStateChanged(ItemEvent e) + { + checkAllAction(chkAll.isSelected()); + } + }); + + hiddenRegionConfPanel.add(chkHiddenSeqs, BorderLayout.CENTER); + hiddenRegionConfPanel.add(chkHiddenCols, BorderLayout.SOUTH); + chkHiddenSeqs.setEnabled(hasHiddenSeq); + chkHiddenCols.setEnabled(hasHiddenCols); + + complexExportPanel.add(chkExportAnnots, BorderLayout.NORTH); + complexExportPanel.add(chkExportFeats, BorderLayout.CENTER); + complexExportPanel.add(chkExportGrps, BorderLayout.SOUTH); + + JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); + actionPanel.add(chkAll); + + JPanel optionsPanel = new JPanel(); + if (this.isComplexAlignFile) + { + optionsPanel.add(complexExportPanel); + } + + if (hasHiddenSeq || hasHiddenCols) + { + optionsPanel.add(hiddenRegionConfPanel); + } + + add(optionsPanel, BorderLayout.NORTH); + add(actionPanel, BorderLayout.SOUTH); + } +} diff --git a/src/jalview/gui/AlignExportSettings.java b/src/jalview/gui/AlignExportSettings.java deleted file mode 100644 index fbe12d8..0000000 --- a/src/jalview/gui/AlignExportSettings.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * 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 . - * 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.JOptionPane; - -@SuppressWarnings("serial") -public class AlignExportSettings extends GAlignExportSettings - implements AlignExportSettingI -{ - boolean cancelled = false; - - JvOptionPane dialog; - - AlignmentPanel alignPanel; - - /** - * settings provided at construction override the GUI - */ - AlignExportSettingBeanI override=null; - - 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() - { - // settings.isExportAnnotations(); - - AlignmentI alignmentToExport = null; - String[] omitHidden = null; - AlignViewportI viewport = alignPanel.getAlignViewport(); - - alignmentToExport = viewport.getAlignment(); - - if (viewport.hasHiddenColumns() && !isExportHiddenColumns()) - { - 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; - } - - @Override - public void cancel_actionPerformed(ActionEvent e) - { - cancelled = true; - } - - @Override - public boolean isExportHiddenSequences() - { - return override!=null ? override.isExportHiddenSequences() : chkHiddenSeqs.isSelected(); - } - - @Override - public boolean isExportHiddenColumns() - { - return override!=null ? override.isExportHiddenColumns() : chkHiddenCols.isSelected(); - } - - @Override - public boolean isExportAnnotations() - { - return override!=null ? override.isExportAnnotations() : chkExportAnnots.isSelected(); - } - - @Override - public boolean isExportFeatures() - { - return override!=null ? override.isExportFeatures() : chkExportFeats.isSelected(); - } - - @Override - public boolean isExportGroups() - { - return override!=null ? override.isExportGroups() : chkExportGrps.isSelected(); - } - - @Override - public boolean isCancelled() - { - 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); - } - } - -} diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 145653a..fb952b7 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -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(); + } } /** diff --git a/src/jalview/gui/FeatureEditor.java b/src/jalview/gui/FeatureEditor.java index 60f26e6..f14d657 100644 --- a/src/jalview/gui/FeatureEditor.java +++ b/src/jalview/gui/FeatureEditor.java @@ -409,7 +409,7 @@ public class FeatureEditor dialog.showInternalDialog(mainPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION, - JvOptionPane.QUESTION_MESSAGE, null, options, + JvOptionPane.PLAIN_MESSAGE, null, options, MessageManager.getString("action.ok")); } diff --git a/src/jalview/io/AlignmentFileReaderI.java b/src/jalview/io/AlignmentFileReaderI.java index a471d9b..c3e148d 100644 --- a/src/jalview/io/AlignmentFileReaderI.java +++ b/src/jalview/io/AlignmentFileReaderI.java @@ -20,7 +20,7 @@ */ package jalview.io; -import jalview.api.AlignExportSettingI; +import jalview.api.AlignExportSettingsI; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureSettingsModelI; import jalview.datamodel.AlignmentI; diff --git a/src/jalview/io/AlignmentFileWriterI.java b/src/jalview/io/AlignmentFileWriterI.java index 6af4d1e..2049032 100644 --- a/src/jalview/io/AlignmentFileWriterI.java +++ b/src/jalview/io/AlignmentFileWriterI.java @@ -20,7 +20,7 @@ */ package jalview.io; -import jalview.api.AlignExportSettingI; +import jalview.api.AlignExportSettingsI; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureSettingsModelI; import jalview.datamodel.AlignmentI; @@ -31,7 +31,7 @@ public interface AlignmentFileWriterI void setNewlineString(String newline); - void setExportSettings(AlignExportSettingI exportSettings); + void setExportSettings(AlignExportSettingsI exportSettings); void configureForView(AlignmentViewPanel viewpanel); diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index 4c25634..b0fee88 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -20,7 +20,7 @@ */ package jalview.io; -import jalview.api.AlignExportSettingI; +import jalview.api.AlignExportSettingsI; import jalview.api.AlignmentViewPanel; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; @@ -73,7 +73,7 @@ public class AppletFormatAdapter */ protected String newline = System.getProperty("line.separator"); - private AlignExportSettingI exportSettings; + private AlignExportSettingsI exportSettings; private File selectedFile; @@ -100,7 +100,7 @@ public class AppletFormatAdapter } public AppletFormatAdapter(AlignmentViewPanel alignPanel, - AlignExportSettingI settings) + AlignExportSettingsI settings) { viewpanel = alignPanel; exportSettings = settings; diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index e8b5ab8..809bc8f 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -20,7 +20,7 @@ */ package jalview.io; -import jalview.api.AlignExportSettingI; +import jalview.api.AlignExportSettingsI; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.FeatureSettingsModelI; @@ -72,7 +72,7 @@ public class FileParse /** * specific settings for exporting data from the current context */ - private AlignExportSettingI exportSettings; + private AlignExportSettingsI exportSettings; /** * sequence counter for FileParse object created from same data source @@ -634,7 +634,7 @@ public class FileParse /** * @return the currently configured exportSettings for writing data. */ - public AlignExportSettingI getExportSettings() + public AlignExportSettingsI getExportSettings() { return exportSettings; } @@ -645,7 +645,7 @@ public class FileParse * @param exportSettings * the exportSettings to set */ - public void setExportSettings(AlignExportSettingI exportSettings) + public void setExportSettings(AlignExportSettingsI exportSettings) { this.exportSettings = exportSettings; } diff --git a/src/jalview/io/FormatAdapter.java b/src/jalview/io/FormatAdapter.java index 2264a6a..85cf48a 100755 --- a/src/jalview/io/FormatAdapter.java +++ b/src/jalview/io/FormatAdapter.java @@ -20,7 +20,7 @@ */ package jalview.io; -import jalview.api.AlignExportSettingI; +import jalview.api.AlignExportSettingsI; import jalview.api.AlignmentViewPanel; import jalview.bin.Cache; import jalview.datamodel.Alignment; @@ -56,7 +56,7 @@ public class FormatAdapter extends AppletFormatAdapter } public FormatAdapter(AlignmentViewPanel alignPanel, - AlignExportSettingI settings) + AlignExportSettingsI settings) { super(alignPanel, settings); } diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index f957e06..7071a9d 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -21,7 +21,7 @@ package jalview.io; -import jalview.api.AlignExportSettingI; +import jalview.api.AlignExportSettingsI; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.ComplexAlignFile; @@ -29,7 +29,7 @@ import jalview.api.FeatureRenderer; import jalview.api.FeatureSettingsModelI; import jalview.api.FeaturesDisplayedI; import jalview.bin.BuildDetails; -import jalview.datamodel.AlignExportSettingBeanI; +import jalview.datamodel.AlignExportSettingsAdapter; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; import jalview.datamodel.Annotation; @@ -124,50 +124,14 @@ public class JSONFile extends AlignFile implements ComplexAlignFile try { AlignmentPojo jsonAlignmentPojo = new AlignmentPojo(); - AlignExportSettingBeanI exportSettings = getExportSettings(); + AlignExportSettingsI exportSettings = getExportSettings(); - // if no export settings were supplied use the following with all values - // defaulting to true + /* + * if no export settings were supplied, provide an 'export all' setting + */ if (exportSettings == null) { - exportSettings = new AlignExportSettingBeanI() - { - @Override - public boolean isExportHiddenSequences() - { - return true; - } - - @Override - public boolean isExportHiddenColumns() - { - return true; - } - - @Override - public boolean isExportGroups() - { - return true; - } - - @Override - public boolean isExportFeatures() - { - return true; - } - - @Override - public boolean isExportAnnotations() - { - return true; - } - - @Override - public boolean isCancelled() - { - return false; - } - }; + exportSettings = new AlignExportSettingsAdapter(true); } int count = 0; diff --git a/src/jalview/jbgui/GAlignExportSettings.java b/src/jalview/jbgui/GAlignExportSettings.java deleted file mode 100644 index cfa57c8..0000000 --- a/src/jalview/jbgui/GAlignExportSettings.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * 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 . - * The Jalview Authors are detailed in the 'AUTHORS' file. - */ -package jalview.jbgui; - -import jalview.io.FileFormatI; -import jalview.util.MessageManager; - -import java.awt.BorderLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ItemEvent; -import java.awt.event.ItemListener; - -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JPanel; - -@SuppressWarnings("serial") -public abstract class GAlignExportSettings extends JPanel -{ - protected JPanel hiddenRegionConfPanel = new JPanel(); - - protected JPanel complexExportPanel = new JPanel(); - - protected JPanel optionsPanel = new JPanel(); - - protected JPanel actionPanel = new JPanel(); - - protected BorderLayout hiddenRegionLayout = new BorderLayout(); - - protected BorderLayout complexExportLayout = new BorderLayout(); - - protected BorderLayout mainLayout = new BorderLayout(); - - protected JCheckBox chkAll = new JCheckBox(); - - protected JCheckBox chkHiddenSeqs = new JCheckBox(); - - protected JCheckBox chkHiddenCols = new JCheckBox(); - - protected JCheckBox chkExportAnnots = new JCheckBox(); - - protected JCheckBox chkExportFeats = new JCheckBox(); - - protected JCheckBox chkExportGrps = new JCheckBox(); - - private boolean hasHiddenSeq, hasHiddenCols, isComplexAlignFile, - showDialog; - - public GAlignExportSettings(boolean hasHiddenSeq, boolean hasHiddenCols, - FileFormatI format) - { - this.hasHiddenSeq = hasHiddenSeq; - this.hasHiddenCols = hasHiddenCols; - this.isComplexAlignFile = format.isComplexAlignFile(); - if (this.hasHiddenCols || this.hasHiddenSeq || this.isComplexAlignFile) - { - this.showDialog = true; - } - init(); - } - - public void init() - { - chkHiddenSeqs.setText( - MessageManager.getString("action.export_hidden_sequences")); - chkHiddenCols.setText( - MessageManager.getString("action.export_hidden_columns")); - chkExportAnnots - .setText(MessageManager.getString("action.export_annotations")); - chkExportFeats - .setText(MessageManager.getString("action.export_features")); - chkExportGrps.setText(MessageManager.getString("action.export_groups")); - chkAll.setText(MessageManager.getString("action.select_all")); - - hiddenRegionConfPanel.setLayout(hiddenRegionLayout); - complexExportPanel.setLayout(complexExportLayout); - setLayout(mainLayout); - - chkAll.addItemListener(new ItemListener() - { - @Override - public void itemStateChanged(ItemEvent e) - { - checkAllAction(); - } - }); - - // hiddenRegionConfPanel.add(chkAll, BorderLayout.NORTH); - hiddenRegionConfPanel.add(chkHiddenSeqs, BorderLayout.CENTER); - hiddenRegionConfPanel.add(chkHiddenCols, BorderLayout.SOUTH); - chkHiddenSeqs.setEnabled(hasHiddenSeq); - chkHiddenCols.setEnabled(hasHiddenCols); - - complexExportPanel.add(chkExportAnnots, BorderLayout.NORTH); - complexExportPanel.add(chkExportFeats, BorderLayout.CENTER); - complexExportPanel.add(chkExportGrps, BorderLayout.SOUTH); - - actionPanel.add(chkAll); - - if (this.isComplexAlignFile) - { - optionsPanel.add(complexExportPanel); - } - - if (hasHiddenSeq || hasHiddenCols) - { - optionsPanel.add(hiddenRegionConfPanel); - } - - add(optionsPanel, BorderLayout.NORTH); - add(actionPanel, BorderLayout.SOUTH); - - } - - private void checkAllAction() - { - boolean isSelected = chkAll.isSelected(); - chkHiddenSeqs.setSelected(chkHiddenSeqs.isEnabled() && isSelected); - chkHiddenCols.setSelected(chkHiddenCols.isEnabled() && isSelected); - chkExportAnnots.setSelected(isComplexAlignFile - && chkExportAnnots.isEnabled() && isSelected); - chkExportFeats.setSelected( - isComplexAlignFile && chkExportFeats.isEnabled() && isSelected); - chkExportGrps.setSelected( - isComplexAlignFile && chkExportGrps.isEnabled() && isSelected); - } - - public boolean isShowDialog() - { - return showDialog; - } - - public void setShowDialog(boolean showDialog) - { - this.showDialog = showDialog; - } - - public abstract void ok_actionPerformed(ActionEvent e); - - public abstract void cancel_actionPerformed(ActionEvent e); -} diff --git a/src/jalview/jbgui/GAlignFrame.java b/src/jalview/jbgui/GAlignFrame.java index 61aa2fc..a83c688 100755 --- a/src/jalview/jbgui/GAlignFrame.java +++ b/src/jalview/jbgui/GAlignFrame.java @@ -224,7 +224,7 @@ public class GAlignFrame extends JInternalFrame @Override public void actionPerformed(ActionEvent e) { - outputText_actionPerformed(e); + outputText_actionPerformed(e.getActionCommand()); } }); @@ -2188,7 +2188,7 @@ public class GAlignFrame extends JInternalFrame { } - protected void outputText_actionPerformed(ActionEvent e) + protected void outputText_actionPerformed(String formatName) { } diff --git a/src/jalview/util/MessageManager.java b/src/jalview/util/MessageManager.java index 1cfe0c6..3dace12 100644 --- a/src/jalview/util/MessageManager.java +++ b/src/jalview/util/MessageManager.java @@ -98,20 +98,20 @@ public class MessageManager public static String formatMessage(String key, Object... params) { - return MessageFormat.format(rb.getString(key), params); + return MessageFormat.format(getString(key), params); } public static String formatMessage(String key, String[] params) { - return MessageFormat.format(rb.getString(key), (Object[]) params); + return MessageFormat.format(getString(key), (Object[]) params); } /** - * lookup and return a key given a root and a human-readable(ish) name that + * Looks up and returns a key given a root and a human-readable(ish) name that * when combined might resolve to an i18n string. If the key doesn't resolve, - * then name is returned.if the key doesn't exist. Use this for - * programatically constructed keys that have have a human readable - * alternative used in the program (e.g. BLOSUM62 and label.score_blosum62) + * then name is returned. Use this for programmatically constructed keys that + * have a human readable alternative used in the program (e.g. BLOSUM62 and + * label.score_blosum62). * * @param keyroot * @param name diff --git a/src/jalview/viewmodel/AlignmentViewport.java b/src/jalview/viewmodel/AlignmentViewport.java index 1366ada..b764959 100644 --- a/src/jalview/viewmodel/AlignmentViewport.java +++ b/src/jalview/viewmodel/AlignmentViewport.java @@ -24,6 +24,7 @@ import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder; import jalview.analysis.Conservation; import jalview.analysis.TreeModel; import jalview.api.AlignCalcManagerI; +import jalview.api.AlignExportSettingsI; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.api.FeaturesDisplayedI; @@ -31,6 +32,7 @@ import jalview.api.ViewStyleI; import jalview.commands.CommandI; import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.AlignmentExportData; import jalview.datamodel.AlignmentI; import jalview.datamodel.AlignmentView; import jalview.datamodel.Annotation; @@ -2954,4 +2956,34 @@ public abstract class AlignmentViewport { return currentTree; } + + @Override + public AlignmentExportData getAlignExportData(AlignExportSettingsI options) + { + AlignmentI alignmentToExport = null; + String[] omitHidden = null; + alignmentToExport = null; + + if (hasHiddenColumns() && !options.isExportHiddenColumns()) + { + omitHidden = getViewAsString(false, + options.isExportHiddenSequences()); + } + + int[] alignmentStartEnd = new int[2]; + if (hasHiddenRows() && options.isExportHiddenSequences()) + { + alignmentToExport = getAlignment().getHiddenSequences() + .getFullAlignment(); + } + else + { + alignmentToExport = getAlignment(); + } + alignmentStartEnd = getAlignment().getHiddenColumns() + .getVisibleStartAndEndIndex(alignmentToExport.getWidth()); + AlignmentExportData ed = new AlignmentExportData(alignmentToExport, + omitHidden, alignmentStartEnd); + return ed; + } }