KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > Util


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.refactoring.spi.impl;
21
22 import java.io.File JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.openide.filesystems.FileChangeListener;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileStateInvalidException;
28 import org.openide.filesystems.FileSystem;
29 import org.openide.filesystems.FileUtil;
30
31 /**
32  *
33  * @author Jan Becicka
34  */

35
36 public class Util {
37
38     //copy - paste programming
39
//http://ant.netbeans.org/source/browse/ant/src-bridge/org/apache/tools/ant/module/bridge/impl/BridgeImpl.java.diff?r1=1.15&r2=1.16
40
//http:/java.netbeans.org/source/browse/java/javacore/src/org/netbeans/modules/javacore/Util.java
41
//http://core.netbeans.org/source/browse/core/ui/src/org/netbeans/core/ui/MenuWarmUpTask.java
42
//http://core.netbeans.org/source/browse/core/src/org/netbeans/core/actions/RefreshAllFilesystemsAction.java
43
//http://java.netbeans.org/source/browse/java/api/src/org/netbeans/api/java/classpath/ClassPath.java
44

45     private static FileSystem[] fileSystems;
46
47     private static FileSystem[] getFileSystems() {
48         if (fileSystems != null) {
49             return fileSystems;
50         }
51         File JavaDoc[] roots = File.listRoots();
52         Set JavaDoc allRoots = new LinkedHashSet JavaDoc();
53         assert roots != null && roots.length > 0 : "Could not list file roots"; // NOI18N
54

55         for (int i = 0; i < roots.length; i++) {
56             File JavaDoc root = roots[i];
57             FileObject random = FileUtil.toFileObject(root);
58             if (random == null) continue;
59             
60             FileSystem fs;
61             try {
62                 fs = random.getFileSystem();
63                 allRoots.add(fs);
64                 
65                 /*Because there is MasterFileSystem impl. that provides conversion to FileObject for all File.listRoots
66                 (except floppy drives and empty CD). Then there is useless to convert all roots into FileObjects including
67                 net drives that might cause performance regression.
68                 */

69                 
70                 if (fs != null) {
71                     break;
72                 }
73             } catch (FileStateInvalidException e) {
74                 throw new AssertionError JavaDoc(e);
75             }
76         }
77         FileSystem[] retVal = new FileSystem [allRoots.size()];
78         allRoots.toArray(retVal);
79         assert retVal.length > 0 : "Could not get any filesystem"; // NOI18N
80

81         return fileSystems = retVal;
82     }
83     
84     /**
85      * Adds given listener to all FileSystems
86      * @param l listener to add
87      */

88     public static void addFileSystemsListener(FileChangeListener l) {
89         FileSystem[] fileSystems = getFileSystems();
90         for (int i=0; i<fileSystems.length; i++) {
91             fileSystems[i].addFileChangeListener(l);
92         }
93     }
94     
95     /**
96      * Removes given listener to all FileSystems
97      * @param l listener to remove
98      */

99     public static void removeFileSystemsListener(FileChangeListener l) {
100         FileSystem[] fileSystems = getFileSystems();
101         for (int i=0; i<fileSystems.length; i++) {
102             fileSystems[i].removeFileChangeListener(l);
103         }
104     }
105 }
Popular Tags