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