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