1 19 20 21 package org.openidex.search; 22 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeSupport ; 27 import java.util.*; 28 29 import org.openide.filesystems.FileObject; 30 import org.openide.loaders.DataObject; 31 import org.openide.nodes.Node; 32 import org.openide.util.WeakListeners; 33 34 35 43 public abstract class SearchGroup extends Object { 44 45 49 public static final String PROP_FOUND = "org.openidex.search.found"; 51 66 public static final String PROP_RESULT = "org.openidex.search.result"; 68 69 70 private PropertyChangeSupport propChangeSupport; 71 72 73 protected SearchType[] searchTypes = new SearchType[0]; 74 75 76 protected final Set searchRoots = new HashSet(5); 77 78 79 protected final Set resultObjects = new LinkedHashSet(50); 80 81 82 protected volatile boolean stopped = false; 83 84 private PropertyChangeListener propListener; 85 86 87 94 protected void add(SearchType searchType) { 95 96 97 for (int i = 0; i < searchTypes.length; i++) { 98 if (searchType.equals(searchTypes[i])) { 99 return; 100 } 101 } 102 103 104 SearchType[] temp = new SearchType[searchTypes.length + 1]; 105 System.arraycopy(searchTypes, 0, temp, 0, searchTypes.length); 106 temp[searchTypes.length] = searchType; 107 searchTypes = temp; 108 } 109 110 116 public SearchType[] getSearchTypes() { 117 return searchTypes; 118 } 119 120 125 public void setSearchRootNodes(Node[] roots) { 126 127 132 for (int i = 0; i < searchTypes.length; i++) { 133 roots = searchTypes[i].acceptSearchRootNodes(roots); 134 } 135 searchRoots.clear(); 136 searchRoots.addAll(Arrays.asList(roots)); 137 } 138 139 140 public Node[] getSearchRoots() { 141 return (Node[]) searchRoots.toArray(new Node[searchRoots.size()]); 142 } 143 144 145 public final void stopSearch() { 146 stopped = true; 147 } 148 149 155 public void search() { 156 resultObjects.clear(); 157 prepareSearch(); 158 doSearch(); 159 } 160 161 164 protected void prepareSearch() { 165 } 166 167 176 protected abstract void doSearch(); 177 178 189 protected void processSearchObject(Object searchObject) { 190 191 196 for (int i = 0; i < searchTypes.length; i++) { 197 if (!searchTypes[i].acceptSearchObject(searchObject)) { 198 return; 199 } 200 } 201 202 205 for (int i = 0; i < searchTypes.length; i++) { 206 searchTypes[i].prepareSearchObject(searchObject); 207 } 208 209 210 for (int i = 0; i < searchTypes.length; i++) { 211 if (!searchTypes[i].testObject(searchObject)) { 212 return; 213 } 214 } 215 216 220 resultObjects.add(searchObject); 221 firePropertyChange(PROP_FOUND, null, searchObject); 222 } 223 224 225 226 public abstract Node getNodeForFoundObject(Object object); 227 228 229 public Set getResultObjects() { 230 return new LinkedHashSet(resultObjects); 231 } 232 233 234 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 235 getPropertySupport().addPropertyChangeListener(l); 236 } 237 238 239 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 240 getPropertySupport().removePropertyChangeListener(l); 241 } 242 243 244 protected void firePropertyChange(String name, Object oldValue, Object newValue) { 245 getPropertySupport().firePropertyChange(name, oldValue, newValue); 246 } 247 248 249 private synchronized PropertyChangeSupport getPropertySupport() { 250 if(propChangeSupport == null) 251 propChangeSupport = new PropertyChangeSupport (this); 252 253 return propChangeSupport; 254 } 255 256 269 public static SearchGroup[] createSearchGroups(SearchType[] items) { 270 271 276 Set classSet = new HashSet(items.length); 277 for (int i = 0; i < items.length; i++) { 278 List classes = Arrays.asList(items[i].getSearchTypeClasses()); 279 if (i == 0) { 280 classSet.addAll(classes); 281 } else { 282 classSet.retainAll(classes); 283 } 284 } 285 286 287 if (classSet.isEmpty()) { 288 return new SearchGroup[0]; 289 } 290 Set groupSet = new HashSet(classSet.size()); 291 for (Iterator it = classSet.iterator(); it.hasNext(); ) { 292 SearchGroup group = Registry.createSearchGroup((Class ) it.next()); 293 if (group != null) { 294 for (int i = 0; i < items.length; i++) { 295 group.add(items[i]); 296 } 297 groupSet.add(group); 298 } 299 } 300 return (SearchGroup[]) groupSet.toArray(new SearchGroup[groupSet.size()]); 301 } 302 303 304 309 public interface Factory { 310 311 public SearchGroup createSearchGroup(); 312 } 314 315 328 public static final class Registry extends Object { 329 330 331 private Registry() {} 332 333 334 335 private static final Map registry = new HashMap(2); 336 337 static { 338 registry.put(DataObject.class, new Factory() { 339 public SearchGroup createSearchGroup() { 340 return new DataObjectSearchGroup(); 341 } 342 }); 343 registry.put(FileObject.class, new Factory() { 344 public SearchGroup createSearchGroup() { 345 return new FileObjectSearchGroup(); 346 } 347 }); 348 } 349 350 351 366 public static synchronized boolean registerSearchGroupFactory( 367 Class searchObjectClass, 368 Factory factory) { 369 Object oldFactory = registry.put(searchObjectClass, factory); 370 if (oldFactory != null) { 371 372 376 registry.put(searchObjectClass, oldFactory); 377 return false; 378 } 379 return true; 380 } 381 382 396 public static SearchGroup createSearchGroup(Class searchObjectType) { 397 Factory factory = (Factory) registry.get(searchObjectType); 398 399 if (factory == null) { 400 return null; 401 } 402 return factory.createSearchGroup(); 403 } 404 405 414 public static boolean hasFactory(Class searchObjectType) { 415 return registry.containsKey(searchObjectType); 416 } 417 418 } 420 } 421 | Popular Tags |