1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

FindReplace dialog now search and replaces in all the opened tabs. Also pre-fill the find field with selected text

This commit is contained in:
Erwin Ried 2014-05-13 03:21:36 -04:00
parent 0aa9590b92
commit 401f9e1634

View File

@ -69,6 +69,9 @@ public class FindReplace extends JFrame implements ActionListener {
JCheckBox wrapAroundBox;
static boolean wrapAround = true;
JCheckBox searchAllFilesBox;
static boolean searchAllFiles = false;
public FindReplace(Editor editor) {
super("Find");
setResizable(false);
@ -88,6 +91,10 @@ public class FindReplace extends JFrame implements ActionListener {
pain.add(replaceField = new JTextField(20));
int fieldHeight = findField.getPreferredSize().height;
// Fill the findString with selected text if no previous value
if(editor.getSelectedText()!=null && editor.getSelectedText().length()>0)
findString = editor.getSelectedText();
if (findString != null) findField.setText(findString);
if (replaceString != null) replaceField.setText(replaceString);
//System.out.println("setting find str to " + findString);
@ -110,6 +117,15 @@ public class FindReplace extends JFrame implements ActionListener {
});
wrapAroundBox.setSelected(wrapAround);
pain.add(wrapAroundBox);
searchAllFilesBox = new JCheckBox(_("Search all Sketch Tabs"));
searchAllFilesBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchAllFiles = searchAllFilesBox.isSelected();
}
});
searchAllFilesBox.setSelected(searchAllFiles);
pain.add(searchAllFilesBox);
JPanel buttons = new JPanel();
@ -180,9 +196,13 @@ public class FindReplace extends JFrame implements ActionListener {
ignoreCaseBox.setBounds(EDGE + labelDimension.width + SMALL,
ypos,
(fieldWidth-SMALL)/2, fieldHeight);
(fieldWidth-SMALL)/4, fieldHeight);
wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + (fieldWidth-SMALL)/2 + SMALL,
wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + (fieldWidth-SMALL)/4 + SMALL,
ypos,
(fieldWidth-SMALL)/4, fieldHeight);
searchAllFilesBox.setBounds(EDGE + labelDimension.width + SMALL + (int)((fieldWidth-SMALL)/1.9) + SMALL,
ypos,
(fieldWidth-SMALL)/2, fieldHeight);
@ -291,8 +311,9 @@ public class FindReplace extends JFrame implements ActionListener {
// look for the next instance of the find string to be found
// once found, select it (and go to that line)
private boolean find(boolean wrap,boolean backwards ) {
private boolean find(boolean wrap,boolean backwards,boolean searchTabs,int originTab) {
//System.out.println("Find: " + originTab);
boolean wrapNeeded = false;
String search = findField.getText();
//System.out.println("finding for " + search + " " + findString);
// this will catch "find next" being called when no search yet
@ -313,7 +334,7 @@ public class FindReplace extends JFrame implements ActionListener {
nextIndex = text.indexOf(search, selectionEnd);
if (wrap && nextIndex == -1) {
// if wrapping, a second chance is ok, start from beginning
nextIndex = text.indexOf(search, 0);
wrapNeeded = true;
}
} else {
//int selectionStart = editor.textarea.getSelectionStart();
@ -326,16 +347,58 @@ public class FindReplace extends JFrame implements ActionListener {
}
if (wrap && nextIndex == -1) {
// if wrapping, a second chance is ok, start from the end
nextIndex = text.lastIndexOf(search);
wrapNeeded = true;
}
}
if (nextIndex != -1) {
editor.setSelection(nextIndex, nextIndex + search.length());
} else {
//Toolkit.getDefaultToolkit().beep();
if (nextIndex == -1) {
//Nothing found on this tab: Search other tabs if required
if(searchTabs)
{
//editor.
Sketch sketch = editor.getSketch();
if(sketch.getCodeCount()>1)
{
int realCurrentTab = sketch.getCodeIndex(sketch.getCurrentCode());
if(originTab!=realCurrentTab)
{
if(originTab <0)
originTab = realCurrentTab;
if(!wrap)
if((!backwards && realCurrentTab+1 >= sketch.getCodeCount()) || (backwards && realCurrentTab-1 < 0))
return false; // Can't continue without wrap
if(backwards)
{
sketch.handlePrevCode();
this.setVisible(true);
int l = editor.getText().length()-1;
editor.setSelection(l,l);
}
else
{
sketch.handleNextCode();
this.setVisible(true);
editor.setSelection(0,0);
}
return find(wrap,backwards,searchTabs,originTab);
}
}
}
if(wrapNeeded)
nextIndex = backwards? text.lastIndexOf(search):text.indexOf(search, 0);
}
return nextIndex != -1;
if (nextIndex != -1) {
editor.setSelection(nextIndex, nextIndex + search.length());
return true;
}
return false;
}
@ -344,6 +407,8 @@ public class FindReplace extends JFrame implements ActionListener {
* replacement text field.
*/
public void replace() {
if(findField.getText().length()==0)
return;
editor.setSelectedText(replaceField.getText());
editor.getSketch().setModified(true); // TODO is this necessary?
}
@ -362,12 +427,14 @@ public class FindReplace extends JFrame implements ActionListener {
* alternately until nothing more found.
*/
public void replaceAll() {
if(findField.getText().length()==0)
return;
// move to the beginning
editor.setSelection(0, 0);
boolean foundAtLeastOne = false;
while ( true ) {
if ( find(false,false) ) {
if ( find(false,false,searchAllFiles,-1)) {
foundAtLeastOne = true;
replace();
} else {
@ -385,13 +452,13 @@ public class FindReplace extends JFrame implements ActionListener {
}
public void findNext() {
if ( !find( wrapAround, false ) ) {
if ( !find( wrapAround, false, searchAllFiles,-1 ) ) {
Toolkit.getDefaultToolkit().beep();
}
}
public void findPrevious() {
if ( !find( wrapAround, true ) ) {
if ( !find( wrapAround, true, searchAllFiles,-1 ) ) {
Toolkit.getDefaultToolkit().beep();
}
}