@@ -69,6 +69,9 @@ public class FindReplace extends JFrame implements ActionListener {
69
69
JCheckBox wrapAroundBox ;
70
70
static boolean wrapAround = true ;
71
71
72
+ JCheckBox searchAllFilesBox ;
73
+ static boolean searchAllFiles = false ;
74
+
72
75
public FindReplace (Editor editor ) {
73
76
super ("Find" );
74
77
setResizable (false );
@@ -88,6 +91,10 @@ public FindReplace(Editor editor) {
88
91
pain .add (replaceField = new JTextField (20 ));
89
92
int fieldHeight = findField .getPreferredSize ().height ;
90
93
94
+ // Fill the findString with selected text if no previous value
95
+ if (editor .getSelectedText ()!=null && editor .getSelectedText ().length ()>0 )
96
+ findString = editor .getSelectedText ();
97
+
91
98
if (findString != null ) findField .setText (findString );
92
99
if (replaceString != null ) replaceField .setText (replaceString );
93
100
//System.out.println("setting find str to " + findString);
@@ -110,6 +117,15 @@ public void actionPerformed(ActionEvent e) {
110
117
});
111
118
wrapAroundBox .setSelected (wrapAround );
112
119
pain .add (wrapAroundBox );
120
+
121
+ searchAllFilesBox = new JCheckBox (_ ("Search all Sketch Tabs" ));
122
+ searchAllFilesBox .addActionListener (new ActionListener () {
123
+ public void actionPerformed (ActionEvent e ) {
124
+ searchAllFiles = searchAllFilesBox .isSelected ();
125
+ }
126
+ });
127
+ searchAllFilesBox .setSelected (searchAllFiles );
128
+ pain .add (searchAllFilesBox );
113
129
114
130
JPanel buttons = new JPanel ();
115
131
@@ -180,9 +196,13 @@ public void focusLost(FocusEvent e) {
180
196
181
197
ignoreCaseBox .setBounds (EDGE + labelDimension .width + SMALL ,
182
198
ypos ,
183
- (fieldWidth -SMALL )/2 , fieldHeight );
199
+ (fieldWidth -SMALL )/4 , fieldHeight );
184
200
185
- wrapAroundBox .setBounds (EDGE + labelDimension .width + SMALL + (fieldWidth -SMALL )/2 + SMALL ,
201
+ wrapAroundBox .setBounds (EDGE + labelDimension .width + SMALL + (fieldWidth -SMALL )/4 + SMALL ,
202
+ ypos ,
203
+ (fieldWidth -SMALL )/4 , fieldHeight );
204
+
205
+ searchAllFilesBox .setBounds (EDGE + labelDimension .width + SMALL + (int )((fieldWidth -SMALL )/1.9 ) + SMALL ,
186
206
ypos ,
187
207
(fieldWidth -SMALL )/2 , fieldHeight );
188
208
@@ -291,8 +311,9 @@ public void actionPerformed(ActionEvent e) {
291
311
// look for the next instance of the find string to be found
292
312
// once found, select it (and go to that line)
293
313
294
- private boolean find (boolean wrap ,boolean backwards ) {
295
-
314
+ private boolean find (boolean wrap ,boolean backwards ,boolean searchTabs ,int originTab ) {
315
+ //System.out.println("Find: " + originTab);
316
+ boolean wrapNeeded = false ;
296
317
String search = findField .getText ();
297
318
//System.out.println("finding for " + search + " " + findString);
298
319
// this will catch "find next" being called when no search yet
@@ -313,7 +334,7 @@ private boolean find(boolean wrap,boolean backwards ) {
313
334
nextIndex = text .indexOf (search , selectionEnd );
314
335
if (wrap && nextIndex == -1 ) {
315
336
// if wrapping, a second chance is ok, start from beginning
316
- nextIndex = text . indexOf ( search , 0 ) ;
337
+ wrapNeeded = true ;
317
338
}
318
339
} else {
319
340
//int selectionStart = editor.textarea.getSelectionStart();
@@ -326,16 +347,58 @@ private boolean find(boolean wrap,boolean backwards ) {
326
347
}
327
348
if (wrap && nextIndex == -1 ) {
328
349
// if wrapping, a second chance is ok, start from the end
329
- nextIndex = text . lastIndexOf ( search ) ;
350
+ wrapNeeded = true ;
330
351
}
331
352
}
332
353
333
- if (nextIndex != -1 ) {
334
- editor .setSelection (nextIndex , nextIndex + search .length ());
335
- } else {
336
- //Toolkit.getDefaultToolkit().beep();
354
+ if (nextIndex == -1 ) {
355
+ //Nothing found on this tab: Search other tabs if required
356
+ if (searchTabs )
357
+ {
358
+ //editor.
359
+ Sketch sketch = editor .getSketch ();
360
+ if (sketch .getCodeCount ()>1 )
361
+ {
362
+ int realCurrentTab = sketch .getCodeIndex (sketch .getCurrentCode ());
363
+
364
+ if (originTab !=realCurrentTab )
365
+ {
366
+ if (originTab <0 )
367
+ originTab = realCurrentTab ;
368
+
369
+ if (!wrap )
370
+ if ((!backwards && realCurrentTab +1 >= sketch .getCodeCount ()) || (backwards && realCurrentTab -1 < 0 ))
371
+ return false ; // Can't continue without wrap
372
+
373
+ if (backwards )
374
+ {
375
+ sketch .handlePrevCode ();
376
+ this .setVisible (true );
377
+ int l = editor .getText ().length ()-1 ;
378
+ editor .setSelection (l ,l );
379
+ }
380
+ else
381
+ {
382
+ sketch .handleNextCode ();
383
+ this .setVisible (true );
384
+ editor .setSelection (0 ,0 );
385
+ }
386
+
387
+ return find (wrap ,backwards ,searchTabs ,originTab );
388
+ }
389
+ }
390
+ }
391
+
392
+ if (wrapNeeded )
393
+ nextIndex = backwards ? text .lastIndexOf (search ):text .indexOf (search , 0 );
337
394
}
338
- return nextIndex != -1 ;
395
+
396
+ if (nextIndex != -1 ) {
397
+ editor .setSelection (nextIndex , nextIndex + search .length ());
398
+ return true ;
399
+ }
400
+
401
+ return false ;
339
402
}
340
403
341
404
@@ -344,6 +407,8 @@ private boolean find(boolean wrap,boolean backwards ) {
344
407
* replacement text field.
345
408
*/
346
409
public void replace () {
410
+ if (findField .getText ().length ()==0 )
411
+ return ;
347
412
editor .setSelectedText (replaceField .getText ());
348
413
editor .getSketch ().setModified (true ); // TODO is this necessary?
349
414
}
@@ -362,12 +427,14 @@ public void replaceAndFindNext() {
362
427
* alternately until nothing more found.
363
428
*/
364
429
public void replaceAll () {
430
+ if (findField .getText ().length ()==0 )
431
+ return ;
365
432
// move to the beginning
366
433
editor .setSelection (0 , 0 );
367
434
368
435
boolean foundAtLeastOne = false ;
369
436
while ( true ) {
370
- if ( find (false ,false ) ) {
437
+ if ( find (false ,false , searchAllFiles ,- 1 ) ) {
371
438
foundAtLeastOne = true ;
372
439
replace ();
373
440
} else {
@@ -385,13 +452,13 @@ public void setFindText( String t ) {
385
452
}
386
453
387
454
public void findNext () {
388
- if ( !find ( wrapAround , false ) ) {
455
+ if ( !find ( wrapAround , false , searchAllFiles ,- 1 ) ) {
389
456
Toolkit .getDefaultToolkit ().beep ();
390
457
}
391
458
}
392
459
393
460
public void findPrevious () {
394
- if ( !find ( wrapAround , true ) ) {
461
+ if ( !find ( wrapAround , true , searchAllFiles ,- 1 ) ) {
395
462
Toolkit .getDefaultToolkit ().beep ();
396
463
}
397
464
}
0 commit comments