KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > ui > WebdavSystemView


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/ui/WebdavSystemView.java,v 1.1.2.1 2004/10/11 08:18:14 luetzkendorf Exp $
3  * $Revision: 1.1.2.1 $
4  * $Date: 2004/10/11 08:18:14 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.ui;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import javax.swing.UIManager JavaDoc;
29 import javax.swing.filechooser.FileSystemView JavaDoc;
30 import org.apache.commons.httpclient.HttpURL;
31 import org.apache.commons.httpclient.HttpsURL;
32 import org.apache.commons.httpclient.URIException;
33 import org.apache.webdav.lib.WebdavResource;
34
35 /**
36  * WebdavSystemView.java
37  */

38 public class WebdavSystemView extends FileSystemView JavaDoc {
39
40
41     /** The WebDAV resource. */
42     private WebdavResource webdavResource = null;
43     private HttpURL rootURL = null;
44     private WebdavFile homedir = null;
45     private String JavaDoc username = null;
46     private String JavaDoc password = null;
47     private String JavaDoc uri = null;
48     private String JavaDoc rootPath = null;
49
50     private static final String JavaDoc newFolderString =
51     UIManager.getString("FileChooser.other.newFolder");
52     static FileSystemView JavaDoc fsv = null;
53
54
55     public WebdavSystemView(String JavaDoc uri, String JavaDoc rootPath, String JavaDoc username,
56                             String JavaDoc password)
57         throws IllegalAccessError JavaDoc, URIException, IOException JavaDoc {
58         try {
59             this.rootURL = this.uriToHttpURL(uri + rootPath);
60             this.uri = uri;
61             this.rootURL.setUserinfo(username, password);
62             this.username = username;
63             this.password = password;
64             this.rootPath = rootPath;
65
66
67             this.connect();
68             //System.out.println("Connected successfully to : " + this.rootURL);
69
this.disconnect();
70
71             // Create home directory object
72
this.homedir = new WebdavFile(this.rootURL, this.rootURL);
73             //System.out.println("Homedir : " + this.homedir);
74
} catch (IllegalAccessError JavaDoc e) {
75             System.err.println(e.toString());
76             e.printStackTrace();
77             throw e;
78         } catch (URIException e) {
79             System.err.println(e.toString());
80             e.printStackTrace();
81             throw e;
82         } catch (IOException JavaDoc e) {
83             System.err.println(e.toString());
84             e.printStackTrace();
85             throw e;
86         }
87
88     }
89
90     private static HttpURL uriToHttpURL(String JavaDoc uri) throws URIException {
91         HttpURL url = null;
92         if (uri.startsWith("http://")) {
93             url = new HttpURL(uri);
94         } else if (uri.startsWith("https://")) {
95             url = new HttpsURL(uri);
96         } else {
97             throw new URIException("Unknown protocol in URL " + uri);
98         }
99         return url;
100     }
101
102     public void disconnect() throws java.lang.UnknownError JavaDoc {
103         try {
104             this.webdavResource.close();
105         } catch (Exception JavaDoc e) {
106             System.err.println(e.toString());
107             throw new UnknownError JavaDoc();
108         }
109     }
110     public void connect() throws java.lang.IllegalAccessError JavaDoc {
111         try {
112             this.webdavResource = new WebdavResource(this.rootURL);
113         } catch (Exception JavaDoc e) {
114             System.err.println(e.toString());
115             throw new IllegalAccessError JavaDoc();
116         }
117     }
118
119     public static FileSystemView JavaDoc getFileSystemView() {
120         try {
121             if (fsv == null) {
122                 fsv = new WebdavSystemView("http://127.0.0.1", "/", "", "");
123             }
124             return fsv;
125         } catch (Exception JavaDoc e) {
126             System.err.println(e.toString());
127             return null;
128         }
129     }
130
131     /**
132      * Returns a File object constructed in dir from the given filename.
133      */

134     public File JavaDoc createFileObject(File JavaDoc dir, String JavaDoc filename) {
135         File JavaDoc file = null;
136         if (dir == null) {
137             file = new File JavaDoc(filename);
138         } else {
139             file = new File JavaDoc(dir, filename);
140         }
141         return file;
142     }
143
144     /**
145      * Returns a File object constructed from the given path string.
146      */

147     public File JavaDoc createFileObject(String JavaDoc path) {
148         File JavaDoc f = new File JavaDoc(path);
149         if (isFileSystemRoot(f)) {
150             f = createFileSystemRoot(f);
151         }
152         return f;
153     }
154
155
156     /**
157      * Creates a new folder with a default folder name.
158      */

159     public File JavaDoc createNewFolder(File JavaDoc containingDir) throws IOException JavaDoc {
160         try {
161             if (containingDir == null) {
162                 throw new IOException JavaDoc("Containing directory is null:");
163             }
164             WebdavFile newFolder = null;
165             HttpURL url = null;
166
167             url = this.uriToHttpURL(containingDir.getPath() +
168                                     WebdavFile.davSeparator + newFolderString);
169             // Need to add user info so has access for queries
170
url.setUserinfo(username, password);
171             newFolder = new WebdavFile(url, this.rootURL);
172             //System.out.println("new folder : " + newFolder.toString());
173

174             this.connect();
175             if (this.webdavResource.mkcolMethod(
176                     newFolder.getAbsolutePath())) {
177                 //System.out.println("succeeded.");
178
return newFolder;
179             } else {
180                 System.err.println("failed.");
181                 System.err.println(this.webdavResource.getStatusMessage());
182                 throw new IOException JavaDoc(
183                     this.webdavResource.getStatusMessage());
184             }
185         } catch (IOException JavaDoc e) {
186             throw e;
187         } catch (Exception JavaDoc e) {
188             System.err.println(e.toString());
189             e.printStackTrace();
190             return null;
191         } finally {
192             this.disconnect();
193         }
194     }
195     /**
196      * Returns all root partitions on this system. For example, on
197      * Windows, this would be the "Desktop" folder, while on DOS this
198      * would be the A: through Z: drives.
199      */

200     public File JavaDoc[] getRoots() {
201         try {
202             return new WebdavFile[] {this.homedir};
203         } catch (Exception JavaDoc e) {
204             System.err.println(e.toString());
205             e.printStackTrace();
206             return null;
207         }
208     }
209
210
211     /**
212      * Returns true if the file (directory) can be visited.
213      * Returns false if the directory cannot be traversed.
214      *
215      * @param f the <code>File</code>
216      * @return <code>true</code> if the file/directory can be traversed,
217      * otherwise <code>false</code>
218      * @see JFileChooser#isTraversable
219      * @see FileView#isTraversable
220      */

221     public Boolean JavaDoc isTraversable(File JavaDoc f) {
222         try {
223             // System.out.println("isTraversable : " + f.getPath());
224
// WebdavFile webdavFile = null;
225
// this.connect();
226
// webdavFile = (WebdavFile) f;
227
// this.webdavResource.setHttpURL(new HttpURL(f.getPath()));
228
// System.out.println(this.webdavResource.getPath() + " : collection : " + this.webdavResource.isCollection());
229
// return Boolean.valueOf(this.webdavResource.isCollection());
230
return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
231         } catch (Exception JavaDoc e) {
232             System.err.println(e.toString());
233             e.printStackTrace();
234             return Boolean.FALSE;
235         } finally {
236             this.disconnect();
237         }
238     }
239
240     /**
241      * Name of a file, directory, or folder as it would be displayed in
242      * a system file browser. Example from Windows: the "M:\" directory
243      * displays as "CD-ROM (M:)"
244      *
245      * The default implementation gets information from the ShellFolder
246      * class.
247      *
248      * @param f a <code>File</code> object
249      * @return the file name as it would be displayed by a native file
250      * chooser
251      * @see JFileChooser#getName
252      */

253     public String JavaDoc getSystemDisplayName(File JavaDoc f) {
254         try {
255             //System.out.println("getSystemDisplayName : getName : "
256
//+ f.getName());
257
//System.out.println("getSystemDisplayName : getAbsolutePath : "
258
//+ f.getAbsolutePath());
259
//System.out.println("getSystemDisplayName : getCanonicalPath : "
260
//+ f.getCanonicalPath());
261
//System.out.println("getSystemDisplayName : getPath : "
262
//+ f.getPath());
263
return f.getName();
264         } catch (Exception JavaDoc e) {
265             System.err.println(e.toString());
266             e.printStackTrace();
267             return null;
268         } finally {
269             this.disconnect();
270         }
271     }
272
273     /**
274      * Type description for a file, directory, or folder as it would be
275      * displayed in
276      * a system file browser. Example from Windows: the "Desktop" folder
277      * is desribed as "Desktop".
278      *
279      * Override for platforms with native ShellFolder implementations.
280      *
281      * @param f a <code>File</code> object
282      * @return the file type description as it would be displayed by a
283      * native file chooser or null if no native information is
284      * available.
285      * @see JFileChooser#getTypeDescription
286      */

287     public String JavaDoc getSystemTypeDescription(File JavaDoc f) {
288         return null;
289     }
290
291
292
293     /**
294      * Checks if <code>f</code> represents a real directory or file as
295      * opposed to a special folder such as <code>"Desktop"</code>. Used by UI
296      * classes to decide if a folder is selectable when doing directory
297      * choosing.
298      *
299      * @param f a <code>File</code> object
300      * @return <code>true</code> if <code>f</code> is a real file or directory.
301      */

302     public boolean isFileSystem(File JavaDoc f) {
303         return true;
304     }
305
306
307     /**
308      * Returns whether a file is hidden or not.
309      */

310     public boolean isHiddenFile(File JavaDoc f) {
311         return f.isHidden();
312     }
313
314
315     /**
316      * Is dir the root of a tree in the file system, such as a drive
317      * or partition. Example: Returns true for "C:\" on Windows 98.
318      *
319      * @param f a <code>File</code> object representing a directory
320      * @return <code>true</code> if <code>f</code> is a root of a filesystem
321      * @see #isRoot
322      */

323     public boolean isFileSystemRoot(File JavaDoc dir) {
324         try {
325             return (rootURL.getPath().equals(dir.getPath()));
326         }
327         catch (Exception JavaDoc e) {
328             System.err.println("isFileSystemRoot" + e.toString());
329             e.printStackTrace();
330             return false;
331         }
332     }
333
334     /**
335      * Used by UI classes to decide whether to display a special icon
336      * for drives or partitions, e.g. a "hard disk" icon.
337      *
338      * The default implementation has no way of knowing, so always returns
339      * false.
340      *
341      * @param dir a directory
342      * @return <code>false</code> always
343      */

344     public boolean isDrive(File JavaDoc dir) {
345         return false;
346     }
347
348     /**
349      * Used by UI classes to decide whether to display a special icon
350      * for a floppy disk. Implies isDrive(dir).
351      *
352      * The default implementation has no way of knowing, so always returns
353      * false.
354      *
355      * @param dir a directory
356      * @return <code>false</code> always
357      */

358     public boolean isFloppyDrive(File JavaDoc dir) {
359         return false;
360     }
361
362     /**
363      * Used by UI classes to decide whether to display a special icon
364      * for a computer node, e.g. "My Computer" or a network server.
365      *
366      * The default implementation has no way of knowing, so always returns
367      * false.
368      *
369      * @param dir a directory
370      * @return <code>false</code> always
371      */

372     public boolean isComputerNode(File JavaDoc dir) {
373         return false;
374     }
375
376     // Providing default implementations for the remaining methods
377
// because most OS file systems will likely be able to use this
378
// code. If a given OS can't, override these methods in its
379
// implementation.
380

381     public File JavaDoc getHomeDirectory() {
382         return this.homedir;
383     }
384
385     /**
386      * Return the user's default starting directory for the file chooser.
387      *
388      * @return a <code>File</code> object representing the default
389      * starting folder
390      */

391     public File JavaDoc getDefaultDirectory() {
392         return this.homedir;
393     }
394
395
396     /**
397      * Gets the list of shown (i.e. not hidden) files.
398      */

399     public File JavaDoc[] getFiles(File JavaDoc dir, boolean useFileHiding) {
400         try {
401
402             String JavaDoc filenames[] = null;
403             WebdavFile files[] = null;
404             HttpURL url = null;
405             String JavaDoc path = null;
406             String JavaDoc localDir = null;
407
408             this.connect();
409             // Now we try to list files
410

411             path = dir.getPath();
412             //System.out.println("getFiles : RAW PATH : '" + path + "'");
413

414             // If path contains the server preamble, we need to extract that
415
// and have the path only
416
if (path.startsWith("http")) {
417                 //System.out.println("getFiles : preample : " + this.uri);
418
path = path.replaceAll(this.uri, "");
419             }
420             if (!path.endsWith("/")) {
421                 path = path + "/";
422             }
423
424             //System.out.println("getFiles : path : " + path);
425

426             this.webdavResource.setPath(path);
427             filenames = this.webdavResource.list();
428             files = new WebdavFile[filenames.length];
429             for (int i = 0; i < filenames.length; i++) {
430                 //System.out.println("file : " + filenames[i]);
431
// Lets try to construct a uri from the dir
432
// given and the current file
433

434                 localDir = dir.getPath();
435                 if (!localDir.endsWith("/")) localDir = localDir + "/";
436
437                 String JavaDoc filepath = localDir + filenames[i];
438                 //System.out.println("getFiles : file fullpath : " + filepath);
439
url = this.uriToHttpURL(filepath);
440                 // Need to add user info so has access for queries
441
url.setUserinfo(username, password);
442                 files[i] = new WebdavFile(url, this.rootURL);
443             }
444             return files;
445         } catch (Exception JavaDoc e) {
446             System.err.println(e.toString());
447             e.printStackTrace();
448             return null;
449         } finally {
450             this.disconnect();
451         }
452     }
453
454
455
456     /**
457      * Returns the parent directory of <code>dir</code>.
458      * @param dir the <code>File</code> being queried
459      * @return the parent directory of <code>dir</code>, or
460      * <code>null</code> if <code>dir</code> is <code>null</code>
461      */

462     public File JavaDoc getParentDirectory(File JavaDoc dir) {
463         //System.out.println("dir : " + dir);
464
if (dir == null) {
465             return null;
466         } else if (dir.equals(this.homedir)) {
467             return this.homedir;
468         } else {
469             //System.out.println("getParentDirectory : calling getParentFile" + dir);
470
return dir.getParentFile();
471         }
472     }
473
474     /**
475      * Creates a new <code>File</code> object for <code>f</code> with
476      * correct behavior for a file system root directory.
477      *
478      * @param f a <code>File</code> object representing a file system root
479      * directory, for example "/" on Unix or "C:\" on Windows.
480      * @return a new <code>File</code> object
481      */

482     protected File JavaDoc createFileSystemRoot(File JavaDoc f) {
483         try {
484             return new FileSystemRoot((WebdavFile) f);
485
486         } catch (Exception JavaDoc e) {
487             System.err.println("createFileSystemRoot : " + e.toString());
488             return null;
489         }
490     }
491
492     static class WebdavFile extends org.apache.webdav.lib.WebdavFile {
493         protected WebdavResource webdavResource = null;
494         protected HttpURL rootUrl = null;
495
496         public WebdavFile(HttpURL pathUrl, HttpURL rootUrl) throws
497             URIException, IOException JavaDoc {
498             super(pathUrl);
499             this.webdavResource = new WebdavResource(pathUrl);
500             this.rootUrl = rootUrl;
501         }
502
503
504         public String JavaDoc getName() {
505             String JavaDoc name = null;
506
507             // Get the base name
508
name = super.getName();
509
510             // If is a directory, we need to add a trailing slash
511
if (this.isDirectory()) {
512                 name = name + "/";
513             }
514             return name;
515         }
516         public boolean isRoot() {
517             try {
518                 String JavaDoc path = null;
519                 String JavaDoc root = null;
520                 root = this.rootUrl.getPath();
521                 path = this.webdavResource.getHttpURL().getPath();
522                 //System.out.println("isRoot : root : " + root);
523
//System.out.println("isRoot : path : " + path);
524

525                 // If we are at the root already
526
if (root .equalsIgnoreCase(path)) {
527                     //System.out.println("isRoot : we are at root");
528
return true;
529                 } else {
530                     // otherwise call original
531
return false;
532                 }
533             }
534             catch (Exception JavaDoc e) {
535                 System.err.println(e.toString());
536                 e.printStackTrace();
537                 return false;
538             }
539         }
540         public String JavaDoc getParent() {
541             try {
542                 // If we are at the root already
543
if (this.isRoot()) {
544                     //System.out.println("getParentFile : we are at root");
545
return null;
546                 } else {
547                     // otherwise call original
548
String JavaDoc escapedPath =
549                         this.webdavResource.getHttpURL().getPath();
550                     String JavaDoc parent = escapedPath.substring(
551                         0, escapedPath.lastIndexOf('/', escapedPath.length() -
552                                                    2) + 1);
553                     //System.out.println("getParent : escapedPath : " + escapedPath);
554
return super.getParent();
555                 }
556             }
557             catch (Exception JavaDoc e) {
558                 System.err.println(e.toString());
559                 e.printStackTrace();
560                 return null;
561             }
562         }
563         public File JavaDoc getParentFile() {
564             try {
565                 HttpURL httpURL = null;
566                 String JavaDoc parent = null;
567                 parent = this.getParent();
568                 if (parent == null) {
569                     //System.out.println("getParentFile : at root so return null");
570
return null;
571                 } else {
572                     httpURL = this.rootUrl;
573                     httpURL.setPath(parent);
574                     //System.out.println("getParentFile : set path to " + parent);
575
return new WebdavFile(httpURL, this.rootUrl);
576                 }
577             } catch (Exception JavaDoc e) {
578                 System.err.println(e.toString());
579                 e.printStackTrace();
580                 return null;
581             }
582
583         }
584         public boolean exists() {
585             //System.out.println("exists : " + this.getPath());
586
return this.webdavResource.exists();
587         }
588         public boolean isDirectory() {
589             //System.out.println("isDirectory : " + this.getPath() + " : " +
590
//this.webdavResource.isCollection());
591
return this.webdavResource.isCollection();
592         }
593     }
594
595
596
597     static class FileSystemRoot extends WebdavFile {
598
599         public FileSystemRoot(HttpURL rootUrl) throws URIException,
600             IOException JavaDoc {
601             super(rootUrl, rootUrl);
602         }
603         public FileSystemRoot(WebdavFile webdavFile) throws URIException,
604             IOException JavaDoc {
605             super(webdavFile.rootUrl, webdavFile.rootUrl);
606         }
607
608     }
609
610     // Test code
611
public static void main(String JavaDoc args[]) throws Exception JavaDoc {
612         javax.swing.JFrame JavaDoc frame = new javax.swing.JFrame JavaDoc();
613
614         // Setup
615
javax.swing.JFileChooser JavaDoc fc = new javax.swing.JFileChooser JavaDoc(
616             new WebdavSystemView(
617                 "http://localhost:8080", "/slide/files", "root", "root"));
618         fc.showOpenDialog(frame);
619     }
620
621 }
622
Popular Tags