upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / search / actions / FindAction.java
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
7  *
8  * Contributors:
9  *     IBM Corp. - Rational Software - initial implementation
10  *     Sergey Prigogin (Google)
11  *******************************************************************************/
12 package org.eclipse.cdt.internal.ui.search.actions;
13
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;
23
24 import org.eclipse.cdt.core.model.ICElement;
25 import org.eclipse.cdt.core.model.ISourceReference;
26 import org.eclipse.cdt.core.model.ITranslationUnit;
27
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;
34
35
36 public abstract class FindAction extends SelectionParseAction {
37         public FindAction(CEditor editor){
38                 super(editor);
39         }
40         
41         public FindAction(IWorkbenchSite site){
42                 super(site);
43         }
44         
45         @Override
46         public void run() {
47                 ISearchQuery searchJob = null;
48
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());
64                                 }
65                                 searchJob = createQuery(element, selNode);
66                         }
67                 } 
68
69                 if (searchJob == null) {
70                         showStatusLineMessage(CSearchMessages.CSearchOperation_operationUnavailable_message);
71                         return;
72                 }
73
74         clearStatusLine();
75                 NewSearchUI.activateSearchResultView();
76                 NewSearchUI.runQueryInBackground(searchJob);
77         }
78
79         protected PDOMSearchQuery createQuery(ISourceReference object) {
80                 return new PDOMSearchElementQuery(getScope(), object, getLimitTo());
81         }
82
83         protected PDOMSearchQuery createQuery(ICElement element, ITextSelection selNode) {
84                 return new PDOMSearchTextSelectionQuery(getScope(),
85                                 (ITranslationUnit) element, selNode, getLimitTo());
86         }
87         
88     abstract protected String getScopeDescription(); 
89
90         abstract protected ICElement[] getScope();
91         
92         abstract protected int getLimitTo();
93 }