KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > queries > BinaryForSourceQueryImpl


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.j2seproject.queries;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.api.java.queries.BinaryForSourceQuery;
34 import org.netbeans.api.java.queries.BinaryForSourceQuery.Result;
35 import org.netbeans.modules.java.j2seproject.SourceRoots;
36 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
37 import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
38 import org.netbeans.spi.project.support.ant.AntProjectHelper;
39 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
40 import org.openide.util.Exceptions;
41
42 /**
43  *
44  * @author Tomas Zezula
45  */

46 public class BinaryForSourceQueryImpl implements BinaryForSourceQueryImplementation {
47     
48     
49     private final Map JavaDoc<URL JavaDoc,BinaryForSourceQuery.Result> cache = new HashMap JavaDoc<URL JavaDoc,BinaryForSourceQuery.Result>();
50     private final SourceRoots src;
51     private final SourceRoots test;
52     private final PropertyEvaluator eval;
53     private final AntProjectHelper helper;
54     
55     /** Creates a new instance of BinaryForSourceQueryImpl */
56     public BinaryForSourceQueryImpl(SourceRoots src, SourceRoots test, AntProjectHelper helper, PropertyEvaluator eval) {
57         assert src != null;
58         assert test != null;
59         assert helper != null;
60         assert eval != null;
61         this.src = src;
62         this.test = test;
63         this.eval = eval;
64         this.helper = helper;
65     }
66     
67     public Result findBinaryRoots(URL JavaDoc sourceRoot) {
68         assert sourceRoot != null;
69         BinaryForSourceQuery.Result result = cache.get(sourceRoot);
70         if (result == null) {
71             for (URL JavaDoc root : this.src.getRootURLs()) {
72                 if (root.equals(sourceRoot)) {
73                     result = new R (J2SEProjectProperties.BUILD_CLASSES_DIR);
74                     cache.put (sourceRoot,result);
75                     break;
76                 }
77             }
78             for (URL JavaDoc root : this.test.getRootURLs()) {
79                 if (root.equals(sourceRoot)) {
80                     result = new R (J2SEProjectProperties.BUILD_TEST_CLASSES_DIR);
81                     cache.put (sourceRoot,result);
82                     break;
83                 }
84             }
85         }
86         return result;
87     }
88     
89     class R implements BinaryForSourceQuery.Result, PropertyChangeListener JavaDoc {
90         
91         private final String JavaDoc propName;
92         private final List JavaDoc<ChangeListener JavaDoc> listeners = new CopyOnWriteArrayList JavaDoc<ChangeListener JavaDoc>();
93         
94         R (final String JavaDoc propName) {
95             assert propName != null;
96             this.propName = propName;
97             eval.addPropertyChangeListener(this);
98         }
99         
100         public URL JavaDoc[] getRoots() {
101             String JavaDoc val = eval.getProperty(propName);
102             if (val != null) {
103                 File JavaDoc f = helper.resolveFile(val);
104                 if (f != null) {
105                     try {
106                         return new URL JavaDoc[] {f.toURI().toURL()};
107                     } catch (MalformedURLException JavaDoc e) {
108                         Exceptions.printStackTrace(e);
109                     }
110                 }
111             }
112             return new URL JavaDoc[0];
113         }
114
115         public void addChangeListener(ChangeListener JavaDoc l) {
116             assert l != null;
117             this.listeners.add (l);
118         }
119
120         public void removeChangeListener(ChangeListener JavaDoc l) {
121             assert l != null;
122             this.listeners.remove (l);
123         }
124
125         public void propertyChange(PropertyChangeEvent JavaDoc event) {
126             ChangeEvent JavaDoc ce = new ChangeEvent JavaDoc (this);
127             for (ChangeListener JavaDoc l : listeners) {
128                 l.stateChanged(ce);
129             }
130         }
131 }
132
133 }
134
Popular Tags