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.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 44 public class ComponentNameSearchProvider extends DeepSchemaVisitor 45 implements SearchProvider { 46 47 private String phrase; 48 49 private boolean wildcarded; 50 51 private SchemaModel model; 52 53 private List <Object > results; 54 55 private Category category; 56 57 private Pattern pattern; 58 59 65 public ComponentNameSearchProvider(SchemaModel model, Category category) { 66 this.model = model; 67 this.category = category; 68 } 69 70 public String getDisplayName() { 71 return NbBundle.getMessage(ComponentNameSearchProvider.class, 72 "LBL_SearchProvider_ComponentName"); 73 } 74 75 public String getInputDescription() { 76 return NbBundle.getMessage(ComponentNameSearchProvider.class, 77 "HELP_SearchProvider_ComponentName"); 78 } 79 80 public String getShortDescription() { 81 return NbBundle.getMessage(ComponentNameSearchProvider.class, 82 "HINT_SearchProvider_ComponentName"); 83 } 84 85 public List <Object > search(Query query) throws SearchException { 86 if (query.isRegularExpression()) { 87 try { 88 pattern = Pattern.compile(query.getQuery()); 89 phrase = null; 90 } catch (PatternSyntaxException 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 <Object >(); 99 if (query.useSelected()) { 101 SchemaComponent component = Providers.getSelectedComponent(category); 102 if (component != null) { 103 component.accept(this); 104 } else { 105 Class <? extends SchemaComponent> childType = 107 Providers.getSelectedChildType(category); 108 if (childType != null) { 109 List <? 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 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 matcher = pattern.matcher(name); 138 if (matcher.find()) { 139 results.add(comp); 140 } 141 } 142 } 143 } 144 super.visitChildren(comp); 146 } 147 } 148 | Popular Tags |