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