KickJava   Java API By Example, From Geeks To Geeks.

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


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.IProject;
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 public class FindInProjectActionDelegate extends FindInRecentScopeActionDelegate {
34     private IEditorPart fEditor= null;
35
36     public FindInProjectActionDelegate() {
37         super(SearchMessages.FindInProjectActionDelegate_text);
38         setActionDefinitionId("org.eclipse.search.ui.performTextSearchProject"); //$NON-NLS-1$
39
}
40
41     public void selectionChanged(IAction action, ISelection selection) {
42         fEditor= null;
43         IWorkbenchPage page= getWorkbenchPage();
44         if (page != null) {
45             IWorkbenchPart part= page.getActivePart();
46             if (part instanceof IEditorPart) {
47                 IEditorPart editor= (IEditorPart) part;
48                 if (editor.getEditorInput() instanceof IFileEditorInput) {
49                     fEditor= editor;
50                 }
51             }
52         }
53         action.setEnabled(fEditor != null);
54     }
55
56     public void setActiveEditor(IAction action, IEditorPart editor) {
57         if (editor != null && editor.getEditorInput() instanceof IFileEditorInput) {
58             fEditor= editor;
59         } else {
60             fEditor= null;
61         }
62         super.setActiveEditor(action, fEditor);
63     }
64
65     protected IProject getCurrentProject() {
66         if (fEditor != null) {
67             IEditorInput ei= fEditor.getEditorInput();
68             if (ei instanceof IFileEditorInput) {
69                 return ((IFileEditorInput) ei).getFile().getProject();
70             }
71         }
72         return null;
73     }
74
75     protected ISearchQuery createQuery(TextSearchQueryProvider provider, String JavaDoc searchForString) throws CoreException {
76         IProject proj= getCurrentProject();
77         if (proj != null) {
78             return provider.createQuery(searchForString, new IResource[] { getCurrentProject() });
79         }
80         return provider.createQuery(searchForString);
81     }
82 }
83
Popular Tags