From 401f9e16340d8892a3a95b1a825c9bfc5d62110d Mon Sep 17 00:00:00 2001 From: Erwin Ried Date: Tue, 13 May 2014 03:21:36 -0400 Subject: [PATCH 1/5] FindReplace dialog now search and replaces in all the opened tabs. Also pre-fill the find field with selected text --- app/src/processing/app/FindReplace.java | 95 +++++++++++++++++++++---- 1 file changed, 81 insertions(+), 14 deletions(-) diff --git a/app/src/processing/app/FindReplace.java b/app/src/processing/app/FindReplace.java index a8b79352d..bf0147fd0 100644 --- a/app/src/processing/app/FindReplace.java +++ b/app/src/processing/app/FindReplace.java @@ -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(); } } From 13321e3c1b32ee9027086b3f9fa650bfcbe43e0e Mon Sep 17 00:00:00 2001 From: Fulvio Ieva Date: Thu, 17 Jul 2014 13:41:48 +0200 Subject: [PATCH 2/5] Change layout in Find/Replace dialog to render correctly on Linux see #2070 --- app/src/processing/app/FindReplace.java | 37 ++++++++++++++++++------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/app/src/processing/app/FindReplace.java b/app/src/processing/app/FindReplace.java index bf0147fd0..137018b90 100644 --- a/app/src/processing/app/FindReplace.java +++ b/app/src/processing/app/FindReplace.java @@ -77,20 +77,35 @@ public class FindReplace extends JFrame implements ActionListener { setResizable(false); this.editor = editor; + + FlowLayout searchLayout = new FlowLayout(FlowLayout.RIGHT,5,0); Container pain = getContentPane(); - pain.setLayout(null); + pain.setLayout(searchLayout); JLabel findLabel = new JLabel(_("Find:")); JLabel replaceLabel = new JLabel(_("Replace with:")); Dimension labelDimension = replaceLabel.getPreferredSize(); + + JPanel find = new JPanel(); - pain.add(findLabel); - pain.add(replaceLabel); + find.add(findLabel); + + find.add(findField = new JTextField(20)); + + pain.add(find); + + JPanel replace = new JPanel(); + + replace.add(replaceLabel); - pain.add(findField = new JTextField(20)); - pain.add(replaceField = new JTextField(20)); + replace.add(replaceField = new JTextField(20)); + + pain.add(replace); + int fieldHeight = findField.getPreferredSize().height; + JPanel checkbox = new JPanel(); + // Fill the findString with selected text if no previous value if(editor.getSelectedText()!=null && editor.getSelectedText().length()>0) findString = editor.getSelectedText(); @@ -107,7 +122,7 @@ public class FindReplace extends JFrame implements ActionListener { } }); ignoreCaseBox.setSelected(ignoreCase); - pain.add(ignoreCaseBox); + checkbox.add(ignoreCaseBox); wrapAroundBox = new JCheckBox(_("Wrap Around")); wrapAroundBox.addActionListener(new ActionListener() { @@ -116,7 +131,7 @@ public class FindReplace extends JFrame implements ActionListener { } }); wrapAroundBox.setSelected(wrapAround); - pain.add(wrapAroundBox); + checkbox.add(wrapAroundBox); searchAllFilesBox = new JCheckBox(_("Search all Sketch Tabs")); searchAllFilesBox.addActionListener(new ActionListener() { @@ -125,11 +140,13 @@ public class FindReplace extends JFrame implements ActionListener { } }); searchAllFilesBox.setSelected(searchAllFiles); - pain.add(searchAllFilesBox); + checkbox.add(searchAllFilesBox); + pain.add(checkbox); + JPanel buttons = new JPanel(); - buttons.setLayout(new FlowLayout(FlowLayout.CENTER,BUTTONGAP,0)); + buttons.setLayout(new FlowLayout(FlowLayout.CENTER,BUTTONGAP,0)); // ordering is different on mac versus pc if (Base.isMacOS()) { @@ -211,7 +228,7 @@ public class FindReplace extends JFrame implements ActionListener { buttons.setBounds(EDGE-BUTTONGAP, ypos, buttonsDimension.width, buttonsDimension.height); - ypos += buttonsDimension.height + EDGE; + ypos += buttonsDimension.height; // Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); From 6efbecd3cc586a96766c8e4e3675ac749bc9a680 Mon Sep 17 00:00:00 2001 From: Fulvio Ieva Date: Tue, 22 Jul 2014 22:06:39 +0200 Subject: [PATCH 3/5] Do not replace textbox in Find/Replace dialog if no text is selected See #2070 --- app/src/processing/app/Editor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 278b54840..2138974d3 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -1236,6 +1236,7 @@ public class Editor extends JFrame implements RunnerListener { if (find == null) { find = new FindReplace(Editor.this); } + if (getSelectedText()!= null) find.setFindText( getSelectedText() ); //new FindReplace(Editor.this).show(); find.setVisible(true); //find.setVisible(true); From cd75cc24a2774a98dc11d7fc501c55b8bf2a6a1e Mon Sep 17 00:00:00 2001 From: Fulvio Ieva Date: Thu, 24 Jul 2014 10:52:56 +0200 Subject: [PATCH 4/5] Fix search and replace #2106 --- app/src/processing/app/FindReplace.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app/src/processing/app/FindReplace.java b/app/src/processing/app/FindReplace.java index 137018b90..a792b935f 100644 --- a/app/src/processing/app/FindReplace.java +++ b/app/src/processing/app/FindReplace.java @@ -426,8 +426,23 @@ public class FindReplace extends JFrame implements ActionListener { public void replace() { if(findField.getText().length()==0) return; - editor.setSelectedText(replaceField.getText()); - editor.getSketch().setModified(true); // TODO is this necessary? + + int newpos = editor.getSelectionStart() - findField.getText().length(); + if (newpos < 0) newpos = 0; + editor.setSelection(newpos, newpos); + + boolean foundAtLeastOne = false; + + if ( find(false,false,searchAllFiles,-1)) { + foundAtLeastOne = true; + editor.setSelectedText(replaceField.getText()); + editor.getSketch().setModified(true); // TODO is this necessary? + } + + if ( !foundAtLeastOne ) { + Toolkit.getDefaultToolkit().beep(); + } + } /** @@ -453,7 +468,8 @@ public class FindReplace extends JFrame implements ActionListener { while ( true ) { if ( find(false,false,searchAllFiles,-1)) { foundAtLeastOne = true; - replace(); + editor.setSelectedText(replaceField.getText()); + editor.getSketch().setModified(true); // TODO is this necessary? } else { break; } From 82401c84bb89948e7267a0533b267b7f53c974a4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 25 Jul 2014 12:10:42 +0200 Subject: [PATCH 5/5] Fix indent and typos on FindReplace.java --- app/src/processing/app/FindReplace.java | 138 +++++++++++------------- 1 file changed, 64 insertions(+), 74 deletions(-) diff --git a/app/src/processing/app/FindReplace.java b/app/src/processing/app/FindReplace.java index a792b935f..66bbb5a22 100644 --- a/app/src/processing/app/FindReplace.java +++ b/app/src/processing/app/FindReplace.java @@ -44,6 +44,7 @@ import javax.swing.*; * Bug 244 * should anyone have clues about how to fix. */ +@SuppressWarnings("serial") public class FindReplace extends JFrame implements ActionListener { static final int EDGE = Base.isMacOS() ? 20 : 13; @@ -76,39 +77,33 @@ public class FindReplace extends JFrame implements ActionListener { super("Find"); setResizable(false); this.editor = editor; - FlowLayout searchLayout = new FlowLayout(FlowLayout.RIGHT,5,0); - Container pain = getContentPane(); - pain.setLayout(searchLayout); + Container pane = getContentPane(); + pane.setLayout(searchLayout); JLabel findLabel = new JLabel(_("Find:")); JLabel replaceLabel = new JLabel(_("Replace with:")); Dimension labelDimension = replaceLabel.getPreferredSize(); JPanel find = new JPanel(); - find.add(findLabel); - find.add(findField = new JTextField(20)); - - pain.add(find); + pane.add(find); JPanel replace = new JPanel(); - replace.add(replaceLabel); - replace.add(replaceField = new JTextField(20)); - - pain.add(replace); + pane.add(replace); int fieldHeight = findField.getPreferredSize().height; JPanel checkbox = new JPanel(); // Fill the findString with selected text if no previous value - if(editor.getSelectedText()!=null && editor.getSelectedText().length()>0) - findString = editor.getSelectedText(); + if (editor.getSelectedText() != null && + editor.getSelectedText().length() > 0) + findString = editor.getSelectedText(); if (findString != null) findField.setText(findString); if (replaceString != null) replaceField.setText(replaceString); @@ -142,11 +137,10 @@ public class FindReplace extends JFrame implements ActionListener { searchAllFilesBox.setSelected(searchAllFiles); checkbox.add(searchAllFilesBox); - pain.add(checkbox); + pane.add(checkbox); JPanel buttons = new JPanel(); - - buttons.setLayout(new FlowLayout(FlowLayout.CENTER,BUTTONGAP,0)); + buttons.setLayout(new FlowLayout(FlowLayout.CENTER, BUTTONGAP, 0)); // ordering is different on mac versus pc if (Base.isMacOS()) { @@ -163,7 +157,7 @@ public class FindReplace extends JFrame implements ActionListener { buttons.add(replaceButton = new JButton(_("Replace"))); buttons.add(replaceAllButton = new JButton(_("Replace All"))); } - pain.add(buttons); + pane.add(buttons); // to fix ugliness.. normally macosx java 1.3 puts an // ugly white border around this object, so turn it off. @@ -369,45 +363,40 @@ public class FindReplace extends JFrame implements ActionListener { } 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; + // 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(!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 (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); + if (wrapNeeded) + nextIndex = backwards ? text.lastIndexOf(search) : text.indexOf(search, 0); } if (nextIndex != -1) { @@ -424,24 +413,25 @@ public class FindReplace extends JFrame implements ActionListener { * replacement text field. */ public void replace() { - if(findField.getText().length()==0) - return; - - int newpos = editor.getSelectionStart() - findField.getText().length(); - if (newpos < 0) newpos = 0; - editor.setSelection(newpos, newpos); + if (findField.getText().length() == 0) + return; + + int newpos = editor.getSelectionStart() - findField.getText().length(); + if (newpos < 0) + newpos = 0; + editor.setSelection(newpos, newpos); boolean foundAtLeastOne = false; - if ( find(false,false,searchAllFiles,-1)) { - foundAtLeastOne = true; - editor.setSelectedText(replaceField.getText()); - editor.getSketch().setModified(true); // TODO is this necessary? - } - - if ( !foundAtLeastOne ) { + if (find(false, false, searchAllFiles, -1)) { + foundAtLeastOne = true; + editor.setSelectedText(replaceField.getText()); + editor.getSketch().setModified(true); // TODO is this necessary? + } + + if (!foundAtLeastOne) { Toolkit.getDefaultToolkit().beep(); - } + } } @@ -459,22 +449,22 @@ public class FindReplace extends JFrame implements ActionListener { * alternately until nothing more found. */ public void replaceAll() { - if(findField.getText().length()==0) - return; + if (findField.getText().length() == 0) + return; // move to the beginning editor.setSelection(0, 0); boolean foundAtLeastOne = false; - while ( true ) { - if ( find(false,false,searchAllFiles,-1)) { + while (true) { + if (find(false, false, searchAllFiles, -1)) { foundAtLeastOne = true; editor.setSelectedText(replaceField.getText()); - editor.getSketch().setModified(true); // TODO is this necessary? - } else { + editor.getSketch().setModified(true); // TODO is this necessary? + } else { break; } } - if ( !foundAtLeastOne ) { + if (!foundAtLeastOne) { Toolkit.getDefaultToolkit().beep(); } }