implementation of find/replace, and bug fix for stuck 'open' button

This commit is contained in:
benfry
2003-09-22 05:30:55 +00:00
parent 658042614b
commit edf3744878
4 changed files with 226 additions and 10 deletions
+18 -2
View File
@@ -405,7 +405,23 @@ public class PdeBase extends Frame
menu.addSeparator();
item = new MenuItem("Find in Reference");
item = new MenuItem("Find...", new MenuShortcut('F'));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.find();
}
});
menu.add(item);
item = new MenuItem("Find Next", new MenuShortcut('G'));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.findNext();
}
});
menu.add(item);
item = new MenuItem("Find in Reference", new MenuShortcut('F', true));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (editor.textarea.isSelectionActive()) {
@@ -502,7 +518,7 @@ public class PdeBase extends Frame
menu = new Menu("Help");
menu.add(new MenuItem("Help"));
menu.add(new MenuItem("Reference", new MenuShortcut('F')));
menu.add(new MenuItem("Reference"));
menu.add(new MenuItem("Proce55ing.net", new MenuShortcut('5')));
// macosx already has its own about menu
+15
View File
@@ -1594,6 +1594,21 @@ public class PdeEditor extends JPanel {
}
PdeEditorFind find;
public void find() {
if (find == null) {
find = new PdeEditorFind(this);
} else {
find.show();
}
}
public void findNext() {
if (find != null) find.find();
}
public void doBeautify() {
String prog = textarea.getText();
makeHistory(prog, BEAUTIFY);
+11 -8
View File
@@ -24,7 +24,7 @@
*/
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
@@ -243,10 +243,12 @@ public class PdeEditorButtons extends JPanel implements MouseInputListener {
}
public void mouseMoved(MouseEvent e) {
//System.out.println(e);
mouseMove(e);
}
public void mouseDragged(MouseEvent e) {
//System.out.println(e);
//mouseMove(e);
}
@@ -364,8 +366,7 @@ public class PdeEditorButtons extends JPanel implements MouseInputListener {
if (currentSelection == OPEN) {
if (popup == null) {
//popup = new JPopupMenu();
popup = new PopupMenu();
popup = new PopupMenu();
add(popup);
}
//popup.addActionListener(this);
@@ -391,6 +392,8 @@ public class PdeEditorButtons extends JPanel implements MouseInputListener {
///public boolean mouseUp(Event e, int x, int y) {
public void mouseReleased(MouseEvent e) {
//System.out.println("mouse is released");
//switch (which[sel]) {
switch (currentSelection) {
@@ -414,17 +417,17 @@ public class PdeEditorButtons extends JPanel implements MouseInputListener {
//case CLOSE: editor.doClose(); break;
//case OPEN: editor.doOpen(); break;
/*
case OPEN:
System.err.println("popup mouseup");
case OPEN:
setState(OPEN, INACTIVE, true);
//System.err.println("popup mouseup");
//popup.setVisible(false);
remove(popup);
//remove(popup);
// kill the popup?
//PopupMenu popup = new PopupMenu();
//editor.base.rebuildSketchbookMenu(popup);
//popup.show(this, x, y);
break;
*/
//editor.doOpen(this, BUTTON_WIDTH, OPEN * BUTTON_HEIGHT);
case NEW: editor.skNew(); break;
+182
View File
@@ -0,0 +1,182 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
Find: [ ]
Replace with: [ ]
[ ] Ignore Case
[Replace All] [Replace] [Replace & Find] [Previous] [Next]
*/
public class PdeEditorFind extends JFrame implements ActionListener {
static final int BIG = 13;
static final int SMALL = 6;
PdeEditor editor;
JTextField findField;
JTextField replaceField;
JButton replaceButton;
JButton replaceAllButton;
//JButton replaceFindButton;
//JButton previousButton;
//JButton nextButton;
JButton findButton;
boolean found;
public PdeEditorFind(PdeEditor editor) {
super("Find");
setResizable(false);
this.editor = editor;
Container pain = getContentPane();
pain.setLayout(null);
JLabel findLabel = new JLabel("Find:");
Dimension d0 = findLabel.getPreferredSize();
JLabel replaceLabel = new JLabel("Replace with:");
Dimension d1 = replaceLabel.getPreferredSize();
pain.add(findLabel);
pain.add(replaceLabel);
pain.add(findField = new JTextField(20));
pain.add(replaceField = new JTextField(20));
Dimension d2 = findField.getPreferredSize();
// +1 since it's better to tend downwards
int yoff = (1 + d2.height - d1.height) / 2;
findLabel.setBounds(BIG + (d1.width-d0.width) + yoff, BIG,
d1.width, d1.height);
replaceLabel.setBounds(BIG, BIG + d2.height + SMALL + yoff,
d1.width, d1.height);
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout());
if ((PdeBase.platform == PdeBase.MACOSX) ||
(PdeBase.platform == PdeBase.MACOS9)) {
buttons.add(replaceButton = new JButton("Replace"));
buttons.add(replaceAllButton = new JButton("Replace All"));
buttons.add(findButton = new JButton("Find"));
} else {
buttons.add(findButton = new JButton("Find"));
buttons.add(replaceButton = new JButton("Replace"));
buttons.add(replaceAllButton = new JButton("Replace All"));
}
pain.add(buttons);
// to fix ugliness.. normally macosx java 1.3 puts an
// ugly white border around this object, so turn it off.
if (PdeBase.platform == PdeBase.MACOSX) {
buttons.setBorder(null);
}
//System.out.println(buttons.getPreferredSize());
Dimension d3 = buttons.getPreferredSize();
buttons.setBounds(BIG, BIG + d2.height*2 + SMALL + BIG,
d3.width, d3.height);
findField.setBounds(BIG + d1.width + SMALL, BIG,
d3.width - (d1.width + SMALL), d2.height);
replaceField.setBounds(BIG + d1.width + SMALL, BIG + d2.height + SMALL,
d3.width - (d1.width + SMALL), d2.height);
replaceButton.addActionListener(this);
replaceAllButton.addActionListener(this);
findButton.addActionListener(this);
// you mustn't replace what you haven't found, my son
replaceButton.setEnabled(false);
// so that typing will go straight to this field
findField.requestFocus();
// make the find button the blinky default
getRootPane().setDefaultButton(findButton);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int wide = d3.width + BIG*2;
int high = BIG + d2.height*2 + SMALL + BIG*3 + d3.height;
setBounds((screen.width - wide) / 2,
(screen.height - high) / 2, wide, high);
show();
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == findButton) {
find();
} else if (source == replaceButton) {
replace();
} else if (source == replaceAllButton) {
replaceAll();
}
}
// look for the next instance of the find string
// to be found later than the current caret selection
// once found, select it (and go to that line)
public void find() {
String search = findField.getText();
if (search.length() == 0) return;
String text = editor.textarea.getText();
search = search.toLowerCase();
text = text.toLowerCase(); // ignore case
//int selectionStart = editor.textarea.getSelectionStart();
int selectionEnd = editor.textarea.getSelectionEnd();
int nextIndex = text.indexOf(search, selectionEnd);
if (nextIndex == -1) {
nextIndex = text.indexOf(search, 0);
if (nextIndex == -1) { // wrapping
found = false;
replaceButton.setEnabled(false);
//Toolkit.getDefaultToolkit().beep();
return;
}
}
found = true;
replaceButton.setEnabled(true);
editor.textarea.select(nextIndex, nextIndex + search.length());
}
// replace the current selection with whatever's in the
// replacement text field
public void replace() {
//System.out.println("replace " + found);
if (!found) return; // don't replace if nothing found
editor.textarea.setSelectedText(replaceField.getText());
editor.setSketchModified(true);
}
// keep doing find and replace alternately until nothing more found
public void replaceAll() {
do {
find();
replace();
} while (found);
}
}