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