JAL-2069 ensure threshline instantiated to avoid NPE
[jalview.git] / src / jalview / gui / AnnotationExporter.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.FeatureColourI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.SequenceI;
26 import jalview.io.AnnotationFile;
27 import jalview.io.FeaturesFile;
28 import jalview.io.JalviewFileChooser;
29 import jalview.io.JalviewFileView;
30 import jalview.util.MessageManager;
31
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.FlowLayout;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.util.List;
38 import java.util.Map;
39
40 import javax.swing.BorderFactory;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JButton;
43 import javax.swing.JInternalFrame;
44 import javax.swing.JLabel;
45 import javax.swing.JLayeredPane;
46 import javax.swing.JPanel;
47 import javax.swing.JRadioButton;
48 import javax.swing.SwingConstants;
49
50 /**
51  * 
52  * GUI dialog for exporting features or alignment annotations depending upon
53  * which method is called.
54  * 
55  * @author AMW
56  * 
57  */
58 public class AnnotationExporter extends JPanel
59 {
60   JInternalFrame frame;
61
62   AlignmentPanel ap;
63
64   boolean features = true;
65
66   private AlignmentAnnotation[] annotations;
67
68   private boolean wholeView;
69
70   public AnnotationExporter()
71   {
72     try
73     {
74       jbInit();
75     } catch (Exception ex)
76     {
77       ex.printStackTrace();
78     }
79
80     frame = new JInternalFrame();
81     frame.setContentPane(this);
82     frame.setLayer(JLayeredPane.PALETTE_LAYER);
83     Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
84             frame.getPreferredSize().height);
85   }
86
87   public void exportFeatures(AlignmentPanel ap)
88   {
89     this.ap = ap;
90     features = true;
91     CSVFormat.setVisible(false);
92     frame.setTitle(MessageManager.getString("label.export_features"));
93   }
94
95   public void exportAnnotations(AlignmentPanel ap)
96   {
97     this.ap = ap;
98     annotations = ap.av.isShowAnnotation() ? null
99             : ap.av.getAlignment().getAlignmentAnnotation();
100     wholeView = true;
101     startExportAnnotation();
102   }
103
104   public void exportAnnotations(AlignmentPanel alp,
105           AlignmentAnnotation[] toExport)
106   {
107     ap = alp;
108     annotations = toExport;
109     wholeView = false;
110     startExportAnnotation();
111   }
112
113   private void startExportAnnotation()
114   {
115     features = false;
116     GFFFormat.setVisible(false);
117     CSVFormat.setVisible(true);
118     frame.setTitle(MessageManager.getString("label.export_annotations"));
119   }
120
121   public void toFile_actionPerformed(ActionEvent e)
122   {
123     JalviewFileChooser chooser = new JalviewFileChooser(
124             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
125
126     chooser.setFileView(new JalviewFileView());
127     chooser.setDialogTitle(features
128             ? MessageManager.getString("label.save_features_to_file")
129             : MessageManager.getString("label.save_annotation_to_file"));
130     chooser.setToolTipText(MessageManager.getString("action.save"));
131
132     int value = chooser.showSaveDialog(this);
133
134     if (value == JalviewFileChooser.APPROVE_OPTION)
135     {
136       String text = getFileContents();
137
138       try
139       {
140         java.io.PrintWriter out = new java.io.PrintWriter(
141                 new java.io.FileWriter(chooser.getSelectedFile()));
142
143         out.print(text);
144         out.close();
145       } catch (Exception ex)
146       {
147         ex.printStackTrace();
148       }
149     }
150
151     close_actionPerformed(null);
152   }
153
154   private String getFileContents()
155   {
156     String text = MessageManager
157             .getString("label.no_features_on_alignment");
158     if (features)
159     {
160       FeaturesFile formatter = new FeaturesFile();
161       SequenceI[] sequences = ap.av.getAlignment().getSequencesArray();
162       Map<String, FeatureColourI> featureColours = ap.getFeatureRenderer()
163               .getDisplayedFeatureCols();
164       List<String> featureGroups = ap.getFeatureRenderer()
165               .getDisplayedFeatureGroups();
166       boolean includeNonPositional = ap.av.isShowNPFeats();
167       if (GFFFormat.isSelected())
168       {
169         text = formatter.printGffFormat(sequences, featureColours,
170                 featureGroups, includeNonPositional);
171       }
172       else
173       {
174         text = formatter.printJalviewFormat(sequences, featureColours,
175                 featureGroups, includeNonPositional);
176       }
177     }
178     else
179     {
180       if (CSVFormat.isSelected())
181       {
182         text = new AnnotationFile().printCSVAnnotations(annotations);
183       }
184       else
185       {
186         if (wholeView)
187         {
188           text = new AnnotationFile().printAnnotationsForView(ap.av);
189         }
190         else
191         {
192           text = new AnnotationFile().printAnnotations(annotations, null,
193                   null);
194         }
195       }
196     }
197     return text;
198   }
199
200   public void toTextbox_actionPerformed(ActionEvent e)
201   {
202     CutAndPasteTransfer cap = new CutAndPasteTransfer();
203
204     try
205     {
206       String text = getFileContents();
207       cap.setText(text);
208       Desktop.addInternalFrame(cap, (features ? MessageManager
209               .formatMessage("label.features_for_params", new String[]
210               { ap.alignFrame.getTitle() })
211               : MessageManager.formatMessage("label.annotations_for_params",
212                       new String[]
213                       { ap.alignFrame.getTitle() })),
214               600, 500);
215     } catch (OutOfMemoryError oom)
216     {
217       new OOMWarning((features ? MessageManager.formatMessage(
218               "label.generating_features_for_params", new String[]
219               { ap.alignFrame.getTitle() })
220               : MessageManager.formatMessage(
221                       "label.generating_annotations_for_params",
222                       new String[]
223                       { ap.alignFrame.getTitle() })),
224               oom);
225       cap.dispose();
226     }
227
228     close_actionPerformed(null);
229   }
230
231   public void close_actionPerformed(ActionEvent e)
232   {
233     try
234     {
235       frame.setClosed(true);
236     } catch (java.beans.PropertyVetoException ex)
237     {
238     }
239   }
240
241   private void jbInit() throws Exception
242   {
243     this.setLayout(new BorderLayout());
244
245     toFile.setText(MessageManager.getString("label.to_file"));
246     toFile.addActionListener(new ActionListener()
247     {
248       @Override
249       public void actionPerformed(ActionEvent e)
250       {
251         toFile_actionPerformed(e);
252       }
253     });
254     toTextbox.setText(MessageManager.getString("label.to_textbox"));
255     toTextbox.addActionListener(new ActionListener()
256     {
257       @Override
258       public void actionPerformed(ActionEvent e)
259       {
260         toTextbox_actionPerformed(e);
261       }
262     });
263     close.setText(MessageManager.getString("action.close"));
264     close.addActionListener(new ActionListener()
265     {
266       @Override
267       public void actionPerformed(ActionEvent e)
268       {
269         close_actionPerformed(e);
270       }
271     });
272     jalviewFormat.setOpaque(false);
273     jalviewFormat.setSelected(true);
274     jalviewFormat.setText("Jalview");
275     GFFFormat.setOpaque(false);
276     GFFFormat.setText("GFF");
277     CSVFormat.setOpaque(false);
278     CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
279     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
280     jLabel1.setText(MessageManager.getString("action.format") + " ");
281     this.setBackground(Color.white);
282     jPanel3.setBorder(BorderFactory.createEtchedBorder());
283     jPanel3.setOpaque(false);
284     jPanel1.setOpaque(false);
285     jPanel1.add(toFile);
286     jPanel1.add(toTextbox);
287     jPanel1.add(close);
288     jPanel3.add(jLabel1);
289     jPanel3.add(jalviewFormat);
290     jPanel3.add(GFFFormat);
291     jPanel3.add(CSVFormat);
292     buttonGroup.add(jalviewFormat);
293     buttonGroup.add(GFFFormat);
294     buttonGroup.add(CSVFormat);
295     this.add(jPanel3, BorderLayout.CENTER);
296     this.add(jPanel1, BorderLayout.SOUTH);
297   }
298
299   JPanel jPanel1 = new JPanel();
300
301   JButton toFile = new JButton();
302
303   JButton toTextbox = new JButton();
304
305   JButton close = new JButton();
306
307   ButtonGroup buttonGroup = new ButtonGroup();
308
309   JRadioButton jalviewFormat = new JRadioButton();
310
311   JRadioButton GFFFormat = new JRadioButton();
312
313   JRadioButton CSVFormat = new JRadioButton();
314
315   JLabel jLabel1 = new JLabel();
316
317   JPanel jPanel3 = new JPanel();
318
319   FlowLayout flowLayout1 = new FlowLayout();
320 }