44
44
* <A HREF="http://dev.processing.org/bugs/show_bug.cgi?id=244"> Bug 244</A>
45
45
* should anyone have clues about how to fix.
46
46
*/
47
+ @ SuppressWarnings ("serial" )
47
48
public class FindReplace extends JFrame implements ActionListener {
48
49
49
50
static final int EDGE = Base .isMacOS () ? 20 : 13 ;
@@ -69,25 +70,41 @@ public class FindReplace extends JFrame implements ActionListener {
69
70
JCheckBox wrapAroundBox ;
70
71
static boolean wrapAround = true ;
71
72
73
+ JCheckBox searchAllFilesBox ;
74
+ static boolean searchAllFiles = false ;
75
+
72
76
public FindReplace (Editor editor ) {
73
77
super ("Find" );
74
78
setResizable (false );
75
79
this .editor = editor ;
76
-
77
- Container pain = getContentPane ();
78
- pain .setLayout (null );
80
+
81
+ FlowLayout searchLayout = new FlowLayout (FlowLayout .RIGHT ,5 ,0 );
82
+ Container pane = getContentPane ();
83
+ pane .setLayout (searchLayout );
79
84
80
85
JLabel findLabel = new JLabel (_ ("Find:" ));
81
86
JLabel replaceLabel = new JLabel (_ ("Replace with:" ));
82
87
Dimension labelDimension = replaceLabel .getPreferredSize ();
83
-
84
- pain .add (findLabel );
85
- pain .add (replaceLabel );
86
-
87
- pain .add (findField = new JTextField (20 ));
88
- pain .add (replaceField = new JTextField (20 ));
88
+
89
+ JPanel find = new JPanel ();
90
+ find .add (findLabel );
91
+ find .add (findField = new JTextField (20 ));
92
+ pane .add (find );
93
+
94
+ JPanel replace = new JPanel ();
95
+ replace .add (replaceLabel );
96
+ replace .add (replaceField = new JTextField (20 ));
97
+ pane .add (replace );
98
+
89
99
int fieldHeight = findField .getPreferredSize ().height ;
90
100
101
+ JPanel checkbox = new JPanel ();
102
+
103
+ // Fill the findString with selected text if no previous value
104
+ if (editor .getSelectedText () != null &&
105
+ editor .getSelectedText ().length () > 0 )
106
+ findString = editor .getSelectedText ();
107
+
91
108
if (findString != null ) findField .setText (findString );
92
109
if (replaceString != null ) replaceField .setText (replaceString );
93
110
//System.out.println("setting find str to " + findString);
@@ -100,7 +117,7 @@ public void actionPerformed(ActionEvent e) {
100
117
}
101
118
});
102
119
ignoreCaseBox .setSelected (ignoreCase );
103
- pain .add (ignoreCaseBox );
120
+ checkbox .add (ignoreCaseBox );
104
121
105
122
wrapAroundBox = new JCheckBox (_ ("Wrap Around" ));
106
123
wrapAroundBox .addActionListener (new ActionListener () {
@@ -109,11 +126,21 @@ public void actionPerformed(ActionEvent e) {
109
126
}
110
127
});
111
128
wrapAroundBox .setSelected (wrapAround );
112
- pain .add (wrapAroundBox );
129
+ checkbox .add (wrapAroundBox );
130
+
131
+ searchAllFilesBox = new JCheckBox (_ ("Search all Sketch Tabs" ));
132
+ searchAllFilesBox .addActionListener (new ActionListener () {
133
+ public void actionPerformed (ActionEvent e ) {
134
+ searchAllFiles = searchAllFilesBox .isSelected ();
135
+ }
136
+ });
137
+ searchAllFilesBox .setSelected (searchAllFiles );
138
+ checkbox .add (searchAllFilesBox );
113
139
114
- JPanel buttons = new JPanel ( );
140
+ pane . add ( checkbox );
115
141
116
- buttons .setLayout (new FlowLayout (FlowLayout .CENTER ,BUTTONGAP ,0 ));
142
+ JPanel buttons = new JPanel ();
143
+ buttons .setLayout (new FlowLayout (FlowLayout .CENTER , BUTTONGAP , 0 ));
117
144
118
145
// ordering is different on mac versus pc
119
146
if (Base .isMacOS ()) {
@@ -130,7 +157,7 @@ public void actionPerformed(ActionEvent e) {
130
157
buttons .add (replaceButton = new JButton (_ ("Replace" )));
131
158
buttons .add (replaceAllButton = new JButton (_ ("Replace All" )));
132
159
}
133
- pain .add (buttons );
160
+ pane .add (buttons );
134
161
135
162
// to fix ugliness.. normally macosx java 1.3 puts an
136
163
// ugly white border around this object, so turn it off.
@@ -180,9 +207,13 @@ public void focusLost(FocusEvent e) {
180
207
181
208
ignoreCaseBox .setBounds (EDGE + labelDimension .width + SMALL ,
182
209
ypos ,
183
- (fieldWidth -SMALL )/2 , fieldHeight );
210
+ (fieldWidth -SMALL )/4 , fieldHeight );
211
+
212
+ wrapAroundBox .setBounds (EDGE + labelDimension .width + SMALL + (fieldWidth -SMALL )/4 + SMALL ,
213
+ ypos ,
214
+ (fieldWidth -SMALL )/4 , fieldHeight );
184
215
185
- wrapAroundBox .setBounds (EDGE + labelDimension .width + SMALL + (fieldWidth -SMALL )/2 + SMALL ,
216
+ searchAllFilesBox .setBounds (EDGE + labelDimension .width + SMALL + (int )(( fieldWidth -SMALL )/1.9 ) + SMALL ,
186
217
ypos ,
187
218
(fieldWidth -SMALL )/2 , fieldHeight );
188
219
@@ -191,7 +222,7 @@ public void focusLost(FocusEvent e) {
191
222
buttons .setBounds (EDGE -BUTTONGAP , ypos ,
192
223
buttonsDimension .width , buttonsDimension .height );
193
224
194
- ypos += buttonsDimension .height + EDGE ;
225
+ ypos += buttonsDimension .height ;
195
226
196
227
// Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
197
228
@@ -291,8 +322,9 @@ public void actionPerformed(ActionEvent e) {
291
322
// look for the next instance of the find string to be found
292
323
// once found, select it (and go to that line)
293
324
294
- private boolean find (boolean wrap ,boolean backwards ) {
295
-
325
+ private boolean find (boolean wrap ,boolean backwards ,boolean searchTabs ,int originTab ) {
326
+ //System.out.println("Find: " + originTab);
327
+ boolean wrapNeeded = false ;
296
328
String search = findField .getText ();
297
329
//System.out.println("finding for " + search + " " + findString);
298
330
// this will catch "find next" being called when no search yet
@@ -313,7 +345,7 @@ private boolean find(boolean wrap,boolean backwards ) {
313
345
nextIndex = text .indexOf (search , selectionEnd );
314
346
if (wrap && nextIndex == -1 ) {
315
347
// if wrapping, a second chance is ok, start from beginning
316
- nextIndex = text . indexOf ( search , 0 ) ;
348
+ wrapNeeded = true ;
317
349
}
318
350
} else {
319
351
//int selectionStart = editor.textarea.getSelectionStart();
@@ -326,16 +358,53 @@ private boolean find(boolean wrap,boolean backwards ) {
326
358
}
327
359
if (wrap && nextIndex == -1 ) {
328
360
// if wrapping, a second chance is ok, start from the end
329
- nextIndex = text . lastIndexOf ( search ) ;
361
+ wrapNeeded = true ;
330
362
}
331
363
}
332
364
333
- if (nextIndex != -1 ) {
334
- editor .setSelection (nextIndex , nextIndex + search .length ());
335
- } else {
336
- //Toolkit.getDefaultToolkit().beep();
365
+ if (nextIndex == -1 ) {
366
+ // Nothing found on this tab: Search other tabs if required
367
+ if (searchTabs ) {
368
+ // editor.
369
+ Sketch sketch = editor .getSketch ();
370
+ if (sketch .getCodeCount () > 1 ) {
371
+ int realCurrentTab = sketch .getCodeIndex (sketch .getCurrentCode ());
372
+
373
+ if (originTab != realCurrentTab ) {
374
+ if (originTab < 0 )
375
+ originTab = realCurrentTab ;
376
+
377
+ if (!wrap )
378
+ if ((!backwards && realCurrentTab + 1 >= sketch .getCodeCount ()) ||
379
+ (backwards && realCurrentTab - 1 < 0 ))
380
+ return false ; // Can't continue without wrap
381
+
382
+ if (backwards ) {
383
+ sketch .handlePrevCode ();
384
+ this .setVisible (true );
385
+ int l = editor .getText ().length () - 1 ;
386
+ editor .setSelection (l , l );
387
+ } else {
388
+ sketch .handleNextCode ();
389
+ this .setVisible (true );
390
+ editor .setSelection (0 , 0 );
391
+ }
392
+
393
+ return find (wrap , backwards , searchTabs , originTab );
394
+ }
395
+ }
396
+ }
397
+
398
+ if (wrapNeeded )
399
+ nextIndex = backwards ? text .lastIndexOf (search ) : text .indexOf (search , 0 );
337
400
}
338
- return nextIndex != -1 ;
401
+
402
+ if (nextIndex != -1 ) {
403
+ editor .setSelection (nextIndex , nextIndex + search .length ());
404
+ return true ;
405
+ }
406
+
407
+ return false ;
339
408
}
340
409
341
410
@@ -344,8 +413,26 @@ private boolean find(boolean wrap,boolean backwards ) {
344
413
* replacement text field.
345
414
*/
346
415
public void replace () {
347
- editor .setSelectedText (replaceField .getText ());
348
- editor .getSketch ().setModified (true ); // TODO is this necessary?
416
+ if (findField .getText ().length () == 0 )
417
+ return ;
418
+
419
+ int newpos = editor .getSelectionStart () - findField .getText ().length ();
420
+ if (newpos < 0 )
421
+ newpos = 0 ;
422
+ editor .setSelection (newpos , newpos );
423
+
424
+ boolean foundAtLeastOne = false ;
425
+
426
+ if (find (false , false , searchAllFiles , -1 )) {
427
+ foundAtLeastOne = true ;
428
+ editor .setSelectedText (replaceField .getText ());
429
+ editor .getSketch ().setModified (true ); // TODO is this necessary?
430
+ }
431
+
432
+ if (!foundAtLeastOne ) {
433
+ Toolkit .getDefaultToolkit ().beep ();
434
+ }
435
+
349
436
}
350
437
351
438
/**
@@ -362,19 +449,22 @@ public void replaceAndFindNext() {
362
449
* alternately until nothing more found.
363
450
*/
364
451
public void replaceAll () {
452
+ if (findField .getText ().length () == 0 )
453
+ return ;
365
454
// move to the beginning
366
455
editor .setSelection (0 , 0 );
367
456
368
457
boolean foundAtLeastOne = false ;
369
- while ( true ) {
370
- if ( find (false ,false ) ) {
458
+ while (true ) {
459
+ if (find (false , false , searchAllFiles , - 1 ) ) {
371
460
foundAtLeastOne = true ;
372
- replace ();
373
- } else {
461
+ editor .setSelectedText (replaceField .getText ());
462
+ editor .getSketch ().setModified (true ); // TODO is this necessary?
463
+ } else {
374
464
break ;
375
465
}
376
466
}
377
- if ( !foundAtLeastOne ) {
467
+ if (!foundAtLeastOne ) {
378
468
Toolkit .getDefaultToolkit ().beep ();
379
469
}
380
470
}
@@ -385,13 +475,13 @@ public void setFindText( String t ) {
385
475
}
386
476
387
477
public void findNext () {
388
- if ( !find ( wrapAround , false ) ) {
478
+ if ( !find ( wrapAround , false , searchAllFiles ,- 1 ) ) {
389
479
Toolkit .getDefaultToolkit ().beep ();
390
480
}
391
481
}
392
482
393
483
public void findPrevious () {
394
- if ( !find ( wrapAround , true ) ) {
484
+ if ( !find ( wrapAround , true , searchAllFiles ,- 1 ) ) {
395
485
Toolkit .getDefaultToolkit ().beep ();
396
486
}
397
487
}
0 commit comments