package org.myrobotlab.control.widget; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JToolBar; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rtextarea.SearchContext; import org.fife.ui.rtextarea.SearchEngine; public class FindAndReplaceDialog extends JDialog implements ActionListener { private static final long serialVersionUID = 1L; private JTextField searchField; private JCheckBox regexCB; private JCheckBox matchCaseCB; private RSyntaxTextArea textArea; private Editor editor; public FindAndReplaceDialog(Editor editor) { JPanel cp = new JPanel(new BorderLayout()); this.editor = editor; this.textArea = this.editor.getTextArea(); // Create a toolbar with searching options. JToolBar toolBar = new JToolBar(); searchField = new JTextField(30); toolBar.add(searchField); final JButton nextButton = new JButton("Find Next"); nextButton.setActionCommand("FindNext"); nextButton.addActionListener(this); toolBar.add(nextButton); searchField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { nextButton.doClick(0); } }); JButton prevButton = new JButton("Find Previous"); prevButton.setActionCommand("FindPrev"); prevButton.addActionListener(this); toolBar.add(prevButton); regexCB = new JCheckBox("Regex"); toolBar.add(regexCB); matchCaseCB = new JCheckBox("Match Case"); toolBar.add(matchCaseCB); cp.add(toolBar, BorderLayout.NORTH); setContentPane(cp); setTitle("Find and Replace Demo"); // setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // "FindNext" => search forward, "FindPrev" => search backward String command = e.getActionCommand(); boolean forward = "FindNext".equals(command); // Create an object defining our search parameters. SearchContext context = new SearchContext(); String text = searchField.getText(); if (text.length() == 0) { return; } context.setSearchFor(text); context.setMatchCase(matchCaseCB.isSelected()); context.setRegularExpression(regexCB.isSelected()); context.setSearchForward(forward); context.setWholeWord(false); boolean found = SearchEngine.find(textArea, context); if (!found) { JOptionPane.showMessageDialog(this, "Text not found"); } } }