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