KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > classpath > PlatformClassPathProvider


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.java.platform.classpath;
21
22
23 import java.util.Collections JavaDoc;
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     /** Creates a new instance of PlatformClassPathProvider */
38     public PlatformClassPathProvider() {
39     }
40     
41     
42     public ClassPath findClassPath(FileObject fo, String JavaDoc type) {
43         if (fo == null || type == null) {
44             throw new IllegalArgumentException JavaDoc();
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