Statistics
| Revision:

root / tags / v2_0_0_Build_2020 / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / JFileChooser.java @ 33964

History | View | Annotate | Download (7.43 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.gui.beans.swing;
42

    
43
import java.awt.Component;
44
import java.awt.HeadlessException;
45
import java.io.File;
46
import java.util.Hashtable;
47

    
48
import javax.swing.filechooser.FileSystemView;
49

    
50
/**
51
 * 
52
 * JFileChooser.java
53
 * <p>JFileChooser that tracks the last visited directory for a given ID. It allows you to
54
 * open the JFileChooser in the same directory where you were the last time you open a file
55
 * in the file system.<br>
56
 * </p>
57
 * <p>
58
 * It needs to specify a string defining the JFileChooser ID in order to know what kind of 
59
 * files you are going to open. For example after creating a JFileChooser by doing this:<br>
60
 * <b><code>new JFileChooser("TEMPLATES_FILECHOOSER", defaultDirectory);</code></b><br> each time
61
 * you create another JFileChooser anywhere giving the same ID, it will point directly to
62
 * the last directory where you opened a file using this JFileChooser with an equal ID.
63
 * </p>
64
 * 
65
 * 
66
 * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 5, 2007
67
 *
68
 */
69
public class JFileChooser extends javax.swing.JFileChooser {
70
        private static final long serialVersionUID = -2419775752576400974L;
71
        private static Hashtable<String, File> jfcLastPaths = new Hashtable<String, File>();
72
        private String fileChooserID;
73
        
74
        /**
75
         * Creates a new JFileChooser with the remind last path feature. 
76
         * @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
77
         * @param defaultDirectory, the default directory to go for the first time. It allows null, which
78
         *        means the user's home directory. 
79
         */
80
        public JFileChooser(String fileChooserID, File defaultDirectory) {
81
                super();
82
                setDragEnabled(true);
83
                if (fileChooserID == null) 
84
                        throw new IllegalArgumentException("JFileChooser's ID cannot be null");
85
                
86
                this.fileChooserID = fileChooserID;
87

    
88
                setCurrentDirectory(getLastPath(fileChooserID, defaultDirectory));
89

    
90
//                if (defaultDirectory == null)
91
//                        defaultDirectory = new File(System.getProperty("user.home"));
92
//                File currentDirectory;
93
//                if (jfcLastPaths.get(fileChooserID) != null)
94
//                        currentDirectory = new File(jfcLastPaths.get(fileChooserID));
95
//                else
96
//                        currentDirectory = defaultDirectory;
97
//                setCurrentDirectory(currentDirectory);
98
//                if (defaultDirectory != null)
99
//                        jfcLastPaths.put(fileChooserID, currentDirectory.getAbsolutePath());
100
        }
101

    
102
        /**
103
         * Returns the Last Path for this fileChooserID
104
         * @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
105
         * @param defaultDirectory, the default directory to go for the first time. It allows null, which
106
         *        means the user's home directory.
107
         * @return
108
         */
109
        static public File getLastPath(String fileChooserID, File defaultDirectory) {
110
                File path = jfcLastPaths.get(fileChooserID);
111

    
112
                if (path != null)
113
                        return path;
114

    
115
                if (defaultDirectory != null)
116
                        return defaultDirectory;
117

    
118
                return FileSystemView.getFileSystemView().getHomeDirectory();
119
        }
120

    
121
        /**
122
         * Returns the Last Path for this JFileChooser
123
         * @param defaultDirectory, the default directory to go for the first time. It allows null, which
124
         *        means the user's home directory.
125
         * @return
126
         */
127
        public File getLastPath(File defaultDirectory) {
128
                return getLastPath(fileChooserID, defaultDirectory);
129
        }
130

    
131
        /**
132
         * Returns the Last Path for this JFileChooser
133
         * @return
134
         */
135
        public File getLastPath() {
136
                return getLastPath(fileChooserID, null);
137
        }
138

    
139
        /**
140
         * Save the Last Path for this fileChooserID
141
         * @param fileChooserID
142
         * @param path
143
         */
144
        static public void setLastPath(String fileChooserID, File path) {
145
                jfcLastPaths.put(fileChooserID, path);
146
        }
147

    
148
        /**
149
         * Save the Last Path for this JFileChooser
150
         * @param path
151
         */
152
        public void setLastPath(File path) {
153
                setLastPath(fileChooserID, path);
154
        }
155

    
156
        /**
157
         * Creates a new JFileChooser with the remind last path feature. 
158
         * @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
159
         * @param defaultDirectory, the default directory to go for the first time. It allows null, which
160
         *        means the user's home directory. 
161
         */
162
        public JFileChooser(String fileChooserID, String defaultDirectory) {
163
                this(fileChooserID, defaultDirectory!=null ? new File(defaultDirectory) : null);
164
        }
165

    
166
    /**
167
     * Constructs a <code>JFileChooser</code> using the given
168
     * <code>FileSystemView</code>.
169
     */
170
    public JFileChooser(String fileChooserID, FileSystemView fsv) {
171
        this(fileChooserID, (File) null, fsv);
172
    }
173

    
174

    
175
    /**
176
     * Constructs a <code>JFileChooser</code> using the given current directory
177
     * and <code>FileSystemView</code>.
178
     */
179
    public JFileChooser(String fileChooserID, File defaultDirectory, FileSystemView fsv) {
180
            this(fileChooserID, defaultDirectory);
181
                setup(fsv);
182
    }
183

    
184
    /**
185
     * Constructs a <code>JFileChooser</code> using the given current directory
186
     * path and <code>FileSystemView</code>.
187
     */
188
    public JFileChooser(String fileChooserID, String defaultDirectoryPath, FileSystemView fsv) {
189
        this(fileChooserID, new File(defaultDirectoryPath), fsv);
190
                }
191

    
192
        @Override
193
        public int showDialog(Component parent, String approveButtonText) throws HeadlessException {
194
        super.setCurrentDirectory(getLastPath());
195
                int response = super.showDialog(parent, approveButtonText);
196
                if ((getSelectedFile() != null) && (getSelectedFile().isDirectory()))
197
                        setLastPath(fileChooserID, getSelectedFile());
198
                else
199
                        setLastPath(fileChooserID, getCurrentDirectory());
200

    
201
                        return response;
202
                }
203

    
204
/* with this version it only reminds when a file was choosen and accepted (ok button clicked)
205
     @Override
206
    public int showDialog(Component parent, String approveButtonText)
207
                    throws HeadlessException {
208
            int response = super.showDialog(parent, approveButtonText);
209
            if (response == javax.swing.JFileChooser.APPROVE_OPTION) {
210
                    File[] ff = super.getSelectedFiles();
211
                    if (ff.length>0) {
212
                            String theFilePath = ff[0].getAbsolutePath().
213
                                    substring(0, ff[0].getAbsolutePath().lastIndexOf(File.pathSeparator));
214
                            jfcLastPaths.put(fileChooserID, theFilePath);
215
                    } else if (super.getSelectedFile() != null) {
216
                            File f = super.getSelectedFile() ;
217
                            String theFilePath = f.getAbsolutePath().
218
                                                substring(0, f.getAbsolutePath().lastIndexOf(File.separator));
219
                            jfcLastPaths.put(fileChooserID, theFilePath);
220

221
                    }
222
            }
223
*/
224
}