2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.api.FeatureRenderer;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.AnnotationFile;
28 import jalview.io.FeaturesFile;
29 import jalview.io.JalviewFileChooser;
30 import jalview.io.JalviewFileView;
31 import jalview.util.MessageManager;
33 import java.awt.BorderLayout;
34 import java.awt.Color;
35 import java.awt.FlowLayout;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.io.FileWriter;
39 import java.io.PrintWriter;
41 import javax.swing.BorderFactory;
42 import javax.swing.ButtonGroup;
43 import javax.swing.JButton;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JLayeredPane;
47 import javax.swing.JPanel;
48 import javax.swing.JRadioButton;
49 import javax.swing.SwingConstants;
53 * GUI dialog for exporting features or alignment annotations depending upon
54 * which method is called.
59 public class AnnotationExporter extends JPanel
61 private JInternalFrame frame;
63 private AlignmentPanel ap;
66 * true if exporting features, false if exporting annotations
68 private boolean exportFeatures = true;
70 private AlignmentAnnotation[] annotations;
72 private boolean wholeView;
74 public AnnotationExporter(AlignmentPanel panel)
80 } catch (Exception ex)
85 frame = new JInternalFrame();
86 frame.setContentPane(this);
87 frame.setLayer(JLayeredPane.PALETTE_LAYER);
88 Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
89 frame.getPreferredSize().height);
93 * Configures the diglog for options to export visible features
95 public void exportFeatures()
97 exportFeatures = true;
98 CSVFormat.setVisible(false);
99 frame.setTitle(MessageManager.getString("label.export_features"));
103 * Configures the dialog for options to export all visible annotations
105 public void exportAnnotations()
107 boolean showAnnotation = ap.av.isShowAnnotation();
108 exportAnnotation(showAnnotation ? null
109 : ap.av.getAlignment().getAlignmentAnnotation(), true);
113 * Configures the dialog for options to export the given annotation row
117 public void exportAnnotation(AlignmentAnnotation toExport)
119 exportAnnotation(new AlignmentAnnotation[] { toExport }, false);
122 private void exportAnnotation(AlignmentAnnotation[] toExport,
123 boolean forWholeView)
125 wholeView = forWholeView;
126 annotations = toExport;
127 exportFeatures = false;
128 GFFFormat.setVisible(false);
129 CSVFormat.setVisible(true);
130 frame.setTitle(MessageManager.getString("label.export_annotations"));
133 private void toFile_actionPerformed()
135 // TODO: JAL-3048 JalviewFileChooser - Save option
136 JalviewFileChooser chooser = new JalviewFileChooser(
137 Cache.getProperty("LAST_DIRECTORY"));
139 chooser.setFileView(new JalviewFileView());
140 chooser.setDialogTitle(exportFeatures
141 ? MessageManager.getString("label.save_features_to_file")
142 : MessageManager.getString("label.save_annotation_to_file"));
143 chooser.setToolTipText(MessageManager.getString("action.save"));
145 int value = chooser.showSaveDialog(this);
147 if (value == JalviewFileChooser.APPROVE_OPTION)
149 String text = getText();
153 PrintWriter out = new PrintWriter(
154 new FileWriter(chooser.getSelectedFile()));
157 } catch (Exception ex)
159 ex.printStackTrace();
163 close_actionPerformed();
167 * Answers the text to output for either Features (in GFF or Jalview format) or
168 * Annotations (in CSV or Jalview format)
172 private String getText()
174 return exportFeatures ? getFeaturesText() : getAnnotationsText();
178 * Returns the text contents for output of annotations in either CSV or Jalview
183 private String getAnnotationsText()
186 if (CSVFormat.isSelected())
188 text = new AnnotationFile().printCSVAnnotations(annotations);
194 text = new AnnotationFile().printAnnotationsForView(ap.av);
198 text = new AnnotationFile().printAnnotations(annotations, null,
206 * Returns the text contents for output of features in either GFF or Jalview
211 private String getFeaturesText()
214 SequenceI[] sequences = ap.av.getAlignment().getSequencesArray();
215 boolean includeNonPositional = ap.av.isShowNPFeats();
217 FeaturesFile formatter = new FeaturesFile();
218 final FeatureRenderer fr = ap.getFeatureRenderer();
219 if (GFFFormat.isSelected())
221 text = formatter.printGffFormat(sequences, fr, includeNonPositional);
225 text = formatter.printJalviewFormat(sequences, fr,
226 includeNonPositional);
231 private void toTextbox_actionPerformed()
233 CutAndPasteTransfer cap = new CutAndPasteTransfer();
237 String text = getText();
239 Desktop.addInternalFrame(cap, (exportFeatures ? MessageManager
240 .formatMessage("label.features_for_params", new String[]
241 { ap.alignFrame.getTitle() })
242 : MessageManager.formatMessage("label.annotations_for_params",
244 { ap.alignFrame.getTitle() })),
246 } catch (OutOfMemoryError oom)
248 new OOMWarning((exportFeatures ? MessageManager.formatMessage(
249 "label.generating_features_for_params", new String[]
250 { ap.alignFrame.getTitle() })
251 : MessageManager.formatMessage(
252 "label.generating_annotations_for_params",
254 { ap.alignFrame.getTitle() })),
259 close_actionPerformed();
262 private void close_actionPerformed()
266 frame.setClosed(true);
267 } catch (java.beans.PropertyVetoException ex)
272 private void jbInit() throws Exception
274 this.setLayout(new BorderLayout());
276 toFile.setText(MessageManager.getString("label.to_file"));
277 toFile.addActionListener(new ActionListener()
280 public void actionPerformed(ActionEvent e)
282 toFile_actionPerformed();
285 toTextbox.setText(MessageManager.getString("label.to_textbox"));
286 toTextbox.addActionListener(new ActionListener()
289 public void actionPerformed(ActionEvent e)
291 toTextbox_actionPerformed();
294 close.setText(MessageManager.getString("action.close"));
295 close.addActionListener(new ActionListener()
298 public void actionPerformed(ActionEvent e)
300 close_actionPerformed();
303 jalviewFormat.setOpaque(false);
304 jalviewFormat.setSelected(true);
305 jalviewFormat.setText("Jalview");
306 GFFFormat.setOpaque(false);
307 GFFFormat.setText("GFF");
308 CSVFormat.setOpaque(false);
309 CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
310 jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
311 jLabel1.setText(MessageManager.getString("action.format") + " ");
312 this.setBackground(Color.white);
313 jPanel3.setBorder(BorderFactory.createEtchedBorder());
314 jPanel3.setOpaque(false);
315 jPanel1.setOpaque(false);
317 jPanel1.add(toTextbox);
319 jPanel3.add(jLabel1);
320 jPanel3.add(jalviewFormat);
321 jPanel3.add(GFFFormat);
322 jPanel3.add(CSVFormat);
323 buttonGroup.add(jalviewFormat);
324 buttonGroup.add(GFFFormat);
325 buttonGroup.add(CSVFormat);
326 this.add(jPanel3, BorderLayout.CENTER);
327 this.add(jPanel1, BorderLayout.SOUTH);
330 JPanel jPanel1 = new JPanel();
332 JButton toFile = new JButton();
334 JButton toTextbox = new JButton();
336 JButton close = new JButton();
338 ButtonGroup buttonGroup = new ButtonGroup();
340 JRadioButton jalviewFormat = new JRadioButton();
342 JRadioButton GFFFormat = new JRadioButton();
344 JRadioButton CSVFormat = new JRadioButton();
346 JLabel jLabel1 = new JLabel();
348 JPanel jPanel3 = new JPanel();
350 FlowLayout flowLayout1 = new FlowLayout();