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