JAL-1482 feature renderer and manipulation code refactored to renderer and abstract...
[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(), ap
116                   .getFeatureRenderer().getDisplayedFeatureCols(), true,
117                   ap.av.isShowNpFeats());// ap.av.featuresDisplayed//);
118         }
119         else
120         {
121           text = new FeaturesFile().printJalviewFormat(ap.av.getAlignment()
122                   .getDataset().getSequencesArray(), ap
123                   .getFeatureRenderer().getDisplayedFeatureCols(), true,
124                   ap.av.isShowNpFeats()); // ap.av.featuresDisplayed);
125         }
126       }
127       else
128       {
129         if (CSVFormat.isSelected())
130         {
131           text = new AnnotationFile().printCSVAnnotations(annotations);
132         }
133         else
134         {
135           text = new AnnotationFile().printAnnotations(annotations,
136                   sequenceGroups, alignmentProperties);
137         }
138       }
139
140       try
141       {
142         java.io.PrintWriter out = new java.io.PrintWriter(
143                 new java.io.FileWriter(chooser.getSelectedFile()));
144
145         out.print(text);
146         out.close();
147       } catch (Exception ex)
148       {
149         ex.printStackTrace();
150       }
151     }
152
153     close_actionPerformed(null);
154   }
155
156   public void toTextbox_actionPerformed(ActionEvent e)
157   {
158     String text = "No features found on alignment";
159     if (features)
160     {
161       if (GFFFormat.isSelected())
162       {
163         text = new FeaturesFile().printGFFFormat(ap.av.getAlignment()
164                 .getDataset().getSequencesArray(), ap.getFeatureRenderer()
165                 .getDisplayedFeatureCols(), true, ap.av.isShowNpFeats());
166       }
167       else
168       {
169         text = new FeaturesFile().printJalviewFormat(ap.av.getAlignment()
170                 .getDataset().getSequencesArray(), ap.getFeatureRenderer()
171                 .getDisplayedFeatureCols(), true, ap.av.isShowNpFeats());
172       }
173     }
174     else if (!features)
175     {
176       if (CSVFormat.isSelected())
177       {
178         text = new AnnotationFile().printCSVAnnotations(annotations);
179       }
180       else
181       {
182         text = new AnnotationFile().printAnnotations(annotations,
183                 sequenceGroups, alignmentProperties);
184       }
185     }
186
187     CutAndPasteTransfer cap = new CutAndPasteTransfer();
188     try
189     {
190       cap.setText(text);
191       Desktop.addInternalFrame(
192               cap,
193               (features ? MessageManager.formatMessage(
194                       "label.features_for_params", new String[]
195                       { ap.alignFrame.getTitle() }) : MessageManager
196                       .formatMessage("label.annotations_for_params",
197                               new String[]
198                               { ap.alignFrame.getTitle() })), 600, 500);
199     } catch (OutOfMemoryError oom)
200     {
201       new OOMWarning((features ? MessageManager.formatMessage(
202               "label.generating_features_for_params", new String[]
203               { ap.alignFrame.getTitle() }) : MessageManager.formatMessage(
204               "label.generating_annotations_for_params", new String[]
205               { ap.alignFrame.getTitle() })), oom);
206       cap.dispose();
207     }
208
209     close_actionPerformed(null);
210   }
211
212   public void close_actionPerformed(ActionEvent e)
213   {
214     try
215     {
216       frame.setClosed(true);
217     } catch (java.beans.PropertyVetoException ex)
218     {
219     }
220   }
221
222   private void jbInit() throws Exception
223   {
224     this.setLayout(new BorderLayout());
225
226     toFile.setText(MessageManager.getString("label.to_file"));
227     toFile.addActionListener(new ActionListener()
228     {
229       public void actionPerformed(ActionEvent e)
230       {
231         toFile_actionPerformed(e);
232       }
233     });
234     toTextbox.setText(MessageManager.getString("label.to_textbox"));
235     toTextbox.addActionListener(new ActionListener()
236     {
237       public void actionPerformed(ActionEvent e)
238       {
239         toTextbox_actionPerformed(e);
240       }
241     });
242     close.setText(MessageManager.getString("action.close"));
243     close.addActionListener(new ActionListener()
244     {
245       public void actionPerformed(ActionEvent e)
246       {
247         close_actionPerformed(e);
248       }
249     });
250     jalviewFormat.setOpaque(false);
251     jalviewFormat.setSelected(true);
252     jalviewFormat.setText("Jalview");
253     GFFFormat.setOpaque(false);
254     GFFFormat.setText("GFF");
255     CSVFormat.setOpaque(false);
256     CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
257     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
258     jLabel1.setText(MessageManager.getString("action.format") + " ");
259     this.setBackground(Color.white);
260     jPanel3.setBorder(BorderFactory.createEtchedBorder());
261     jPanel3.setOpaque(false);
262     jPanel1.setOpaque(false);
263     jPanel1.add(toFile);
264     jPanel1.add(toTextbox);
265     jPanel1.add(close);
266     jPanel3.add(jLabel1);
267     jPanel3.add(jalviewFormat);
268     jPanel3.add(GFFFormat);
269     jPanel3.add(CSVFormat);
270     buttonGroup.add(jalviewFormat);
271     buttonGroup.add(GFFFormat);
272     buttonGroup.add(CSVFormat);
273     this.add(jPanel3, BorderLayout.CENTER);
274     this.add(jPanel1, BorderLayout.SOUTH);
275   }
276
277   JPanel jPanel1 = new JPanel();
278
279   JButton toFile = new JButton();
280
281   JButton toTextbox = new JButton();
282
283   JButton close = new JButton();
284
285   ButtonGroup buttonGroup = new ButtonGroup();
286
287   JRadioButton jalviewFormat = new JRadioButton();
288
289   JRadioButton GFFFormat = new JRadioButton();
290
291   JRadioButton CSVFormat = new JRadioButton();
292
293   JLabel jLabel1 = new JLabel();
294
295   JPanel jPanel3 = new JPanel();
296
297   FlowLayout flowLayout1 = new FlowLayout();
298
299 }