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.java.api.common.queries;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.beans.PropertyChangeListener;
045: import org.netbeans.api.project.ProjectManager;
046: import org.openide.filesystems.FileObject;
047: import org.netbeans.api.queries.FileBuiltQuery;
048: import org.netbeans.modules.java.api.common.SourceRoots;
049: import org.netbeans.spi.queries.FileBuiltQueryImplementation;
050: import org.netbeans.spi.project.support.ant.AntProjectHelper;
051: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
052: import org.openide.util.Mutex.Action;
053:
054: /**
055: * Default implementation of {@link FileBuiltQueryImplementation}.
056: * @author Jesse Glick, Tomas Zezula
057: */
058: final class FileBuiltQueryImpl implements FileBuiltQueryImplementation,
059: PropertyChangeListener {
060:
061: private FileBuiltQueryImplementation delegate;
062: private final AntProjectHelper helper;
063: private final PropertyEvaluator evaluator;
064: private final SourceRoots sourceRoots;
065: private final SourceRoots testRoots;
066:
067: FileBuiltQueryImpl(AntProjectHelper helper,
068: PropertyEvaluator evaluator, SourceRoots sourceRoots,
069: SourceRoots testRoots) {
070: assert helper != null;
071: assert evaluator != null;
072: assert sourceRoots != null;
073: assert testRoots != null;
074:
075: this .helper = helper;
076: this .evaluator = evaluator;
077: this .sourceRoots = sourceRoots;
078: this .testRoots = testRoots;
079: this .sourceRoots.addPropertyChangeListener(this );
080: this .testRoots.addPropertyChangeListener(this );
081: }
082:
083: public FileBuiltQuery.Status getStatus(final FileObject file) {
084: return ProjectManager.mutex().readAccess(
085: new Action<FileBuiltQuery.Status>() {
086: public FileBuiltQuery.Status run() {
087: return getStatusImpl(file);
088: }
089: });
090: }
091:
092: private synchronized FileBuiltQuery.Status getStatusImpl(
093: FileObject file) {
094: if (delegate == null) {
095: delegate = createDelegate();
096: }
097: return delegate.getStatus(file);
098: }
099:
100: private FileBuiltQueryImplementation createDelegate() {
101: String[] srcRoots = sourceRoots.getRootProperties();
102: String[] tstRoots = testRoots.getRootProperties();
103: String[] from = new String[srcRoots.length + tstRoots.length];
104: String[] to = new String[srcRoots.length + tstRoots.length];
105: for (int i = 0; i < srcRoots.length; i++) {
106: from[i] = "${" + srcRoots[i] + "}/*.java"; // NOI18N
107: to[i] = "${build.classes.dir}/*.class"; // NOI18N
108: }
109: for (int i = 0; i < tstRoots.length; i++) {
110: from[srcRoots.length + i] = "${" + tstRoots[i] + "}/*.java"; // NOI18N
111: to[srcRoots.length + i] = "${build.test.classes.dir}/*.class"; // NOI18N
112: }
113: return helper.createGlobFileBuiltQuery(evaluator, from, to); // save to pass APH
114: }
115:
116: public void propertyChange(PropertyChangeEvent evt) {
117: if (SourceRoots.PROP_ROOT_PROPERTIES.equals(evt
118: .getPropertyName())) {
119: synchronized (this ) {
120: delegate = null;
121: // XXX: what to do with already returned Statuses
122: }
123: }
124: }
125: }
|