KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > queries > UpdateTrackingFileOwnerQuery


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.apisupport.project.queries;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.modules.apisupport.project.Util;
30 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
31 import org.netbeans.modules.apisupport.project.universe.ModuleList;
32 import org.netbeans.modules.apisupport.project.universe.TestEntry;
33 import org.netbeans.spi.project.FileOwnerQueryImplementation;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37
38 /**
39  * Associated built module files with their owning project.
40  * @author Jesse Glick
41  */

42 public final class UpdateTrackingFileOwnerQuery implements FileOwnerQueryImplementation {
43     
44     /** Default constructor for lookup. */
45     public UpdateTrackingFileOwnerQuery() {}
46
47     public Project getOwner(URI JavaDoc file) {
48         if (!ModuleList.existKnownEntries()) {
49             return null; // #65700
50
}
51         if (file.getScheme().equals("file")) { // NOI18N
52
return getOwner(new File JavaDoc(file));
53         } else {
54             return null;
55         }
56     }
57
58     public Project getOwner(FileObject file) {
59         if (!ModuleList.existKnownEntries()) {
60             return null; // #65700
61
}
62         File JavaDoc f = FileUtil.toFile(file);
63         if (f != null) {
64             return getOwner(f);
65         } else {
66             return null;
67         }
68     }
69     
70     private Project getOwner(File JavaDoc file) {
71         Set JavaDoc<ModuleEntry> entries = ModuleList.getKnownEntries(file);
72         Iterator JavaDoc it = entries.iterator();
73         while (it.hasNext()) {
74             File JavaDoc sourcedir = ((ModuleEntry) it.next()).getSourceLocation();
75             if (sourcedir != null) {
76                 FileObject sourcedirFO = FileUtil.toFileObject(sourcedir);
77                 if (sourcedirFO != null) {
78                     try {
79                         Project p = ProjectManager.getDefault().findProject(sourcedirFO);
80                         if (p != null) {
81                             return p;
82                         }
83                     } catch (IOException JavaDoc e) {
84                         Util.err.notify(ErrorManager.INFORMATIONAL, e);
85                     }
86                 }
87             }
88         }
89         // may be tests.jar in tests distribution
90
TestEntry test = TestEntry.get(file);
91         if (test != null) {
92             Project p = test.getProject() ;
93             if (p != null) {
94                 return p;
95             }
96         }
97         return null;
98     }
99     
100 }
101
Popular Tags