KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > 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.javacore;
21
22 import java.io.File JavaDoc;
23 import java.util.LinkedHashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
26 import org.openide.filesystems.FileChangeListener;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileStateInvalidException;
29 import org.openide.filesystems.FileSystem;
30 import org.openide.filesystems.FileUtil;
31
32 /**
33  *
34  * @author Jan Becicka
35  */

36
37 public class Util {
38
39     //copy - paste programming
40
//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
41
//http:/java.netbeans.org/source/browse/java/javacore/src/org/netbeans/modules/javacore/Util.java
42
//http://core.netbeans.org/source/browse/core/ui/src/org/netbeans/core/ui/MenuWarmUpTask.java
43
//http://core.netbeans.org/source/browse/core/src/org/netbeans/core/actions/RefreshAllFilesystemsAction.java
44
//http://java.netbeans.org/source/browse/java/api/src/org/netbeans/api/java/classpath/ClassPath.java
45

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

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

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

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

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

100     public static void removeFileSystemsListener(FileChangeListener l) {
101         FileSystem[] fileSystems = getFileSystems();
102         for (int i=0; i<fileSystems.length; i++) {
103             fileSystems[i].removeFileChangeListener(l);
104         }
105     }
106     
107     public static FileObject getCPRoot(FileObject fo) {
108         return ((JMManager) JavaMetamodel.getManager()).getMergedClassPathImpl().findOwnerRoot(fo);
109     }
110     
111     public static boolean isJavaFile(FileObject fo) {
112         return isJavaFile(fo, false);
113     }
114     
115     public static boolean isJavaFile(FileObject fo, boolean includeVirtuals) {
116         if (!includeVirtuals)
117             if (fo.isVirtual())
118                 return false;
119         if (fo.isFolder())
120             return false;
121         String JavaDoc ext = fo.getExt();
122         return ("java".equals(ext) || "class".equals(ext)) && (getCPRoot(fo) != null); // NOI18N
123
}
124 }
Popular Tags