~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to ide/projectimport/bluej/src/org/netbeans/bluej/SFBQueryImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.bluej;
 
43
 
 
44
import java.io.File;
 
45
import java.net.MalformedURLException;
 
46
import java.net.URL;
 
47
import java.util.HashMap;
 
48
import java.util.Map;
 
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;
 
59
 
 
60
/**
 
61
 *
 
62
 * @author mkleint
 
63
 */
 
64
public class SFBQueryImpl implements SourceForBinaryQueryImplementation, JavadocForBinaryQueryImplementation {
 
65
    
 
66
    private final AntProjectHelper helper;
 
67
    private final PropertyEvaluator evaluator;
 
68
    private Map/*<URL,SourceForBinaryQuery.Result>*/  cache = new HashMap ();
 
69
    private DocResult docResult;
 
70
 
 
71
    private BluejProject project;
 
72
 
 
73
    public SFBQueryImpl(BluejProject proj, AntProjectHelper helper, PropertyEvaluator evaluator) {
 
74
        this.helper = helper;
 
75
        this.evaluator = evaluator;
 
76
        project = proj;
 
77
    }
 
78
 
 
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
 
83
        }
 
84
        SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get (binaryRoot);
 
85
        if (res != null) {
 
86
            return res;
 
87
        }
 
88
        FileObject src = null;
 
89
        if (matches(binaryRoot,"build.classes.dir")) {   // NOI18N
 
90
            src = project.getProjectDirectory();
 
91
        }
 
92
        else if (matches (binaryRoot,"dist.jar")) {      // NOI18N
 
93
            src = project.getProjectDirectory();
 
94
        }
 
95
        else if (matches (binaryRoot,"build.test.classes.dir")) {    // NOI18N
 
96
            src = project.getProjectDirectory();
 
97
        }
 
98
        if (src == null) {
 
99
            return null;
 
100
        }
 
101
        else {
 
102
            res = new Result(src);
 
103
            cache.put (binaryRoot, res);
 
104
            return res;
 
105
        }
 
106
    }
 
107
 
 
108
 
 
109
    private boolean matches (URL binaryRoot, String binaryProperty) {
 
110
        try {
 
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
 
116
                    // non-existing 
 
117
                    assert !url.toExternalForm().endsWith("/") : f; // NOI18N
 
118
                    url = new URL(url.toExternalForm() + "/"); // NOI18N
 
119
                }
 
120
                if (url.equals (binaryRoot)) {
 
121
                    return true;
 
122
                }
 
123
            }
 
124
        } catch (MalformedURLException malformedURL) {
 
125
            ErrorManager.getDefault().notify(malformedURL);
 
126
        }
 
127
        return false;
 
128
    }
 
129
 
 
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
 
134
        }
 
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
 
141
                try {
 
142
                    docResult = new DocResult(fil.toURI().toURL());
 
143
                } catch (MalformedURLException ex) {
 
144
                    ex.printStackTrace();
 
145
                }
 
146
            }
 
147
        }
 
148
        return docResult;
 
149
    }
 
150
    
 
151
    private static class Result implements SourceForBinaryQuery.Result {
 
152
        private FileObject[] sourceRoots;
 
153
        public Result(FileObject fo) {
 
154
            this.sourceRoots = new FileObject[] {fo};
 
155
        }
 
156
        
 
157
        public FileObject[] getRoots () {
 
158
            return this.sourceRoots; 
 
159
        }
 
160
        
 
161
        public synchronized void addChangeListener (ChangeListener l) {
 
162
        }
 
163
        
 
164
        public synchronized void removeChangeListener (ChangeListener l) {
 
165
        }
 
166
    }
 
167
 
 
168
    private static class DocResult implements JavadocForBinaryQuery.Result {
 
169
 
 
170
        private URL[] urls;
 
171
        public DocResult(URL url) {
 
172
            if (!url.toExternalForm().endsWith("/")) {  // NOI18N
 
173
                try {
 
174
                    url = new URL(url.toExternalForm() + "/"); // NOI18N
 
175
                } catch (MalformedURLException ex) {
 
176
                    ex.printStackTrace();
 
177
                } // NOI18N
 
178
            }
 
179
            urls = new URL[] {url};
 
180
 
 
181
        }
 
182
 
 
183
        public URL[] getRoots() {
 
184
            return urls;
 
185
        }
 
186
 
 
187
        public void addChangeListener(ChangeListener l) {
 
188
        }
 
189
 
 
190
        public void removeChangeListener(ChangeListener l) {
 
191
        }
 
192
    }
 
193
    
 
194
}