📄 gotodialog.java~1~
字号:
/*
* 11/14/2003
*
* GoToDialog.java - A dialog allowing you to skip to a specific line number
* in a document in RText.
* Copyright (C) 2003 Robert Futrell
* email@address.com
* www.website.com
*
* This file is a part of RText.
*
* RText 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.
*
* RText 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.rtext;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import org.fife.RUtilities;
import org.fife.ui.RButton;
/**
* A "Go To" dialog allowing you to go to a specific line number in a document
* in RText.
*
* @author Robert Futrell
* @version 1.0
*/
public class GoToDialog extends JDialog {
private JButton okButton;
private JButton cancelButton;
private JTextField lineNumberField;
private int maxLineNumberAllowed; // Number of lines in the document.
private int lineNumber; // The line to go to, or -1 for Cancel.
private RText owner;
/*****************************************************************************/
/**
* Creates a new <code>GoToDialog</code>.
*
* @param owner The rtext window that owns this dialog.
* @param actionListener The component that listens for GoTo events.
*/
public GoToDialog(RText owner, AbstractMainView actionListener) {
// Let it be known who the owner of this dialog is.
super(owner);
this.owner = owner;
ResourceBundle bundle = owner.getResourceBundle();
lineNumber = 1;
maxLineNumberAllowed = 1; // Empty document has 1 line.
// Set the main content pane for the "GoTo" dialog.
JPanel contentPane = new JPanel();
BoxLayout box = new BoxLayout(contentPane, BoxLayout.Y_AXIS);
contentPane.setLayout(box);
contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
setContentPane(contentPane);
// Make a panel containing the "Line Number" edit box.
JPanel enterLineNumberPane = new JPanel();
box = new BoxLayout(enterLineNumberPane, BoxLayout.X_AXIS);
enterLineNumberPane.setLayout(box);
lineNumberField = new JTextField(Integer.toString(lineNumber));
lineNumberField.setPreferredSize(null); // Asks OS.
AbstractDocument doc = (AbstractDocument)lineNumberField.getDocument();
doc.addDocumentListener(new GoToDocumentListener());
doc.setDocumentFilter(new GoToDocumentFilter());
enterLineNumberPane.add(new JLabel(bundle.getString("LineNumber")));
enterLineNumberPane.add(lineNumberField);
GoToActionListener gtal = new GoToActionListener();
// Make a panel containing the OK and Cancel buttons.
JPanel buttonPanel = new JPanel();
GridLayout grid = new GridLayout(1,2, 5,5);
buttonPanel.setLayout(grid);
okButton = RUtilities.createRButton(bundle, "OKButtonLabel", "OKButtonMnemonic");
okButton.setActionCommand("GoToOK");
okButton.addActionListener(gtal);
cancelButton = RUtilities.createRButton(bundle, "Cancel", "CancelMnemonic");
cancelButton.setActionCommand("GoToCancel");
cancelButton.addActionListener(gtal);
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
// Put everything into a neat little package.
contentPane.add(enterLineNumberPane);
contentPane.add(Box.createRigidArea(new Dimension(0,10)));
contentPane.add(buttonPanel);
JRootPane rootPane = getRootPane();
rootPane.setDefaultButton(okButton);
setTitle(owner.getResourceBundle().getString("GotoDialogTitle"));
setModal(true);
setResizable(false);
pack();
setLocationRelativeTo(owner);
// Make the escape key hide the dialog.
InputMap inputMap = rootPane.getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = rootPane.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "OnEsc");
actionMap.put("OnEsc", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
cancelPressed();
}
}
);
}
/*****************************************************************************/
/**
* Called when they've clicked OK or pressed Enter; check the line number
* they entered for validity and if it's okay, close this dialog. If it
* isn't okay, display an error message.
*/
private void attemptToGetGoToLine() {
try {
lineNumber = Integer.parseInt(lineNumberField.getText());
if (lineNumber<1 || lineNumber>maxLineNumberAllowed)
throw new NumberFormatException();
// If we have a valid line number, close the dialog!
setVisible(false);
} catch (NumberFormatException nfe) {
ResourceBundle msg = owner.getResourceBundle();
JOptionPane.showMessageDialog(this,
msg.getString("LineNumberRange")+maxLineNumberAllowed+".",
msg.getString("ErrorDialogTitle"),
JOptionPane.ERROR_MESSAGE);
return;
}
}
/*****************************************************************************/
/**
* Called when the user clicks Cancel or hits the Escape key. This
* hides the dialog.
*/
protected void cancelPressed() {
lineNumber = -1;
setVisible(false);
}
/*****************************************************************************/
/**
* Gets the line number the user entered to go to.
*
* @return The line number the user decided to go to, or <code>-1</code>
* if the dialog was cancelled.
*/
public int getLineNumber() {
return lineNumber;
}
/*****************************************************************************/
/**
* Sets the maximum line number for them to enter.
*
* @param max The new maximum line number value.
*/
public void setMaxLineNumberAllowed(int max) {
this.maxLineNumberAllowed = max;
}
/*****************************************************************************/
/**
* Overrides <code>JDialog</code>'s <code>setVisible</code> method; decides
* whether or not buttons are enabled if the user is enabling the dialog.
*/
public void setVisible(boolean visible) {
if (visible) {
lineNumber = -1;
okButton.setEnabled(lineNumberField.getDocument().getLength()>0);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lineNumberField.requestFocusInWindow();
lineNumberField.selectAll();
}
});
}
super.setVisible(visible);
}
/*****************************************************************************/
/************************ PRIVATE INNER CLASSES ******************************/
/*****************************************************************************/
/**
* A document filter that only allows numbers.
*/
class GoToDocumentFilter extends DocumentFilter {
private final String cleanse(String text) {
boolean beep = false;
if (text!=null) {
int length = text.length();
for (int i=0; i<length; i++) {
if (!Character.isDigit(text.charAt(i))) {
text = text.substring(0,i) + text.substring(i+1);
i--;
length--;
beep = true;
}
}
}
if (beep)
UIManager.getLookAndFeel().provideErrorFeedback(null);
return text;
}
public void insertString(DocumentFilter.FilterBypass fb,
int offset, String text, AttributeSet attr)
throws BadLocationException {
fb.insertString(offset, cleanse(text), attr);
}
public void remove(DocumentFilter.FilterBypass fb,
int offset, int length)
throws BadLocationException {
fb.remove(offset, length);
}
public void replace(DocumentFilter.FilterBypass fb,
int offset, int length, String text, AttributeSet attr)
throws BadLocationException {
fb.replace(offset, length, cleanse(text), attr);
}
}
/*****************************************************************************/
/**
* Listens for updates to the text field in the goto dialog.
*/
private class GoToDocumentListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
if (lineNumberField.getText().length()==1)
okButton.setEnabled(true);
}
public void removeUpdate(DocumentEvent e) {
if (lineNumberField.getText().length()==0)
okButton.setEnabled(false);
}
public void changedUpdate(DocumentEvent e) {
}
}
/*****************************************************************************/
/**
* Listens for action events in the goto dialog.
*/
private class GoToActionListener implements ActionListener {
// Callback for whenever the user presses the OK or Cancel buttons.
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("GoToOK"))
attemptToGetGoToLine();
else if (actionCommand.equals("GoToCancel"))
cancelPressed();
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -