01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.search;
11:
12: import org.eclipse.search.ui.NewSearchUI;
13:
14: import org.eclipse.jdt.core.ISourceReference;
15: import org.eclipse.jdt.core.ITypeRoot;
16: import org.eclipse.jdt.core.JavaModelException;
17: import org.eclipse.jdt.core.dom.CompilationUnit;
18:
19: import org.eclipse.jdt.ui.SharedASTProvider;
20:
21: public final class FindOccurrencesEngine {
22:
23: public static FindOccurrencesEngine create(ITypeRoot root,
24: IOccurrencesFinder finder) {
25: if (root == null || finder == null)
26: return null;
27: return new FindOccurrencesEngine(root, finder);
28: }
29:
30: private IOccurrencesFinder fFinder;
31: private ITypeRoot fTypeRoot;
32:
33: private FindOccurrencesEngine(ITypeRoot typeRoot,
34: IOccurrencesFinder finder) {
35: fFinder = finder;
36: fTypeRoot = typeRoot;
37: }
38:
39: public CompilationUnit createAST() {
40: return SharedASTProvider.getAST(fTypeRoot,
41: SharedASTProvider.WAIT_YES, null);
42: }
43:
44: public ITypeRoot getInput() {
45: return fTypeRoot;
46: }
47:
48: public IOccurrencesFinder getOccurrencesFinder() {
49: return fFinder;
50: }
51:
52: public String run(int offset, int length) throws JavaModelException {
53: ISourceReference sr = getInput();
54: if (sr.getSourceRange() == null) {
55: return SearchMessages.FindOccurrencesEngine_noSource_text;
56: }
57:
58: final CompilationUnit root = createAST();
59: if (root == null) {
60: return SearchMessages.FindOccurrencesEngine_cannotParse_text;
61: }
62: String message = fFinder.initialize(root, offset, length);
63: if (message != null)
64: return message;
65:
66: performNewSearch(fFinder, getInput());
67: return null;
68: }
69:
70: private void performNewSearch(IOccurrencesFinder finder,
71: ITypeRoot element) {
72: NewSearchUI.runQueryInBackground(new OccurrencesSearchQuery(
73: finder, element));
74: }
75: }
|