JAL-2015 JAL-1956 rollout of FeatureColourI in place of
[jalview.git] / src / jalview / gui / AnnotationExporter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.api.FeatureColourI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.io.AnnotationFile;
26 import jalview.io.FeaturesFile;
27 import jalview.io.JalviewFileChooser;
28 import jalview.io.JalviewFileView;
29 import jalview.util.MessageManager;
30
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.FlowLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.Map;
37
38 import javax.swing.BorderFactory;
39 import javax.swing.ButtonGroup;
40 import javax.swing.JButton;
41 import javax.swing.JInternalFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JLayeredPane;
44 import javax.swing.JPanel;
45 import javax.swing.JRadioButton;
46 import javax.swing.SwingConstants;
47
48 /**
49  * 
50  * GUI dialog for exporting features or alignment annotations depending upon
51  * which method is called.
52  * 
53  * @author AMW
54  * 
55  */
56 public class AnnotationExporter extends JPanel
57 {
58   JInternalFrame frame;
59
60   AlignmentPanel ap;
61
62   boolean features = true;
63
64   private AlignmentAnnotation[] annotations;
65
66   private boolean wholeView;
67
68   public AnnotationExporter()
69   {
70     try
71     {
72       jbInit();
73     } catch (Exception ex)
74     {
75       ex.printStackTrace();
76     }
77
78     frame = new JInternalFrame();
79     frame.setContentPane(this);
80     frame.setLayer(JLayeredPane.PALETTE_LAYER);
81     Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
82             frame.getPreferredSize().height);
83   }
84
85   public void exportFeatures(AlignmentPanel ap)
86   {
87     this.ap = ap;
88     features = true;
89     CSVFormat.setVisible(false);
90     frame.setTitle(MessageManager.getString("label.export_features"));
91   }
92
93   public void exportAnnotations(AlignmentPanel ap)
94   {
95     this.ap = ap;
96     annotations = ap.av.isShowAnnotation() ? null : ap.av.getAlignment()
97             .getAlignmentAnnotation();
98     wholeView = true;
99     startExportAnnotation();
100   }
101
102   public void exportAnnotations(AlignmentPanel alp,
103           AlignmentAnnotation[] toExport)
104   {
105     ap = alp;
106     annotations = toExport;
107     wholeView = false;
108     startExportAnnotation();
109   }
110
111   private void startExportAnnotation()
112   {
113     features = false;
114     GFFFormat.setVisible(false);
115     CSVFormat.setVisible(true);
116     frame.setTitle(MessageManager.getString("label.export_annotations"));
117   }
118
119   public void toFile_actionPerformed(ActionEvent e)
120   {
121     JalviewFileChooser chooser = new JalviewFileChooser(
122             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
123
124     chooser.setFileView(new JalviewFileView());
125     chooser.setDialogTitle(features ? MessageManager
126             .getString("label.save_features_to_file") : MessageManager
127             .getString("label.save_annotation_to_file"));
128     chooser.setToolTipText(MessageManager.getString("action.save"));
129
130     int value = chooser.showSaveDialog(this);
131
132     if (value == JalviewFileChooser.APPROVE_OPTION)
133     {
134       String text = getFileContents();
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   private String getFileContents()
153   {
154     String text = MessageManager
155             .getString("label.no_features_on_alignment");
156     if (features)
157     {
158       Map<String, FeatureColourI> displayedFeatureColours = ap
159               .getFeatureRenderer().getDisplayedFeatureCols();
160       if (GFFFormat.isSelected())
161       {
162         text = new FeaturesFile().printGFFFormat(ap.av.getAlignment()
163                 .getDataset().getSequencesArray(), displayedFeatureColours, true, ap.av.isShowNPFeats());// ap.av.featuresDisplayed//);
164       }
165       else
166       {
167         text = new FeaturesFile().printJalviewFormat(ap.av.getAlignment()
168                 .getDataset().getSequencesArray(), displayedFeatureColours, true, ap.av.isShowNPFeats()); // ap.av.featuresDisplayed);
169       }
170     }
171     else
172     {
173       if (CSVFormat.isSelected())
174       {
175         text = new AnnotationFile().printCSVAnnotations(annotations);
176       }
177       else
178       {
179         if (wholeView)
180         {
181           text = new AnnotationFile().printAnnotationsForView(ap.av);
182         }
183         else
184         {
185           text = new AnnotationFile().printAnnotations(annotations, null,
186                   null);
187         }
188       }
189     }
190     return text;
191   }
192
193   public void toTextbox_actionPerformed(ActionEvent e)
194   {
195     CutAndPasteTransfer cap = new CutAndPasteTransfer();
196
197     try
198     {
199       String text = getFileContents();
200       cap.setText(text);
201       Desktop.addInternalFrame(
202               cap,
203               (features ? MessageManager.formatMessage(
204                       "label.features_for_params",
205                       new String[] { ap.alignFrame.getTitle() })
206                       : MessageManager.formatMessage(
207                               "label.annotations_for_params",
208                               new String[] { ap.alignFrame.getTitle() })),
209               600, 500);
210     } catch (OutOfMemoryError oom)
211     {
212       new OOMWarning((features ? MessageManager.formatMessage(
213               "label.generating_features_for_params",
214               new String[] { ap.alignFrame.getTitle() })
215               : MessageManager.formatMessage(
216                       "label.generating_annotations_for_params",
217                       new String[] { ap.alignFrame.getTitle() })), oom);
218       cap.dispose();
219     }
220
221     close_actionPerformed(null);
222   }
223
224   public void close_actionPerformed(ActionEvent e)
225   {
226     try
227     {
228       frame.setClosed(true);
229     } catch (java.beans.PropertyVetoException ex)
230     {
231     }
232   }
233
234   private void jbInit() throws Exception
235   {
236     this.setLayout(new BorderLayout());
237
238     toFile.setText(MessageManager.getString("label.to_file"));
239     toFile.addActionListener(new ActionListener()
240     {
241       @Override
242       public void actionPerformed(ActionEvent e)
243       {
244         toFile_actionPerformed(e);
245       }
246     });
247     toTextbox.setText(MessageManager.getString("label.to_textbox"));
248     toTextbox.addActionListener(new ActionListener()
249     {
250       @Override
251       public void actionPerformed(ActionEvent e)
252       {
253         toTextbox_actionPerformed(e);
254       }
255     });
256     close.setText(MessageManager.getString("action.close"));
257     close.addActionListener(new ActionListener()
258     {
259       @Override
260       public void actionPerformed(ActionEvent e)
261       {
262         close_actionPerformed(e);
263       }
264     });
265     jalviewFormat.setOpaque(false);
266     jalviewFormat.setSelected(true);
267     jalviewFormat.setText("Jalview");
268     GFFFormat.setOpaque(false);
269     GFFFormat.setText("GFF");
270     CSVFormat.setOpaque(false);
271     CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
272     jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
273     jLabel1.setText(MessageManager.getString("action.format") + " ");
274     this.setBackground(Color.white);
275     jPanel3.setBorder(BorderFactory.createEtchedBorder());
276     jPanel3.setOpaque(false);
277     jPanel1.setOpaque(false);
278     jPanel1.add(toFile);
279     jPanel1.add(toTextbox);
280     jPanel1.add(close);
281     jPanel3.add(jLabel1);
282     jPanel3.add(jalviewFormat);
283     jPanel3.add(GFFFormat);
284     jPanel3.add(CSVFormat);
285     buttonGroup.add(jalviewFormat);
286     buttonGroup.add(GFFFormat);
287     buttonGroup.add(CSVFormat);
288     this.add(jPanel3, BorderLayout.CENTER);
289     this.add(jPanel1, BorderLayout.SOUTH);
290   }
291
292   JPanel jPanel1 = new JPanel();
293
294   JButton toFile = new JButton();
295
296   JButton toTextbox = new JButton();
297
298   JButton close = new JButton();
299
300   ButtonGroup buttonGroup = new ButtonGroup();
301
302   JRadioButton jalviewFormat = new JRadioButton();
303
304   JRadioButton GFFFormat = new JRadioButton();
305
306   JRadioButton CSVFormat = new JRadioButton();
307
308   JLabel jLabel1 = new JLabel();
309
310   JPanel jPanel3 = new JPanel();
311
312   FlowLayout flowLayout1 = new FlowLayout();
313 }