KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.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 javax.xml.transform.Result JavaDoc;
34 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
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 J2SE project.
41  * @author David Konecny, Jesse Glick
42  */

43 public class JavadocForBinaryQueryImpl implements JavadocForBinaryQueryImplementation {
44     
45     private static final String JavaDoc PROP_JAVADOC_DIR = "dist.javadoc.dir"; //NOI18N
46

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

184 }
185
Popular Tags