package notepad;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.text.BadLocationException;
public class Find implements ActionListener
{
private static final long serialVersionUID = 1L;
private JLabel jLabelFind, jLabelReplace;
private JTextField jTextFind, jTextReplace;
private JButton jBtnFind, jBtnReplace, jBtnReplaceAll, jBtnCancel;
private JCheckBox jUpperOrLower;
private JRadioButton jRadioUp, jRadioDn;
private JTextArea jNotepad;
private JDialog findDialog;
private int startIndex = -1;
public Find(JNotepad owner, JTextArea jText)
{
jNotepad = jText;
jLabelFind = new JLabel("查找:");
jLabelFind.setBounds(16, 22, 42, 22);
jLabelReplace = new JLabel("替换:");
jLabelReplace.setBounds(16, 52, 42, 22);
jTextFind = new JTextField();
jTextFind.setBounds(60, 22, 180, 22);
jTextReplace = new JTextField();
jTextReplace.setBounds(60, 52, 180, 22);
jBtnFind = new JButton("查找(F)");
jBtnFind.setMnemonic('F');
jBtnFind.setBounds(250, 22, 102, 22);
jBtnFind.addActionListener(this);
jBtnReplace = new JButton("替换(R)");
jBtnReplace.setMnemonic('R');
jBtnReplace.setBounds(250, 52, 102, 22);
jBtnReplace.addActionListener(this);
jBtnReplaceAll = new JButton("全部替换(A)");
jBtnReplaceAll.setMnemonic('A');
jBtnReplaceAll.setBounds(250, 82, 102, 22);
jBtnReplaceAll.addActionListener(this);
jBtnCancel = new JButton("取消(X)");
jBtnCancel.setMnemonic('X');
jBtnCancel.setBounds(250, 112, 102, 22);
jBtnCancel.addActionListener(this);
jUpperOrLower = new JCheckBox("区分大小写(C)", false);
jUpperOrLower.setMnemonic('C');
jUpperOrLower.setBounds(14, 100, 106, 22);
jRadioUp = new JRadioButton("向上", false);
jRadioUp.setBounds(132, 100, 52, 22);
jRadioDn = new JRadioButton("向下", true);
jRadioDn.setBounds(184, 100, 52, 22);
ButtonGroup group = new ButtonGroup();
group.add(jRadioUp);
group.add(jRadioDn);
findDialog = new JDialog(owner, "查找/替换");
Container findPane = findDialog.getContentPane();
findPane.setLayout(null);
findPane.add(jLabelFind, null);
findPane.add(jLabelReplace, null);
findPane.add(jTextFind, null);
findPane.add(jTextReplace, null);
findPane.add(jBtnFind, null);
findPane.add(jBtnReplace, null);
findPane.add(jBtnReplaceAll, null);
findPane.add(jBtnCancel, null);
findPane.add(jUpperOrLower, null);
findPane.add(jRadioUp, null);
findPane.add(jRadioDn, null);
findDialog.setBounds(400, 200, 372, 188);
findDialog.setModal(false); // 无模式
findDialog.setResizable(false); // 固定窗体大小
findDialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
jTextReplace.setText(null);
jTextFind.setText(null);
jTextFind.requestFocusInWindow();
}
});
}
public void setVisible(boolean flag)
{
jTextFind.requestFocusInWindow();
findDialog.setVisible(flag);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(jBtnFind))
{
if(jRadioDn.isSelected())
{
if(jUpperOrLower.isSelected())
startIndex = jNotepad.getText().indexOf(jTextFind.getText(), jNotepad.getSelectionEnd());
else
startIndex = jNotepad.getText().toLowerCase().indexOf(jTextFind.getText().toLowerCase(), jNotepad.getSelectionEnd());
}
else
{
try
{
if(jUpperOrLower.isSelected())
startIndex = jNotepad.getText(0, jNotepad.getSelectionStart()).lastIndexOf(jTextFind.getText());
else
startIndex = jNotepad.getText(0, jNotepad.getSelectionStart()).toLowerCase().lastIndexOf(jTextFind.getText().toLowerCase());
}
catch(BadLocationException se)
{
System.out.print(se.getMessage());
}
}
if(startIndex > -1)
jNotepad.select(startIndex, startIndex + jTextFind.getText().length());
else
{
JOptionPane.showMessageDialog(findDialog, String.format("找不到“%s” 已完成对文档的搜索。", jTextFind.getText()), "记事本", 1);
jTextReplace.setText(null);
jTextFind.requestFocusInWindow();
jTextFind.selectAll();
}
}
else if(e.getSource().equals(jBtnReplace))
{
jNotepad.replaceSelection(jTextReplace.getText());
jNotepad.select(jNotepad.getSelectionStart() - jTextReplace.getText().length(), jNotepad.getSelectionStart());
}
else if(e.getSource().equals(jBtnReplaceAll))
{
jNotepad.setSelectionStart(0);
while(jNotepad.getSelectionStart() < jNotepad.getText().length())
{
if(jUpperOrLower.isSelected())
startIndex = jNotepad.getText().indexOf(jTextFind.getText(), jNotepad.getSelectionStart());
else
startIndex = jNotepad.getText().toLowerCase().indexOf(jTextFind.getText().toLowerCase(), jNotepad.getSelectionStart());
if(startIndex > -1)
{
jNotepad.select(startIndex, startIndex + jTextFind.getText().length());
jNotepad.replaceSelection(jTextReplace.getText());
}
else
break;
}
}
else
{
jTextReplace.setText(null);
jTextFind.setText(null);
jTextFind.requestFocusInWindow();
findDialog.setVisible(false);
}
}
}