1 /*******************************************************************************
2 * Copyright (c) 2004, 2009 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
9 * IBM Corp. - Rational Software - initial implementation
10 * Sergey Prigogin (Google)
11 *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.search.actions;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IRegion;
16 import org.eclipse.jface.text.ITextSelection;
17 import org.eclipse.jface.text.TextSelection;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.search.ui.ISearchQuery;
21 import org.eclipse.search.ui.NewSearchUI;
22 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.cdt.core.model.ICElement;
25 import org.eclipse.cdt.core.model.ISourceReference;
26 import org.eclipse.cdt.core.model.ITranslationUnit;
28 import org.eclipse.cdt.internal.ui.editor.CEditor;
29 import org.eclipse.cdt.internal.ui.search.CSearchMessages;
30 import org.eclipse.cdt.internal.ui.search.PDOMSearchElementQuery;
31 import org.eclipse.cdt.internal.ui.search.PDOMSearchQuery;
32 import org.eclipse.cdt.internal.ui.search.PDOMSearchTextSelectionQuery;
33 import org.eclipse.cdt.internal.ui.text.CWordFinder;
36 public abstract class FindAction extends SelectionParseAction {
37 public FindAction(CEditor editor){
41 public FindAction(IWorkbenchSite site){
47 ISearchQuery searchJob = null;
49 ISelection selection = getSelection();
50 if (selection instanceof IStructuredSelection) {
51 Object object = ((IStructuredSelection) selection).getFirstElement();
52 if (object instanceof ISourceReference)
53 searchJob = createQuery((ISourceReference) object);
54 } else if (selection instanceof ITextSelection) {
55 ITextSelection selNode = (ITextSelection) selection;
56 ICElement element = fEditor.getInputCElement();
57 while (element != null && !(element instanceof ITranslationUnit))
58 element = element.getParent();
59 if (element != null) {
60 if (selNode.getLength() == 0) {
61 IDocument document= fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
62 IRegion reg= CWordFinder.findWord(document, selNode.getOffset());
63 selNode = new TextSelection(document, reg.getOffset(), reg.getLength());
65 searchJob = createQuery(element, selNode);
69 if (searchJob == null) {
70 showStatusLineMessage(CSearchMessages.CSearchOperation_operationUnavailable_message);
75 NewSearchUI.activateSearchResultView();
76 NewSearchUI.runQueryInBackground(searchJob);
79 protected PDOMSearchQuery createQuery(ISourceReference object) {
80 return new PDOMSearchElementQuery(getScope(), object, getLimitTo());
83 protected PDOMSearchQuery createQuery(ICElement element, ITextSelection selNode) {
84 return new PDOMSearchTextSelectionQuery(getScope(),
85 (ITranslationUnit) element, selNode, getLimitTo());
88 abstract protected String getScopeDescription();
90 abstract protected ICElement[] getScope();
92 abstract protected int getLimitTo();