1919import java .util .Collections ;
2020import java .util .HashSet ;
2121import java .util .Iterator ;
22+ import java .util .LinkedHashSet ;
2223import java .util .List ;
2324import java .util .Set ;
2425
5051import org .eclipse .jface .viewers .IStructuredSelection ;
5152import org .eclipse .jface .window .Window ;
5253
54+ import org .eclipse .ui .IEditorInput ;
55+ import org .eclipse .ui .IEditorPart ;
56+ import org .eclipse .ui .IEditorReference ;
5357import org .eclipse .ui .IWorkbenchPage ;
5458import org .eclipse .ui .IWorkingSet ;
5559import org .eclipse .ui .IWorkingSetManager ;
60+ import org .eclipse .ui .PartInitException ;
5661import org .eclipse .ui .PlatformUI ;
5762import org .eclipse .ui .dialogs .IWorkingSetSelectionDialog ;
5863
@@ -77,9 +82,11 @@ public class ScopePart {
7782 private Button fUseSelection ;
7883 private Button fUseProject ;
7984 private Button fUseWorkingSet ;
85+ private Button fUseOpenedEditors ;
8086
8187 private int fScope ;
8288 private boolean fCanSearchEnclosingProjects ;
89+ private boolean fCanSearchOpenedEditors ;
8390 private Text fWorkingSetText ;
8491 private IWorkingSet [] fWorkingSets ;
8592
@@ -89,36 +96,50 @@ public class ScopePart {
8996 private boolean fActiveEditorCanProvideScopeSelection ;
9097
9198 /**
92- * Returns a new scope part with workspace as initial scope.
93- * The part is not yet created.
94- * @param searchDialog The parent container
95- * @param searchEnclosingProjects If true, add the 'search enclosing project' radio button
99+ * Returns a new scope part with workspace as initial scope. The part is not
100+ * yet created.
101+ *
102+ * @param searchDialog
103+ * The parent container
104+ * @param searchEnclosingProjects
105+ * If true, add the 'search enclosing project' radio button
106+ * @param searchOpenedEditors
107+ * If true, add the 'search opened editors' radio button
96108 */
97- public ScopePart (SearchDialog searchDialog , boolean searchEnclosingProjects ) {
109+ public ScopePart (SearchDialog searchDialog , boolean searchEnclosingProjects , boolean searchOpenedEditors ) {
98110 fSearchDialog = searchDialog ;
99111 fCanSearchEnclosingProjects = searchEnclosingProjects ;
112+ fCanSearchOpenedEditors = searchOpenedEditors ;
100113
101114 fSettingsStore = SearchPlugin .getDefault ().getDialogSettingsSection (DIALOG_SETTINGS_KEY );
102- fScope = getStoredScope (fSettingsStore , searchEnclosingProjects );
115+ fScope = getStoredScope (fSettingsStore , searchEnclosingProjects , searchOpenedEditors );
103116
104117 fWorkingSets = getStoredWorkingSets ();
105118 }
106119
107- private static int getStoredScope (IDialogSettings settingsStore , boolean canSearchEnclosingProjects ) {
120+ private static int getStoredScope (IDialogSettings settingsStore , boolean canSearchEnclosingProjects ,
121+ boolean cansearchOpenedEditors ) {
108122 int scope ;
109123 try {
110124 scope = settingsStore .getInt (STORE_SCOPE );
111125 } catch (NumberFormatException ex ) {
112126 scope = ISearchPageContainer .WORKSPACE_SCOPE ;
113127 }
114- if (scope != ISearchPageContainer .WORKING_SET_SCOPE
115- && scope != ISearchPageContainer .SELECTION_SCOPE
116- && scope != ISearchPageContainer .SELECTED_PROJECTS_SCOPE
117- && scope != ISearchPageContainer .WORKSPACE_SCOPE )
118- scope = ISearchPageContainer .WORKSPACE_SCOPE ;
128+ if (scope != ISearchPageContainer .WORKING_SET_SCOPE
129+ && scope != ISearchPageContainer .SELECTION_SCOPE
130+ && scope != ISearchPageContainer .SELECTED_PROJECTS_SCOPE
131+ && scope != ISearchPageContainer .OPENED_EDITORS_SCOPE
132+ && scope != ISearchPageContainer .WORKSPACE_SCOPE ) {
133+ scope = ISearchPageContainer .WORKSPACE_SCOPE ;
134+ }
135+
136+ if (!canSearchEnclosingProjects && scope == ISearchPageContainer .SELECTED_PROJECTS_SCOPE ) {
137+ scope = ISearchPageContainer .WORKSPACE_SCOPE ;
138+ }
119139
120- if (!canSearchEnclosingProjects && scope == ISearchPageContainer .SELECTED_PROJECTS_SCOPE )
140+ if (!cansearchOpenedEditors && scope == ISearchPageContainer .OPENED_EDITORS_SCOPE ) {
121141 scope = ISearchPageContainer .WORKSPACE_SCOPE ;
142+ }
122143
123144 return scope ;
124145 }
@@ -203,6 +224,43 @@ public static List<IResource> selectedResourcesFromContainer(ISearchPageContaine
203224 return resources ;
204225 }
205226
227+ public static List <IResource > selectedResourcesFromEditors () {
228+ IEditorReference [] editorReferences = getEditorReferences ();
229+ Set <IResource > resources = new LinkedHashSet <>();
230+ for (IEditorReference ref : editorReferences ) {
231+ IFile file ;
232+ IResource resource ;
233+ try {
234+ IEditorInput editorInput = ref .getEditorInput ();
235+ resource = editorInput .getAdapter (IResource .class );
236+ if (resource != null ) {
237+ resources .add (resource );
238+ continue ;
239+ }
240+ file = editorInput .getAdapter (IFile .class );
241+ if (file != null ) {
242+ resources .add (file );
243+ continue ;
244+ }
245+ // May trigger editor init
246+ IEditorPart editor = ref .getEditor (true );
247+ resource = editor .getAdapter (IResource .class );
248+ if (resource != null ) {
249+ resources .add (resource );
250+ continue ;
251+ }
252+ file = editor .getAdapter (IFile .class );
253+ if (file != null ) {
254+ resources .add (file );
255+ continue ;
256+ }
257+ } catch (PartInitException e ) {
258+ // continue
259+ }
260+ }
261+ return new ArrayList <>(resources );
262+ }
263+
206264 private String getSelectedResurcesButtonText () {
207265 int size = selectedResourcesFromContainer (fSearchDialog ).size ();
208266 if (size == 1 ) {
@@ -242,11 +300,13 @@ public int getSelectedScope() {
242300 * @param scope the scope to be selected in this part
243301 */
244302 public void setSelectedScope (int scope ) {
245- Assert .isLegal (scope >= 0 && scope <= 3 );
303+ Assert .isLegal (
304+ scope >= ISearchPageContainer .WORKSPACE_SCOPE && scope <= ISearchPageContainer .OPENED_EDITORS_SCOPE );
246305 Assert .isNotNull (fUseWorkspace );
247306 Assert .isNotNull (fUseSelection );
248307 Assert .isNotNull (fUseWorkingSet );
249308 Assert .isNotNull (fUseProject );
309+ Assert .isNotNull (fUseOpenedEditors );
250310
251311 fSettingsStore .put (STORE_SCOPE , scope );
252312
@@ -259,13 +319,22 @@ public void setSelectedScope(int scope) {
259319 }
260320 } else if (scope == ISearchPageContainer .SELECTION_SCOPE && !fUseSelection .isEnabled ()) {
261321 scope = fUseProject .isEnabled () ? ISearchPageContainer .SELECTED_PROJECTS_SCOPE : ISearchPageContainer .WORKSPACE_SCOPE ;
322+ } else if (scope == ISearchPageContainer .OPENED_EDITORS_SCOPE ) {
323+ if (!fCanSearchOpenedEditors ) {
324+ SearchPlugin .log (new Status (IStatus .WARNING , NewSearchUI .PLUGIN_ID , IStatus .WARNING ,
325+ "Opened editors scope set on search page that does not support it" , null )); //$NON-NLS-1$
326+ scope = ISearchPageContainer .WORKSPACE_SCOPE ;
327+ } else if (!fUseOpenedEditors .isEnabled ()) {
328+ scope = ISearchPageContainer .WORKSPACE_SCOPE ;
329+ }
262330 }
263331 fScope = scope ;
264332
265333 fUseWorkspace .setSelection (scope == ISearchPageContainer .WORKSPACE_SCOPE );
266334 fUseSelection .setSelection (scope == ISearchPageContainer .SELECTION_SCOPE );
267335 fUseProject .setSelection (scope == ISearchPageContainer .SELECTED_PROJECTS_SCOPE );
268336 fUseWorkingSet .setSelection (scope == ISearchPageContainer .WORKING_SET_SCOPE );
337+ fUseOpenedEditors .setSelection (scope == ISearchPageContainer .OPENED_EDITORS_SCOPE );
269338
270339 updateSearchPageContainerActionPerformedEnablement ();
271340
@@ -276,7 +345,7 @@ public void setActiveEditorCanProvideScopeSelection(boolean state) {
276345 fUseSelection .setEnabled (canSearchInSelection ());
277346
278347 // Reinitialize the controls
279- fScope = getStoredScope (fSettingsStore , fCanSearchEnclosingProjects );
348+ fScope = getStoredScope (fSettingsStore , fCanSearchEnclosingProjects , fCanSearchOpenedEditors );
280349 setSelectedScope (fScope );
281350 }
282351
@@ -381,6 +450,16 @@ public Composite createPart(Composite parent) {
381450 if (!fCanSearchEnclosingProjects )
382451 fUseProject .setVisible (false );
383452
453+ fUseOpenedEditors = new Button (fPart , SWT .RADIO );
454+ fUseOpenedEditors .setData (Integer .valueOf (ISearchPageContainer .OPENED_EDITORS_SCOPE ));
455+ fUseOpenedEditors .setText (SearchMessages .ScopePart_openedEditorsScope_text );
456+ fUseOpenedEditors .setToolTipText (SearchMessages .ScopePart_openedEditorsScope_tooltip_text );
457+ fUseOpenedEditors .setEnabled (!selectedResourcesFromEditors ().isEmpty ());
458+
459+ gd = new GridData (GridData .HORIZONTAL_ALIGN_BEGINNING );
460+ gd .horizontalSpan = 4 ;
461+ fUseOpenedEditors .setLayoutData (gd );
462+
384463 fUseWorkingSet = new Button (fPart , SWT .RADIO );
385464 fUseWorkingSet .setData (Integer .valueOf (ISearchPageContainer .WORKING_SET_SCOPE ));
386465 fUseWorkingSet .setText (SearchMessages .ScopePart_workingSetScope_text );
@@ -421,6 +500,7 @@ public void widgetSelected(SelectionEvent e) {
421500 fUseSelection .addSelectionListener (scopeChangedLister );
422501 fUseProject .addSelectionListener (scopeChangedLister );
423502 fUseWorkingSet .addSelectionListener (scopeChangedLister );
503+ fUseOpenedEditors .addSelectionListener (scopeChangedLister );
424504
425505 // Set initial scope
426506 setSelectedScope (fScope );
@@ -432,6 +512,10 @@ public void widgetSelected(SelectionEvent e) {
432512 return fPart ;
433513 }
434514
515+ private static IEditorReference [] getEditorReferences () {
516+ return PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ().getEditorReferences ();
517+ }
518+
435519 private boolean canSearchInSelection () {
436520 ISelection selection = fSearchDialog .getSelection ();
437521 return (selection instanceof IStructuredSelection ) && !selection .isEmpty ()
0 commit comments