19
19
import java .util .Collections ;
20
20
import java .util .HashSet ;
21
21
import java .util .Iterator ;
22
+ import java .util .LinkedHashSet ;
22
23
import java .util .List ;
23
24
import java .util .Set ;
24
25
50
51
import org .eclipse .jface .viewers .IStructuredSelection ;
51
52
import org .eclipse .jface .window .Window ;
52
53
54
+ import org .eclipse .ui .IEditorInput ;
55
+ import org .eclipse .ui .IEditorPart ;
56
+ import org .eclipse .ui .IEditorReference ;
53
57
import org .eclipse .ui .IWorkbenchPage ;
54
58
import org .eclipse .ui .IWorkingSet ;
55
59
import org .eclipse .ui .IWorkingSetManager ;
60
+ import org .eclipse .ui .PartInitException ;
56
61
import org .eclipse .ui .PlatformUI ;
57
62
import org .eclipse .ui .dialogs .IWorkingSetSelectionDialog ;
58
63
@@ -77,9 +82,11 @@ public class ScopePart {
77
82
private Button fUseSelection ;
78
83
private Button fUseProject ;
79
84
private Button fUseWorkingSet ;
85
+ private Button fUseOpenedEditors ;
80
86
81
87
private int fScope ;
82
88
private boolean fCanSearchEnclosingProjects ;
89
+ private boolean fCanSearchOpenedEditors ;
83
90
private Text fWorkingSetText ;
84
91
private IWorkingSet [] fWorkingSets ;
85
92
@@ -89,36 +96,50 @@ public class ScopePart {
89
96
private boolean fActiveEditorCanProvideScopeSelection ;
90
97
91
98
/**
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
96
108
*/
97
- public ScopePart (SearchDialog searchDialog , boolean searchEnclosingProjects ) {
109
+ public ScopePart (SearchDialog searchDialog , boolean searchEnclosingProjects , boolean searchOpenedEditors ) {
98
110
fSearchDialog = searchDialog ;
99
111
fCanSearchEnclosingProjects = searchEnclosingProjects ;
112
+ fCanSearchOpenedEditors = searchOpenedEditors ;
100
113
101
114
fSettingsStore = SearchPlugin .getDefault ().getDialogSettingsSection (DIALOG_SETTINGS_KEY );
102
- fScope = getStoredScope (fSettingsStore , searchEnclosingProjects );
115
+ fScope = getStoredScope (fSettingsStore , searchEnclosingProjects , searchOpenedEditors );
103
116
104
117
fWorkingSets = getStoredWorkingSets ();
105
118
}
106
119
107
- private static int getStoredScope (IDialogSettings settingsStore , boolean canSearchEnclosingProjects ) {
120
+ private static int getStoredScope (IDialogSettings settingsStore , boolean canSearchEnclosingProjects ,
121
+ boolean cansearchOpenedEditors ) {
108
122
int scope ;
109
123
try {
110
124
scope = settingsStore .getInt (STORE_SCOPE );
111
125
} catch (NumberFormatException ex ) {
112
126
scope = ISearchPageContainer .WORKSPACE_SCOPE ;
113
127
}
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
+ }
119
139
120
- if (!canSearchEnclosingProjects && scope == ISearchPageContainer .SELECTED_PROJECTS_SCOPE )
140
+ if (!cansearchOpenedEditors && scope == ISearchPageContainer .OPENED_EDITORS_SCOPE ) {
121
141
scope = ISearchPageContainer .WORKSPACE_SCOPE ;
142
+ }
122
143
123
144
return scope ;
124
145
}
@@ -203,6 +224,43 @@ public static List<IResource> selectedResourcesFromContainer(ISearchPageContaine
203
224
return resources ;
204
225
}
205
226
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
+
206
264
private String getSelectedResurcesButtonText () {
207
265
int size = selectedResourcesFromContainer (fSearchDialog ).size ();
208
266
if (size == 1 ) {
@@ -242,11 +300,13 @@ public int getSelectedScope() {
242
300
* @param scope the scope to be selected in this part
243
301
*/
244
302
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 );
246
305
Assert .isNotNull (fUseWorkspace );
247
306
Assert .isNotNull (fUseSelection );
248
307
Assert .isNotNull (fUseWorkingSet );
249
308
Assert .isNotNull (fUseProject );
309
+ Assert .isNotNull (fUseOpenedEditors );
250
310
251
311
fSettingsStore .put (STORE_SCOPE , scope );
252
312
@@ -259,13 +319,22 @@ public void setSelectedScope(int scope) {
259
319
}
260
320
} else if (scope == ISearchPageContainer .SELECTION_SCOPE && !fUseSelection .isEnabled ()) {
261
321
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
+ }
262
330
}
263
331
fScope = scope ;
264
332
265
333
fUseWorkspace .setSelection (scope == ISearchPageContainer .WORKSPACE_SCOPE );
266
334
fUseSelection .setSelection (scope == ISearchPageContainer .SELECTION_SCOPE );
267
335
fUseProject .setSelection (scope == ISearchPageContainer .SELECTED_PROJECTS_SCOPE );
268
336
fUseWorkingSet .setSelection (scope == ISearchPageContainer .WORKING_SET_SCOPE );
337
+ fUseOpenedEditors .setSelection (scope == ISearchPageContainer .OPENED_EDITORS_SCOPE );
269
338
270
339
updateSearchPageContainerActionPerformedEnablement ();
271
340
@@ -276,7 +345,7 @@ public void setActiveEditorCanProvideScopeSelection(boolean state) {
276
345
fUseSelection .setEnabled (canSearchInSelection ());
277
346
278
347
// Reinitialize the controls
279
- fScope = getStoredScope (fSettingsStore , fCanSearchEnclosingProjects );
348
+ fScope = getStoredScope (fSettingsStore , fCanSearchEnclosingProjects , fCanSearchOpenedEditors );
280
349
setSelectedScope (fScope );
281
350
}
282
351
@@ -381,6 +450,16 @@ public Composite createPart(Composite parent) {
381
450
if (!fCanSearchEnclosingProjects )
382
451
fUseProject .setVisible (false );
383
452
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
+
384
463
fUseWorkingSet = new Button (fPart , SWT .RADIO );
385
464
fUseWorkingSet .setData (Integer .valueOf (ISearchPageContainer .WORKING_SET_SCOPE ));
386
465
fUseWorkingSet .setText (SearchMessages .ScopePart_workingSetScope_text );
@@ -421,6 +500,7 @@ public void widgetSelected(SelectionEvent e) {
421
500
fUseSelection .addSelectionListener (scopeChangedLister );
422
501
fUseProject .addSelectionListener (scopeChangedLister );
423
502
fUseWorkingSet .addSelectionListener (scopeChangedLister );
503
+ fUseOpenedEditors .addSelectionListener (scopeChangedLister );
424
504
425
505
// Set initial scope
426
506
setSelectedScope (fScope );
@@ -432,6 +512,10 @@ public void widgetSelected(SelectionEvent e) {
432
512
return fPart ;
433
513
}
434
514
515
+ private static IEditorReference [] getEditorReferences () {
516
+ return PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ().getEditorReferences ();
517
+ }
518
+
435
519
private boolean canSearchInSelection () {
436
520
ISelection selection = fSearchDialog .getSelection ();
437
521
return (selection instanceof IStructuredSelection ) && !selection .isEmpty ()
0 commit comments