KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > text2 > FindInFileActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2006 Wind River Systems 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  * Markus Schorn - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.search2.internal.ui.text2;
13
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.viewers.ISelection;
21
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IFileEditorInput;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchPart;
27
28 import org.eclipse.search.ui.ISearchQuery;
29 import org.eclipse.search.ui.text.TextSearchQueryProvider;
30
31 import org.eclipse.search2.internal.ui.SearchMessages;
32
33
34 public class FindInFileActionDelegate extends FindInRecentScopeActionDelegate {
35     private IEditorPart fEditor= null;
36     
37     public FindInFileActionDelegate() {
38         super(SearchMessages.FindInFileActionDelegate_text);
39         setActionDefinitionId("org.eclipse.search.ui.performTextSearchFile"); //$NON-NLS-1$
40
}
41
42     public void selectionChanged(IAction action, ISelection selection) {
43         fEditor= null;
44         IWorkbenchPage page= getWorkbenchPage();
45         if (page != null) {
46             IWorkbenchPart part= page.getActivePart();
47             if (part instanceof IEditorPart) {
48                 IEditorPart editor= (IEditorPart) part;
49                 if (editor.getEditorInput() instanceof IFileEditorInput) {
50                     fEditor= editor;
51                 }
52             }
53         }
54         action.setEnabled(fEditor != null);
55     }
56
57     public void setActiveEditor(IAction action, IEditorPart editor) {
58         if (editor != null && editor.getEditorInput() instanceof IFileEditorInput) {
59             fEditor= editor;
60         } else {
61             fEditor= null;
62         }
63         super.setActiveEditor(action, editor);
64     }
65
66     
67     private IFile getFile() {
68         if (fEditor != null) {
69             IEditorInput ei= fEditor.getEditorInput();
70             if (ei instanceof IFileEditorInput) {
71                 return ((IFileEditorInput) ei).getFile();
72             }
73         }
74         return null;
75     }
76
77     protected ISearchQuery createQuery(TextSearchQueryProvider provider, String JavaDoc searchForString) throws CoreException {
78         return provider.createQuery(searchForString, new IResource[] { getFile() });
79     }
80     
81 }
82
Popular Tags