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