/* * FindReplaceDialog.java * Created 11/29/13 * By Randall Twede */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.regex.*; import java.util.HashMap; import javax.swing.text.*; public class FindReplaceDialog extends JDialog implements ActionListener { private JPanel wordPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); private JPanel casePanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); private JPanel inputs = new JPanel(new GridLayout(6, 1)); private JPanel buttons = new JPanel(new GridLayout(5, 1)); private JLabel findWhat = new JLabel("Find What"); private JTextField what = new JTextField(); private JLabel replaceWith = new JLabel("Replace With"); private JTextField with = new JTextField(); private JCheckBox word = new JCheckBox(); private JLabel wordLabel = new JLabel("Match whole word only"); private JCheckBox matchCase = new JCheckBox(); private JLabel matchCaseLabel = new JLabel("Match case"); private JButton find = new JButton("Find"); private JButton findAll = new JButton("Find All"); private JButton replace = new JButton("Replace"); private JButton replaceAll = new JButton("Replace All"); private JButton close = new JButton("Close"); private JFrame owner; private JTextComponent pane; private HashMap actions; private Highlighter highlighter; private Highlighter.HighlightPainter painter; private String lastQuery = ""; private int start = 0; public FindReplaceDialog(JFrame owner, JTextComponent pane) { super(owner, "Find & Replace", false); this.owner = owner; this.pane = pane; initComponents(); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(inputs, "Center"); c.add(buttons, "East"); pack(); setLocationRelativeTo(owner); setVisible(true); if(pane.getText() == null) { JOptionPane.showMessageDialog(null, "Cannot parse input"); dispose(); } } public void actionPerformed(ActionEvent e) { if(e.getSource() == what) { with.requestFocusInWindow(); } if(e.getSource() == find) { highlighter.removeAllHighlights(); if(!word.isSelected() && !matchCase.isSelected()) { String toFind = what.getText(); Pattern p = Pattern.compile(toFind, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(pane.getText()); if(!what.getText().equals(lastQuery)) { m.find(); lastQuery = what.getText(); } else { m.find(pane.getCaretPosition()); } selectText(m); } else if(!word.isSelected() && matchCase.isSelected()) { String toFind = what.getText(); Pattern p = Pattern.compile(toFind); Matcher m = p.matcher(pane.getText()); if(!what.getText().equals(lastQuery)) { m.find(); lastQuery = what.getText(); } else { m.find(pane.getCaretPosition()); } selectText(m); } else if(word.isSelected() && !matchCase.isSelected()) { String toFind = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toFind, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(pane.getText()); if(!what.getText().equals(lastQuery)) { m.find(); lastQuery = what.getText(); } else { m.find(pane.getCaretPosition()); } selectText(m); } else if(word.isSelected() && matchCase.isSelected()) { String toFind = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toFind); Matcher m = p.matcher(pane.getText()); if(!what.getText().equals(lastQuery)) { m.find(); lastQuery = what.getText(); } else { m.find(pane.getCaretPosition()); } selectText(m); } } if(e.getSource() == findAll) { highlighter.removeAllHighlights(); if(!word.isSelected() && !matchCase.isSelected()) { String toFind = what.getText(); Pattern p = Pattern.compile(toFind, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(pane.getText()); while (m.find()) { highlight(m.start(), m.end()); pane.setCaretPosition( m.end()); } } else if(!word.isSelected() && matchCase.isSelected()) { String toFind = what.getText(); Pattern p = Pattern.compile(toFind); Matcher m = p.matcher(pane.getText()); while (m.find()) { highlight(m.start(), m.end()); pane.setCaretPosition( m.end()); } } else if(word.isSelected() && !matchCase.isSelected()) { String toFind = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toFind, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(pane.getText()); while (m.find()) { highlight(m.start(), m.end()); pane.setCaretPosition( m.end()); } } else if(word.isSelected() && matchCase.isSelected()) { String toFind = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toFind); Matcher m = p.matcher(pane.getText()); while (m.find()) { highlight(m.start(), m.end()); pane.setCaretPosition( m.end()); } } } else if(e.getSource() == replace) { String replacement = with.getText(); String text = pane.getText(); String toSearch = text.substring(start); String noSearch = text.substring(0, start); if(!word.isSelected() && !matchCase.isSelected()) { String toReplace = what.getText(); Pattern p = Pattern.compile(toReplace, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(toSearch); String toReturn = noSearch + m.replaceFirst(replacement); pane.setText(toReturn); } else if(!word.isSelected() && matchCase.isSelected()) { String toReplace = what.getText(); Pattern p = Pattern.compile(toReplace); Matcher m = p.matcher(toSearch); String toReturn = noSearch + m.replaceFirst(replacement); pane.setText(toReturn); } else if(word.isSelected() && !matchCase.isSelected()) { String toReplace = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toReplace, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(toSearch); String toReturn = noSearch + m.replaceFirst(replacement); pane.setText(toReturn); } else if(word.isSelected() && matchCase.isSelected()) { String toReplace = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toReplace); Matcher m = p.matcher(toSearch); String toReturn = noSearch + m.replaceFirst(replacement); pane.setText(toReturn); } start = 0; lastQuery = ""; } else if(e.getSource() == replaceAll) { String replacement = with.getText(); String text = pane.getText(); if(!word.isSelected() && !matchCase.isSelected()) { String toReplace = what.getText(); Pattern p = Pattern.compile(toReplace, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(text); text = m.replaceAll(replacement); pane.setText(text); } else if(!word.isSelected() && matchCase.isSelected()) { String toReplace = what.getText(); Pattern p = Pattern.compile(toReplace); Matcher m = p.matcher(text); text = m.replaceAll(replacement); pane.setText(text); } else if(word.isSelected() && !matchCase.isSelected()) { String toReplace = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toReplace, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(text); text = m.replaceAll(replacement); pane.setText(text); } else if(word.isSelected() && matchCase.isSelected()) { String toReplace = ("\\b" + what.getText() + "\\b"); Pattern p = Pattern.compile(toReplace); Matcher m = p.matcher(text); text = m.replaceAll(replacement); pane.setText(text); } } else if(e.getSource() == close) { dispose(); } } private void initComponents() { what.addActionListener(this); find.addActionListener(this); findAll.addActionListener(this); replace.addActionListener(this); replaceAll.addActionListener(this); close.addActionListener(this); wordPanel.add(word); wordPanel.add(wordLabel); casePanel.add(matchCase); casePanel.add(matchCaseLabel); inputs.add(findWhat); inputs.add(what); inputs.add(replaceWith); inputs.add(with); inputs.add(wordPanel); inputs.add(casePanel); buttons.add(find); buttons.add(findAll); buttons.add(replace); buttons.add(replaceAll); buttons.add(close); highlighter = new DefaultHighlighter(); painter = new DefaultHighlighter.DefaultHighlightPainter(Color.cyan); pane.setHighlighter(highlighter); } private void highlight(int start, int end) { try { highlighter.addHighlight(start, end, painter); } catch(BadLocationException ex) { } } private void selectText(Matcher m) { try { highlight(m.start(), m.end()); pane.setCaretPosition( m.end()); start = m.start(); } catch(IllegalStateException ex) { JOptionPane.showMessageDialog(null, "No more matches"); lastQuery = ""; start = 0; } } }