1 19 package org.netbeans.modules.java.j2seplatform.libraries; 20 21 22 import org.openide.filesystems.FileObject; 23 import org.openide.filesystems.FileUtil; 24 import org.netbeans.api.project.libraries.Library; 25 import org.netbeans.api.project.libraries.LibraryManager; 26 import org.netbeans.api.java.classpath.ClassPath; 27 import org.netbeans.api.java.platform.JavaPlatformManager; 28 import org.netbeans.spi.java.classpath.ClassPathProvider; 29 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 30 import org.netbeans.modules.java.j2seplatform.platformdefinition.Util; 31 32 import java.util.List ; 33 import java.net.URL ; 34 35 36 37 38 public class J2SELibraryClassPathProvider implements ClassPathProvider { 39 40 public ClassPath findClassPath(FileObject file, String type) { 41 assert file != null; 42 Library[] libraries; 43 Library ll = this.getLastUsedLibrary(file); 44 if (ll != null) { 45 libraries = new Library[] {ll}; 46 } 47 else { 48 libraries = LibraryManager.getDefault().getLibraries(); 49 } 50 for (int i=0; i< libraries.length; i++) { 51 if (J2SELibraryTypeProvider.LIBRARY_TYPE.equalsIgnoreCase(libraries[i].getType())) { 52 List resources = libraries[i].getContent (J2SELibraryTypeProvider.VOLUME_TYPE_SRC); 53 ClassPath sourcePath = ClassPathSupport.createClassPath((URL []) resources.toArray(new URL [resources.size()])); 54 FileObject root; 55 if ((root = sourcePath.findOwnerRoot(file))!=null) { 56 this.setLastUsedLibrary(root,libraries[i]); 57 if (ClassPath.SOURCE.equals(type)) { 58 return sourcePath; 59 } 60 else if (ClassPath.COMPILE.equals(type)) { 61 resources = libraries[i].getContent(J2SELibraryTypeProvider.VOLUME_TYPE_CLASSPATH); 62 return ClassPathSupport.createClassPath((URL []) resources.toArray(new URL [resources.size()])); 63 } 64 else if (ClassPath.BOOT.equals(type)) { 65 return JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(); 66 } 67 else { 68 break; 69 } 70 } 71 } 72 } 73 return null; 74 } 75 76 77 private synchronized Library getLastUsedLibrary (FileObject fo) { 78 if (this.lastUsedRoot != null && FileUtil.isParentOf(this.lastUsedRoot,fo)) { 79 return this.lastUsedLibrary; 80 } 81 else { 82 return null; 83 } 84 } 85 86 private synchronized void setLastUsedLibrary (FileObject root, Library lib) { 87 this.lastUsedRoot = root; 88 this.lastUsedLibrary = lib; 89 } 90 91 private FileObject lastUsedRoot; 92 private Library lastUsedLibrary; 93 } 94 | Popular Tags |