e255cd676acafa7b1aba62d548263eb44def3c0e
[jalview.git] / src / jalview / gui / AnnotationExporter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.util.*;
21 import java.util.List;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26
27 import jalview.datamodel.*;
28 import jalview.io.*;
29
30 /**
31  * 
32  * GUI dialog for exporting features or alignment annotations depending upon
33  * which method is called.
34  * 
35  * @author AMW
36  * 
37  */
38 public class AnnotationExporter extends JPanel
39 {
40   JInternalFrame frame;
41
42   AlignmentPanel ap;
43
44   boolean features = true;
45
46   AlignmentAnnotation[] annotations;
47
48   List<SequenceGroup> sequenceGroups;
49
50   Hashtable alignmentProperties;
51
52   public AnnotationExporter()
53   {
54     try
55     {
56       jbInit();
57     } catch (Exception ex)
58     {
59       ex.printStackTrace();
60     }
61
62     frame = new JInternalFrame();
63     frame.setContentPane(this);
64     frame.setLayer(JLayeredPane.PALETTE_LAYER);
65     Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
66             frame.getPreferredSize().height);
67   }
68
69   public void exportFeatures(AlignmentPanel ap)
70   {
71     this.ap = ap;
72     features = true;
73     CSVFormat.setVisible(false);
74     frame.setTitle("Export Features");
75   }
76
77   public void exportAnnotations(AlignmentPanel ap,
78           AlignmentAnnotation[] annotations, List<SequenceGroup> list,
79           Hashtable alProperties)
80   {
81     this.ap = ap;
82     features = false;
83     GFFFormat.setVisible(false);
84     CSVFormat.setVisible(true);
85     this.annotations = annotations;
86     this.sequenceGroups = list;
87     this.alignmentProperties = alProperties;
88     frame.setTitle("Export Annotations");
89   }
90
91   public void toFile_actionPerformed(ActionEvent e)
92   {
93     JalviewFileChooser chooser = new JalviewFileChooser(
94             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
95
96     chooser.setFileView(new JalviewFileView());
97     chooser.setDialogTitle(features ? "Save Features to File"
98             : "Save Annotation to File");
99     chooser.setToolTipText("Save");
100
101     int value = chooser.showSaveDialog(this);
102
103     if (value == JalviewFileChooser.APPROVE_OPTION)
104     {
105       String text = "No features found on alignment";
106       if (features)
107       {
108         if (GFFFormat.isSelected())
109         {
110           text = new FeaturesFile().printGFFFormat(ap.av.getAlignment()
111                   .getDataset().getSequencesArray(),
112                   getDisplayedFeatureCols(), true, ap.av.isShowNpFeats());// ap.av.featuresDisplayed//);
113         }
114         else
115         {
116           text = new FeaturesFile().printJalviewFormat(ap.av.getAlignment()
117                   .getDataset().getSequencesArray(),
118                   getDisplayedFeatureCols(), true, ap.av.isShowNpFeats()); // ap.av.featuresDisplayed);
119         }
120       }
121       else
122       {
123         if (CSVFormat.isSelected())
124         {
125           text = new AnnotationFile().printCSVAnnotations(annotations);
126         }
127         else
128         {
129           text = new AnnotationFile().printAnnotations(annotations,
130                   sequenceGroups, alignmentProperties);
131         }
132       }
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   public void toTextbox_actionPerformed(ActionEvent e)
151   {
152     String text = "No features found on alignment";
153     if (features)
154     {
155       if (GFFFormat.isSelected())
156       {
157         text = new FeaturesFile().printGFFFormat(ap.av.getAlignment()
158                 .getDataset().getSequencesArray(),
159                 getDisplayedFeatureCols(), true, ap.av.isShowNpFeats());
160       }
161       else
162       {
163         text = new FeaturesFile().printJalviewFormat(ap.av.getAlignment()
164                 .getDataset().getSequencesArray(),
165                 getDisplayedFeatureCols(), true, ap.av.isShowNpFeats());
166       }
167     }
168     else if (!features)
169     {
170       if (CSVFormat.isSelected())
171       {
172         text = new AnnotationFile().printCSVAnnotations(annotations);
173       }
174       else
175       {
176         text = new AnnotationFile().printAnnotations(annotations,
177                 sequenceGroups, alignmentProperties);
178       }
179     }
180
181     CutAndPasteTransfer cap = new CutAndPasteTransfer();
182     try
183     {
184       cap.setText(text);
185       Desktop.addInternalFrame(cap, (features ? "Features for - "
186               : "Annotations for - ") + ap.alignFrame.getTitle(), 600, 500);
187     } catch (OutOfMemoryError oom)
188     {
189       new OOMWarning("generating "
190               + (features ? "Features for - " : "Annotations for - ")
191               + ap.alignFrame.getTitle(), oom);
192       cap.dispose();
193     }
194
195     close_actionPerformed(null);
196   }
197
198   private Hashtable getDisplayedFeatureCols()
199   {
200     Hashtable fcols = new Hashtable();
201     if (ap.av.featuresDisplayed == null)
202     {
203       return fcols;
204     }
205     Enumeration en = ap.av.featuresDisplayed.keys();
206     FeatureRenderer fr = ap.seqPanel.seqCanvas.getFeatureRenderer(); // consider
207                                                                      // higher
208                                                                      // level
209                                                                      // method ?
210     while (en.hasMoreElements())
211     {
212       Object col = en.nextElement();
213       fcols.put(col, fr.featureColours.get(col));
214     }
215     return fcols;
216   }
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("to File");
233     toFile.addActionListener(new ActionListener()
234     {
235       public void actionPerformed(ActionEvent e)
236       {
237         toFile_actionPerformed(e);
238       }
239     });
240     toTextbox.setText("to Textbox");
241     toTextbox.addActionListener(new ActionListener()
242     {
243       public void actionPerformed(ActionEvent e)
244       {
245         toTextbox_actionPerformed(e);
246       }
247     });
248     close.setText("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("CSV(Spreadsheet)");
263     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
264     jLabel1.setText("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
305 }