56c6e2de45b97e9c5443d6df60fa66acb88f9902
[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(MessageManager.getString("action.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 ? MessageManager.formatMessage("label.features_for_params", new String[]{ap.alignFrame.getTitle()})
188               : MessageManager.formatMessage("label.annotations_for_params", new String[]{ap.alignFrame.getTitle()})), 600, 500);
189     } catch (OutOfMemoryError oom)
190     {
191       new OOMWarning((features ? MessageManager.formatMessage("label.generating_features_for_params", new String[]{ap.alignFrame.getTitle()}) : MessageManager.formatMessage("label.generating_annotations_for_params", new String[]{ap.alignFrame.getTitle()}))
192               , 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(MessageManager.getString("label.to_file"));
234     toFile.addActionListener(new ActionListener()
235     {
236       public void actionPerformed(ActionEvent e)
237       {
238         toFile_actionPerformed(e);
239       }
240     });
241     toTextbox.setText(MessageManager.getString("label.to_textbox"));
242     toTextbox.addActionListener(new ActionListener()
243     {
244       public void actionPerformed(ActionEvent e)
245       {
246         toTextbox_actionPerformed(e);
247       }
248     });
249     close.setText(MessageManager.getString("action.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(MessageManager.getString("label.csv_spreadsheet"));
264     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
265     jLabel1.setText(MessageManager.getString("action.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 }