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