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