ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editor
.getEditorInput());
Query query = new Query(typeRoot);
logger.debug("Sourcecode\r\n" + query.getSource() + "\r\n");
logger.debug("Query: " + query.getMqlQuery());
final StandardSearch search = new StandardSearch(editor, "Search for ",
query);
// Delegate the search event listeners to the search
for (SearchEventListener listener : listeners) {
search.addSearchEventListener(listener);
}
if (searches.get(editor) != null) {
logger.debug("Search in progress... Cancel old search.");
for (Search s : searches.get(editor)) {
s.cancel();
try {
s.join();
} catch (InterruptedException e) {
CrashReporter.reportException(e);
logger.debug("Could not cancel Job: " + e.getMessage());
}
}
} else {
searches.put(editor, new ArrayList<Search>());
}
searches.get(editor).add(search);
// This listener ensures that only one search can be
// performed at the same time for an editor
search.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
logger.debug("Search completed successfully.");
search.getSearchResult().notifyObservers(
search.getSearchResult());
} else {
logger.debug("Search aborted.");
}
CodeConjurer.getInstance().setSearchFinished(search);
}
});
// The rule for one search per editor
ParallelSearchPrevention concurrencyRule = null;
if (searchJobs.containsKey(editor)) {
concurrencyRule = searchJobs.get(editor);
} else {
concurrencyRule = new ParallelSearchPrevention();
searchJobs.put(editor, concurrencyRule);
}
search.setRule(concurrencyRule);
search.setPriority(Job.LONG);
search.schedule(1000);
}