1 19 20 package org.netbeans.modules.java.platform.classpath; 21 22 23 import java.util.Collections ; 24 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileUtil; 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.netbeans.spi.java.classpath.ClassPathProvider; 29 import org.netbeans.api.java.platform.JavaPlatform; 30 import org.netbeans.api.java.platform.JavaPlatformManager; 31 32 33 public class PlatformClassPathProvider implements ClassPathProvider { 34 35 36 37 38 public PlatformClassPathProvider() { 39 } 40 41 42 public ClassPath findClassPath(FileObject fo, String type) { 43 if (fo == null || type == null) { 44 throw new IllegalArgumentException (); 45 } 46 JavaPlatform lp = this.getLastUsedPlatform(fo); 47 JavaPlatform[] platforms; 48 if (lp != null) { 49 platforms = new JavaPlatform[] {lp}; 50 } 51 else { 52 JavaPlatformManager manager = JavaPlatformManager.getDefault(); 53 platforms = manager.getInstalledPlatforms(); 54 } 55 for (int i=0; i<platforms.length; i++) { 56 ClassPath bootClassPath = platforms[i].getBootstrapLibraries(); 57 ClassPath libraryPath = platforms[i].getStandardLibraries(); 58 ClassPath sourcePath = platforms[i].getSourceFolders(); 59 FileObject root = null; 60 if (ClassPath.SOURCE.equals(type) && sourcePath != null && 61 (root = sourcePath.findOwnerRoot(fo))!=null) { 62 this.setLastUsedPlatform (root,platforms[i]); 63 return sourcePath; 64 } 65 else if (ClassPath.BOOT.equals(type) && 66 ((bootClassPath != null && (root = bootClassPath.findOwnerRoot (fo))!=null) || 67 (sourcePath != null && (root = sourcePath.findOwnerRoot(fo)) != null) || 68 (libraryPath != null && (root = libraryPath.findOwnerRoot(fo))!=null))) { 69 this.setLastUsedPlatform (root,platforms[i]); 70 return bootClassPath; 71 } 72 else if (ClassPath.COMPILE.equals(type)) { 73 if (libraryPath != null && (root = libraryPath.findOwnerRoot(fo))!=null) { 74 this.setLastUsedPlatform (root,platforms[i]); 75 return libraryPath; 76 } 77 else if ((bootClassPath != null && (root = bootClassPath.findOwnerRoot (fo))!=null) || 78 (sourcePath != null && (root = sourcePath.findOwnerRoot(fo)) != null)) { 79 return this.getEmptyClassPath (); 80 } 81 } 82 } 83 return null; 84 } 85 86 private synchronized ClassPath getEmptyClassPath () { 87 if (this.emptyCp == null ) { 88 this.emptyCp = ClassPathSupport.createClassPath(Collections.EMPTY_LIST); 89 } 90 return this.emptyCp; 91 } 92 93 private synchronized void setLastUsedPlatform (FileObject root, JavaPlatform platform) { 94 this.lastUsedRoot = root; 95 this.lastUsedPlatform = platform; 96 } 97 98 private synchronized JavaPlatform getLastUsedPlatform (FileObject file) { 99 if (this.lastUsedRoot != null && FileUtil.isParentOf(this.lastUsedRoot,file)) { 100 return lastUsedPlatform; 101 } 102 else { 103 return null; 104 } 105 } 106 107 private FileObject lastUsedRoot; 108 private JavaPlatform lastUsedPlatform; 109 private ClassPath emptyCp; 110 } 111 | Popular Tags |