1 19 20 package org.netbeans.modules.javacore; 21 22 import java.io.File ; 23 import java.util.LinkedHashSet ; 24 import java.util.Set ; 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 36 37 public class Util { 38 39 46 private static FileSystem[] fileSystems; 47 48 private static FileSystem[] getFileSystems() { 49 if (fileSystems != null) { 50 return fileSystems; 51 } 52 File [] roots = File.listRoots(); 53 Set allRoots = new LinkedHashSet (); 54 assert roots != null && roots.length > 0 : "Could not list file roots"; 56 for (int i = 0; i < roots.length; i++) { 57 File 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 70 71 if (fs != null) { 72 break; 73 } 74 } catch (FileStateInvalidException e) { 75 throw new AssertionError (e); 76 } 77 } 78 FileSystem[] retVal = new FileSystem [allRoots.size()]; 79 allRoots.toArray(retVal); 80 assert retVal.length > 0 : "Could not get any filesystem"; 82 return fileSystems = retVal; 83 } 84 85 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 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 ext = fo.getExt(); 122 return ("java".equals(ext) || "class".equals(ext)) && (getCPRoot(fo) != null); } 124 } | Popular Tags |