2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.bluej;
45
import java.net.MalformedURLException;
47
import java.util.HashMap;
49
import javax.swing.event.ChangeListener;
50
import org.netbeans.api.java.queries.JavadocForBinaryQuery;
51
import org.netbeans.api.java.queries.SourceForBinaryQuery;
52
import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
53
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
54
import org.netbeans.spi.project.support.ant.AntProjectHelper;
55
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
56
import org.openide.ErrorManager;
57
import org.openide.filesystems.FileObject;
58
import org.openide.filesystems.FileUtil;
64
public class SFBQueryImpl implements SourceForBinaryQueryImplementation, JavadocForBinaryQueryImplementation {
66
private final AntProjectHelper helper;
67
private final PropertyEvaluator evaluator;
68
private Map/*<URL,SourceForBinaryQuery.Result>*/ cache = new HashMap ();
69
private DocResult docResult;
71
private BluejProject project;
73
public SFBQueryImpl(BluejProject proj, AntProjectHelper helper, PropertyEvaluator evaluator) {
75
this.evaluator = evaluator;
79
public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) {
80
if (FileUtil.getArchiveFile(binaryRoot) != null) {
81
binaryRoot = FileUtil.getArchiveFile(binaryRoot);
82
// XXX check whether this is really the root
84
SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get (binaryRoot);
88
FileObject src = null;
89
if (matches(binaryRoot,"build.classes.dir")) { // NOI18N
90
src = project.getProjectDirectory();
92
else if (matches (binaryRoot,"dist.jar")) { // NOI18N
93
src = project.getProjectDirectory();
95
else if (matches (binaryRoot,"build.test.classes.dir")) { // NOI18N
96
src = project.getProjectDirectory();
102
res = new Result(src);
103
cache.put (binaryRoot, res);
109
private boolean matches (URL binaryRoot, String binaryProperty) {
111
String outDir = evaluator.getProperty(binaryProperty);
112
if (outDir != null) {
113
File f = helper.resolveFile (outDir);
114
URL url = f.toURI().toURL();
115
if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { // NOI18N
117
assert !url.toExternalForm().endsWith("/") : f; // NOI18N
118
url = new URL(url.toExternalForm() + "/"); // NOI18N
120
if (url.equals (binaryRoot)) {
124
} catch (MalformedURLException malformedURL) {
125
ErrorManager.getDefault().notify(malformedURL);
130
public JavadocForBinaryQuery.Result findJavadoc(URL binaryRoot) {
131
if (FileUtil.getArchiveFile(binaryRoot) != null) {
132
binaryRoot = FileUtil.getArchiveFile(binaryRoot);
133
// XXX check whether this is really the root
135
if (matches (binaryRoot, "build.classes.dir") || matches (binaryRoot, "dist.jar") || // NOI18N
136
matches (binaryRoot, "build.test.classes.dir")) { // NOI18N
137
if (docResult == null) {
138
//TODO make this relative to property?? the location should not be changed anyway because then
139
// it stops working against bluej itself..
140
File fil = new File(FileUtil.toFile(project.getProjectDirectory()), "doc"); // NOI18N
142
docResult = new DocResult(fil.toURI().toURL());
143
} catch (MalformedURLException ex) {
144
ex.printStackTrace();
151
private static class Result implements SourceForBinaryQuery.Result {
152
private FileObject[] sourceRoots;
153
public Result(FileObject fo) {
154
this.sourceRoots = new FileObject[] {fo};
157
public FileObject[] getRoots () {
158
return this.sourceRoots;
161
public synchronized void addChangeListener (ChangeListener l) {
164
public synchronized void removeChangeListener (ChangeListener l) {
168
private static class DocResult implements JavadocForBinaryQuery.Result {
171
public DocResult(URL url) {
172
if (!url.toExternalForm().endsWith("/")) { // NOI18N
174
url = new URL(url.toExternalForm() + "/"); // NOI18N
175
} catch (MalformedURLException ex) {
176
ex.printStackTrace();
179
urls = new URL[] {url};
183
public URL[] getRoots() {
187
public void addChangeListener(ChangeListener l) {
190
public void removeChangeListener(ChangeListener l) {