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:
042: package org.netbeans.modules.apisupport.project.ui.customizer;
043:
044: import java.text.Collator;
045: import java.util.ArrayList;
046: import java.util.LinkedHashSet;
047: import java.util.List;
048: import java.util.Locale;
049: import java.util.Set;
050: import java.util.TreeSet;
051:
052: /**
053: * Implements filtering for Add Module Dependency panel.
054: * @author Jesse Glick
055: */
056: final class AddModuleFilter {
057:
058: private final Set<ModuleDependency> universe;
059: private final String dependingModuleCNB;
060:
061: /**
062: * Construct a filter given a list of possible dependencies.
063: */
064: public AddModuleFilter(Set<ModuleDependency> universe,
065: String dependingModuleCNB) {
066: this .universe = universe;
067: this .dependingModuleCNB = dependingModuleCNB;
068: // Prime the cache:
069: for (ModuleDependency dep : universe) {
070: dep.getFilterTokens(dependingModuleCNB);
071: }
072: // To test "Please wait" use:
073: //try{Thread.sleep(2000);}catch(InterruptedException e){}
074: }
075:
076: /**
077: * Find matches for a search string.
078: */
079: public Set<ModuleDependency> getMatches(String text) {
080: String textLC = text.toLowerCase(Locale.ENGLISH);
081: List<Set<ModuleDependency>> matches = new ArrayList<Set<ModuleDependency>>(
082: 3);
083: for (int i = 0; i < 3; i++) {
084: // Within groups, just sort by module display name:
085: matches.add(new TreeSet<ModuleDependency>(
086: ModuleDependency.LOCALIZED_NAME_COMPARATOR));
087: }
088: for (ModuleDependency dep : universe) {
089: int matchLevel = 3;
090: for (String tok : dep.getFilterTokens(dependingModuleCNB)) {
091: String token = tok.toLowerCase(Locale.ENGLISH);
092: // Presort by relevance (#71995):
093: if (token.equals(textLC)
094: || token.endsWith("." + textLC)) { // NOI18N
095: // Exact match (possibly after dot).
096: matchLevel = Math.min(0, matchLevel);
097: } else if (token.indexOf("." + textLC) != -1) { // NOI18N
098: // Starts with match (after dot).
099: matchLevel = Math.min(1, matchLevel);
100: } else if (token.indexOf(textLC) != -1) {
101: // Substring match.
102: matchLevel = Math.min(2, matchLevel);
103: }
104: }
105: if (matchLevel < 3) {
106: matches.get(matchLevel).add(dep);
107: }
108: }
109: Set<ModuleDependency> result = new LinkedHashSet<ModuleDependency>();
110: for (Set<ModuleDependency> deps : matches) {
111: result.addAll(deps);
112: }
113: return result;
114: }
115:
116: /**
117: * Find which tokens actually matched a given dependency.
118: */
119: public Set<String> getMatchesFor(String text, ModuleDependency dep) {
120: String textLC = text.toLowerCase(Locale.US);
121: Set<String> tokens = new TreeSet<String>(Collator.getInstance());
122: for (String token : dep.getFilterTokens(dependingModuleCNB)) {
123: if (token.toLowerCase(Locale.US).indexOf(textLC) != -1) {
124: tokens.add(token);
125: }
126: }
127: return tokens;
128: }
129:
130: }
|