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