1 11 package org.eclipse.help.internal.search; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.core.runtime.SubProgressMonitor; 23 import org.eclipse.core.runtime.jobs.Job; 24 import org.eclipse.help.internal.base.HelpBasePlugin; 25 import org.eclipse.help.internal.base.remote.RemoteHelp; 26 import org.eclipse.help.internal.base.remote.RemoteSearchManager; 27 import org.eclipse.help.internal.search.federated.FederatedSearchEntry; 28 import org.eclipse.help.internal.search.federated.FederatedSearchJob; 29 30 33 public class SearchManager { 34 35 private LocalSearchManager localManager = new LocalSearchManager(); 36 private RemoteSearchManager remoteManager = new RemoteSearchManager(); 37 38 private IProgressMonitor localMonitor; 39 private IProgressMonitor remoteMonitor; 40 41 private ISearchQuery searchQuery; 42 private ISearchHitCollector collector; 43 private BufferedSearchHitCollector bufferedCollector = new BufferedSearchHitCollector(); 44 45 private Job localSearchJob; 46 private Job remoteSearchJob; 47 48 51 public SearchManager() { 52 56 localSearchJob = new Job("localSearchJob") { protected IStatus run(IProgressMonitor monitor) { 58 localManager.search(searchQuery, bufferedCollector, localMonitor); 59 return Status.OK_STATUS; 60 } 61 }; 62 remoteSearchJob = new Job("remoteSearchJob") { protected IStatus run(IProgressMonitor monitor) { 64 remoteManager.search(searchQuery, bufferedCollector, remoteMonitor); 65 return Status.OK_STATUS; 66 } 67 }; 68 localSearchJob.setSystem(true); 69 remoteSearchJob.setSystem(true); 70 } 71 72 75 public void search(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm) 76 throws QueryTooComplexException { 77 if (RemoteHelp.isEnabled()) { 78 searchLocalAndRemote(searchQuery, collector, pm); 79 } 80 else { 81 searchLocal(searchQuery, collector, pm); 82 } 83 } 84 85 88 public void searchLocal(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm) 89 throws QueryTooComplexException { 90 localManager.search(searchQuery, collector, pm); 91 } 92 93 96 public void searchLocalAndRemote(ISearchQuery searchQuery, ISearchHitCollector collector, IProgressMonitor pm) 97 throws QueryTooComplexException { 98 this.searchQuery = searchQuery; 99 this.collector = collector; 100 101 pm.beginTask("", 100); 103 localMonitor = new SubProgressMonitor(pm, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL); 105 remoteMonitor = new SubProgressMonitor(pm, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL); 106 107 localSearchJob.schedule(); 109 remoteSearchJob.schedule(); 110 111 try { 113 localSearchJob.join(); 114 remoteSearchJob.join(); 115 } 116 catch (InterruptedException e) { 117 String msg = "Unexpected InterruptedException while waiting for help search jobs to finish"; HelpBasePlugin.logError(msg, e); 119 } 120 121 bufferedCollector.flush(); 123 pm.done(); 124 } 125 126 129 public void search(String expression, FederatedSearchEntry[] entries) { 130 for (int i = 0; i < entries.length; i++) { 131 FederatedSearchJob job = new FederatedSearchJob(expression, entries[i]); 132 job.schedule(); 133 } 134 } 135 136 139 public LocalSearchManager getLocalSearchManager() { 140 return localManager; 141 } 142 143 146 public RemoteSearchManager getRemoteSearchManager() { 147 return remoteManager; 148 } 149 150 153 public void close() { 154 localManager.close(); 155 } 156 157 161 private class BufferedSearchHitCollector implements ISearchHitCollector { 162 private Set allHits = new HashSet (); 163 private String wordsSearched = null; 164 165 168 public void addHits(List hits, String wordsSearched) { 169 if (wordsSearched != null) { 170 this.wordsSearched = wordsSearched; 171 } 172 allHits.addAll(hits); 173 } 174 175 179 public void flush() { 180 List hitsList = new ArrayList (allHits); 182 Collections.sort(hitsList); 183 collector.addHits(hitsList, wordsSearched); 184 allHits.clear(); 185 wordsSearched = null; 186 } 187 } 188 } 189
| Popular Tags
|