KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > AddModuleFilter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui.customizer;
21
22 import java.text.Collator JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedHashSet JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28
29 /**
30  * Implements filtering for Add Module Dependency panel.
31  * @author Jesse Glick
32  */

33 final class AddModuleFilter {
34
35     private final Set JavaDoc<ModuleDependency> universe;
36     private final String JavaDoc dependingModuleCNB;
37
38     /**
39      * Construct a filter given a list of possible dependencies.
40      */

41     public AddModuleFilter(Set JavaDoc<ModuleDependency> universe, String JavaDoc dependingModuleCNB) {
42         this.universe = universe;
43         this.dependingModuleCNB = dependingModuleCNB;
44         // Prime the cache:
45
Iterator JavaDoc it = universe.iterator();
46         while (it.hasNext()) {
47             ModuleDependency dep = (ModuleDependency) it.next();
48             dep.getFilterTokens(dependingModuleCNB);
49         }
50         // To test "Please wait" use:
51
//try{Thread.sleep(2000);}catch(InterruptedException e){}
52
}
53     
54     /**
55      * Find matches for a search string.
56      */

57     public Set JavaDoc<ModuleDependency> getMatches(String JavaDoc text) {
58         String JavaDoc textLC = text.toLowerCase(Locale.ENGLISH);
59         Set JavaDoc<ModuleDependency>[] matches = new Set JavaDoc[3];
60         for (int i = 0; i < matches.length; i++) {
61             // Within groups, just sort by module display name:
62
matches[i] = new TreeSet JavaDoc(ModuleDependency.LOCALIZED_NAME_COMPARATOR);
63         }
64         Iterator JavaDoc it = universe.iterator();
65         while (it.hasNext()) {
66             ModuleDependency dep = (ModuleDependency) it.next();
67             int matchLevel = matches.length;
68             Set JavaDoc<String JavaDoc> tokens = dep.getFilterTokens(dependingModuleCNB);
69             Iterator JavaDoc it2 = tokens.iterator();
70             while (it2.hasNext()) {
71                 String JavaDoc token = ((String JavaDoc) it2.next()).toLowerCase(Locale.ENGLISH);
72                 // Presort by relevance (#71995):
73
if (token.equals(textLC) || token.endsWith("." + textLC)) { // NOI18N
74
// Exact match (possibly after dot).
75
matchLevel = Math.min(0, matchLevel);
76                 } else if (token.indexOf("." + textLC) != -1) { // NOI18N
77
// Starts with match (after dot).
78
matchLevel = Math.min(1, matchLevel);
79                 } else if (token.indexOf(textLC) != -1) {
80                     // Substring match.
81
matchLevel = Math.min(2, matchLevel);
82                 }
83             }
84             if (matchLevel < matches.length) {
85                 matches[matchLevel].add(dep);
86             }
87         }
88         Set JavaDoc result = new LinkedHashSet JavaDoc();
89         for (int i = 0; i < matches.length; i++) {
90             result.addAll(matches[i]);
91         }
92         return result;
93     }
94     
95     /**
96      * Find which tokens actually matched a given dependency.
97      */

98     public Set JavaDoc<String JavaDoc> getMatchesFor(String JavaDoc text, ModuleDependency dep) {
99         String JavaDoc textLC = text.toLowerCase(Locale.US);
100         Set JavaDoc<String JavaDoc> tokens = new TreeSet JavaDoc(Collator.getInstance());
101         Iterator JavaDoc it = dep.getFilterTokens(dependingModuleCNB).iterator();
102         while (it.hasNext()) {
103             String JavaDoc token = (String JavaDoc) it.next();
104             if (token.toLowerCase(Locale.US).indexOf(textLC) != -1) {
105                 tokens.add(token);
106             }
107         }
108         return tokens;
109     }
110     
111 }
112
Popular Tags