Skip to content

Commit 401f9e1

Browse files
committed
FindReplace dialog now search and replaces in all the opened tabs. Also pre-fill the find field with selected text
1 parent 0aa9590 commit 401f9e1

File tree

1 file changed

+81
-14
lines changed

1 file changed

+81
-14
lines changed

app/src/processing/app/FindReplace.java

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public class FindReplace extends JFrame implements ActionListener {
6969
JCheckBox wrapAroundBox;
7070
static boolean wrapAround = true;
7171

72+
JCheckBox searchAllFilesBox;
73+
static boolean searchAllFiles = false;
74+
7275
public FindReplace(Editor editor) {
7376
super("Find");
7477
setResizable(false);
@@ -88,6 +91,10 @@ public FindReplace(Editor editor) {
8891
pain.add(replaceField = new JTextField(20));
8992
int fieldHeight = findField.getPreferredSize().height;
9093

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+
9198
if (findString != null) findField.setText(findString);
9299
if (replaceString != null) replaceField.setText(replaceString);
93100
//System.out.println("setting find str to " + findString);
@@ -110,6 +117,15 @@ public void actionPerformed(ActionEvent e) {
110117
});
111118
wrapAroundBox.setSelected(wrapAround);
112119
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);
113129

114130
JPanel buttons = new JPanel();
115131

@@ -180,9 +196,13 @@ public void focusLost(FocusEvent e) {
180196

181197
ignoreCaseBox.setBounds(EDGE + labelDimension.width + SMALL,
182198
ypos,
183-
(fieldWidth-SMALL)/2, fieldHeight);
199+
(fieldWidth-SMALL)/4, fieldHeight);
184200

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,
186206
ypos,
187207
(fieldWidth-SMALL)/2, fieldHeight);
188208

@@ -291,8 +311,9 @@ public void actionPerformed(ActionEvent e) {
291311
// look for the next instance of the find string to be found
292312
// once found, select it (and go to that line)
293313

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;
296317
String search = findField.getText();
297318
//System.out.println("finding for " + search + " " + findString);
298319
// this will catch "find next" being called when no search yet
@@ -313,7 +334,7 @@ private boolean find(boolean wrap,boolean backwards ) {
313334
nextIndex = text.indexOf(search, selectionEnd);
314335
if (wrap && nextIndex == -1) {
315336
// if wrapping, a second chance is ok, start from beginning
316-
nextIndex = text.indexOf(search, 0);
337+
wrapNeeded = true;
317338
}
318339
} else {
319340
//int selectionStart = editor.textarea.getSelectionStart();
@@ -326,16 +347,58 @@ private boolean find(boolean wrap,boolean backwards ) {
326347
}
327348
if (wrap && nextIndex == -1) {
328349
// if wrapping, a second chance is ok, start from the end
329-
nextIndex = text.lastIndexOf(search);
350+
wrapNeeded = true;
330351
}
331352
}
332353

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);
337394
}
338-
return nextIndex != -1;
395+
396+
if (nextIndex != -1) {
397+
editor.setSelection(nextIndex, nextIndex + search.length());
398+
return true;
399+
}
400+
401+
return false;
339402
}
340403

341404

@@ -344,6 +407,8 @@ private boolean find(boolean wrap,boolean backwards ) {
344407
* replacement text field.
345408
*/
346409
public void replace() {
410+
if(findField.getText().length()==0)
411+
return;
347412
editor.setSelectedText(replaceField.getText());
348413
editor.getSketch().setModified(true); // TODO is this necessary?
349414
}
@@ -362,12 +427,14 @@ public void replaceAndFindNext() {
362427
* alternately until nothing more found.
363428
*/
364429
public void replaceAll() {
430+
if(findField.getText().length()==0)
431+
return;
365432
// move to the beginning
366433
editor.setSelection(0, 0);
367434

368435
boolean foundAtLeastOne = false;
369436
while ( true ) {
370-
if ( find(false,false) ) {
437+
if ( find(false,false,searchAllFiles,-1)) {
371438
foundAtLeastOne = true;
372439
replace();
373440
} else {
@@ -385,13 +452,13 @@ public void setFindText( String t ) {
385452
}
386453

387454
public void findNext() {
388-
if ( !find( wrapAround, false ) ) {
455+
if ( !find( wrapAround, false, searchAllFiles,-1 ) ) {
389456
Toolkit.getDefaultToolkit().beep();
390457
}
391458
}
392459

393460
public void findPrevious() {
394-
if ( !find( wrapAround, true ) ) {
461+
if ( !find( wrapAround, true, searchAllFiles,-1 ) ) {
395462
Toolkit.getDefaultToolkit().beep();
396463
}
397464
}

0 commit comments

Comments
 (0)