2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
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.
42
package org.netbeans.api.java.source.ui;
45
import javax.lang.model.element.TypeElement;
46
import org.netbeans.api.java.source.ClassIndex;
47
import org.netbeans.api.java.source.ClasspathInfo;
48
import org.netbeans.api.java.source.ElementHandle;
49
import org.netbeans.api.jumpto.type.TypeBrowser;
50
import org.netbeans.modules.java.source.ui.JavaTypeDescription;
51
import org.netbeans.modules.java.source.ui.JavaTypeProvider;
52
import org.netbeans.spi.jumpto.type.TypeDescriptor;
53
import org.openide.util.NbBundle;
56
* Support for browsing of the Java types. Opens search dialog for the type name
57
* with possibility to filter possible results.
59
* @author Martin Adamek
63
public final class TypeElementFinder {
66
* Searches for classes on given classpath using defined restrictions.
68
* @param cpInfo classpath used for search; optional, can be null,
69
* everything available will be searched
70
* @param customizer possibility to add restrictions to search result;
71
* optional, can be null, no restriction will be applied
72
* @return found type or null if dialog was canceled
74
public static ElementHandle<TypeElement> find(ClasspathInfo cpInfo, final Customizer customizer) {
76
// create filter only if client wants to customize the result
77
TypeBrowser.Filter typeBrowserFilter = null;
78
if (customizer != null) {
79
typeBrowserFilter = new TypeBrowser.Filter() {
80
public boolean accept(TypeDescriptor typeDescriptor) {
81
JavaTypeDescription javaTypeDesc = toJavaTypeDescription(typeDescriptor);
82
if (customizer != null && javaTypeDesc != null) {
83
return customizer.accept(javaTypeDesc.getHandle());
90
TypeDescriptor typeDescriptor = TypeBrowser.browse(
91
NbBundle.getMessage(TypeElementFinder.class, "DLG_FindType"),
93
new JavaTypeProvider(cpInfo, customizer == null ? null : customizer)
95
JavaTypeDescription javaTypeDesc = toJavaTypeDescription(typeDescriptor);
97
return javaTypeDesc == null ? null : javaTypeDesc.getHandle();
101
* Customization of search scope and results
103
public static interface Customizer {
106
* Set the scope of the search on particular classpath. Too expensive queries
107
* will affect performance of dialog, consider also using in combination
108
* with {@link #query}
110
Set<ElementHandle<TypeElement>> query(ClasspathInfo classpathInfo, String textForQuery, ClassIndex.NameKind nameKind, Set<ClassIndex.SearchScope> searchScopes);
113
* Filter results directly in dialog.
114
* Useful when filtering operation itself is too expensive to perform globally
115
* in <code>query</code> method and when ratio of filtered elements is reasonably high.
116
* (e.g. visibility query)
118
boolean accept(ElementHandle<TypeElement> typeHandle);
124
private static JavaTypeDescription toJavaTypeDescription(TypeDescriptor typeDescriptor) {
125
if (typeDescriptor instanceof JavaTypeDescription) {
126
return (JavaTypeDescription) typeDescriptor;