KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > FindOccurrencesInFileAction


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.ui.actions;
12
13 import org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.jface.action.IStatusLineManager;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17
18 import org.eclipse.jface.text.ITextSelection;
19
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.ui.IEditorSite;
23 import org.eclipse.ui.IViewPart;
24 import org.eclipse.ui.IViewSite;
25 import org.eclipse.ui.IWorkbenchSite;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.part.IPageSite;
28 import org.eclipse.ui.part.Page;
29 import org.eclipse.ui.texteditor.IEditorStatusLine;
30
31 import org.eclipse.jdt.core.IClassFile;
32 import org.eclipse.jdt.core.IJavaElement;
33 import org.eclipse.jdt.core.IMember;
34 import org.eclipse.jdt.core.ISourceRange;
35 import org.eclipse.jdt.core.JavaModelException;
36
37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
40 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
42 import org.eclipse.jdt.internal.ui.search.FindOccurrencesEngine;
43 import org.eclipse.jdt.internal.ui.search.OccurrencesFinder;
44 import org.eclipse.jdt.internal.ui.search.SearchMessages;
45
46 /**
47  * Action to find all occurrences of a compilation unit member (e.g.
48  * fields, methods, types, and local variables) in a file.
49  * <p>
50  * Action is applicable to selections containing elements of type
51  * <tt>IMember</tt>.
52  *
53  * <p>
54  * This class may be instantiated; it is not intended to be subclassed.
55  * </p>
56  *
57  * @since 2.1
58  */

59 public class FindOccurrencesInFileAction extends SelectionDispatchAction {
60     
61     private JavaEditor fEditor;
62     private IActionBars fActionBars;
63     
64     /**
65      * Creates a new <code>FindOccurrencesInFileAction</code>. The action requires
66      * that the selection provided by the view part's selection provider is of type <code>
67      * org.eclipse.jface.viewers.IStructuredSelection</code>.
68      *
69      * @param part the part providing context information for this action
70      */

71     public FindOccurrencesInFileAction(IViewPart part) {
72         this(part.getSite());
73     }
74     
75     /**
76      * Creates a new <code>FindOccurrencesInFileAction</code>. The action requires
77      * that the selection provided by the page's selection provider is of type <code>
78      * org.eclipse.jface.viewers.IStructuredSelection</code>.
79      *
80      * @param page the page providing context information for this action
81      */

82     public FindOccurrencesInFileAction(Page page) {
83         this(page.getSite());
84     }
85     
86     /**
87      * Note: This constructor is for internal use only. Clients should not call this constructor.
88      * @param editor the Java editor
89      */

90     public FindOccurrencesInFileAction(JavaEditor editor) {
91         this(editor.getEditorSite());
92         fEditor= editor;
93         setEnabled(getEditorInput(editor) != null);
94     }
95     
96     /**
97      * Creates a new <code>FindOccurrencesInFileAction</code>. The action
98      * requires that the selection provided by the site's selection provider is of type
99      * <code>IStructuredSelection</code>.
100      *
101      * @param site the site providing context information for this action
102      * @since 3.1
103      */

104     public FindOccurrencesInFileAction(IWorkbenchSite site) {
105         super(site);
106         
107         if (site instanceof IViewSite)
108             fActionBars= ((IViewSite)site).getActionBars();
109         else if (site instanceof IEditorSite)
110             fActionBars= ((IEditorSite)site).getActionBars();
111         else if (site instanceof IPageSite)
112             fActionBars= ((IPageSite)site).getActionBars();
113         
114         setText(SearchMessages.Search_FindOccurrencesInFile_label);
115         setToolTipText(SearchMessages.Search_FindOccurrencesInFile_tooltip);
116         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.FIND_OCCURRENCES_IN_FILE_ACTION);
117     }
118     
119     //---- Structured Selection -------------------------------------------------------------
120

121     /* (non-JavaDoc)
122      * Method declared in SelectionDispatchAction.
123      */

124     public void selectionChanged(IStructuredSelection selection) {
125         setEnabled(getMember(selection) != null);
126     }
127     
128     /* (non-JavaDoc)
129      * Method declared in SelectionDispatchAction.
130      */

131     private IMember getMember(IStructuredSelection selection) {
132         if (selection.size() != 1)
133             return null;
134         Object JavaDoc o= selection.getFirstElement();
135         if (o instanceof IMember) {
136             IMember member= (IMember)o;
137             try {
138                 if (member.getNameRange() == null)
139                     return null;
140             } catch (JavaModelException ex) {
141                 return null;
142             }
143             
144             IClassFile file= member.getClassFile();
145             if (file != null) {
146                 try {
147                     if (file.getSourceRange() != null)
148                         return member;
149                 } catch (JavaModelException e) {
150                     return null;
151                 }
152             }
153             return member;
154         }
155         return null;
156     }
157     
158     public void run(IStructuredSelection selection) {
159         IMember member= getMember(selection);
160         if (!ActionUtil.isProcessable(getShell(), member))
161             return;
162         FindOccurrencesEngine engine= FindOccurrencesEngine.create(member, new OccurrencesFinder());
163         try {
164             ISourceRange range= member.getNameRange();
165             String JavaDoc result= engine.run(range.getOffset(), range.getLength());
166             if (result != null)
167                 showMessage(getShell(), fActionBars, result);
168         } catch (JavaModelException e) {
169             JavaPlugin.log(e);
170         }
171     }
172     
173     private static void showMessage(Shell shell, IActionBars actionBars, String JavaDoc msg) {
174         if (actionBars != null) {
175             IStatusLineManager statusLine= actionBars.getStatusLineManager();
176             if (statusLine != null)
177                 statusLine.setMessage(msg);
178         }
179         shell.getDisplay().beep();
180     }
181     
182     //---- Text Selection ----------------------------------------------------------------------
183

184     /* (non-JavaDoc)
185      * Method declared in SelectionDispatchAction.
186      */

187     public void selectionChanged(ITextSelection selection) {
188     }
189
190     /* (non-JavaDoc)
191      * Method declared in SelectionDispatchAction.
192      */

193     public final void run(ITextSelection ts) {
194         IJavaElement input= getEditorInput(fEditor);
195         if (!ActionUtil.isProcessable(getShell(), input))
196             return;
197         FindOccurrencesEngine engine= FindOccurrencesEngine.create(input, new OccurrencesFinder());
198         try {
199             String JavaDoc result= engine.run(ts.getOffset(), ts.getLength());
200             if (result != null)
201                 showMessage(getShell(), fEditor, result);
202         } catch (JavaModelException e) {
203             JavaPlugin.log(e);
204         }
205     }
206
207     private static IJavaElement getEditorInput(JavaEditor editor) {
208         IEditorInput input= editor.getEditorInput();
209         if (input instanceof IClassFileEditorInput)
210             return ((IClassFileEditorInput)input).getClassFile();
211         return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input);
212     }
213         
214     private static void showMessage(Shell shell, JavaEditor editor, String JavaDoc msg) {
215         IEditorStatusLine statusLine= (IEditorStatusLine) editor.getAdapter(IEditorStatusLine.class);
216         if (statusLine != null)
217             statusLine.setMessage(true, msg, null);
218         shell.getDisplay().beep();
219     }
220 }
221
Popular Tags