1 19 20 package org.netbeans.modules.xml.schema.ui.basic.search; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import java.util.regex.PatternSyntaxException ; 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 43 public class ComponentTypeSearchProvider extends DeepSchemaVisitor 44 implements SearchProvider { 45 46 private String phrase; 47 48 private boolean wildcarded; 49 50 private SchemaModel model; 51 52 private List <Object > results; 53 54 private Category category; 55 56 private Pattern pattern; 57 58 64 public ComponentTypeSearchProvider(SchemaModel model, Category category) { 65 this.model = model; 66 this.category = category; 67 } 68 69 public String getDisplayName() { 70 return NbBundle.getMessage(ComponentTypeSearchProvider.class, 71 "LBL_SearchProvider_ComponentType"); 72 } 73 74 public String getInputDescription() { 75 return NbBundle.getMessage(ComponentTypeSearchProvider.class, 76 "HELP_SearchProvider_ComponentType"); 77 } 78 79 public String getShortDescription() { 80 return NbBundle.getMessage(ComponentTypeSearchProvider.class, 81 "HINT_SearchProvider_ComponentType"); 82 } 83 84 public List <Object > search(Query query) throws SearchException { 85 if (query.isRegularExpression()) { 86 try { 87 pattern = Pattern.compile(query.getQuery()); 88 phrase = null; 89 } catch (PatternSyntaxException 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 <Object >(); 98 if (query.useSelected()) { 100 SchemaComponent component = Providers.getSelectedComponent(category); 101 if (component != null) { 102 component.accept(this); 103 } else { 104 Class <? extends SchemaComponent> childType = 106 Providers.getSelectedChildType(category); 107 if (childType != null) { 108 List <? 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 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 matcher = pattern.matcher(name); 134 if (matcher.find()) { 135 results.add(comp); 136 } 137 } 138 super.visitChildren(comp); 140 } 141 } 142 | Popular Tags |