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-2007 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:
042: package org.netbeans.api.java.source.ui;
043:
044: import java.util.Set;
045: import javax.lang.model.element.TypeElement;
046: import org.netbeans.api.java.source.ClassIndex;
047: import org.netbeans.api.java.source.ClasspathInfo;
048: import org.netbeans.api.java.source.ElementHandle;
049: import org.netbeans.api.jumpto.type.TypeBrowser;
050: import org.netbeans.modules.java.source.ui.JavaTypeDescription;
051: import org.netbeans.modules.java.source.ui.JavaTypeProvider;
052: import org.netbeans.spi.jumpto.type.TypeDescriptor;
053: import org.openide.util.NbBundle;
054:
055: /**
056: * Support for browsing of the Java types. Opens search dialog for the type name
057: * with possibility to filter possible results.
058: *
059: * @author Martin Adamek
060: *
061: * @since 1.3
062: */
063: public final class TypeElementFinder {
064:
065: /**
066: * Searches for classes on given classpath using defined restrictions.
067: *
068: * @param cpInfo classpath used for search; optional, can be null,
069: * everything available will be searched
070: * @param customizer possibility to add restrictions to search result;
071: * optional, can be null, no restriction will be applied
072: * @return found type or null if dialog was canceled
073: */
074: public static ElementHandle<TypeElement> find(ClasspathInfo cpInfo,
075: final Customizer customizer) {
076:
077: // create filter only if client wants to customize the result
078: TypeBrowser.Filter typeBrowserFilter = null;
079: if (customizer != null) {
080: typeBrowserFilter = new TypeBrowser.Filter() {
081: public boolean accept(TypeDescriptor typeDescriptor) {
082: JavaTypeDescription javaTypeDesc = toJavaTypeDescription(typeDescriptor);
083: if (customizer != null && javaTypeDesc != null) {
084: return customizer.accept(javaTypeDesc
085: .getHandle());
086: }
087: return true;
088: }
089: };
090: }
091:
092: TypeDescriptor typeDescriptor = TypeBrowser.browse(NbBundle
093: .getMessage(TypeElementFinder.class, "DLG_FindType"),
094: typeBrowserFilter, new JavaTypeProvider(cpInfo,
095: customizer == null ? null : customizer));
096: JavaTypeDescription javaTypeDesc = toJavaTypeDescription(typeDescriptor);
097:
098: return javaTypeDesc == null ? null : javaTypeDesc.getHandle();
099: }
100:
101: /**
102: * Customization of search scope and results
103: */
104: public static interface Customizer {
105:
106: /**
107: * Set the scope of the search on particular classpath. Too expensive queries
108: * will affect performance of dialog, consider also using in combination
109: * with {@link #query}
110: */
111: Set<ElementHandle<TypeElement>> query(
112: ClasspathInfo classpathInfo, String textForQuery,
113: ClassIndex.NameKind nameKind,
114: Set<ClassIndex.SearchScope> searchScopes);
115:
116: /**
117: * Filter results directly in dialog.
118: * Useful when filtering operation itself is too expensive to perform globally
119: * in <code>query</code> method and when ratio of filtered elements is reasonably high.
120: * (e.g. visibility query)
121: */
122: boolean accept(ElementHandle<TypeElement> typeHandle);
123:
124: }
125:
126: // private
127:
128: private static JavaTypeDescription toJavaTypeDescription(
129: TypeDescriptor typeDescriptor) {
130: if (typeDescriptor instanceof JavaTypeDescription) {
131: return (JavaTypeDescription) typeDescriptor;
132: }
133: return null;
134: }
135:
136: }
|