~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to java/sourceui/src/org/netbeans/api/java/source/ui/TypeElementFinder.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.api.java.source.ui;
 
43
 
 
44
import java.util.Set;
 
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;
 
54
 
 
55
/**
 
56
 * Support for browsing of the Java types. Opens search dialog for the type name 
 
57
 * with possibility to filter possible results.
 
58
 *
 
59
 * @author Martin Adamek
 
60
 * 
 
61
 * @since 1.3
 
62
 */
 
63
public final class TypeElementFinder {
 
64
 
 
65
    /**
 
66
     * Searches for classes on given classpath using defined restrictions.
 
67
     * 
 
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
 
73
     */
 
74
    public static ElementHandle<TypeElement> find(ClasspathInfo cpInfo, final Customizer customizer) {
 
75
 
 
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());
 
84
                    }
 
85
                    return true;
 
86
                }
 
87
            };
 
88
        }
 
89
        
 
90
        TypeDescriptor typeDescriptor = TypeBrowser.browse(
 
91
                NbBundle.getMessage(TypeElementFinder.class, "DLG_FindType"), 
 
92
                typeBrowserFilter,
 
93
                new JavaTypeProvider(cpInfo, customizer == null ? null : customizer)
 
94
                );
 
95
        JavaTypeDescription javaTypeDesc = toJavaTypeDescription(typeDescriptor);
 
96
        
 
97
        return javaTypeDesc == null ? null : javaTypeDesc.getHandle();
 
98
    }
 
99
 
 
100
    /**
 
101
     * Customization of search scope and results
 
102
     */
 
103
    public static interface Customizer {
 
104
        
 
105
        /**
 
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}
 
109
         */
 
110
        Set<ElementHandle<TypeElement>> query(ClasspathInfo classpathInfo, String textForQuery, ClassIndex.NameKind nameKind, Set<ClassIndex.SearchScope> searchScopes);
 
111
        
 
112
        /**
 
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)
 
117
         */
 
118
        boolean accept(ElementHandle<TypeElement> typeHandle);
 
119
        
 
120
    }
 
121
    
 
122
    // private
 
123
    
 
124
    private static JavaTypeDescription toJavaTypeDescription(TypeDescriptor typeDescriptor) {
 
125
        if (typeDescriptor instanceof JavaTypeDescription) {
 
126
            return (JavaTypeDescription) typeDescriptor;
 
127
        }
 
128
        return null;
 
129
    }
 
130
    
 
131
}