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     * @(#)GUITrackIO.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 javax.swing.filechooser.FileFilter;
029    
030    /**
031     * Static methods used to handle Track IO.
032     *
033     * @XXX Need to make it easy for users to set the initial default
034     * values for SHOW_ALL_FILES and USE_FILE_EXTENSIONS.
035     * @XXX Need to allow for user setting of valid file extensions.
036     *
037     * @author  Stephen Fisher
038     * @version $Id: GUITrackIO.java,v 1.1.2.9 2007/03/01 21:17:33 fisher Exp $
039     */
040    
041    public class GUITrackIO {
042    
043             /** 
044              * Define 'filter' as a static global variable so that it persists
045              * across instances of the fileChoosers and thus will 'remember'
046              * what the user last used.  This isn't in GUIUtils because the
047              * user might have a different 'default' here than in
048              * GUISequenceIO.
049              */
050             static FileFilter filter = null;
051    
052        //--------------------------------------------------------------------------
053        // Miscellaneous Methods
054    
055             /** 
056              * Use a FileChooser to select a Track file to load.  The file
057              * can have a FASTA, GenBank, GFF, or GloDB (binary) file format.
058              */
059             public static Track loadTrack() {
060                      HashMap file = GUIUtils.openFileChooser(GloDBUtils.TRACK, filter);
061                      if (file == null) return null;
062    
063                      // save the current filter for the next time we open a file
064                      filter = (FileFilter) file.get("filter");
065    
066                      String filename = (String) file.get("name");
067                      String desc = filter.getDescription();
068    
069                      TrackFile trackFile = FileIO.getTrackFileType(desc);
070                      if (trackFile == null) return null;
071    
072                      Track track = trackFile.load(filename);
073                      if (track == null) return null;
074                      String msg = "loadTrack(\"" + filename + "\", " + trackFile.getID() + ")";
075    
076                      // add command to history but don't run via console.
077                      // instead we're running it here so we can return a
078                      // reference to the Track loaded
079                      Root.runCommand(msg, false);
080                      return track;
081             }
082    
083             /** 
084              * Use a FileChooser to save the given Track, returning the
085              * filename.  The file can have a FASTA, GenBank, GFF, or GloDB
086              * (binary) file format.
087              */
088             public static String saveTrack(String track) {
089                      HashMap file = GUIUtils.saveFileChooser(GloDBUtils.TRACK, filter);
090                      if ((file == null) || (track == "")) return "";
091    
092                      // save the current filter for the next time we open a file
093                      filter = (FileFilter) file.get("filter");
094    
095                      String filename = (String) file.get("name");
096                      String desc = filter.getDescription();
097    
098                      TrackFile trackFile = FileIO.getTrackFileType(desc);
099                      if (trackFile == null) return "";
100    
101                      String msg = "saveTrack(\"" + track + "\", " + trackFile.getID() 
102                                    + ", \"" + filename + "\", 1)";
103    
104                      // run command in console
105                      Root.runCommand(msg, true);
106    
107                      return filename;
108             }
109    
110    } // GUITrackIO.java