svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / JFileChooser.java @ 44237
History | View | Annotate | Download (7.25 KB)
1 | 40561 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40561 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40561 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | 40561 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40561 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.gui.beans.swing; |
||
25 | |||
26 | import java.awt.Component; |
||
27 | import java.awt.HeadlessException; |
||
28 | import java.io.File; |
||
29 | import java.util.Hashtable; |
||
30 | |||
31 | import javax.swing.filechooser.FileSystemView; |
||
32 | |||
33 | /**
|
||
34 | *
|
||
35 | * JFileChooser.java
|
||
36 | * <p>JFileChooser that tracks the last visited directory for a given ID. It allows you to
|
||
37 | * open the JFileChooser in the same directory where you were the last time you open a file
|
||
38 | * in the file system.<br>
|
||
39 | * </p>
|
||
40 | * <p>
|
||
41 | * It needs to specify a string defining the JFileChooser ID in order to know what kind of
|
||
42 | * files you are going to open. For example after creating a JFileChooser by doing this:<br>
|
||
43 | * <b><code>new JFileChooser("TEMPLATES_FILECHOOSER", defaultDirectory);</code></b><br> each time
|
||
44 | * you create another JFileChooser anywhere giving the same ID, it will point directly to
|
||
45 | * the last directory where you opened a file using this JFileChooser with an equal ID.
|
||
46 | * </p>
|
||
47 | *
|
||
48 | *
|
||
49 | * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 5, 2007
|
||
50 | 44237 | jjdelcerro | * @deprecated use FolderManager or FileDialogChosserManager
|
51 | 40435 | jjdelcerro | */
|
52 | public class JFileChooser extends javax.swing.JFileChooser { |
||
53 | private static final long serialVersionUID = -2419775752576400974L; |
||
54 | private static Hashtable<String, File> jfcLastPaths = new Hashtable<String, File>(); |
||
55 | private String fileChooserID; |
||
56 | |||
57 | /**
|
||
58 | * Creates a new JFileChooser with the remind last path feature.
|
||
59 | * @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
|
||
60 | * @param defaultDirectory, the default directory to go for the first time. It allows null, which
|
||
61 | * means the user's home directory.
|
||
62 | */
|
||
63 | public JFileChooser(String fileChooserID, File defaultDirectory) { |
||
64 | super();
|
||
65 | setDragEnabled(true);
|
||
66 | if (fileChooserID == null) |
||
67 | throw new IllegalArgumentException("JFileChooser's ID cannot be null"); |
||
68 | |||
69 | this.fileChooserID = fileChooserID;
|
||
70 | |||
71 | setCurrentDirectory(getLastPath(fileChooserID, defaultDirectory)); |
||
72 | |||
73 | // if (defaultDirectory == null)
|
||
74 | // defaultDirectory = new File(System.getProperty("user.home"));
|
||
75 | // File currentDirectory;
|
||
76 | // if (jfcLastPaths.get(fileChooserID) != null)
|
||
77 | // currentDirectory = new File(jfcLastPaths.get(fileChooserID));
|
||
78 | // else
|
||
79 | // currentDirectory = defaultDirectory;
|
||
80 | // setCurrentDirectory(currentDirectory);
|
||
81 | // if (defaultDirectory != null)
|
||
82 | // jfcLastPaths.put(fileChooserID, currentDirectory.getAbsolutePath());
|
||
83 | } |
||
84 | |||
85 | /**
|
||
86 | * Returns the Last Path for this fileChooserID
|
||
87 | * @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
|
||
88 | * @param defaultDirectory, the default directory to go for the first time. It allows null, which
|
||
89 | * means the user's home directory.
|
||
90 | * @return
|
||
91 | */
|
||
92 | static public File getLastPath(String fileChooserID, File defaultDirectory) { |
||
93 | File path = jfcLastPaths.get(fileChooserID);
|
||
94 | |||
95 | if (path != null) |
||
96 | return path;
|
||
97 | |||
98 | if (defaultDirectory != null) |
||
99 | return defaultDirectory;
|
||
100 | |||
101 | return FileSystemView.getFileSystemView().getHomeDirectory(); |
||
102 | } |
||
103 | |||
104 | /**
|
||
105 | * Returns the Last Path for this JFileChooser
|
||
106 | * @param defaultDirectory, the default directory to go for the first time. It allows null, which
|
||
107 | * means the user's home directory.
|
||
108 | * @return
|
||
109 | */
|
||
110 | public File getLastPath(File defaultDirectory) { |
||
111 | return getLastPath(fileChooserID, defaultDirectory);
|
||
112 | } |
||
113 | |||
114 | /**
|
||
115 | * Returns the Last Path for this JFileChooser
|
||
116 | * @return
|
||
117 | */
|
||
118 | public File getLastPath() { |
||
119 | return getLastPath(fileChooserID, getCurrentDirectory());
|
||
120 | } |
||
121 | |||
122 | /**
|
||
123 | * Save the Last Path for this fileChooserID
|
||
124 | * @param fileChooserID
|
||
125 | * @param path
|
||
126 | */
|
||
127 | static public void setLastPath(String fileChooserID, File path) { |
||
128 | jfcLastPaths.put(fileChooserID, path); |
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * Save the Last Path for this JFileChooser
|
||
133 | * @param path
|
||
134 | */
|
||
135 | public void setLastPath(File path) { |
||
136 | setLastPath(fileChooserID, path); |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * Creates a new JFileChooser with the remind last path feature.
|
||
141 | * @param fileChooserID, the id that distinguishes the wanted files (i.e. "TEMPLATES_FILECHOOSER")
|
||
142 | * @param defaultDirectory, the default directory to go for the first time. It allows null, which
|
||
143 | * means the user's home directory.
|
||
144 | */
|
||
145 | public JFileChooser(String fileChooserID, String defaultDirectory) { |
||
146 | this(fileChooserID, defaultDirectory!=null ? new File(defaultDirectory) : null); |
||
147 | } |
||
148 | |||
149 | /**
|
||
150 | * Constructs a <code>JFileChooser</code> using the given
|
||
151 | * <code>FileSystemView</code>.
|
||
152 | */
|
||
153 | public JFileChooser(String fileChooserID, FileSystemView fsv) { |
||
154 | this(fileChooserID, (File) null, fsv); |
||
155 | } |
||
156 | |||
157 | |||
158 | /**
|
||
159 | * Constructs a <code>JFileChooser</code> using the given current directory
|
||
160 | * and <code>FileSystemView</code>.
|
||
161 | */
|
||
162 | public JFileChooser(String fileChooserID, File defaultDirectory, FileSystemView fsv) { |
||
163 | this(fileChooserID, defaultDirectory);
|
||
164 | setup(fsv); |
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * Constructs a <code>JFileChooser</code> using the given current directory
|
||
169 | * path and <code>FileSystemView</code>.
|
||
170 | */
|
||
171 | public JFileChooser(String fileChooserID, String defaultDirectoryPath, FileSystemView fsv) { |
||
172 | this(fileChooserID, new File(defaultDirectoryPath), fsv); |
||
173 | } |
||
174 | |||
175 | @Override
|
||
176 | public int showDialog(Component parent, String approveButtonText) throws HeadlessException { |
||
177 | super.setCurrentDirectory(getLastPath());
|
||
178 | int response = super.showDialog(parent, approveButtonText); |
||
179 | if ((getSelectedFile() != null) && (getSelectedFile().isDirectory())) |
||
180 | setLastPath(fileChooserID, getSelectedFile()); |
||
181 | else
|
||
182 | setLastPath(fileChooserID, getCurrentDirectory()); |
||
183 | |||
184 | return response;
|
||
185 | } |
||
186 | |||
187 | /* with this version it only reminds when a file was choosen and accepted (ok button clicked)
|
||
188 | @Override
|
||
189 | public int showDialog(Component parent, String approveButtonText)
|
||
190 | throws HeadlessException {
|
||
191 | int response = super.showDialog(parent, approveButtonText);
|
||
192 | if (response == javax.swing.JFileChooser.APPROVE_OPTION) {
|
||
193 | File[] ff = super.getSelectedFiles();
|
||
194 | if (ff.length>0) {
|
||
195 | String theFilePath = ff[0].getAbsolutePath().
|
||
196 | substring(0, ff[0].getAbsolutePath().lastIndexOf(File.pathSeparator));
|
||
197 | jfcLastPaths.put(fileChooserID, theFilePath);
|
||
198 | } else if (super.getSelectedFile() != null) {
|
||
199 | File f = super.getSelectedFile() ;
|
||
200 | String theFilePath = f.getAbsolutePath().
|
||
201 | substring(0, f.getAbsolutePath().lastIndexOf(File.separator));
|
||
202 | jfcLastPaths.put(fileChooserID, theFilePath);
|
||
203 | |||
204 | }
|
||
205 | }
|
||
206 | */
|
||
207 | } |