001    /*
002     * LAPIS lightweight structured text processing system
003     *
004     * Copyright (C) 1998-2002 Carnegie Mellon University,
005     * Copyright (C) 2003 Massachusetts Institute of Technology.
006     * All rights reserved.
007     *
008     * This library is free software; you can redistribute it
009     * and/or modify it under the terms of the GNU General
010     * Public License as published by the Free Software
011     * Foundation, version 2.
012     *
013     * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014     */
015    package lapisx.swing;
016    
017    import javax.swing.*;
018    import javax.swing.filechooser.*;
019    import java.beans.*;
020    import java.io.*;
021    
022    /**
023     * Extends JFileChooser to preserve the current directory from one
024     * invocation of the dialog box to the next.
025     */
026    public class FileChooser extends JFileChooser {
027        private static File savedDirectory = 
028            new File (System.getProperty ("user.dir", "."));
029    
030        /**
031         * Creates a JFileChooser pointing to the user's home directory.
032         */
033        public FileChooser() {
034            super ();
035            setCurrentDirectory (savedDirectory);
036            setup2 ();
037        }
038        
039        /**
040         * Creates a FileChooser using the given path. Passing in a null
041         * string causes the file chooser to point to the users home directory.
042         *
043         * @param path  a String giving the path to a file or directory
044         */
045        public FileChooser(String currentDirectoryPath) {
046            super (currentDirectoryPath);
047            setup2 ();
048        }
049    
050        /**
051         * Creates a FileChooser using the given File as the path. Passing
052         * in a null file causes the file chooser to point to the users's
053         * home directory.
054         *
055         * @param directory  a File object specifying the path to a file 
056         *                   or directory
057         */
058        public FileChooser(File currentDirectory) {
059            super (currentDirectory);
060            setup2 ();
061        }
062    
063        /**
064         * Creates a FileChooser using the given FileSystemView
065         */
066        public FileChooser(FileSystemView fsv) {
067            super (fsv);
068            setCurrentDirectory (savedDirectory);
069            setup2 ();
070        }
071    
072    
073        /**
074         * Creates a FileChooser using the given current directory and FileSystemView
075         */
076        public FileChooser(File currentDirectory, FileSystemView fsv) {
077            super (currentDirectory, fsv);
078            setup2 ();
079        }
080    
081        /**
082         * Creates a FileChooser using the given current directory path and FileSystemView
083         */
084        public FileChooser(String currentDirectoryPath, FileSystemView fsv) {
085            super (currentDirectoryPath, fsv);
086            setup2 ();
087        }
088    
089        protected void setup2 () {
090            savedDirectory = getCurrentDirectory ();
091            addPropertyChangeListener (new PropertyChangeListener () {
092                public void propertyChange (PropertyChangeEvent event) {
093                    if (event.getPropertyName().equals (DIRECTORY_CHANGED_PROPERTY))
094                        savedDirectory = (File)event.getNewValue ();
095                }
096            });
097        }        
098    }