JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / gui / AlignExportOptions.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.AlignExportSettingsI;
24 import jalview.api.AlignViewportI;
25 import jalview.io.FileFormatI;
26 import jalview.util.MessageManager;
27
28 import java.awt.BorderLayout;
29 import java.awt.FlowLayout;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.ItemEvent;
33 import java.awt.event.ItemListener;
34
35 import javax.swing.JCheckBox;
36 import javax.swing.JPanel;
37
38 /**
39  * A dialog that allows the user to specify whether to include hidden columns or
40  * sequences in an alignment export, and possibly features, annotations and
41  * groups, if applicable to the output file format
42  */
43 @SuppressWarnings("serial")
44 public class AlignExportOptions extends JPanel
45 {
46   protected JCheckBox chkHiddenSeqs = new JCheckBox();
47
48   protected JCheckBox chkHiddenCols = new JCheckBox();
49
50   protected JCheckBox chkExportAnnots = new JCheckBox();
51
52   protected JCheckBox chkExportFeats = new JCheckBox();
53
54   protected JCheckBox chkExportGrps = new JCheckBox();
55
56   protected AlignExportSettingsI settings;
57
58   private boolean isComplexAlignFile;
59
60   JvOptionPane dialog;
61
62   /**
63    * A convenience method that answers true if this dialog should be shown -
64    * that is, if the alignment has any hidden rows or columns, or if the file
65    * format is one that can (optionally) represent annotations, features or
66    * groups data - else false
67    * 
68    * @param viewport
69    * @param format
70    * @return
71    */
72   public static boolean isNeeded(AlignViewportI viewport,
73           FileFormatI format)
74   {
75     if (viewport.hasHiddenColumns() || viewport.hasHiddenRows()
76             || format.isComplexAlignFile())
77     {
78       return true;
79     }
80     return false;
81   }
82
83   /**
84    * Constructor that passes in an initial set of export options. User choices
85    * in the dialog should update this object, and the <em>same</em> object
86    * should be used in any action handler set by calling
87    * <code>setResponseAction</code>.
88    * 
89    * @param viewport
90    * @param format
91    * @param defaults
92    */
93   public AlignExportOptions(AlignViewportI viewport, FileFormatI format,
94           AlignExportSettingsI defaults)
95   {
96     this.settings = defaults;
97     this.isComplexAlignFile = format.isComplexAlignFile();
98     init(viewport.hasHiddenRows(), viewport.hasHiddenColumns());
99     dialog = JvOptionPane.newOptionDialog(Desktop.desktop);
100   }
101
102   /**
103    * Shows the dialog, and runs any registered response actions that correspond
104    * to user choices
105    */
106   public void showDialog()
107   {
108     Object[] options = new Object[] { MessageManager.getString("action.ok"),
109         MessageManager.getString("action.cancel") };
110     dialog.showInternalDialog(this,
111             MessageManager.getString("label.export_settings"),
112             JvOptionPane.OK_CANCEL_OPTION, JvOptionPane.PLAIN_MESSAGE, null,
113             options, MessageManager.getString("action.ok"));
114   }
115
116   /**
117    * Registers a Runnable action to be performed for a particular user response
118    * in the dialog
119    * 
120    * @param action
121    */
122   public void setResponseAction(Object response, Runnable action)
123   {
124     dialog.setResponseHandler(response, action);
125   }
126
127   /**
128    * Selects/deselects all enabled and shown options on 'Check all' selected or
129    * deselected
130    * 
131    * @param isSelected
132    */
133   void checkAllAction(boolean isSelected)
134   {
135     boolean set = chkHiddenSeqs.isEnabled() && isSelected;
136     chkHiddenSeqs.setSelected(set);
137     settings.setExportHiddenSequences(set);
138
139     set = chkHiddenCols.isEnabled() && isSelected;
140     chkHiddenCols.setSelected(set);
141     settings.setExportHiddenColumns(set);
142
143     set = isComplexAlignFile && chkExportAnnots.isEnabled() && isSelected;
144     chkExportAnnots.setSelected(set);
145     settings.setExportAnnotations(set);
146
147     set = isComplexAlignFile && chkExportFeats.isEnabled() && isSelected;
148     chkExportFeats.setSelected(set);
149     settings.setExportFeatures(set);
150
151     set = isComplexAlignFile && chkExportGrps.isEnabled() && isSelected;
152     chkExportGrps.setSelected(set);
153     settings.setExportGroups(set);
154   }
155
156   /**
157    * Initialises the components of the display
158    * 
159    * @param hasHiddenSeq
160    * @param hasHiddenCols
161    */
162   private void init(boolean hasHiddenSeq, boolean hasHiddenCols)
163   {
164     chkHiddenSeqs.setText(
165             MessageManager.getString("action.export_hidden_sequences"));
166     chkHiddenSeqs.addActionListener(new ActionListener()
167     {
168       @Override
169       public void actionPerformed(ActionEvent e)
170       {
171         settings.setExportHiddenSequences(chkHiddenSeqs.isSelected());
172       }
173     });
174
175     chkHiddenCols.setText(
176             MessageManager.getString("action.export_hidden_columns"));
177     chkHiddenCols.addActionListener(new ActionListener()
178     {
179       @Override
180       public void actionPerformed(ActionEvent e)
181       {
182         settings.setExportHiddenColumns(chkHiddenCols.isSelected());
183       }
184     });
185
186     chkExportAnnots
187             .setText(MessageManager.getString("action.export_annotations"));
188     chkExportAnnots.addActionListener(new ActionListener()
189     {
190       @Override
191       public void actionPerformed(ActionEvent e)
192       {
193         settings.setExportAnnotations(chkExportAnnots.isSelected());
194       }
195     });
196
197     chkExportFeats
198             .setText(MessageManager.getString("action.export_features"));
199     chkExportFeats.addActionListener(new ActionListener()
200     {
201       @Override
202       public void actionPerformed(ActionEvent e)
203       {
204         settings.setExportFeatures(chkExportFeats.isSelected());
205       }
206     });
207
208     chkExportGrps.setText(MessageManager.getString("action.export_groups"));
209     chkExportGrps.addActionListener(new ActionListener()
210     {
211       @Override
212       public void actionPerformed(ActionEvent e)
213       {
214         settings.setExportGroups(chkExportGrps.isSelected());
215       }
216     });
217
218     JCheckBox chkAll = new JCheckBox(
219             MessageManager.getString("action.select_all"));
220
221     JPanel hiddenRegionConfPanel = new JPanel(new BorderLayout());
222     JPanel complexExportPanel = new JPanel(new BorderLayout());
223     this.setLayout(new BorderLayout());
224
225     chkAll.addItemListener(new ItemListener()
226     {
227       @Override
228       public void itemStateChanged(ItemEvent e)
229       {
230         checkAllAction(chkAll.isSelected());
231       }
232     });
233
234     hiddenRegionConfPanel.add(chkHiddenSeqs, BorderLayout.CENTER);
235     hiddenRegionConfPanel.add(chkHiddenCols, BorderLayout.SOUTH);
236     chkHiddenSeqs.setEnabled(hasHiddenSeq);
237     chkHiddenCols.setEnabled(hasHiddenCols);
238
239     complexExportPanel.add(chkExportAnnots, BorderLayout.NORTH);
240     complexExportPanel.add(chkExportFeats, BorderLayout.CENTER);
241     complexExportPanel.add(chkExportGrps, BorderLayout.SOUTH);
242
243     JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
244     actionPanel.add(chkAll);
245
246     JPanel optionsPanel = new JPanel();
247     if (this.isComplexAlignFile)
248     {
249       optionsPanel.add(complexExportPanel);
250     }
251
252     if (hasHiddenSeq || hasHiddenCols)
253     {
254       optionsPanel.add(hiddenRegionConfPanel);
255     }
256
257     add(optionsPanel, BorderLayout.NORTH);
258     add(actionPanel, BorderLayout.SOUTH);
259   }
260 }