001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.web.project;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.net.MalformedURLException;
045: import java.util.List;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.beans.PropertyChangeListener;
049: import java.beans.PropertyChangeSupport;
050: import java.net.URL;
051: import javax.swing.event.ChangeEvent;
052: import javax.swing.event.ChangeListener;
053: import org.netbeans.api.project.SourceGroup;
054: import org.netbeans.api.project.Sources;
055: import org.netbeans.modules.gsfpath.spi.classpath.ClassPathImplementation;
056: import org.netbeans.modules.gsfpath.spi.classpath.PathResourceImplementation;
057: import org.netbeans.modules.gsfpath.spi.classpath.support.ClassPathSupport;
058: import org.netbeans.spi.project.support.ant.AntProjectHelper;
059: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.util.Exceptions;
062:
063: /**
064: * Implementation of a single classpath that is derived from one Ant property.
065: */
066: final class GsfSourcePathImplementation implements
067: ClassPathImplementation, PropertyChangeListener, ChangeListener {
068:
069: private static final String PROP_BUILD_DIR = "build.dir"; //NOI18N
070:
071: private final PropertyChangeSupport support = new PropertyChangeSupport(
072: this );
073: private List<PathResourceImplementation> resources;
074: private final WebSources sourceRoots;
075: private final AntProjectHelper projectHelper;
076: private final PropertyEvaluator evaluator;
077: private String urlString;
078:
079: /**
080: * Construct the implementation.
081: * @param sourceRoots used to get the roots information and events
082: */
083: public GsfSourcePathImplementation(WebSources sourceRoots) {
084: assert sourceRoots != null;
085: this .sourceRoots = sourceRoots;
086: this .projectHelper = null;
087: this .evaluator = null;
088: // sourceRoots.addPropertyChangeListener (this);
089: sourceRoots.addChangeListener(this );
090: }
091:
092: /**
093: * Construct the implementation.
094: * @param sourceRoots used to get the roots information and events
095: * @param projectHelper used to obtain the project root
096: */
097: public GsfSourcePathImplementation(WebSources sourceRoots,
098: AntProjectHelper projectHelper, PropertyEvaluator evaluator) {
099: assert sourceRoots != null && projectHelper != null
100: && evaluator != null;
101: this .sourceRoots = sourceRoots;
102: //sourceRoots.addPropertyChangeListener (this);
103: sourceRoots.addChangeListener(this );
104: this .projectHelper = projectHelper;
105: this .evaluator = evaluator;
106: evaluator.addPropertyChangeListener(this );
107: }
108:
109: public List<PathResourceImplementation> getResources() {
110: synchronized (this ) {
111: if (this .resources != null) {
112: return this .resources;
113: }
114: }
115:
116: // TODO - synchronize
117: List<URL> roots = new ArrayList<URL>();
118: SourceGroup[] sourceGroups = sourceRoots
119: .getSourceGroups(Sources.TYPE_GENERIC);
120: for (SourceGroup sg : sourceGroups) {
121: try {
122: roots.add(FileUtil.toFile(sg.getRootFolder()).toURI()
123: .toURL());
124: } catch (MalformedURLException ex) {
125: Exceptions.printStackTrace(ex);
126: }
127: }
128:
129: //URL[] roots = sourceRoots.getRootURLs();
130: synchronized (this ) {
131: if (this .resources == null) {
132: List<PathResourceImplementation> result = new ArrayList<PathResourceImplementation>(
133: roots.size());
134: for (URL root : roots) {
135: urlString = root.toExternalForm();
136: result.add(ClassPathSupport.createResource(root));
137: }
138: this .resources = Collections.unmodifiableList(result);
139: }
140: return this .resources;
141: }
142: }
143:
144: public void addPropertyChangeListener(
145: PropertyChangeListener listener) {
146: support.addPropertyChangeListener(listener);
147: }
148:
149: public void removePropertyChangeListener(
150: PropertyChangeListener listener) {
151: support.removePropertyChangeListener(listener);
152: }
153:
154: public void propertyChange(PropertyChangeEvent evt) {
155: /* if (SourceRoots.PROP_ROOTS.equals (evt.getPropertyName())) {
156: synchronized (this) {
157: this.resources = null;
158: }
159: this.support.firePropertyChange (PROP_RESOURCES,null,null);
160: }
161: else */if (this .evaluator != null
162: && evt.getSource() == this .evaluator
163: && (evt.getPropertyName() == null || PROP_BUILD_DIR
164: .equals(evt.getPropertyName()))) {
165: synchronized (this ) {
166: this .resources = null;
167: }
168: this .support.firePropertyChange(PROP_RESOURCES, null, null);
169: }
170: }
171:
172: public void stateChanged(ChangeEvent e) {
173: this.support.firePropertyChange(PROP_RESOURCES, null, null);
174: }
175:
176: }
|