KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > SFBQueryImpl


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.bluej;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
29 import org.netbeans.api.java.queries.SourceForBinaryQuery;
30 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
31 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
32 import org.netbeans.spi.project.support.ant.AntProjectHelper;
33 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37
38 /**
39  *
40  * @author mkleint
41  */

42 public class SFBQueryImpl implements SourceForBinaryQueryImplementation, JavadocForBinaryQueryImplementation {
43     
44     private final AntProjectHelper helper;
45     private final PropertyEvaluator evaluator;
46     private Map JavaDoc/*<URL,SourceForBinaryQuery.Result>*/ cache = new HashMap JavaDoc ();
47     private DocResult docResult;
48
49     private BluejProject project;
50
51     public SFBQueryImpl(BluejProject proj, AntProjectHelper helper, PropertyEvaluator evaluator) {
52         this.helper = helper;
53         this.evaluator = evaluator;
54         project = proj;
55     }
56
57     public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
58         if (FileUtil.getArchiveFile(binaryRoot) != null) {
59             binaryRoot = FileUtil.getArchiveFile(binaryRoot);
60             // XXX check whether this is really the root
61
}
62         SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get (binaryRoot);
63         if (res != null) {
64             return res;
65         }
66         FileObject src = null;
67         if (matches(binaryRoot,"build.classes.dir")) { // NOI18N
68
src = project.getProjectDirectory();
69         }
70         else if (matches (binaryRoot,"dist.jar")) { // NOI18N
71
src = project.getProjectDirectory();
72         }
73         else if (matches (binaryRoot,"build.test.classes.dir")) { // NOI18N
74
src = project.getProjectDirectory();
75         }
76         if (src == null) {
77             return null;
78         }
79         else {
80             res = new Result(src);
81             cache.put (binaryRoot, res);
82             return res;
83         }
84     }
85
86
87     private boolean matches (URL JavaDoc binaryRoot, String JavaDoc binaryProperty) {
88         try {
89             String JavaDoc outDir = evaluator.getProperty(binaryProperty);
90             if (outDir != null) {
91                 File JavaDoc f = helper.resolveFile (outDir);
92                 URL JavaDoc url = f.toURI().toURL();
93                 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { // NOI18N
94
// non-existing
95
assert !url.toExternalForm().endsWith("/") : f; // NOI18N
96
url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
97
}
98                 if (url.equals (binaryRoot)) {
99                     return true;
100                 }
101             }
102         } catch (MalformedURLException JavaDoc malformedURL) {
103             ErrorManager.getDefault().notify(malformedURL);
104         }
105         return false;
106     }
107
108     public JavadocForBinaryQuery.Result findJavadoc(URL JavaDoc binaryRoot) {
109         if (FileUtil.getArchiveFile(binaryRoot) != null) {
110             binaryRoot = FileUtil.getArchiveFile(binaryRoot);
111             // XXX check whether this is really the root
112
}
113         if (matches (binaryRoot, "build.classes.dir") || matches (binaryRoot, "dist.jar") || // NOI18N
114
matches (binaryRoot, "build.test.classes.dir")) { // NOI18N
115
if (docResult == null) {
116                 //TODO make this relative to property?? the location should not be changed anyway because then
117
// it stops working against bluej itself..
118
File JavaDoc fil = new File JavaDoc(FileUtil.toFile(project.getProjectDirectory()), "doc"); // NOI18N
119
try {
120                     docResult = new DocResult(fil.toURI().toURL());
121                 } catch (MalformedURLException JavaDoc ex) {
122                     ex.printStackTrace();
123                 }
124             }
125         }
126         return docResult;
127     }
128     
129     private static class Result implements SourceForBinaryQuery.Result {
130         private FileObject[] sourceRoots;
131         public Result(FileObject fo) {
132             this.sourceRoots = new FileObject[] {fo};
133         }
134         
135         public FileObject[] getRoots () {
136             return this.sourceRoots;
137         }
138         
139         public synchronized void addChangeListener (ChangeListener JavaDoc l) {
140         }
141         
142         public synchronized void removeChangeListener (ChangeListener JavaDoc l) {
143         }
144     }
145
146     private static class DocResult implements JavadocForBinaryQuery.Result {
147
148         private URL JavaDoc[] urls;
149         public DocResult(URL JavaDoc url) {
150             if (!url.toExternalForm().endsWith("/")) { // NOI18N
151
try {
152                     url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
153
} catch (MalformedURLException JavaDoc ex) {
154                     ex.printStackTrace();
155                 } // NOI18N
156
}
157             urls = new URL JavaDoc[] {url};
158
159         }
160
161         public URL JavaDoc[] getRoots() {
162             return urls;
163         }
164
165         public void addChangeListener(ChangeListener JavaDoc l) {
166         }
167
168         public void removeChangeListener(ChangeListener JavaDoc l) {
169         }
170     }
171     
172 }
173
Popular Tags