ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editor
.getEditorInput());
Query query = new Query(typeRoot);
logger.debug("Sourcecode\r\n" + query.getSource() + "\r\n");
logger.debug("Query: " + query.getQuery());
final Search search;
String querytype = query.getType();
if (querytype.equals("tds")) {
if (!byAgent) {
// search = new TestDrivenSearch(editor,
// "Test-Driven Search for "
// + editor.getTitle(), query);
search = new StandardSearch(editor, "Test-Driven Search for "
+ editor.getTitle(), query, Search.TEST_DRIVEN_SEARCH);
logger.debug("Test-Driven Search created");
} else {
// TODO: Implement a background-agent that reacts to JUnit
// assertions instead interface changes
logger.debug("We do not support test-driven searches triggered by the agent, yet. "
+ "We need a better algorithm for this.");
return;
}
} else {
search = new StandardSearch(editor,
"Search for reusable assets for " + editor.getTitle(),
query, Search.STANDARD_SEARCH);
logger.debug("Standard Search created");
}
// 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);
}