1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*******************************************************************************
 * Copyright (c) 2006, 2008 Wind River Systems, IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Markus Schorn - initial API and implementation
 *******************************************************************************/

package org.eclipse.search2.internal.ui.text2;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.text.TextSearchQueryProvider;

import org.eclipse.search2.internal.ui.SearchMessages;


public class FindInFileActionDelegate extends FindInRecentScopeActionDelegate {
    private IEditorPart fEditor= null;

    public FindInFileActionDelegate() {
        super(SearchMessages.FindInFileActionDelegate_text);
        setActionDefinitionId("org.eclipse.search.ui.performTextSearchFile"); //$NON-NLS-1$
    }

    public void selectionChanged(IAction action, ISelection selection) {
        fEditor= null;
        IWorkbenchPage page= getWorkbenchPage();
        if (page != null) {
            IWorkbenchPart part= page.getActivePart();
            if (part instanceof IEditorPart) {
                IEditorPart editor= (IEditorPart) part;
                if (editor.getEditorInput() instanceof IFileEditorInput) {
                    fEditor= editor;
                }
            }
        }
        action.setEnabled(fEditor != null);
    }

    public void setActiveEditor(IAction action, IEditorPart editor) {
        if (editor != null && editor.getEditorInput() instanceof IFileEditorInput) {
            fEditor= editor;
        } else {
            fEditor= null;
        }
        super.setActiveEditor(action, editor);
    }


    private IFile getFile() {
        if (fEditor != null) {
            IEditorInput ei= fEditor.getEditorInput();
            if (ei instanceof IFileEditorInput) {
                return ((IFileEditorInput) ei).getFile();
            }
        }
        return null;
    }

    protected ISearchQuery createQuery(TextSearchQueryProvider provider, String searchForString) throws CoreException {
        return provider.createQuery(searchForString, new IResource[] { getFile() });
    }

}
			
			

Browsed Source: [clear]