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