📄 replacedialog.java
字号:
/*
* 11/14/2003
*
* ReplaceDialog.java - Dialog for replacing text in a GUI.
* Copyright (C) 2003 Robert Futrell
* email@address.com
* www.website.com
*
* 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.search;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.fife.RUtilities;
import org.fife.ui.MaxWidthComboBox;
import org.fife.ui.RComboBoxModel;
import org.fife.ui.UIUtilities;
/**
* A "Replace" dialog similar to those found in most Windows text editing
* applications. Contains many search options, including:<br>
* <ul>
* <li>Match Case</li>
* <li>Match Whole Word</li>
* <li>Use Regular Expressions</li>
* <li>Search Forwards or Backwards</li>
* </ul>
* The dialog also remembers your previous several selections in a combo box.
* <p>An application can use a <code>ReplaceDialog</code> as follows. It is suggested
* that you create an <code>Action</code> or something similar to facilitate
* "bringing up" the Replace dialog. Have the main application contain an object
* that implements both <code>PropertyChangeListener</code> and
* <code>ActionListener</code>. This object will receive the following events from
* the Replace dialog:
* <ul>
* <li>"Replace" action when the user clicks the "Replace" button.</li>
* <li>"Replace All" action when the user clicks the "Replace All" button.</li>
* <li>"SearchDialog.MatchCase" property change when the user checks/unchecks the
* Match Case checkbox.</li>
* <li>"SearchDialog.MatchWholeWord" property change when the user checks/unchecks
* the Whole Word checkbox.</li>
* <li>"SearchDialog.UseRegularExpressions" property change when the user checks/unchecks
* the "Regular Expressions" checkbox.</li>
* <li>"SearchDialog.SearchDownward" property change when the user clicks either
* the Up or Down direction radio button.</li>
* </ul>
* Upon receiving the "Replace" or "ReplaceAll" action from the Replace dialog, the
* application can then use one of the Replace dialog's static <code>getNextMatchPos</code>
* or <code>getNextMatchPosRegEx</code> methods to run the search on a given text
* component via its document.
* <p>The property events listed can all be ignored in a simple case; the Replace dialog will
* remember the state of its checkboxes between invocations. However, if your application
* has both a Find and Replace dialog, you may wish to use these messages to synchronize
* the two search dialogs' options.
*
* @author Robert Futrell
* @version 1.0
*/
public class ReplaceDialog extends AbstractFindReplaceDialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -8435320721873575740L;
private JButton findNextButton;
private JButton replaceButton;
private JButton replaceAllButton;
private JLabel findFieldLabel;
private JLabel replaceFieldLabel;
private MaxWidthComboBox replaceWithComboBox;
// This helps us work around the "bug" where JComboBox eats the first Enter
// press.
private String lastSearchString;
private String lastReplaceString;
/*****************************************************************************/
/**
* Creates a new <code>ReplaceDialog</code>.
*
* @param owner The main window that owns this dialog.
* @param actionListener The component that listens for "Replace" actions.
*/
public ReplaceDialog(Frame owner, ActionListener actionListener) {
this(owner, actionListener,
ResourceBundle.getBundle("org.fife.ui.search.Search"));
}
/*****************************************************************************/
/**
* Creates a new <code>ReplaceDialog</code>.
*
* @param owner The main window that owns this dialog.
* @param actionListener The component that listens for "Replace" actions.
* @param resources The resource bundle from which to get strings, etc.
*/
public ReplaceDialog(Frame owner, ActionListener actionListener,
ResourceBundle resources) {
// Let it be known who the owner of this dialog is.
super(owner, resources, true);
// Create a panel for the "Find what" and "Replace with" text fields.
JPanel searchPanel = new JPanel(new SpringLayout());
// Create the "Find what" label.
findFieldLabel = new JLabel(resources.getString("FindWhat"));
findFieldLabel.setDisplayedMnemonic((int)resources.getString("FindWhatMnemonic").charAt(0));
findFieldLabel.setLabelFor(findTextComboBox);
searchPanel.add(findFieldLabel);
// Create listeners for the combo boxes.
ReplaceFocusAdapter replaceFocusAdapter = new ReplaceFocusAdapter();
ReplaceKeyListener replaceKeyListener = new ReplaceKeyListener();
ReplaceDocumentListener replaceDocumentListener = new ReplaceDocumentListener();
// Create the "Find what" text field.
JTextField textField = (JTextField)findTextComboBox.getEditor().getEditorComponent();
textField.addFocusListener(replaceFocusAdapter);
textField.addKeyListener(replaceKeyListener);
textField.getDocument().addDocumentListener(replaceDocumentListener);
searchPanel.add(findTextComboBox);
// Create the "Replace with" label.
replaceFieldLabel = new JLabel(resources.getString("ReplaceWith"));
replaceFieldLabel.setDisplayedMnemonic((int)resources.getString("ReplaceWithMnemonic").charAt(0));
searchPanel.add(replaceFieldLabel);
// Create the "Replace with" text field.
replaceWithComboBox = new MaxWidthComboBox(new RComboBoxModel(), 200);
replaceFieldLabel.setLabelFor(replaceWithComboBox);
replaceWithComboBox.setEditable(true);
textField = (JTextField)replaceWithComboBox.getEditor().getEditorComponent();
textField.addFocusListener(replaceFocusAdapter);
textField.addKeyListener(replaceKeyListener);
textField.getDocument().addDocumentListener(replaceDocumentListener);
searchPanel.add(replaceWithComboBox);
RUtilities.makeSpringCompactGrid(searchPanel, 2, 2, //rows, cols
0,0, //initX, initY
6, 6); //xPad, yPad
// Make a panel containing the inherited search direction radio
// buttons and the inherited search options.
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
bottomPanel.setBorder(UIUtilities.getEmpty5Border());
bottomPanel.add(searchConditionsPanel);
bottomPanel.add(dirPanel);
// Now, make a panel containing all the above stuff.
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setBorder(UIUtilities.getEmpty5Border());
leftPanel.add(searchPanel);
leftPanel.add(bottomPanel);
// Make a panel containing the action buttons.
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,1, 5,5));
findNextButton = RUtilities.createRButton(resources, "Find", "FindMnemonic");
findNextButton.setActionCommand("FindNext");
findNextButton.addActionListener(this);
findNextButton.setDefaultCapable(true);
findNextButton.setEnabled(false); // Initially, nothing to look for.
replaceButton = RUtilities.createRButton(resources, "Replace", "ReplaceMnemonic");
replaceButton.setActionCommand("Replace");
replaceButton.addActionListener(this);
replaceButton.setEnabled(false);
replaceButton.setIcon(null);
replaceButton.setToolTipText(null);
replaceAllButton = RUtilities.createRButton(resources, "ReplaceAll",
"ReplaceAllMnemonic");
replaceAllButton.setActionCommand("ReplaceAll");
replaceAllButton.addActionListener(this);
replaceAllButton.setEnabled(false);
replaceAllButton.setIcon(null);
replaceAllButton.setToolTipText(null);
buttonPanel.add(findNextButton);
buttonPanel.add(replaceButton);
buttonPanel.add(replaceAllButton);
buttonPanel.add(cancelButton); // Defined in superclass.
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(buttonPanel, BorderLayout.NORTH);
// Put it all together!
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(UIUtilities.getEmpty5Border());
contentPane.add(leftPanel);
contentPane.add(rightPanel, BorderLayout.EAST);
setContentPane(contentPane);
getRootPane().setDefaultButton(findNextButton);
setTitle(resources.getString("ReplaceDialogTitle"));
setResizable(false);
pack();
setLocationRelativeTo(owner);
registerExtraKeyboardActions();
}
/*****************************************************************************/
// Listens for an action in this Replace dialog. Note that we don't have to check
// the actionCommand since we do the same thing for all things we listen to.
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
findTextComboBox.addItem((Object)((JTextField)findTextComboBox.getEditor().getEditorComponent()).getText());
// If they just searched for an item that's already in the list other than
// the first, move it to the first position.
if (findTextComboBox.getSelectedIndex()>0) {
Object item = findTextComboBox.getSelectedItem();
findTextComboBox.removeItem(item);
findTextComboBox.insertItemAt(item, 0);
findTextComboBox.setSelectedIndex(0);
}
String replaceWithText = ((JTextField)replaceWithComboBox.getEditor().getEditorComponent()).getText();
if (!replaceWithText.equals(""))
replaceWithComboBox.addItem(replaceWithText);
// If they just searched for an item that's already in the list other than
// the first, move it to the first position.
if (replaceWithComboBox.getSelectedIndex()>0) {
Object item = replaceWithComboBox.getSelectedItem();
replaceWithComboBox.removeItem(item);
replaceWithComboBox.insertItemAt(item, 0);
replaceWithComboBox.setSelectedIndex(0);
}
}
/*****************************************************************************/
/**
* Adds an <code>ActionListener</code> to this dialog. The listener will
* receive notification when the user clicks the "Find" button with an
* actionCommand string of "FindNext", an actionCommand string of "Replace"
* when the user clicks the "Replace" button, and an actionCommand string of
* "ReplaceAll" when the user clicks the "Replace All" button.
*
* @param l The listener to add.
* @see #removeActionListener
*/
public void addActionListener(ActionListener l) {
findNextButton.addActionListener(l);
replaceButton.addActionListener(l);
replaceAllButton.addActionListener(l);
}
/*****************************************************************************/
/**
* Returns the text on the "Find" button.
*
* @return The text on the Find button.
* @see #setFindButtonText
*/
public final String getFindButtonText() {
return findNextButton.getText();
}
/*****************************************************************************/
/**
* Returns the label on the "Find what" text field.
*
* @return The text on the "Find what" text field.
* @see #setFindWhatLabelText
*/
public final String getFindWhatLabelText() {
return findFieldLabel.getText();
}
/*****************************************************************************/
/**
* Returns the text on the "Replace" button.
*
* @return The text on the Replace button.
* @see #setReplaceButtonText
*/
public final String getReplaceButtonText() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -