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

« back to all changes in this revision

Viewing changes to j2ee/clientproject/src/org/netbeans/modules/j2ee/clientproject/classpath/SourcePathImplementation.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
package org.netbeans.modules.j2ee.clientproject.classpath;
 
42
 
 
43
import java.beans.PropertyChangeEvent;
 
44
import java.io.File;
 
45
import java.net.MalformedURLException;
 
46
import java.util.List;
 
47
import java.util.ArrayList;
 
48
import java.util.Collections;
 
49
import java.beans.PropertyChangeListener;
 
50
import java.beans.PropertyChangeSupport;
 
51
import java.net.URL;
 
52
import org.netbeans.spi.java.classpath.ClassPathImplementation;
 
53
import org.netbeans.spi.java.classpath.PathResourceImplementation;
 
54
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
 
55
import org.netbeans.modules.j2ee.clientproject.SourceRoots;
 
56
import org.netbeans.spi.project.support.ant.AntProjectHelper;
 
57
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
 
58
import org.openide.util.Exceptions;
 
59
 
 
60
 
 
61
/**
 
62
 * Implementation of a single classpath that is derived from one Ant property.
 
63
 */
 
64
final class SourcePathImplementation implements ClassPathImplementation, PropertyChangeListener {
 
65
 
 
66
    private static final String PROP_BUILD_DIR = "build.dir";   //NOI18N
 
67
    
 
68
    private final PropertyChangeSupport support = new PropertyChangeSupport(this);
 
69
    private List<PathResourceImplementation> resources;
 
70
    private final SourceRoots sourceRoots;
 
71
    private final AntProjectHelper projectHelper;
 
72
    private final PropertyEvaluator evaluator;
 
73
    
 
74
    /**
 
75
     * Construct the implementation.
 
76
     * @param sourceRoots used to get the roots information and events
 
77
     */
 
78
    public SourcePathImplementation(SourceRoots sourceRoots) {
 
79
        assert sourceRoots != null;
 
80
        this.sourceRoots = sourceRoots;
 
81
        this.projectHelper = null;
 
82
        this.evaluator = null;
 
83
        sourceRoots.addPropertyChangeListener (this);
 
84
    }
 
85
    
 
86
    /**
 
87
     * Construct the implementation.
 
88
     * @param sourceRoots used to get the roots information and events
 
89
     * @param projectHelper used to obtain the project root
 
90
     */
 
91
    public SourcePathImplementation(SourceRoots sourceRoots, AntProjectHelper projectHelper, PropertyEvaluator evaluator) {
 
92
        assert sourceRoots != null && projectHelper != null && evaluator != null;
 
93
        this.sourceRoots = sourceRoots;
 
94
        sourceRoots.addPropertyChangeListener (this);
 
95
        this.projectHelper = projectHelper;
 
96
        this.evaluator = evaluator;
 
97
        evaluator.addPropertyChangeListener (this);
 
98
    }
 
99
 
 
100
    public List<PathResourceImplementation> getResources() {
 
101
        synchronized (this) {
 
102
            if (this.resources != null) {
 
103
                return this.resources;
 
104
            }
 
105
        }                                
 
106
        URL[] roots = sourceRoots.getRootURLs();                                
 
107
        String buildDir = null;
 
108
        if (projectHelper != null) {
 
109
            buildDir = projectHelper.getStandardPropertyEvaluator().getProperty(PROP_BUILD_DIR);
 
110
        }
 
111
        synchronized (this) {
 
112
            if (this.resources == null) {
 
113
                List<PathResourceImplementation> result = new ArrayList<PathResourceImplementation>(roots.length);
 
114
                for (int i = 0; i < roots.length; i++) {
 
115
                    PathResourceImplementation res = ClassPathSupport.createResource(roots[i]);
 
116
                    result.add (res);
 
117
                }
 
118
                // adds build/generated/wsclient and build/generated/wsimport/client to resources to be available for code completion
 
119
                if (projectHelper!=null) {
 
120
                    try {
 
121
                        if (buildDir != null) {
 
122
                            // generated/wsclient
 
123
                            File f =  new File (this.projectHelper.resolveFile (buildDir),"generated/wsclient"); //NOI18N
 
124
                            URL url = f.toURI().toURL();
 
125
                            if (!f.exists()) {  //NOI18N
 
126
                                assert !url.toExternalForm().endsWith("/");  //NOI18N
 
127
                                url = new URL (url.toExternalForm()+'/');   //NOI18N
 
128
                            }
 
129
                            result.add(ClassPathSupport.createResource(url));
 
130
                            // generated/wsimport/client
 
131
                            f = new File (projectHelper.resolveFile(buildDir),"generated/wsimport/client"); //NOI18N
 
132
                            url = f.toURI().toURL();
 
133
                            if (!f.exists()) {  //NOI18N
 
134
                                assert !url.toExternalForm().endsWith("/");  //NOI18N
 
135
                                url = new URL (url.toExternalForm()+'/');   //NOI18N
 
136
                            }
 
137
                            result.add(ClassPathSupport.createResource(url));
 
138
                        }
 
139
                    } catch (MalformedURLException ex) {
 
140
                        Exceptions.printStackTrace(ex);
 
141
                    }
 
142
                }
 
143
                this.resources = Collections.unmodifiableList(result);
 
144
            }
 
145
            return this.resources;
 
146
        }
 
147
    }
 
148
 
 
149
    public void addPropertyChangeListener(PropertyChangeListener listener) {
 
150
        support.addPropertyChangeListener (listener);
 
151
    }
 
152
 
 
153
    public void removePropertyChangeListener(PropertyChangeListener listener) {
 
154
        support.removePropertyChangeListener (listener);
 
155
    }
 
156
 
 
157
 
 
158
    public void propertyChange(PropertyChangeEvent evt) {
 
159
        if (SourceRoots.PROP_ROOTS.equals (evt.getPropertyName())) {
 
160
            synchronized (this) {
 
161
                this.resources = null;
 
162
            }
 
163
            this.support.firePropertyChange (PROP_RESOURCES,null,null);
 
164
        }
 
165
        else if (this.evaluator != null && evt.getSource() == this.evaluator && 
 
166
            (evt.getPropertyName() == null || PROP_BUILD_DIR.equals(evt.getPropertyName()))) {
 
167
            synchronized (this) {
 
168
                this.resources = null;
 
169
            }
 
170
            this.support.firePropertyChange (PROP_RESOURCES,null,null);
 
171
        }
 
172
    }
 
173
 
 
174
}