KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > queries > CompiledSourceForBinaryQuery


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 package org.netbeans.modules.j2ee.clientproject.queries;
20
21 import java.io.File JavaDoc;
22 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
23 import org.netbeans.spi.project.support.ant.AntProjectHelper;
24 import org.openide.ErrorManager;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.FileUtil;
27
28 import java.net.URL JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import javax.swing.event.ChangeEvent JavaDoc;
38
39 import org.netbeans.api.java.queries.SourceForBinaryQuery;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41 import org.netbeans.modules.j2ee.clientproject.SourceRoots;
42 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
43
44 /**
45  * Finds sources corresponding to binaries in a J2SE project.
46  * @author Jesse Glick, Tomas Zezula
47  */

48 public class CompiledSourceForBinaryQuery implements SourceForBinaryQueryImplementation {
49     
50     private final AntProjectHelper helper;
51     private final PropertyEvaluator evaluator;
52     private final SourceRoots sourceRoots;
53     private final SourceRoots testRoots;
54     private Map JavaDoc<URL JavaDoc,SourceForBinaryQuery.Result> cache = new HashMap JavaDoc<URL JavaDoc,SourceForBinaryQuery.Result>();
55     
56     public CompiledSourceForBinaryQuery(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots srcRoots, SourceRoots testRoots) {
57         this.helper = helper;
58         this.evaluator = evaluator;
59         this.sourceRoots = srcRoots;
60         this.testRoots = testRoots;
61     }
62     
63     public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
64         if (FileUtil.getArchiveFile(binaryRoot) != null) {
65             binaryRoot = FileUtil.getArchiveFile(binaryRoot);
66             // XXX check whether this is really the root
67
}
68         SourceForBinaryQuery.Result res = cache.get(binaryRoot);
69         if (res != null) {
70             return res;
71         }
72         SourceRoots src = null;
73         if (hasSources(binaryRoot, AppClientProjectProperties.BUILD_CLASSES_DIR)) { //NOI18N
74
src = this.sourceRoots;
75         } else if (hasSources(binaryRoot, AppClientProjectProperties.DIST_JAR)) { //NOI18N
76
src = this.sourceRoots;
77         } else if (hasSources(binaryRoot, AppClientProjectProperties.BUILD_TEST_CLASSES_DIR)) { //NOI18N
78
src = this.testRoots;
79         }
80         if (src == null) {
81             return null;
82         } else {
83             res = new Result(src);
84             cache.put(binaryRoot, res);
85             return res;
86         }
87     }
88     
89     
90     private boolean hasSources(URL JavaDoc binaryRoot, String JavaDoc binaryProperty) {
91         try {
92             String JavaDoc outDir = evaluator.getProperty(binaryProperty);
93             if (outDir != null) {
94                 File JavaDoc f = helper.resolveFile(outDir);
95                 URL JavaDoc url = f.toURI().toURL();
96                 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { // NOI18N
97
// non-existing
98
assert !url.toExternalForm().endsWith("/") : f; // NOI18N
99
url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
100
}
101                 if (url.equals(binaryRoot)) {
102                     return true;
103                 }
104             }
105         } catch (MalformedURLException JavaDoc malformedURL) {
106             ErrorManager.getDefault().notify(malformedURL);
107         }
108         return false;
109     }
110     
111     private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener JavaDoc {
112         
113         private ArrayList JavaDoc<ChangeListener JavaDoc> listeners;
114         private SourceRoots sourceRoots;
115         
116         public Result(SourceRoots sourceRoots) {
117             this.sourceRoots = sourceRoots;
118             this.sourceRoots.addPropertyChangeListener(this);
119         }
120         
121         public FileObject[] getRoots() {
122             return this.sourceRoots.getRoots(); //No need to cache it, SourceRoots does
123
}
124         
125         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
126             if (this.listeners == null) {
127                 this.listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
128             }
129             this.listeners.add(l);
130         }
131         
132         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
133             if (this.listeners == null) {
134                 return;
135             }
136             this.listeners.remove(l);
137         }
138         
139         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
140             if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) {
141                 this.fireChange();
142             }
143         }
144         
145         private void fireChange() {
146             Iterator JavaDoc<ChangeListener JavaDoc> it;
147             synchronized (this) {
148                 if (this.listeners == null) {
149                     return;
150                 }
151                 @SuppressWarnings JavaDoc("unchecked")
152                 ArrayList JavaDoc<ChangeListener JavaDoc> cloned = (ArrayList JavaDoc) this.listeners.clone();
153                 it = cloned.iterator();
154             }
155             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
156             while (it.hasNext()) {
157                 (it.next()).stateChanged(event);
158             }
159         }
160         
161     }
162     
163 }
164
Popular Tags