JAL-3048 inlined 1-line methods, small refactor of ImageMaker.TYPE
[jalview.git] / src / jalview / gui / AlignExportSettings.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.api.AlignExportSettingI;
24 import jalview.api.AlignViewportI;
25 import jalview.bin.Jalview;
26 import jalview.datamodel.AlignExportSettingBeanI;
27 import jalview.datamodel.AlignmentExportData;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.HiddenSequences;
30 import jalview.io.FileFormatI;
31 import jalview.jbgui.GAlignExportSettings;
32 import jalview.util.MessageManager;
33 import jalview.util.dialogrunner.RunResponse;
34
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37
38 import javax.swing.JOptionPane;
39
40 @SuppressWarnings("serial")
41 public class AlignExportSettings extends GAlignExportSettings
42         implements AlignExportSettingI
43 {
44   boolean cancelled = false;
45
46   JvOptionPane dialog;
47
48   AlignmentPanel alignPanel;
49   
50   /**
51    * settings provided at construction override the GUI 
52    */
53   AlignExportSettingBeanI override=null;
54
55   public AlignExportSettings(AlignmentPanel alignmentPanel,
56           FileFormatI format, AlignExportSettingBeanI exportSettings)
57   {
58     super(alignmentPanel.getAlignViewport().hasHiddenRows(),
59             alignmentPanel.getAlignViewport().hasHiddenColumns(), format);
60     this.alignPanel = alignmentPanel;
61     override = exportSettings;
62   }     
63
64   /**
65    * @param viewport
66    * @return a bean with data for export
67    */
68   @Override
69   public AlignmentExportData getAlignExportData()
70   {
71     // settings.isExportAnnotations();
72
73     AlignmentI alignmentToExport = null;
74     String[] omitHidden = null;
75     AlignViewportI viewport = alignPanel.getAlignViewport();
76
77     alignmentToExport = viewport.getAlignment();
78
79     if (viewport.hasHiddenColumns() && !isExportHiddenColumns())
80     {
81       omitHidden = viewport.getViewAsString(false,
82               isExportHiddenSequences());
83     }
84
85     int[] alignmentStartEnd = new int[2];
86     if (viewport.hasHiddenRows() && isExportHiddenSequences())
87     {
88       alignmentToExport = viewport.getAlignment().getHiddenSequences()
89               .getFullAlignment();
90     }
91     else
92     {
93       alignmentToExport = viewport.getAlignment();
94     }
95     alignmentStartEnd = viewport.getAlignment().getHiddenColumns()
96             .getVisibleStartAndEndIndex(alignmentToExport.getWidth());
97     AlignmentExportData ed = new AlignmentExportData(alignmentToExport,
98             omitHidden, alignmentStartEnd, this);
99     return ed;
100   }
101
102   @Override
103   public void ok_actionPerformed(ActionEvent e)
104   {
105     cancelled = false;
106   }
107
108   @Override
109   public void cancel_actionPerformed(ActionEvent e)
110   {
111     cancelled = true;
112   }
113
114   @Override
115   public boolean isExportHiddenSequences()
116   {
117     return override!=null ? override.isExportHiddenSequences() : chkHiddenSeqs.isSelected();
118   }
119
120   @Override
121   public boolean isExportHiddenColumns()
122   {
123     return override!=null ? override.isExportHiddenColumns() : chkHiddenCols.isSelected();
124   }
125
126   @Override
127   public boolean isExportAnnotations()
128   {
129     return override!=null ? override.isExportAnnotations() : chkExportAnnots.isSelected();
130   }
131
132   @Override
133   public boolean isExportFeatures()
134   {
135     return override!=null ? override.isExportFeatures() : chkExportFeats.isSelected();
136   }
137
138   @Override
139   public boolean isExportGroups()
140   {
141     return override!=null ? override.isExportGroups() : chkExportGrps.isSelected();
142   }
143
144   @Override
145   public boolean isCancelled()
146   {
147     return override!=null ? override.isCancelled() : cancelled;
148   }
149
150   ActionListener afterShownAction = null;
151
152   @Override
153   public void addActionListener(ActionListener actionListener)
154   {
155     afterShownAction = actionListener;
156   }
157
158   @Override
159   public void doShowSettings()
160   {
161     if (!Jalview.isHeadlessMode() && override==null && isShowDialog())
162     {
163
164       dialog = JvOptionPane.newOptionDialog(alignPanel);
165
166       dialog.response(new RunResponse(JvOptionPane.OK_OPTION)
167       {
168         public void run()
169         {
170           ok_actionPerformed(null);
171           afterShownAction.actionPerformed(null);
172         }
173       }).defaultResponse(new Runnable()
174       {
175         @Override
176         public void run()
177         {
178           cancelled=true;
179           afterShownAction.actionPerformed(null);
180         }
181       });
182       String ok = MessageManager.getString("action.ok"),
183               cancel = MessageManager.getString("action.cancel");
184       dialog.showInternalDialog(this,
185               MessageManager.getString("label.export_settings"),
186               JOptionPane.DEFAULT_OPTION, JvOptionPane.DEFAULT_OPTION, null,
187               new Object[]
188               { ok, cancel }, ok);
189     }
190     else
191     {
192       afterShownAction.actionPerformed(null);
193     }
194   }
195
196 }