KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > FindOccurrencesEngine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.search;
12
13 import org.eclipse.jface.text.Document;
14 import org.eclipse.jface.text.IDocument;
15
16 import org.eclipse.search.ui.NewSearchUI;
17
18 import org.eclipse.jdt.core.IClassFile;
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.ISourceReference;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24
25 import org.eclipse.jdt.internal.ui.JavaPlugin;
26 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
27
28 public abstract class FindOccurrencesEngine {
29     
30     private IOccurrencesFinder fFinder;
31     
32     private static class FindOccurencesClassFileEngine extends FindOccurrencesEngine {
33         private IClassFile fClassFile;
34         
35         public FindOccurencesClassFileEngine(IClassFile file, IOccurrencesFinder finder) {
36             super(finder);
37             fClassFile= file;
38         }
39         protected CompilationUnit createAST() {
40             return JavaPlugin.getDefault().getASTProvider().getAST(fClassFile, ASTProvider.WAIT_YES, null);
41         }
42         protected IJavaElement getInput() {
43             return fClassFile;
44         }
45         protected ISourceReference getSourceReference() {
46             return fClassFile;
47         }
48     }
49
50     private static class FindOccurencesCUEngine extends FindOccurrencesEngine {
51         private ICompilationUnit fCUnit;
52         
53         public FindOccurencesCUEngine(ICompilationUnit unit, IOccurrencesFinder finder) {
54             super(finder);
55             fCUnit= unit;
56         }
57         protected CompilationUnit createAST() {
58             return JavaPlugin.getDefault().getASTProvider().getAST(fCUnit, ASTProvider.WAIT_YES, null);
59         }
60         protected IJavaElement getInput() {
61             return fCUnit;
62         }
63         protected ISourceReference getSourceReference() {
64             return fCUnit;
65         }
66     }
67     
68     protected FindOccurrencesEngine(IOccurrencesFinder finder) {
69         fFinder= finder;
70     }
71     
72     public static FindOccurrencesEngine create(IJavaElement root, IOccurrencesFinder finder) {
73         if (root == null || finder == null)
74             return null;
75         
76         ICompilationUnit unit= (ICompilationUnit)root.getAncestor(IJavaElement.COMPILATION_UNIT);
77         if (unit != null)
78             return new FindOccurencesCUEngine(unit, finder);
79         IClassFile cf= (IClassFile)root.getAncestor(IJavaElement.CLASS_FILE);
80         if (cf != null)
81             return new FindOccurencesClassFileEngine(cf, finder);
82         return null;
83     }
84
85     protected abstract CompilationUnit createAST();
86     
87     protected abstract IJavaElement getInput();
88     
89     protected abstract ISourceReference getSourceReference();
90     
91     protected IOccurrencesFinder getOccurrencesFinder() {
92         return fFinder;
93     }
94
95     public String JavaDoc run(int offset, int length) throws JavaModelException {
96         ISourceReference sr= getSourceReference();
97         if (sr.getSourceRange() == null) {
98             return SearchMessages.FindOccurrencesEngine_noSource_text;
99         }
100         
101         final CompilationUnit root= createAST();
102         if (root == null) {
103             return SearchMessages.FindOccurrencesEngine_cannotParse_text;
104         }
105         String JavaDoc message= fFinder.initialize(root, offset, length);
106         if (message != null)
107             return message;
108         
109         final IDocument document= new Document(getSourceReference().getSource());
110         
111         performNewSearch(fFinder, document, getInput());
112         return null;
113     }
114     
115     private void performNewSearch(IOccurrencesFinder finder, IDocument document, IJavaElement element) {
116         NewSearchUI.runQueryInBackground(new OccurrencesSearchQuery(finder, document, element));
117     }
118 }
119
Popular Tags