| 1 |
|
| 2 |
//============================================================================ |
| 3 |
// |
| 4 |
// file: SaveAsFastaAction.java |
| 5 |
// |
| 6 |
// Copyright (c) 2007, Michael E. Smoot |
| 7 |
// |
| 8 |
// This program is free software; you can redistribute it and/or modify it |
| 9 |
// under the terms of the GNU General Public License as published by the |
| 10 |
// Free Software Foundation; either version 2 of the License, or (at your |
| 11 |
// option) any later version. |
| 12 |
// |
| 13 |
// This program is distributed in the hope that it will be useful, but |
| 14 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 |
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 |
// for more details. |
| 17 |
// |
| 18 |
// You should have received a copy of the GNU General Public License along |
| 19 |
// with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
// |
| 22 |
//============================================================================ |
| 23 |
|
| 24 |
package amap; |
| 25 |
|
| 26 |
import java.util.*; |
| 27 |
import java.beans.*; |
| 28 |
import java.io.*; |
| 29 |
import javax.swing.*; |
| 30 |
import java.awt.event.ActionEvent; |
| 31 |
import java.awt.Component; |
| 32 |
|
| 33 |
|
| 34 |
public class SaveAsFastaAction extends AbstractAction implements PropertyChangeListener { |
| 35 |
|
| 36 |
Alignment a; |
| 37 |
Component parent; |
| 38 |
JFileChooser fc; |
| 39 |
|
| 40 |
SaveAsFastaAction(Component parent ) { |
| 41 |
super("Save as Mulitple FASTA"); |
| 42 |
this.parent = parent; |
| 43 |
fc = new JFileChooser(); |
| 44 |
PropertyChangeHandler.getPropertyChangeSupport() |
| 45 |
.addPropertyChangeListener(PropertyChangeIDs.CHANGE_ALIGNMENT.toString(), this); |
| 46 |
a = null; |
| 47 |
} |
| 48 |
|
| 49 |
public void actionPerformed(ActionEvent e) { |
| 50 |
System.out.println("saving as multiple fasta"); |
| 51 |
if ( a != null ) { |
| 52 |
try { |
| 53 |
int returnVal = fc.showSaveDialog(parent); |
| 54 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 55 |
File file = fc.getSelectedFile(); |
| 56 |
FileWriter fw = new FileWriter(file); |
| 57 |
String mfa = a.toMultiFasta(); |
| 58 |
fw.write(mfa,0,mfa.length()); |
| 59 |
fw.close(); |
| 60 |
} |
| 61 |
} catch (Exception ioe) { |
| 62 |
JOptionPane.showMessageDialog(parent, |
| 63 |
"Failed to Save Multiple FASTA file:\n\n" + ioe.getMessage(), |
| 64 |
"ERROR Saving File!", |
| 65 |
JOptionPane.ERROR_MESSAGE); |
| 66 |
ioe.printStackTrace(); |
| 67 |
} |
| 68 |
} else |
| 69 |
System.err.println("no alignment found"); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public void propertyChange(PropertyChangeEvent e) { |
| 74 |
a = (Alignment)e.getNewValue(); |
| 75 |
} |
| 76 |
} |