KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > basic > search > ComponentTypeSearchProvider


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.ui.basic.search;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import java.util.regex.PatternSyntaxException JavaDoc;
27 import org.netbeans.modules.xml.schema.model.SchemaComponent;
28 import org.netbeans.modules.xml.schema.model.SchemaModel;
29 import org.netbeans.modules.xml.schema.model.visitor.DeepSchemaVisitor;
30 import org.netbeans.modules.xml.xam.ui.category.Category;
31 import org.netbeans.modules.xml.xam.ui.search.Query;
32 import org.netbeans.modules.xml.xam.ui.search.SearchException;
33 import org.netbeans.modules.xml.xam.ui.search.SearchProvider;
34 import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher;
35 import org.openide.util.NbBundle;
36
37 /**
38  * Implements a SearchProvider that compares the name of the document tag
39  * with the query string, using a case-insensitive string comparison.
40  *
41  * @author Nathan Fiedler
42  */

43 public class ComponentTypeSearchProvider extends DeepSchemaVisitor
44         implements SearchProvider {
45     /** The last query submitted by the user, if any, lower-cased. */
46     private String JavaDoc phrase;
47     /** True if the phrase contains wildcards (e.g. * or ?). */
48     private boolean wildcarded;
49     /** Model in which to perform the search. */
50     private SchemaModel model;
51     /** List of matching schema components. */
52     private List JavaDoc<Object JavaDoc> results;
53     /** Provides the selected component, if needed. */
54     private Category category;
55     /** The compiled regular expression pattern, if provided. */
56     private Pattern JavaDoc pattern;
57
58     /**
59      * Creates a new instance of ComponentTypeSearchProvider.
60      *
61      * @param model schema model in which to perform search.
62      * @param category provides the selected component.
63      */

64     public ComponentTypeSearchProvider(SchemaModel model, Category category) {
65         this.model = model;
66         this.category = category;
67     }
68
69     public String JavaDoc getDisplayName() {
70         return NbBundle.getMessage(ComponentTypeSearchProvider.class,
71                 "LBL_SearchProvider_ComponentType");
72     }
73
74     public String JavaDoc getInputDescription() {
75         return NbBundle.getMessage(ComponentTypeSearchProvider.class,
76                 "HELP_SearchProvider_ComponentType");
77     }
78
79     public String JavaDoc getShortDescription() {
80         return NbBundle.getMessage(ComponentTypeSearchProvider.class,
81                 "HINT_SearchProvider_ComponentType");
82     }
83
84     public List JavaDoc<Object JavaDoc> search(Query query) throws SearchException {
85         if (query.isRegularExpression()) {
86             try {
87                 pattern = Pattern.compile(query.getQuery());
88                 phrase = null;
89             } catch (PatternSyntaxException JavaDoc pse) {
90                 throw new SearchException(pse.getMessage(), pse);
91             }
92         } else {
93             pattern = null;
94             phrase = query.getQuery().toLowerCase();
95             wildcarded = WildcardStringMatcher.containsWildcards(phrase);
96         }
97         results = new ArrayList JavaDoc<Object JavaDoc>();
98         // Search for named components with the given tag name.
99
if (query.useSelected()) {
100             SchemaComponent component = Providers.getSelectedComponent(category);
101             if (component != null) {
102                 component.accept(this);
103             } else {
104                 // Maybe it is a category node that is selected.
105
Class JavaDoc<? extends SchemaComponent> childType =
106                         Providers.getSelectedChildType(category);
107                 if (childType != null) {
108                     List JavaDoc<? extends SchemaComponent> components =
109                             model.getSchema().getChildren(childType);
110                     for (SchemaComponent comp : components) {
111                         comp.accept(this);
112                     }
113                 }
114             }
115         } else {
116             model.getSchema().accept(this);
117         }
118         return results;
119     }
120
121     protected void visitChildren(SchemaComponent comp) {
122         String JavaDoc name = comp.getPeer().getTagName();
123         if (phrase != null) {
124             name = name.toLowerCase();
125             if (wildcarded) {
126                 if (WildcardStringMatcher.match(name, phrase)) {
127                     results.add(comp);
128                 }
129             } else if (name.indexOf(phrase) > -1) {
130                 results.add(comp);
131             }
132         } else if (pattern != null) {
133             Matcher JavaDoc matcher = pattern.matcher(name);
134             if (matcher.find()) {
135                 results.add(comp);
136             }
137         }
138         // Visit the children last, to get results in breadth-first order.
139
super.visitChildren(comp);
140     }
141 }
142
Popular Tags