📄 searchdialog.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/8/17
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.alphaminer.tools;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.text.JTextComponent;
import eti.bi.alphaminer.core.observer.Observer;
import eti.bi.common.Locale.Resource;
public class SearchDialog extends JDialog implements Observer, WindowConstants{
/**
*
*/
private static final long serialVersionUID = 1L;
private class Result {
private int start, end;
private Result(int start, int end) {
this.start = start;
this.end = end;
}
public String toString() {
return "start: " + start + ", end: " + end;
}
}
private JTextField patternField = new JTextField(20);
private JTextField replaceField = new JTextField(20);
private JCheckBox caseSensitive = new JCheckBox(Resource.srcStr("SearchDialogCasesensitive"));
private JCheckBox regExp = new JCheckBox(Resource.srcStr("SearchDialogRegularexpression"));
private JRadioButton up = new JRadioButton(Resource.srcStr("SearchDialogUp"));
private JRadioButton down = new JRadioButton(Resource.srcStr("SearchDialogUpDown"));
private JLabel searchlabel = new JLabel(Resource.srcStr("SearchDialogSearch")+":");
private JLabel replaceLabel = new JLabel(Resource.srcStr("SearchDialogReplace")+":");
private JButton replacebutton = new JButton(Resource.srcStr("SearchDialogReplace"));
private JButton searchbutton = new JButton(Resource.srcStr("SearchDialogSearch"));
private JButton cancel = new JButton(Resource.srcStr("m_ButtonClose"));
private JTextComponent textComponent;
private boolean allowReplace;
public SearchDialog(Component owner, JTextComponent textComponent) {
this(owner, textComponent, false);
}
public SearchDialog(Component owner, JTextComponent textComponent, boolean allowReplace) {
super();
this.textComponent = textComponent;
this.textComponent.requestFocus();
this.textComponent.setCaretPosition(0);
if (allowReplace)
setTitle(Resource.srcStr("SearchDialogSearchandReplace"));
else
setTitle(Resource.srcStr("SearchDialogSearch"));
this.allowReplace = allowReplace;
setModal(false);
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5,5,5,5);
c.ipadx = 10;
c.weightx = 1.0d;
c.gridwidth = 1;
layout.setConstraints(searchlabel, c);
getContentPane().add(searchlabel);
c.gridwidth = GridBagConstraints.RELATIVE;
layout.setConstraints(patternField, c);
getContentPane().add(patternField);
getRootPane().setDefaultButton(searchbutton);
searchbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
search();
getRootPane().setDefaultButton(searchbutton);
}
});
c.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(searchbutton, c);
getContentPane().add(searchbutton);
if (allowReplace) {
c.gridwidth = 1;
replaceLabel = new JLabel("Replace:");
layout.setConstraints(replaceLabel, c);
getContentPane().add(replaceLabel);
c.gridwidth = GridBagConstraints.RELATIVE;
layout.setConstraints(replaceField, c);
getContentPane().add(replaceField);
replacebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
replace();
search();
getRootPane().setDefaultButton(replacebutton);
}
});
c.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(replacebutton, c);
getContentPane().add(replacebutton);
}
JPanel directionPanel = new JPanel();
directionPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(7, 0, 0, 7), BorderFactory.createTitledBorder("Direction")));
ButtonGroup directionGroup = new ButtonGroup();
up.setMnemonic(KeyEvent.VK_U);
directionGroup.add(up);
directionPanel.add(up);
down.setMnemonic(KeyEvent.VK_D);
directionGroup.add(down);
down.setSelected(true);
directionPanel.add(down);
c.gridwidth = 1;
layout.setConstraints(directionPanel, c);
getContentPane().add(directionPanel);
JPanel optionsPanel = new JPanel();
optionsPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(7, 0, 0, 7), BorderFactory.createTitledBorder("Options")));
optionsPanel.add(caseSensitive);
optionsPanel.add(regExp);
c.gridwidth = GridBagConstraints.RELATIVE;
layout.setConstraints(optionsPanel, c);
getContentPane().add(optionsPanel);
c.gridwidth = GridBagConstraints.REMAINDER;
JPanel dummyPanel = new JPanel();
layout.setConstraints(dummyPanel, c);
getContentPane().add(dummyPanel);
JPanel closeButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SearchDialog.this.textComponent.moveCaretPosition(SearchDialog.this.textComponent.getCaretPosition());
dispose();
}
});
closeButtonPanel.add(cancel);
c.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(closeButtonPanel, c);
getContentPane().add(closeButtonPanel);
pack();
setDefaultCloseOperation(HIDE_ON_CLOSE);
setLocationRelativeTo(owner);
setAlwaysOnTop(true);
setResizable(false);
Resource.registerInterest(this);
}
private void search() {
String pattern = patternField.getText().trim();
if (pattern.length() == 0)
return;
int startPos = textComponent.getCaretPosition();
String text = textComponent.getText();
if (startPos > text.length())
startPos = 0;
if (down.isSelected()) {
Result result = search(startPos, pattern, text);
if (result == null) {
noMoreHits();
return;
} else {
textComponent.setCaretPosition(result.start);
textComponent.moveCaretPosition(result.end);
}
} else {
Result lastResult = null;
int pos = 0;
while (true) {
Result result = search(pos, pattern, text);
if (result != null) {
if (result.end < startPos) {
pos = result.start + 1;
lastResult = result;
} else {
break;
}
} else {
break;
}
}
if (lastResult == null) {
noMoreHits();
} else {
textComponent.setCaretPosition(lastResult.start);
textComponent.moveCaretPosition(lastResult.end);
}
}
}
private void replace() {
textComponent.replaceSelection(replaceField.getText());
}
private Result search(int start, String pattern, String text) {
text = text.replaceAll("\r", "");
if (regExp.isSelected()) {
Matcher matcher = Pattern.compile(pattern, caseSensitive.isSelected() ? 0 : Pattern.CASE_INSENSITIVE).matcher(text.subSequence(start, text.length()));
if (matcher.find()) {
return new Result(start + matcher.start(), start + matcher.end());
} else {
return null;
}
} else {
if (!caseSensitive.isSelected()) {
text = text.toLowerCase();
pattern = pattern.toLowerCase();
}
int result = text.indexOf(pattern, start);
if (result == -1) {
return null;
} else {
return new Result(result, result + pattern.length());
}
}
}
private void noMoreHits() {
String restartAt = up.isSelected() ? "end" : "beginning";
switch (JOptionPane.showConfirmDialog(this, "Search string not found. Search from " + restartAt + "?", "String not found", JOptionPane.YES_NO_OPTION)) {
case JOptionPane.YES_OPTION:
textComponent.setCaretPosition(up.isSelected() ? textComponent.getText().replaceAll("\r","").length() : 0);
search();
break;
case JOptionPane.NO_OPTION:
default:
return;
}
}
public void toshow() {
pack();
setVisible(true);
}
public void sendNotify(String a_Message) {
if(a_Message.equals(Resource.CHANGE_LOCALE)) {
if (allowReplace)
setTitle(Resource.srcStr("SearchDialogSearchandReplace"));
else
setTitle(Resource.srcStr("SearchDialogSearch"));
caseSensitive.setText(Resource.srcStr("SearchDialogCasesensitive"));
regExp.setText(Resource.srcStr("SearchDialogRegularexpression"));
up.setText(Resource.srcStr("SearchDialogUp"));
down.setText(Resource.srcStr("SearchDialogUpDown"));
searchlabel.setText(Resource.srcStr("SearchDialogSearch")+":");
replaceLabel.setText(Resource.srcStr("SearchDialogReplace")+":");
replacebutton.setText(Resource.srcStr("SearchDialogReplace"));
searchbutton.setText(Resource.srcStr("SearchDialogSearch"));
cancel.setText(Resource.srcStr("m_ButtonClose"));
}
}
public void sendNotify(int a_Message) {
}
public void sendNotify(int a_Message, Object a_Object) {
}
public void dispose() {
Resource.removeIntereat(this);
super.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -