Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b19bc1bf3ab20026469eb9cb7e1714b2a0385d5 (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Created on Aug 12, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.cdt.internal.ui.editor;

import java.util.List;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.search.ui.SearchUI;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * @author bgheorgh
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SearchDialogAction extends Action {

	private static final String PREFIX= "SearchDialogAction.";
	private static final String C_SEARCH_PAGE_ID= "org.eclipse.cdt.ui.CSearchPage"; 

	private ISelectionProvider fSelectionProvider;
	private ITextEditor fEditor;
	
	public SearchDialogAction(ISelectionProvider provider, CEditor editor) {
		super(CUIPlugin.getResourceString(PREFIX + "label"));
		setDescription(CUIPlugin.getResourceString(PREFIX + "description"));
		setToolTipText(CUIPlugin.getResourceString(PREFIX + "tooltip"));
		
		if(provider instanceof CContentOutlinePage) {
			CPluginImages.setImageDescriptors(this, CPluginImages.T_LCL, CPluginImages.IMG_MENU_OPEN_INCLUDE);
		}
		
		fSelectionProvider= provider;
		fEditor = editor;
	}
			
	public void run() {
		String search_name;
		
		ISelection selection= fSelectionProvider.getSelection();
		if(selection instanceof ITextSelection) {
			search_name = ((ITextSelection)selection).getText();
			if(search_name.length() == 0) return;
		} else {
			ICElement element= getElement(selection);
			if (element == null) {
				return;
			}
			search_name = element.getElementName();
		}
		
		SearchUI.openSearchDialog(fEditor.getEditorSite().getWorkbenchWindow(),C_SEARCH_PAGE_ID);
		
//		// @@@ we rely on the internal functions of the Search plugin, since
//		// none of these are actually exported. This is probably going to change
//		// with 2.0.
//		TextSearchResultCollector col = new TextSearchResultCollector();
//		try {
//			//TextSearchPage
//			//ProgressMonitor monitor = new ProgressMonitor();
//			//col.setProgressMonitor(monitor)
//			SearchUI.activateSearchResultView();
//			//col.aboutToStart();
//		
//			// We now have the element, start a search on the string
//			//TextSearchEngine engine = new TextSearchEngine();
//			TextSearchScope scope= TextSearchScope.newWorkspaceScope();
//			// Add the extensions from the C editor definition for now
//			// FIXME: For C/C++ not all files rely on extension to be C++ for <cstring>
//			String[] cexts = CoreModel.getDefault().getTranslationUnitExtensions();
//			for (int i = 0; i < cexts.length; i++) {
//				scope.addExtension("*." + cexts[i]);
//			}
////			scope.addExtension("*.c");
////			scope.addExtension("*.h");
////			scope.addExtension("*.cc");
////			scope.addExtension("*.hh");
//			
//			TextSearchOperation op= new TextSearchOperation(
//				CUIPlugin.getWorkspace(),
//				search_name,
//				"",
//				scope,
//				col);
//
//
//			//engine.search(CUIPlugin.getWorkspace(), element.getName(),
//			//	null, scope, col);
//			IRunnableContext context=  null;
//			//context= getContainer().getRunnableContext();
//			
//			Shell shell= new Shell(); // getShell();
//			if (context == null)
//				context= new ProgressMonitorDialog(shell);
//
//
//			try {			
//				context.run(true, true, op);
//			} catch (InvocationTargetException ex) {
//				ExceptionHandler.handle(ex, "Error","Error"); //$NON-NLS-2$ //$NON-NLS-1$
//			} catch (InterruptedException e) {
//			}
//		} catch (Exception e) {}
//		
	}


	private static ICElement getElement(ISelection sel) {
		if (!sel.isEmpty() && sel instanceof IStructuredSelection) {
			List list= ((IStructuredSelection)sel).toList();
			if (list.size() == 1) {
				Object element= list.get(0);
				if (element instanceof ICElement) {
					return (ICElement)element;
				}
			}
		}
		return null;
	}
	
	public static boolean canActionBeAdded(ISelection selection) {
		if(selection instanceof ITextSelection) {
			return (((ITextSelection)selection).getLength() > 0);
		} else {
			return getElement(selection) != null;
		}
	}	


	public static String getEditorID(String name) {
		IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry();
		if (registry != null) {
			IEditorDescriptor descriptor = registry.getDefaultEditor(name);
			if (descriptor != null) {
				return descriptor.getId();
			} else {
				return registry.getDefaultEditor().getId();
			}
		}
		return null;
	}

}

Back to the top