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