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.source.classpath;
042:
043: import java.net.URL;
044: import javax.swing.event.ChangeListener;
045: import org.netbeans.api.java.classpath.GlobalPathRegistry;
046: import org.netbeans.api.java.queries.SourceForBinaryQuery;
047: import org.netbeans.modules.java.source.usages.Index;
048: import org.netbeans.modules.java.source.usages.RepositoryUpdater;
049: import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
050: import org.openide.filesystems.FileObject;
051: import org.openide.filesystems.URLMapper;
052: import org.openide.util.Lookup;
053:
054: /**
055: *
056: * @author Tomas Zezula
057: */
058: public class CacheSourceForBinaryQueryImpl implements
059: SourceForBinaryQueryImplementation {
060:
061: private String FILE_PROTOCOL = "file"; //NOI18N
062:
063: /** Creates a new instance of CacheSourceForBinaryQueryImpl */
064: public CacheSourceForBinaryQueryImpl() {
065: }
066:
067: public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) {
068: if (!FILE_PROTOCOL.equals(binaryRoot.getProtocol())) {
069: return null;
070: }
071: URL sourceURL = Index.getSourceRootForClassFolder(binaryRoot);
072: SourceForBinaryQuery.Result result = null;
073: if (sourceURL != null) {
074: for (SourceForBinaryQueryImplementation impl : Lookup
075: .getDefault().lookupAll(
076: SourceForBinaryQueryImplementation.class)) {
077: if (impl != this ) {
078: result = impl.findSourceRoots(sourceURL);
079: if (result != null) {
080: break;
081: }
082: }
083: }
084: result = new R(sourceURL, result);
085: }
086: return result;
087: }
088:
089: private static class R implements SourceForBinaryQuery.Result {
090:
091: private final FileObject sourceRoot;
092: private final SourceForBinaryQuery.Result delegate;
093:
094: public R(final URL sourceRootURL,
095: final SourceForBinaryQuery.Result delegate) {
096: assert sourceRootURL != null;
097: this .sourceRoot = URLMapper.findFileObject(sourceRootURL);
098: this .delegate = delegate;
099: }
100:
101: public void removeChangeListener(ChangeListener l) {
102: //Imutable, not needed
103: }
104:
105: public void addChangeListener(ChangeListener l) {
106: //Imutable, not needed
107: }
108:
109: public FileObject[] getRoots() {
110: FileObject[] result;
111: //Is here SFBQ.Result for root?
112: if (delegate != null) {
113: //Yes - either [root*] or [] - nothing or unknown
114: result = this .delegate.getRoots();
115: if (result.length == 0) {
116: //nothing or unkown
117: if (this .sourceRoot != null
118: && GlobalPathRegistry.getDefault()
119: .getSourceRoots().contains(
120: this .sourceRoot)) {
121: //nothing
122: result = new FileObject[] { this .sourceRoot };
123: } else {
124: //unknown
125: result = new FileObject[0];
126: }
127: }
128: } else {
129: //No - unknown file - treat it like a source root
130: if (this .sourceRoot == null) {
131: result = new FileObject[0];
132: } else {
133: result = new FileObject[] { this.sourceRoot };
134: }
135: }
136: return result;
137: }
138: }
139: }
|