svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / JFileChooser.java @ 17360
History | View | Annotate | Download (6.64 KB)
1 | 17166 | jdominguez | /* 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 | 17359 | bsanchez | *
|
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 | 17166 | jdominguez | */
|
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 | 17359 | bsanchez | |
50 | 17166 | jdominguez | /**
|
51 | 17360 | bsanchez | *
|
52 | 17166 | jdominguez | * 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 | 17360 | bsanchez | * It needs to specify a string defining the JFileChooser ID in order to know what kind of
|
59 | 17166 | jdominguez | * 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 | 17360 | bsanchez | *
|
65 | *
|
||
66 | 17356 | bsanchez | * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 5, 2007
|
67 | 17359 | bsanchez | *
|
68 | 17166 | jdominguez | */
|
69 | public class JFileChooser extends javax.swing.JFileChooser { |
||
70 | private static final long serialVersionUID = -2419775752576400974L; |
||
71 | 17356 | bsanchez | private static Hashtable<String, File> jfcLastPaths = new Hashtable<String, File>(); |
72 | 17359 | bsanchez | private String fileChooserID; |
73 | 17360 | bsanchez | |
74 | 17166 | jdominguez | /**
|
75 | 17360 | bsanchez | * Creates a new JFileChooser with the remind last path feature.
|
76 | 17166 | jdominguez | * @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 | 17360 | bsanchez | * means the user's home directory.
|
79 | 17166 | jdominguez | */
|
80 | public JFileChooser(String fileChooserID, File defaultDirectory) { |
||
81 | super();
|
||
82 | 17360 | bsanchez | if (fileChooserID == null) |
83 | 17166 | jdominguez | throw new IllegalArgumentException("JFileChooser's ID cannot be null"); |
84 | 17360 | bsanchez | |
85 | 17166 | jdominguez | this.fileChooserID = fileChooserID;
|
86 | 17356 | bsanchez | |
87 | setCurrentDirectory(getLastPath(fileChooserID, defaultDirectory)); |
||
88 | 17359 | bsanchez | |
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 | 17356 | bsanchez | } |
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 | 17176 | jdominguez | if (defaultDirectory != null) |
115 | 17356 | bsanchez | return defaultDirectory;
|
116 | |||
117 | return FileSystemView.getFileSystemView().getDefaultDirectory(); |
||
118 | } |
||
119 | |||
120 | 17169 | jdominguez | /**
|
121 | 17356 | bsanchez | * 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 | 17360 | bsanchez | } |
128 | 17356 | bsanchez | |
129 | /**
|
||
130 | 17360 | bsanchez | * Creates a new JFileChooser with the remind last path feature.
|
131 | 17169 | jdominguez | * @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 | 17360 | bsanchez | * means the user's home directory.
|
134 | 17169 | jdominguez | */
|
135 | public JFileChooser(String fileChooserID, String defaultDirectory) { |
||
136 | 17359 | bsanchez | this(fileChooserID, defaultDirectory!=null ? new File(defaultDirectory) : null); |
137 | 17169 | jdominguez | } |
138 | 17166 | jdominguez | |
139 | 17360 | bsanchez | /**
|
140 | * Constructs a <code>JFileChooser</code> using the given
|
||
141 | * <code>FileSystemView</code>.
|
||
142 | */
|
||
143 | public JFileChooser(String fileChooserID, FileSystemView fsv) { |
||
144 | 17359 | bsanchez | this(fileChooserID, (File) null, fsv); |
145 | 17360 | bsanchez | } |
146 | 17166 | jdominguez | |
147 | 17359 | bsanchez | |
148 | 17360 | bsanchez | /**
|
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 | 17166 | jdominguez | setup(fsv); |
155 | 17360 | bsanchez | } |
156 | 17166 | jdominguez | |
157 | 17360 | bsanchez | /**
|
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 | 17359 | bsanchez | this(fileChooserID, new File(defaultDirectoryPath), fsv); |
163 | } |
||
164 | 17166 | jdominguez | |
165 | 17188 | bsanchez | @Override
|
166 | public int showDialog(Component parent, String approveButtonText) throws HeadlessException { |
||
167 | int response = super.showDialog(parent, approveButtonText); |
||
168 | 17356 | bsanchez | setLastPath(fileChooserID, getCurrentDirectory()); |
169 | 17360 | bsanchez | |
170 | return response;
|
||
171 | } |
||
172 | |||
173 | 17359 | bsanchez | /* with this version it only reminds when a file was choosen and accepted (ok button clicked)
|
174 | 17360 | bsanchez | @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 | 17359 | bsanchez | substring(0, f.getAbsolutePath().lastIndexOf(File.separator));
|
188 | 17360 | bsanchez | jfcLastPaths.put(fileChooserID, theFilePath);
|
189 | 17359 | bsanchez | |
190 | 17360 | bsanchez | }
|
191 | }
|
||
192 | 17359 | bsanchez | */
|
193 | } |