b67a2f78d1d9edd807764066275d2ea761a86067
[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.bin.Cache;
24 import jalview.datamodel.AlignmentAnnotation;
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
37 import javax.swing.BorderFactory;
38 import javax.swing.ButtonGroup;
39 import javax.swing.JButton;
40 import javax.swing.JInternalFrame;
41 import javax.swing.JLabel;
42 import javax.swing.JLayeredPane;
43 import javax.swing.JPanel;
44 import javax.swing.JRadioButton;
45 import javax.swing.SwingConstants;
46
47 /**
48  * 
49  * GUI dialog for exporting features or alignment annotations depending upon
50  * which method is called.
51  * 
52  * @author AMW
53  * 
54  */
55 public class AnnotationExporter extends JPanel
56 {
57   JInternalFrame frame;
58
59   AlignmentPanel ap;
60
61   boolean features = true;
62
63   private AlignmentAnnotation[] annotations;
64
65   private boolean wholeView;
66
67   public AnnotationExporter()
68   {
69     try
70     {
71       jbInit();
72     } catch (Exception ex)
73     {
74       ex.printStackTrace();
75     }
76
77     frame = new JInternalFrame();
78     frame.setContentPane(this);
79     frame.setLayer(JLayeredPane.PALETTE_LAYER);
80     Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
81             frame.getPreferredSize().height);
82   }
83
84   public void exportFeatures(AlignmentPanel ap)
85   {
86     this.ap = ap;
87     features = true;
88     CSVFormat.setVisible(false);
89     frame.setTitle(MessageManager.getString("label.export_features"));
90   }
91
92   public void exportAnnotations(AlignmentPanel ap)
93   {
94     this.ap = ap;
95     annotations = ap.av.isShowAnnotation() ? null : ap.av.getAlignment()
96             .getAlignmentAnnotation();
97     wholeView = true;
98     startExportAnnotation();
99   }
100
101   public void exportAnnotations(AlignmentPanel alp,
102           AlignmentAnnotation[] toExport)
103   {
104     ap = alp;
105     annotations = toExport;
106     wholeView = false;
107     startExportAnnotation();
108   }
109
110   private void startExportAnnotation()
111   {
112     features = false;
113     GFFFormat.setVisible(false);
114     CSVFormat.setVisible(true);
115     frame.setTitle(MessageManager.getString("label.export_annotations"));
116   }
117
118   public void toFile_actionPerformed(ActionEvent e)
119   {
120     JalviewFileChooser chooser = new JalviewFileChooser(
121             Cache.getProperty("LAST_DIRECTORY"));
122
123     chooser.setFileView(new JalviewFileView());
124     chooser.setDialogTitle(features ? MessageManager.getString("label.save_features_to_file")
125             : MessageManager.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   public void toTextbox_actionPerformed(ActionEvent e)
191   {
192     CutAndPasteTransfer cap = new CutAndPasteTransfer();
193
194     try
195     {
196       String text = getFileContents();
197       cap.setText(text);
198       Desktop.addInternalFrame(
199               cap,
200               (features ? MessageManager.formatMessage(
201                       "label.features_for_params", new String[]
202                       { ap.alignFrame.getTitle() }) : MessageManager
203                       .formatMessage("label.annotations_for_params",
204                               new String[]
205                               { ap.alignFrame.getTitle() })), 600, 500);
206     } catch (OutOfMemoryError oom)
207     {
208       new OOMWarning((features ? MessageManager.formatMessage(
209               "label.generating_features_for_params", new String[]
210               { ap.alignFrame.getTitle() }) : MessageManager.formatMessage(
211               "label.generating_annotations_for_params", new String[]
212               { ap.alignFrame.getTitle() })), oom);
213       cap.dispose();
214     }
215
216     close_actionPerformed(null);
217   }
218   public void close_actionPerformed(ActionEvent e)
219   {
220     try
221     {
222       frame.setClosed(true);
223     } catch (java.beans.PropertyVetoException ex)
224     {
225     }
226   }
227
228   private void jbInit() throws Exception
229   {
230     this.setLayout(new BorderLayout());
231
232     toFile.setText(MessageManager.getString("label.to_file"));
233     toFile.addActionListener(new ActionListener()
234     {
235       public void actionPerformed(ActionEvent e)
236       {
237         toFile_actionPerformed(e);
238       }
239     });
240     toTextbox.setText(MessageManager.getString("label.to_textbox"));
241     toTextbox.addActionListener(new ActionListener()
242     {
243       public void actionPerformed(ActionEvent e)
244       {
245         toTextbox_actionPerformed(e);
246       }
247     });
248     close.setText(MessageManager.getString("action.close"));
249     close.addActionListener(new ActionListener()
250     {
251       public void actionPerformed(ActionEvent e)
252       {
253         close_actionPerformed(e);
254       }
255     });
256     jalviewFormat.setOpaque(false);
257     jalviewFormat.setSelected(true);
258     jalviewFormat.setText("Jalview");
259     GFFFormat.setOpaque(false);
260     GFFFormat.setText("GFF");
261     CSVFormat.setOpaque(false);
262     CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
263     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
264     jLabel1.setText(MessageManager.getString("action.format") + " ");
265     this.setBackground(Color.white);
266     jPanel3.setBorder(BorderFactory.createEtchedBorder());
267     jPanel3.setOpaque(false);
268     jPanel1.setOpaque(false);
269     jPanel1.add(toFile);
270     jPanel1.add(toTextbox);
271     jPanel1.add(close);
272     jPanel3.add(jLabel1);
273     jPanel3.add(jalviewFormat);
274     jPanel3.add(GFFFormat);
275     jPanel3.add(CSVFormat);
276     buttonGroup.add(jalviewFormat);
277     buttonGroup.add(GFFFormat);
278     buttonGroup.add(CSVFormat);
279     this.add(jPanel3, BorderLayout.CENTER);
280     this.add(jPanel1, BorderLayout.SOUTH);
281   }
282
283   JPanel jPanel1 = new JPanel();
284
285   JButton toFile = new JButton();
286
287   JButton toTextbox = new JButton();
288
289   JButton close = new JButton();
290
291   ButtonGroup buttonGroup = new ButtonGroup();
292
293   JRadioButton jalviewFormat = new JRadioButton();
294
295   JRadioButton GFFFormat = new JRadioButton();
296
297   JRadioButton CSVFormat = new JRadioButton();
298
299   JLabel jLabel1 = new JLabel();
300
301   JPanel jPanel3 = new JPanel();
302
303   FlowLayout flowLayout1 = new FlowLayout();
304 }