KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > libraries > J2SELibraryClassPathProvider


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 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 JavaDoc;
33 import java.net.URL JavaDoc;
34
35
36
37
38 public class J2SELibraryClassPathProvider implements ClassPathProvider {
39
40     public ClassPath findClassPath(FileObject file, String JavaDoc 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 JavaDoc resources = libraries[i].getContent (J2SELibraryTypeProvider.VOLUME_TYPE_SRC);
53                 ClassPath sourcePath = ClassPathSupport.createClassPath((URL JavaDoc[]) resources.toArray(new URL JavaDoc[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 JavaDoc[]) resources.toArray(new URL JavaDoc[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