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.FeatureColourI;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.SequenceI;
27 import jalview.datamodel.features.FeatureMatcherSetI;
28 import jalview.io.AnnotationFile;
29 import jalview.io.FeaturesFile;
30 import jalview.io.JalviewFileChooser;
31 import jalview.io.JalviewFileView;
32 import jalview.util.MessageManager;
34 import java.awt.BorderLayout;
35 import java.awt.Color;
36 import java.awt.FlowLayout;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.io.FileWriter;
40 import java.io.PrintWriter;
41 import java.util.List;
44 import javax.swing.BorderFactory;
45 import javax.swing.ButtonGroup;
46 import javax.swing.JButton;
47 import javax.swing.JInternalFrame;
48 import javax.swing.JLabel;
49 import javax.swing.JLayeredPane;
50 import javax.swing.JPanel;
51 import javax.swing.JRadioButton;
52 import javax.swing.SwingConstants;
56 * GUI dialog for exporting features or alignment annotations depending upon
57 * which method is called.
62 public class AnnotationExporter extends JPanel
64 private JInternalFrame frame;
66 private AlignmentPanel ap;
69 * true if exporting features, false if exporting annotations
71 private boolean exportFeatures = true;
73 private AlignmentAnnotation[] annotations;
75 private boolean wholeView;
77 public AnnotationExporter(AlignmentPanel panel)
83 } catch (Exception ex)
88 frame = new JInternalFrame();
89 frame.setContentPane(this);
90 frame.setLayer(JLayeredPane.PALETTE_LAYER);
91 Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
92 frame.getPreferredSize().height);
96 * Configures the diglog for options to export visible features
98 public void exportFeatures()
100 exportFeatures = true;
101 CSVFormat.setVisible(false);
102 frame.setTitle(MessageManager.getString("label.export_features"));
106 * Configures the dialog for options to export all visible annotations
108 public void exportAnnotations()
110 boolean showAnnotation = ap.av.isShowAnnotation();
111 exportAnnotation(showAnnotation ? null
112 : ap.av.getAlignment().getAlignmentAnnotation(), true);
116 * Configures the dialog for options to export the given annotation row
120 public void exportAnnotation(AlignmentAnnotation toExport)
122 exportAnnotation(new AlignmentAnnotation[] { toExport }, false);
125 private void exportAnnotation(AlignmentAnnotation[] toExport,
126 boolean forWholeView)
128 wholeView = forWholeView;
129 annotations = toExport;
130 exportFeatures = false;
131 GFFFormat.setVisible(false);
132 CSVFormat.setVisible(true);
133 frame.setTitle(MessageManager.getString("label.export_annotations"));
136 private void toFile_actionPerformed()
138 // TODO: JAL-3048 JalviewFileChooser - Save option
139 JalviewFileChooser chooser = new JalviewFileChooser(
140 Cache.getProperty("LAST_DIRECTORY"));
142 chooser.setFileView(new JalviewFileView());
143 chooser.setDialogTitle(exportFeatures
144 ? MessageManager.getString("label.save_features_to_file")
145 : MessageManager.getString("label.save_annotation_to_file"));
146 chooser.setToolTipText(MessageManager.getString("action.save"));
148 int value = chooser.showSaveDialog(this);
150 if (value == JalviewFileChooser.APPROVE_OPTION)
152 String text = getText();
156 PrintWriter out = new PrintWriter(
157 new FileWriter(chooser.getSelectedFile()));
160 } catch (Exception ex)
162 ex.printStackTrace();
166 close_actionPerformed();
170 * Answers the text to output for either Features (in GFF or Jalview format) or
171 * Annotations (in CSV or Jalview format)
175 private String getText()
177 return exportFeatures ? getFeaturesText() : getAnnotationsText();
181 * Returns the text contents for output of annotations in either CSV or Jalview
186 private String getAnnotationsText()
189 if (CSVFormat.isSelected())
191 text = new AnnotationFile().printCSVAnnotations(annotations);
197 text = new AnnotationFile().printAnnotationsForView(ap.av);
201 text = new AnnotationFile().printAnnotations(annotations, null,
209 * Returns the text contents for output of features in either GFF or Jalview
214 private String getFeaturesText()
217 SequenceI[] sequences = ap.av.getAlignment().getSequencesArray();
218 Map<String, FeatureColourI> featureColours = ap.getFeatureRenderer()
219 .getDisplayedFeatureCols();
220 Map<String, FeatureMatcherSetI> featureFilters = ap.getFeatureRenderer()
221 .getFeatureFilters();
222 List<String> featureGroups = ap.getFeatureRenderer()
223 .getDisplayedFeatureGroups();
224 boolean includeNonPositional = ap.av.isShowNPFeats();
226 FeaturesFile formatter = new FeaturesFile();
227 if (GFFFormat.isSelected())
229 text = formatter.printGffFormat(sequences, featureColours,
230 featureGroups, includeNonPositional);
234 text = formatter.printJalviewFormat(sequences, featureColours,
235 featureFilters, featureGroups, includeNonPositional);
240 private void toTextbox_actionPerformed()
242 CutAndPasteTransfer cap = new CutAndPasteTransfer();
246 String text = getText();
248 Desktop.addInternalFrame(cap, (exportFeatures ? MessageManager
249 .formatMessage("label.features_for_params", new String[]
250 { ap.alignFrame.getTitle() })
251 : MessageManager.formatMessage("label.annotations_for_params",
253 { ap.alignFrame.getTitle() })),
255 } catch (OutOfMemoryError oom)
257 new OOMWarning((exportFeatures ? MessageManager.formatMessage(
258 "label.generating_features_for_params", new String[]
259 { ap.alignFrame.getTitle() })
260 : MessageManager.formatMessage(
261 "label.generating_annotations_for_params",
263 { ap.alignFrame.getTitle() })),
268 close_actionPerformed();
271 private void close_actionPerformed()
275 frame.setClosed(true);
276 } catch (java.beans.PropertyVetoException ex)
281 private void jbInit() throws Exception
283 this.setLayout(new BorderLayout());
285 toFile.setText(MessageManager.getString("label.to_file"));
286 toFile.addActionListener(new ActionListener()
289 public void actionPerformed(ActionEvent e)
291 toFile_actionPerformed();
294 toTextbox.setText(MessageManager.getString("label.to_textbox"));
295 toTextbox.addActionListener(new ActionListener()
298 public void actionPerformed(ActionEvent e)
300 toTextbox_actionPerformed();
303 close.setText(MessageManager.getString("action.close"));
304 close.addActionListener(new ActionListener()
307 public void actionPerformed(ActionEvent e)
309 close_actionPerformed();
312 jalviewFormat.setOpaque(false);
313 jalviewFormat.setSelected(true);
314 jalviewFormat.setText("Jalview");
315 GFFFormat.setOpaque(false);
316 GFFFormat.setText("GFF");
317 CSVFormat.setOpaque(false);
318 CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
319 jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
320 jLabel1.setText(MessageManager.getString("action.format") + " ");
321 this.setBackground(Color.white);
322 jPanel3.setBorder(BorderFactory.createEtchedBorder());
323 jPanel3.setOpaque(false);
324 jPanel1.setOpaque(false);
326 jPanel1.add(toTextbox);
328 jPanel3.add(jLabel1);
329 jPanel3.add(jalviewFormat);
330 jPanel3.add(GFFFormat);
331 jPanel3.add(CSVFormat);
332 buttonGroup.add(jalviewFormat);
333 buttonGroup.add(GFFFormat);
334 buttonGroup.add(CSVFormat);
335 this.add(jPanel3, BorderLayout.CENTER);
336 this.add(jPanel1, BorderLayout.SOUTH);
339 JPanel jPanel1 = new JPanel();
341 JButton toFile = new JButton();
343 JButton toTextbox = new JButton();
345 JButton close = new JButton();
347 ButtonGroup buttonGroup = new ButtonGroup();
349 JRadioButton jalviewFormat = new JRadioButton();
351 JRadioButton GFFFormat = new JRadioButton();
353 JRadioButton CSVFormat = new JRadioButton();
355 JLabel jLabel1 = new JLabel();
357 JPanel jPanel3 = new JPanel();
359 FlowLayout flowLayout1 = new FlowLayout();