Revision 17360
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/swing/JFileChooser.java | ||
---|---|---|
48 | 48 |
import javax.swing.filechooser.FileSystemView; |
49 | 49 |
|
50 | 50 |
/** |
51 |
* |
|
51 |
*
|
|
52 | 52 |
* JFileChooser.java |
53 | 53 |
* <p>JFileChooser that tracks the last visited directory for a given ID. It allows you to |
54 | 54 |
* open the JFileChooser in the same directory where you were the last time you open a file |
55 | 55 |
* in the file system.<br> |
56 | 56 |
* </p> |
57 | 57 |
* <p> |
58 |
* It needs to specify a string defining the JFileChooser ID in order to know what kind of |
|
58 |
* It needs to specify a string defining the JFileChooser ID in order to know what kind of
|
|
59 | 59 |
* files you are going to open. For example after creating a JFileChooser by doing this:<br> |
60 | 60 |
* <b><code>new JFileChooser("TEMPLATES_FILECHOOSER", defaultDirectory);</code></b><br> each time |
61 | 61 |
* you create another JFileChooser anywhere giving the same ID, it will point directly to |
62 | 62 |
* the last directory where you opened a file using this JFileChooser with an equal ID. |
63 | 63 |
* </p> |
64 |
* |
|
65 |
* |
|
64 |
*
|
|
65 |
*
|
|
66 | 66 |
* @author jaume dominguez faus - jaume.dominguez@iver.es Dec 5, 2007 |
67 | 67 |
* |
68 | 68 |
*/ |
... | ... | |
70 | 70 |
private static final long serialVersionUID = -2419775752576400974L; |
71 | 71 |
private static Hashtable<String, File> jfcLastPaths = new Hashtable<String, File>(); |
72 | 72 |
private String fileChooserID; |
73 |
|
|
73 |
|
|
74 | 74 |
/** |
75 |
* Creates a new JFileChooser with the remind last path feature. |
|
75 |
* Creates a new JFileChooser with the remind last path feature.
|
|
76 | 76 |
* @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER") |
77 | 77 |
* @param defaultDirectory, the default directory to go for the first time. It allows null, which |
78 |
* means the user's home directory. |
|
78 |
* means the user's home directory.
|
|
79 | 79 |
*/ |
80 | 80 |
public JFileChooser(String fileChooserID, File defaultDirectory) { |
81 | 81 |
super(); |
82 |
if (fileChooserID == null) |
|
82 |
if (fileChooserID == null)
|
|
83 | 83 |
throw new IllegalArgumentException("JFileChooser's ID cannot be null"); |
84 |
|
|
84 |
|
|
85 | 85 |
this.fileChooserID = fileChooserID; |
86 | 86 |
|
87 | 87 |
setCurrentDirectory(getLastPath(fileChooserID, defaultDirectory)); |
... | ... | |
124 | 124 |
*/ |
125 | 125 |
static public void setLastPath(String fileChooserID, File path) { |
126 | 126 |
jfcLastPaths.put(fileChooserID, path); |
127 |
} |
|
127 |
}
|
|
128 | 128 |
|
129 | 129 |
/** |
130 |
* Creates a new JFileChooser with the remind last path feature. |
|
130 |
* Creates a new JFileChooser with the remind last path feature.
|
|
131 | 131 |
* @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER") |
132 | 132 |
* @param defaultDirectory, the default directory to go for the first time. It allows null, which |
133 |
* means the user's home directory. |
|
133 |
* means the user's home directory.
|
|
134 | 134 |
*/ |
135 | 135 |
public JFileChooser(String fileChooserID, String defaultDirectory) { |
136 | 136 |
this(fileChooserID, defaultDirectory!=null ? new File(defaultDirectory) : null); |
137 | 137 |
} |
138 | 138 |
|
139 |
/**
|
|
140 |
* Constructs a <code>JFileChooser</code> using the given
|
|
141 |
* <code>FileSystemView</code>.
|
|
142 |
*/
|
|
143 |
public JFileChooser(String fileChooserID, FileSystemView fsv) {
|
|
139 |
/**
|
|
140 |
* Constructs a <code>JFileChooser</code> using the given
|
|
141 |
* <code>FileSystemView</code>.
|
|
142 |
*/
|
|
143 |
public JFileChooser(String fileChooserID, FileSystemView fsv) {
|
|
144 | 144 |
this(fileChooserID, (File) null, fsv); |
145 |
}
|
|
145 |
}
|
|
146 | 146 |
|
147 | 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);
|
|
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 | 154 |
setup(fsv); |
155 |
}
|
|
155 |
}
|
|
156 | 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) {
|
|
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 | 162 |
this(fileChooserID, new File(defaultDirectoryPath), fsv); |
163 | 163 |
} |
164 | 164 |
|
... | ... | |
166 | 166 |
public int showDialog(Component parent, String approveButtonText) throws HeadlessException { |
167 | 167 |
int response = super.showDialog(parent, approveButtonText); |
168 | 168 |
setLastPath(fileChooserID, getCurrentDirectory()); |
169 |
|
|
170 |
return response;
|
|
171 |
}
|
|
172 |
|
|
169 |
|
|
170 |
return response;
|
|
171 |
}
|
|
172 |
|
|
173 | 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().
|
|
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 | 187 |
substring(0, f.getAbsolutePath().lastIndexOf(File.separator)); |
188 |
jfcLastPaths.put(fileChooserID, theFilePath);
|
|
188 |
jfcLastPaths.put(fileChooserID, theFilePath);
|
|
189 | 189 |
|
190 |
}
|
|
191 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 | 192 |
*/ |
193 | 193 |
} |
Also available in: Unified diff