svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / JFileChooser.java @ 17360
History | View | Annotate | Download (6.64 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 |
if (fileChooserID == null) |
83 |
throw new IllegalArgumentException("JFileChooser's ID cannot be null"); |
84 |
|
85 |
this.fileChooserID = fileChooserID;
|
86 |
|
87 |
setCurrentDirectory(getLastPath(fileChooserID, defaultDirectory)); |
88 |
|
89 |
// if (defaultDirectory == null)
|
90 |
// defaultDirectory = new File(System.getProperty("user.home"));
|
91 |
// File currentDirectory;
|
92 |
// if (jfcLastPaths.get(fileChooserID) != null)
|
93 |
// currentDirectory = new File(jfcLastPaths.get(fileChooserID));
|
94 |
// else
|
95 |
// currentDirectory = defaultDirectory;
|
96 |
// setCurrentDirectory(currentDirectory);
|
97 |
// if (defaultDirectory != null)
|
98 |
// jfcLastPaths.put(fileChooserID, currentDirectory.getAbsolutePath());
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Returns the Last Path for this fileChooserID
|
103 |
* @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
|
104 |
* @param defaultDirectory, the default directory to go for the first time. It allows null, which
|
105 |
* means the user's home directory.
|
106 |
* @return
|
107 |
*/
|
108 |
static public File getLastPath(String fileChooserID, File defaultDirectory) { |
109 |
File path = jfcLastPaths.get(fileChooserID);
|
110 |
|
111 |
if (path != null) |
112 |
return path;
|
113 |
|
114 |
if (defaultDirectory != null) |
115 |
return defaultDirectory;
|
116 |
|
117 |
return FileSystemView.getFileSystemView().getDefaultDirectory(); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Save the Last Path for this fileChooserID
|
122 |
* @param fileChooserID
|
123 |
* @param path
|
124 |
*/
|
125 |
static public void setLastPath(String fileChooserID, File path) { |
126 |
jfcLastPaths.put(fileChooserID, path); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Creates a new JFileChooser with the remind last path feature.
|
131 |
* @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
|
132 |
* @param defaultDirectory, the default directory to go for the first time. It allows null, which
|
133 |
* means the user's home directory.
|
134 |
*/
|
135 |
public JFileChooser(String fileChooserID, String defaultDirectory) { |
136 |
this(fileChooserID, defaultDirectory!=null ? new File(defaultDirectory) : null); |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Constructs a <code>JFileChooser</code> using the given
|
141 |
* <code>FileSystemView</code>.
|
142 |
*/
|
143 |
public JFileChooser(String fileChooserID, FileSystemView fsv) { |
144 |
this(fileChooserID, (File) null, fsv); |
145 |
} |
146 |
|
147 |
|
148 |
/**
|
149 |
* Constructs a <code>JFileChooser</code> using the given current directory
|
150 |
* and <code>FileSystemView</code>.
|
151 |
*/
|
152 |
public JFileChooser(String fileChooserID, File defaultDirectory, FileSystemView fsv) { |
153 |
this(fileChooserID, defaultDirectory);
|
154 |
setup(fsv); |
155 |
} |
156 |
|
157 |
/**
|
158 |
* Constructs a <code>JFileChooser</code> using the given current directory
|
159 |
* path and <code>FileSystemView</code>.
|
160 |
*/
|
161 |
public JFileChooser(String fileChooserID, String defaultDirectoryPath, FileSystemView fsv) { |
162 |
this(fileChooserID, new File(defaultDirectoryPath), fsv); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public int showDialog(Component parent, String approveButtonText) throws HeadlessException { |
167 |
int response = super.showDialog(parent, approveButtonText); |
168 |
setLastPath(fileChooserID, getCurrentDirectory()); |
169 |
|
170 |
return response;
|
171 |
} |
172 |
|
173 |
/* with this version it only reminds when a file was choosen and accepted (ok button clicked)
|
174 |
@Override
|
175 |
public int showDialog(Component parent, String approveButtonText)
|
176 |
throws HeadlessException {
|
177 |
int response = super.showDialog(parent, approveButtonText);
|
178 |
if (response == javax.swing.JFileChooser.APPROVE_OPTION) {
|
179 |
File[] ff = super.getSelectedFiles();
|
180 |
if (ff.length>0) {
|
181 |
String theFilePath = ff[0].getAbsolutePath().
|
182 |
substring(0, ff[0].getAbsolutePath().lastIndexOf(File.pathSeparator));
|
183 |
jfcLastPaths.put(fileChooserID, theFilePath);
|
184 |
} else if (super.getSelectedFile() != null) {
|
185 |
File f = super.getSelectedFile() ;
|
186 |
String theFilePath = f.getAbsolutePath().
|
187 |
substring(0, f.getAbsolutePath().lastIndexOf(File.separator));
|
188 |
jfcLastPaths.put(fileChooserID, theFilePath);
|
189 |
|
190 |
}
|
191 |
}
|
192 |
*/
|
193 |
} |