1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package uk.ac.rdg.resc.jstyx.client;
30
31 import java.io.File;
32 import javax.swing.JFileChooser;
33 import javax.swing.filechooser.FileSystemView;
34
35 import org.apache.log4j.Logger;
36
37 import uk.ac.rdg.resc.jstyx.client.StyxConnection;
38 import uk.ac.rdg.resc.jstyx.StyxException;
39
40 /***
41 * A FileSystemView for a Styx filesystem that can be used by a JFileChooser
42 * to select files on a remote Styx server
43 * @todo How can we make sure the correct FileSystemView is gotten by
44 * FileSystemView.getFileSystemView()?
45 *
46 * @author Jon Blower
47 * $Revision: 259 $
48 * $Date: 2005-05-23 17:48:23 +0100 (Mon, 23 May 2005) $
49 * $Log$
50 * Revision 1.4 2005/05/23 16:48:17 jonblower
51 * Overhauled CStyxFile (esp. asynchronous methods) and StyxConnection (added cache of CStyxFiles)
52 *
53 * Revision 1.3 2005/03/11 13:58:25 jonblower
54 * Merged MINA-Test_20059309 into main line of development
55 *
56 * Revision 1.2.2.1 2005/03/11 08:29:52 jonblower
57 * Moved to log4j logging system (from apache commons logging)
58 *
59 *
60 * Revision 1.1 2005/03/07 08:27:51 jonblower
61 * Initial import
62 *
63 */
64 public class StyxFileSystemView extends FileSystemView
65 {
66
67 private static final Logger log = Logger.getLogger(StyxFileSystemView.class);
68 private StyxConnection conn;
69 private FileWrapper[] roots;
70
71 /***
72 * Creates a new instance of StyxFileSystemView to view the contents of the
73 * Styx server on the given StyxConnection.
74 * @todo Should the connection be open, or shall we just call connect()
75 * anyway?
76 */
77 public StyxFileSystemView(StyxConnection conn)
78 {
79 log.debug("Creating StyxFileSystemView");
80 this.conn = conn;
81 this.roots = new FileWrapper[]{new FileWrapper(this.conn.getRootDirectory())};
82 }
83
84
85 public static FileSystemView getFileSystemView()
86 {
87 log.debug("Called static getFileSystemView");
88 return FileSystemView.getFileSystemView();
89 }
90
91 /***
92 * @return true if the given File is a root in the filesystem. Simply returns
93 * true if the file's path is "/"
94 */
95 public boolean isRoot(File f)
96 {
97 log.debug("Called isRoot(" + f.getClass().getName() + ", " + f.getPath() + ")");
98 if (!(f instanceof FileWrapper))
99 {
100 log.warn("in isRoot: f is not a FileWrapper");
101 }
102 return f.getPath().equals("/");
103 }
104
105 /***
106 * @return true if this file is the root directory (i.e. its path equals "/")
107 * Simply calls this.isRoot(f)
108 */
109 public boolean isFileSystemRoot(File dir)
110 {
111 log.debug("Called isFileSystemRoot()");
112 return this.isRoot(dir);
113 }
114
115 /***
116 * @return FileWrapper that wraps the root directory of the remote Styx server
117 */
118 public File getHomeDirectory()
119 {
120 log.debug("Called getHomeDirectory()");
121 return this.roots[0];
122 }
123
124 /***
125 * Simply calls getHomeDirectory();
126 */
127 public File getDefaultDirectory()
128 {
129 log.debug("Called getDefaultDirectory()");
130 return this.getHomeDirectory();
131 }
132
133 /***
134 * gets the list of files in the given directory
135 */
136 public File[] getFiles(File dir, boolean useFileHiding)
137 {
138 log.debug("Called getFiles(" + dir.getClass().getName() + ", " +
139 dir.getPath() + ", " + useFileHiding + ")");
140 return dir.listFiles();
141 }
142
143 /***
144 * @return a FileWrapper representing the parent directory of the given File
145 */
146 public File getParentDirectory(File f)
147 {
148 log.debug("Called getParentDirectory(" + f.getPath() + ")");
149
150 return super.getParentDirectory(f);
151 }
152
153 /***
154 * Gets the root of the remote Styx server, as a (single-membered) array
155 * of FileWrappers that wraps the CStyxFile that represents the root of
156 * the server
157 */
158 public File[] getRoots()
159 {
160 log.debug("Called getRoots()");
161 return this.roots;
162 }
163
164 /***
165 * Creates a new folder with a default folder name
166 * @todo doesn't do anything yet!
167 */
168 public File createNewFolder(File containingDir)
169 {
170 log.debug("Called createNewFolder(" + containingDir.toString() + ")");
171 return new File("/");
172 }
173
174 /***
175 * @return a FileWrapper object constructed from the given directory and
176 * filename
177 */
178 public File createFileObject(File dir, String filename)
179 {
180 log.debug("Called createFileObject(" + dir.getPath() + ", " + filename + ")");
181 if (dir instanceof FileWrapper)
182 {
183 CStyxFile file = ((FileWrapper)dir).getCStyxFile();
184 CStyxFile newFile = file.getFile(filename);
185 return new FileWrapper(newFile);
186 }
187 else
188 {
189 log.error("In createFileObject, dir argument was not a FileWrapper");
190 return null;
191 }
192 }
193
194 /***
195 * @return a FileWrapper object constructed from the given path string
196 */
197 public File createFileObject(String path)
198 {
199 log.debug("Called createFileObject(" + path + ")");
200 CStyxFile file = this.conn.getFile(path);
201 return new FileWrapper(file);
202 }
203
204 /***
205 * @return a new FileWrapper representing the given child of the given
206 * directory
207 */
208 public File getChild(File parent, String fileName)
209 {
210 log.debug("Called getChild(" + parent.getPath() + ", " + fileName);
211
212 return super.getChild(parent, fileName);
213 }
214
215 /***
216 * @return true if folder contains file
217 */
218 public boolean isParent(File folder, File file)
219 {
220 log.debug("Called isParent(" + folder.getPath() + ", " + file.getPath());
221
222 return super.isParent(folder, file);
223 }
224
225 /***
226 * Always returns false (all files are "real" files)
227 */
228 public boolean isFileSystem(File f)
229 {
230 log.debug("Called isFileSystem(" + f.getPath() + ")");
231 return false;
232 }
233
234 /***
235 * Always returns the name of the file (not the full path)
236 */
237 public String getSystemDisplayName(File f)
238 {
239
240 return f.getName();
241 }
242
243 /***
244 * Always returns false
245 * @todo Should return true if filename starts with a period?
246 */
247 public boolean isHiddenFile(File f)
248 {
249 log.debug("Called isHiddenFile(" + f.getPath() + ")");
250 return false;
251 }
252
253 public boolean isDrive(File f)
254 {
255 return false;
256 }
257
258 public boolean isFloppyDrive(File f)
259 {
260 return false;
261 }
262
263 public boolean isComputerNode(File f)
264 {
265 return false;
266 }
267
268 /***
269 * @return true if the file is a directory
270 */
271 public Boolean isTraversable(File f)
272 {
273 log.debug("Called isTraversable(" + f.getClass().getName()
274 + ", " + f.getPath() + "); returning " + f.isDirectory());
275 return Boolean.valueOf(f.isDirectory());
276 }
277
278 protected File createFileSystemRoot(File f)
279 {
280 log.debug("Called createFileSystemRoot(" + f.getPath() + ")");
281
282 return super.createFileSystemRoot(f);
283 }
284
285 /***
286 * Test function for StyxFileSystemView
287 */
288 public static void main (String[] args)
289 {
290 StyxConnection conn = null;
291 try
292 {
293 conn = new StyxConnection("localhost", 7778);
294 conn.connect();
295 JFileChooser chooser = new JFileChooser(new StyxFileSystemView(conn));
296 int returnVal = chooser.showOpenDialog(null);
297 if(returnVal == JFileChooser.APPROVE_OPTION)
298 {
299 System.out.println("You chose to open this file: " +
300 chooser.getSelectedFile().getPath());
301 }
302 }
303 catch(Exception e)
304 {
305 e.printStackTrace();
306 }
307 finally
308 {
309 if (conn != null)
310 {
311
312 conn.close();
313 }
314 }
315 }
316
317 }