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.modules.xml.wsdl.ui.search;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048: import java.util.regex.PatternSyntaxException;
049: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
050: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
051: import org.netbeans.modules.xml.wsdl.model.visitor.ChildVisitor;
052: import org.netbeans.modules.xml.xam.Named;
053: import org.netbeans.modules.xml.xam.ui.category.Category;
054: import org.netbeans.modules.xml.xam.ui.search.Query;
055: import org.netbeans.modules.xml.xam.ui.search.SearchException;
056: import org.netbeans.modules.xml.xam.ui.search.SearchProvider;
057: import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * Implements a SearchProvider that compares the value of the name attribute
062: * with the query string, using a case-insensitive string comparison.
063: *
064: * @author Nathan Fiedler
065: */
066: public class ComponentNameSearchProvider extends ChildVisitor implements
067: SearchProvider {
068: /** The last query submitted by the user, if any, lower-cased. */
069: private String phrase;
070: /** True if the phrase contains wildcards (e.g. * or ?). */
071: private boolean wildcarded;
072: /** Model in which to perform the search. */
073: private WSDLModel model;
074: /** List of matching components. */
075: private List<Object> results;
076: /** Provides the selected component, if needed. */
077: private Category category;
078: /** The compiled regular expression pattern, if provided. */
079: private Pattern pattern;
080:
081: /**
082: * Creates a new instance of ComponentNameSearchProvider.
083: *
084: * @param model model in which to perform search.
085: * @param category provides the selected component.
086: */
087: public ComponentNameSearchProvider(WSDLModel model,
088: Category category) {
089: this .model = model;
090: this .category = category;
091: }
092:
093: public String getDisplayName() {
094: return NbBundle.getMessage(ComponentNameSearchProvider.class,
095: "LBL_SearchProvider_ComponentName");
096: }
097:
098: public String getInputDescription() {
099: return NbBundle.getMessage(ComponentNameSearchProvider.class,
100: "HELP_SearchProvider_ComponentName");
101: }
102:
103: public String getShortDescription() {
104: return NbBundle.getMessage(ComponentNameSearchProvider.class,
105: "HINT_SearchProvider_ComponentName");
106: }
107:
108: public List<Object> search(Query query) throws SearchException {
109: if (query.isRegularExpression()) {
110: try {
111: pattern = Pattern.compile(query.getQuery());
112: phrase = null;
113: } catch (PatternSyntaxException pse) {
114: throw new SearchException(pse.getMessage(), pse);
115: }
116: } else {
117: pattern = null;
118: phrase = query.getQuery().toLowerCase();
119: wildcarded = WildcardStringMatcher
120: .containsWildcards(phrase);
121: }
122: results = new ArrayList<Object>();
123: // Search for named components with the given name.
124: if (query.useSelected()) {
125: WSDLComponent component = Providers
126: .getSelectedComponent(category);
127: if (component != null) {
128: component.accept(this );
129: } else {
130: // Maybe it is a category node that is selected.
131: Class<? extends WSDLComponent> childType = Providers
132: .getSelectedChildType(category);
133: if (childType != null) {
134: List<? extends WSDLComponent> components = model
135: .getDefinitions().getChildren(childType);
136: for (WSDLComponent comp : components) {
137: comp.accept(this );
138: }
139: }
140: }
141: } else {
142: model.getDefinitions().accept(this );
143: }
144: return results;
145: }
146:
147: protected void visitComponent(WSDLComponent comp) {
148: if (comp instanceof Named) {
149: Named ncontainer = (Named) comp;
150: String name = ncontainer.getName();
151: if (name != null) {
152: if (phrase != null) {
153: name = name.toLowerCase();
154: if (wildcarded) {
155: if (WildcardStringMatcher.match(name, phrase)) {
156: results.add(comp);
157: }
158: } else if (name.indexOf(phrase) > -1) {
159: results.add(comp);
160: }
161: } else if (pattern != null) {
162: Matcher matcher = pattern.matcher(name);
163: if (matcher.find()) {
164: results.add(comp);
165: }
166: }
167: }
168: }
169: // Visit the children last, to get results in breadth-first order.
170: super.visitComponent(comp);
171: }
172: }
|