001    /*
002     * Copyright 2007, 2012 Stephen Fisher and Junhyong Kim, University of
003     * Pennsylvania.
004     *
005     * This file is part of Glo-DB.
006     * 
007     * Glo-DB is free software: you can redistribute it and/or modify it
008     * under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (at your option) any later version.
011     * 
012     * Glo-DB is distributed in the hope that it will be useful, but
013     * WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * General Public License for more details.
016     * 
017     * You should have received a copy of the GNU General Public License
018     * along with Glo-DB. If not, see <http://www.gnu.org/licenses/>.
019     *
020     * @(#)GUISequenceIO.java
021     */
022    
023    package edu.upenn.gloDB.gui;
024    
025    import edu.upenn.gloDB.*;
026    import edu.upenn.gloDB.io.*;
027    import java.util.HashMap;
028    import java.util.Set;
029    import javax.swing.*;
030    import javax.swing.filechooser.FileFilter;
031    
032    /**
033     * Static methods used throughout the GUI.
034     *
035     * @author  Stephen Fisher
036     * @version $Id: GUISequenceIO.java,v 1.6.2.11 2007/03/01 21:17:33 fisher Exp $
037     */
038    
039    public class GUISequenceIO {
040    
041             /** 
042              * Define 'filter' as a static global variable so that it persists
043              * across instances of the fileChoosers and thus will 'remember'
044              * what the user last used.  This isn't in GUIUtils because the
045              * user might have a different 'default' here than in
046              * GUIFeatureIO.
047              */
048             static FileFilter filter = null;
049    
050        //--------------------------------------------------------------------------
051        // Miscellaneous Methods
052    
053             public static Sequence newSequence() {
054                      String msg = "Create a sequence with ID:";
055                      String id = JOptionPane.showInputDialog(null, msg,
056                                                                                                                                    "New Sequence",
057                                                                                                                                    JOptionPane.YES_NO_OPTION);
058                      if ((id != null) && (id.length() > 0)) {
059                                    msg = "newSequence(\"" + id + "\")";
060                                    
061                                    // add command to history but don't run via console.
062                                    // instead we're running it here so we can return a
063                                    // reference to the Set of Sequences loaded
064                                    Root.runCommand(msg, false);
065                                    
066                                    // create new sequence here
067                                    return new Sequence(id);
068                      } else {
069                                    return null;
070                      }
071             }
072    
073             /** 
074              * Use a FileChooser to select a Sequence file to load.  The file
075              * can have a FASTA, GenBank, or GloDB (binary) file format.
076              */
077             public static Set loadSequence() {
078                      HashMap file = GUIUtils.openFileChooser(GloDBUtils.SEQUENCE, filter);
079                      if (file == null) return null;
080    
081                      // save the current filter for the next time we open a file
082                      filter = (FileFilter) file.get("filter");
083    
084                      String filename = (String) file.get("name");
085                      String desc = filter.getDescription();
086    
087                      SequenceFile sequenceFile = FileIO.getSequenceFileType(desc);
088                      if (sequenceFile == null) return null;
089    
090                      Set sequences = sequenceFile.loadAll(filename);
091                      if (sequences == null) return null;
092                      String msg = "loadSequence(\"" + filename + "\", " + sequenceFile.getID() + ")";
093    
094                      // add command to history but don't run via console.
095                      // instead we're running it here so we can return a
096                      // reference to the Set of Sequences loaded
097                      Root.runCommand(msg, false);
098    
099                      return sequences;
100             }
101    
102             /** 
103              * Use a FileChooser to save the given Sequence, returning the
104              * filename.  The output file can have a FASTA, GenBank, or GloDB
105              * (binary) file format.
106              */
107             public static String saveSequence(String sequence) {
108                      HashMap file = GUIUtils.saveFileChooser(GloDBUtils.SEQUENCE, filter);
109                      if ((file == null) || (sequence == "")) return "";
110    
111                      // save the current filter for the next time we open a file
112                      filter = (FileFilter) file.get("filter");
113    
114                      String filename = (String) file.get("name");
115                      String desc = filter.getDescription();
116    
117                      SequenceFile sequenceFile = FileIO.getSequenceFileType(desc);
118                      if (sequenceFile == null) return "";
119    
120                      String msg = "saveSequence(\"" + sequence + "\", " + sequenceFile.getID() 
121                                    + ", \"" + filename + "\", 1)";
122    
123                      // run command in console
124                      Root.runCommand(msg, true);
125             
126                      return filename;
127             }
128    
129    } // GUISequenceIO.java