001/*
002 *                    PDB web development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 *
015 * Created on Jul 28, 2009
016 * Created by ap3
017 *
018 */
019
020package org.biojava.nbio.structure.align.gui;
021
022import org.biojava.nbio.structure.Structure;
023import org.biojava.nbio.structure.align.gui.jmol.AbstractAlignmentJmol;
024import javax.swing.*;
025
026import java.awt.event.ActionEvent;
027import java.awt.event.ActionListener;
028import java.io.File;
029import java.io.FileWriter;
030import java.io.IOException;
031import java.io.PrintWriter;
032
033public class MyExportListener implements ActionListener{
034
035        AbstractAlignmentJmol parent;
036        MyExportListener(AbstractAlignmentJmol parent){
037                this.parent = parent;
038        }
039        @Override
040public void actionPerformed(ActionEvent arg0)
041        {
042                final JFileChooser fc = new JFileChooser();
043
044                int returnVal = fc.showSaveDialog(null);
045
046                if (returnVal == JFileChooser.APPROVE_OPTION) {
047                        File file = fc.getSelectedFile();
048                        //This is where a real application would open the file.
049                        System.out.println("Exporting PDB file to: " + file.getName());
050
051                        Structure s = parent.getStructure();
052
053                        try {
054                                PrintWriter pw = new PrintWriter(new FileWriter(file));
055                                pw.println(s.toPDB());
056                                pw.close();
057                        } catch (IOException e){
058                         JOptionPane.showMessageDialog(null,"Could not export file. Exception: " + e.getMessage());
059                        }
060
061
062                } else {
063                        System.out.println("Export command cancelled by user.");
064                }
065
066
067        }
068}