KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > queries > JavadocForBinaryQueryImpl


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.web.project.queries;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import org.netbeans.spi.project.support.ant.AntProjectHelper;
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileUtil;
30 import java.net.URL JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
34 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
35 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
36 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
37 import org.openide.util.WeakListeners;
38
39 /**
40  * Finds Javadoc (if it is built) corresponding to binaries in web project.
41  * @author David Konecny, Jesse Glick
42  */

43 public class JavadocForBinaryQueryImpl implements JavadocForBinaryQueryImplementation {
44     
45     private final AntProjectHelper helper;
46     private final PropertyEvaluator evaluator;
47
48     public JavadocForBinaryQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator) {
49         this.helper = helper;
50         this.evaluator = evaluator;
51     }
52
53     public JavadocForBinaryQuery.Result findJavadoc(final URL JavaDoc binaryRoot) {
54         
55         class R implements JavadocForBinaryQuery.Result, PropertyChangeListener JavaDoc {
56             
57             private List JavaDoc listeners;
58             private URL JavaDoc[] result;
59             
60             public R () {
61                 JavadocForBinaryQueryImpl.this.evaluator.addPropertyChangeListener (WeakListeners.propertyChange(this,JavadocForBinaryQueryImpl.this.evaluator));
62             }
63             
64             public synchronized URL JavaDoc[] getRoots() {
65                 if (this.result == null) {
66                     String JavaDoc javadocDir = evaluator.getProperty(WebProjectProperties.DIST_JAVADOC_DIR);
67                     if (javadocDir != null) {
68                         File JavaDoc f = helper.resolveFile(javadocDir);
69                         try {
70                             URL JavaDoc url = f.toURI().toURL();
71                             if (!f.exists()) {
72                                 assert !url.toExternalForm().endsWith("/") : f; // NOI18N
73
url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
74
}
75                             this.result = new URL JavaDoc[] {url};
76                         } catch (MalformedURLException JavaDoc e) {
77                             this.result = new URL JavaDoc[0];
78                             ErrorManager.getDefault().notify(e);
79                         }
80                     }
81                     else {
82                         this.result = new URL JavaDoc[0];
83                     }
84                 }
85                 return this.result;
86             }
87             public synchronized void addChangeListener(final ChangeListener JavaDoc l) {
88                 assert l != null;
89                 if (this.listeners == null) {
90                     this.listeners = new ArrayList JavaDoc ();
91                 }
92                 this.listeners.add (l);
93             }
94             public synchronized void removeChangeListener(final ChangeListener JavaDoc l) {
95                 assert l != null;
96                 if (this.listeners == null) {
97                     return;
98                 }
99                 this.listeners.remove (l);
100             }
101             
102             public void propertyChange (final PropertyChangeEvent JavaDoc event) {
103                 if (WebProjectProperties.DIST_JAVADOC_DIR.equals(event.getPropertyName())) {
104                     synchronized (this) {
105                         result = null;
106                     }
107                     this.fireChange ();
108                 }
109             }
110             
111             private void fireChange () {
112                 ChangeListener JavaDoc[] _listeners;
113                 synchronized (this) {
114                     if (this.listeners == null) {
115                         return;
116                     }
117                     _listeners = (ChangeListener JavaDoc[]) this.listeners.toArray (new ChangeListener JavaDoc[this.listeners.size()]);
118                 }
119                 ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
120                 for (int i=0; i<_listeners.length; i++) {
121                     _listeners[i].stateChanged(event);
122                 }
123             }
124         }
125         if (isRootOwner(binaryRoot, WebProjectProperties.BUILD_CLASSES_DIR) || isRootOwner (binaryRoot, WebProjectProperties.DIST_WAR)) {
126             return new R();
127         }
128         return null;
129     }
130
131     private boolean isRootOwner (URL JavaDoc binaryRoot, String JavaDoc binaryProperty) {
132         try {
133             if (FileUtil.getArchiveFile(binaryRoot) != null) {
134                 binaryRoot = FileUtil.getArchiveFile(binaryRoot);
135                 // XXX check whether this is really the root
136
}
137             String JavaDoc outDir = evaluator.getProperty(binaryProperty);
138             if (outDir != null) {
139                 File JavaDoc f = helper.resolveFile (outDir);
140                 URL JavaDoc url = f.toURI().toURL();
141                 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { // NOI18N
142
assert !url.toExternalForm().endsWith("/") : f; // NOI18N
143
url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
144
}
145                 return url.equals(binaryRoot) ||
146                         binaryRoot.toExternalForm().startsWith(url.toExternalForm());
147             }
148         } catch (MalformedURLException JavaDoc malformedURL) {
149             ErrorManager.getDefault().notify(malformedURL);
150         }
151         return false;
152     }
153
154 // private URL getJavadoc(URL binaryRoot, String binaryProperty, String javadocProperty) {
155
// try {
156
// if (FileUtil.getArchiveFile(binaryRoot) != null) {
157
// binaryRoot = FileUtil.getArchiveFile(binaryRoot);
158
// }
159
// String outDir = evaluator.getProperty(binaryProperty);
160
// if (outDir != null) {
161
// File f = helper.resolveFile (outDir);
162
// URL url = f.toURI().toURL();
163
// if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) {
164
// assert !url.toExternalForm().endsWith("/") : f;
165
// url = new URL(url.toExternalForm() + "/");
166
// }
167
// if (url.equals(binaryRoot) ||
168
// binaryRoot.toExternalForm().startsWith(url.toExternalForm())) {
169
// String javadocDir = evaluator.getProperty(javadocProperty);
170
// if (javadocDir != null) {
171
// f = helper.resolveFile(javadocDir);
172
// return f.toURI().toURL();
173
// }
174
// }
175
// }
176
// } catch (MalformedURLException malformedURL) {
177
// ErrorManager.getDefault().notify(malformedURL);
178
// }
179
// return null;
180
// }
181

182 }
183
Popular Tags