KickJava   Java API By Example, From Geeks To Geeks.

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


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.Named;
31 import org.netbeans.modules.xml.xam.ui.category.Category;
32 import org.netbeans.modules.xml.xam.ui.search.Query;
33 import org.netbeans.modules.xml.xam.ui.search.SearchException;
34 import org.netbeans.modules.xml.xam.ui.search.SearchProvider;
35 import org.netbeans.modules.xml.xam.ui.search.WildcardStringMatcher;
36 import org.openide.util.NbBundle;
37
38 /**
39  * Implements a SearchProvider that compares the value of the name attribute
40  * with the query string, using a case-insensitive string comparison.
41  *
42  * @author Nathan Fiedler
43  */

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

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