5d4d4909a610bd56647e5ef2c6a71461a80826bc
[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 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.getString("label.save_features_to_file")
124             : MessageManager.getString("label.save_annotation_to_file"));
125     chooser.setToolTipText(MessageManager.getString("action.save"));
126
127     int value = chooser.showSaveDialog(this);
128
129     if (value == JalviewFileChooser.APPROVE_OPTION)
130     {
131       String text = getFileContents();
132
133       try
134       {
135         java.io.PrintWriter out = new java.io.PrintWriter(
136                 new java.io.FileWriter(chooser.getSelectedFile()));
137
138         out.print(text);
139         out.close();
140       } catch (Exception ex)
141       {
142         ex.printStackTrace();
143       }
144     }
145
146     close_actionPerformed(null);
147   }
148
149   private String getFileContents()
150   {
151     String text = MessageManager
152             .getString("label.no_features_on_alignment");
153     if (features)
154     {
155       if (GFFFormat.isSelected())
156       {
157         text = new FeaturesFile().printGFFFormat(ap.av.getAlignment()
158                 .getDataset().getSequencesArray(), ap.getFeatureRenderer()
159                 .getDisplayedFeatureCols(), true, ap.av.isShowNpFeats());// ap.av.featuresDisplayed//);
160       }
161       else
162       {
163         text = new FeaturesFile().printJalviewFormat(ap.av.getAlignment()
164                 .getDataset().getSequencesArray(), ap.getFeatureRenderer()
165                 .getDisplayedFeatureCols(), true, ap.av.isShowNpFeats()); // ap.av.featuresDisplayed);
166       }
167     }
168     else
169     {
170       if (CSVFormat.isSelected())
171       {
172         text = new AnnotationFile().printCSVAnnotations(annotations);
173       }
174       else
175       {
176         if (wholeView)
177         {
178           text = new AnnotationFile().printAnnotationsForView(ap.av);
179         }
180         else
181         {
182           text = new AnnotationFile().printAnnotations(annotations, null,
183                   null);
184         }
185       }
186     }
187     return text;
188   }
189   public void toTextbox_actionPerformed(ActionEvent e)
190   {
191     CutAndPasteTransfer cap = new CutAndPasteTransfer();
192
193     try
194     {
195       String text = getFileContents();
196       cap.setText(text);
197       Desktop.addInternalFrame(
198               cap,
199               (features ? MessageManager.formatMessage(
200                       "label.features_for_params", new String[]
201                       { ap.alignFrame.getTitle() }) : MessageManager
202                       .formatMessage("label.annotations_for_params",
203                               new String[]
204                               { ap.alignFrame.getTitle() })), 600, 500);
205     } catch (OutOfMemoryError oom)
206     {
207       new OOMWarning((features ? MessageManager.formatMessage(
208               "label.generating_features_for_params", new String[]
209               { ap.alignFrame.getTitle() }) : MessageManager.formatMessage(
210               "label.generating_annotations_for_params", new String[]
211               { ap.alignFrame.getTitle() })), oom);
212       cap.dispose();
213     }
214
215     close_actionPerformed(null);
216   }
217   public void close_actionPerformed(ActionEvent e)
218   {
219     try
220     {
221       frame.setClosed(true);
222     } catch (java.beans.PropertyVetoException ex)
223     {
224     }
225   }
226
227   private void jbInit() throws Exception
228   {
229     this.setLayout(new BorderLayout());
230
231     toFile.setText(MessageManager.getString("label.to_file"));
232     toFile.addActionListener(new ActionListener()
233     {
234       public void actionPerformed(ActionEvent e)
235       {
236         toFile_actionPerformed(e);
237       }
238     });
239     toTextbox.setText(MessageManager.getString("label.to_textbox"));
240     toTextbox.addActionListener(new ActionListener()
241     {
242       public void actionPerformed(ActionEvent e)
243       {
244         toTextbox_actionPerformed(e);
245       }
246     });
247     close.setText(MessageManager.getString("action.close"));
248     close.addActionListener(new ActionListener()
249     {
250       public void actionPerformed(ActionEvent e)
251       {
252         close_actionPerformed(e);
253       }
254     });
255     jalviewFormat.setOpaque(false);
256     jalviewFormat.setSelected(true);
257     jalviewFormat.setText("Jalview");
258     GFFFormat.setOpaque(false);
259     GFFFormat.setText("GFF");
260     CSVFormat.setOpaque(false);
261     CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
262     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
263     jLabel1.setText(MessageManager.getString("action.format") + " ");
264     this.setBackground(Color.white);
265     jPanel3.setBorder(BorderFactory.createEtchedBorder());
266     jPanel3.setOpaque(false);
267     jPanel1.setOpaque(false);
268     jPanel1.add(toFile);
269     jPanel1.add(toTextbox);
270     jPanel1.add(close);
271     jPanel3.add(jLabel1);
272     jPanel3.add(jalviewFormat);
273     jPanel3.add(GFFFormat);
274     jPanel3.add(CSVFormat);
275     buttonGroup.add(jalviewFormat);
276     buttonGroup.add(GFFFormat);
277     buttonGroup.add(CSVFormat);
278     this.add(jPanel3, BorderLayout.CENTER);
279     this.add(jPanel1, BorderLayout.SOUTH);
280   }
281
282   JPanel jPanel1 = new JPanel();
283
284   JButton toFile = new JButton();
285
286   JButton toTextbox = new JButton();
287
288   JButton close = new JButton();
289
290   ButtonGroup buttonGroup = new ButtonGroup();
291
292   JRadioButton jalviewFormat = new JRadioButton();
293
294   JRadioButton GFFFormat = new JRadioButton();
295
296   JRadioButton CSVFormat = new JRadioButton();
297
298   JLabel jLabel1 = new JLabel();
299
300   JPanel jPanel3 = new JPanel();
301
302   FlowLayout flowLayout1 = new FlowLayout();
303 }