📄 standardtoolbar.java
字号:
/*
* 11/14/2003
*
* StandardToolBar.java - Toolbar used by 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.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URLClassLoader;
import java.util.ResourceBundle;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import org.fife.ui.CustomizableToolBar;
import org.fife.ui.rtextarea.IconGroup;
import org.fife.ui.rtextarea.RTextArea;
/**
* The toolbar used by <code>org.fife.rtext.RText</code>.
*
* @author Robert Futrell
* @version 1.0
*/
class StandardToolBar extends CustomizableToolBar {
private JButton newButton;
private JButton openButton;
private JButton saveButton;
private JButton saveAllButton;
private JButton closeButton;
private JButton printButton;
private JButton printPreviewButton;
private JButton cutButton;
private JButton copyButton;
private JButton pasteButton;
private JButton deleteButton;
private JButton findButton;
private JButton findNextButton;
private JButton replaceButton;
private JButton replaceNextButton;
private JButton undoButton;
private JButton redoButton;
private RText owner;
private boolean mouseInNewButton;
/*****************************************************************************/
/**
* Creates the tool bar.
*
* @param title The title of this toolbar when it is floating.
* @param rtext The main application that owns this toolbar.
* @param mouseListener The status bar that displays a status message
* when the mouse hovers over this toolbar.
*/
public StandardToolBar(String title, RText rtext,StatusBar mouseListener) {
super(title);
this.owner = rtext;
AbstractMainView mainView = owner.getMainView();
ResourceBundle msg = rtext.getResourceBundle();
// Add the standard buttons.
newButton = new JButton(rtext.getAction(RText.NEW_ACTION));
configure(newButton, mouseListener, msg.getString("DescNew"));
add(newButton);
newButton.addFocusListener(new NewButtonFocusListener());
newButton.addMouseListener(new NewButtonMouseListener());
openButton = new JButton(rtext.getAction(RText.OPEN_ACTION));
configure(openButton, mouseListener, msg.getString("DescOpen"));
add(openButton);
saveButton = new JButton(rtext.getAction(RText.SAVE_ACTION));
configure(saveButton, mouseListener, msg.getString("DescSave"));
add(saveButton);
saveAllButton = new JButton(rtext.getAction(RText.SAVE_ALL_ACTION));
configure(saveAllButton, mouseListener, msg.getString("DescSaveAll"));
add(saveAllButton);
closeButton = new JButton(mainView.getAction(AbstractMainView.CLOSE_ACTION));
configure(closeButton, mouseListener, msg.getString("DescClose"));
add(closeButton);
addSeparator();
printButton = new JButton(mainView.getAction(AbstractMainView.PRINT_ACTION));
configure(printButton, mouseListener, msg.getString("DescPrint"));
add(printButton);
printPreviewButton = new JButton(mainView.getAction(AbstractMainView.PRINT_PREVIEW_ACTION));
configure(printPreviewButton, mouseListener, msg.getString("DescPrintPreview"));
add(printPreviewButton);
addSeparator();
cutButton = new JButton(RTextArea.getAction(RTextArea.CUT_ACTION));
configure(cutButton, mouseListener, msg.getString("DescCut"));
add(cutButton);
copyButton = new JButton(RTextArea.getAction(RTextArea.COPY_ACTION));
configure(copyButton, mouseListener, msg.getString("DescCopy"));
add(copyButton);
pasteButton = new JButton(RTextArea.getAction(RTextArea.PASTE_ACTION));
configure(pasteButton, mouseListener, msg.getString("DescPaste"));
add(pasteButton);
deleteButton = new JButton(RTextArea.getAction(RTextArea.DELETE_ACTION));
configure(deleteButton, mouseListener, msg.getString("DescDelete"));
add(deleteButton);
addSeparator();
findButton = new JButton(mainView.getAction(AbstractMainView.FIND_ACTION));
configure(findButton, mouseListener, msg.getString("DescFind"));
add(findButton);
findNextButton = new JButton(mainView.getAction(AbstractMainView.FIND_NEXT_ACTION));
configure(findNextButton, mouseListener, msg.getString("DescFindNext"));
add(findNextButton);
replaceButton = new JButton(mainView.getAction(AbstractMainView.REPLACE_ACTION));
configure(replaceButton, mouseListener, msg.getString("DescReplace"));
add(replaceButton);
replaceNextButton = new JButton(mainView.getAction(AbstractMainView.REPLACE_NEXT_ACTION));
configure(replaceNextButton, mouseListener, msg.getString("DescReplaceNext"));
add(replaceNextButton);
addSeparator();
undoButton = new JButton(RTextArea.getAction(RTextArea.UNDO_ACTION));
configure(undoButton, mouseListener, msg.getString("DescUndo"));
// Necessary to keep button size from changing when undo text changes.
undoButton.putClientProperty("hideActionText", Boolean.TRUE);
add(undoButton);
redoButton = new JButton(RTextArea.getAction(RTextArea.REDO_ACTION));
configure(redoButton, mouseListener, msg.getString("DescRedo"));
// Necessary to keep button size from changing when undo text changes.
redoButton.putClientProperty("hideActionText", Boolean.TRUE);
add(redoButton);
// Make the toolbar have the right-click customize menu.
makeCustomizable();
}
/*****************************************************************************/
/**
* Checks whether the current icon group has large icons, and if it does,
* uses these large icons for the toolbar.
*/
void checkForLargeIcons() {
IconGroup group = owner.getIconGroup();
if (group.hasSeparateLargeIcons()) {
Icon icon = group.getLargeIcon("new");
newButton.setIcon(icon);
icon = group.getLargeIcon("open");
openButton.setIcon(icon);
icon = group.getLargeIcon("save");
saveButton.setIcon(icon);
icon = group.getLargeIcon("saveall");
saveAllButton.setIcon(icon);
icon = group.getLargeIcon("close");
closeButton.setIcon(icon);
icon = group.getLargeIcon("print");
printButton.setIcon(icon);
icon = group.getLargeIcon("printpreview");
printPreviewButton.setIcon(icon);
icon = group.getLargeIcon("cut");
cutButton.setIcon(icon);
icon = group.getLargeIcon("copy");
copyButton.setIcon(icon);
icon = group.getLargeIcon("paste");
pasteButton.setIcon(icon);
icon = group.getLargeIcon("delete");
deleteButton.setIcon(icon);
icon = group.getLargeIcon("find");
findButton.setIcon(icon);
icon = group.getLargeIcon("findnext");
findNextButton.setIcon(icon);
icon = group.getLargeIcon("replace");
replaceButton.setIcon(icon);
icon = group.getLargeIcon("replacenext");
replaceNextButton.setIcon(icon);
icon = group.getLargeIcon("undo");
undoButton.setIcon(icon);
icon = group.getLargeIcon("redo");
redoButton.setIcon(icon);
}
}
/*****************************************************************************/
/**
* Configures the specified menu bar button.
*
* @param button The button.
* @param mouseListener A mouse listener to add.
* @param desc A description of the button's action.
*/
private Insets emptyInsets = new Insets(0,0,0,0);
private final void configure(JButton button, StatusBar mouseListener,
String desc) {
button.setMargin(emptyInsets);
button.setText(null);
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.BOTTOM);
button.getAccessibleContext().setAccessibleDescription(desc);
button.addMouseListener(mouseListener);
}
/*****************************************************************************/
/********************** PRIVATE INNER CLASSES ********************************/
/*****************************************************************************/
/**
* This class keeps track of whether or not the mouse position is inside
* the "New" button's bounds. This is part of an elaborate hack to fix
* what seems to be a focus issue (bug) in JRE1.4. Note that in JRE 1.5,
* it does not happen.
*
* What happens is this: Whenever the user clicks on a tab to change the
* current document, the focused component gets switched not to the text
* area corresponding to the tab they clicked, but rather the "New" button
* on the toolbar (actually, it gets switched to the text area, then to the
* New button). This behavior stops if the user changes the Look and Feel.
*/
private class NewButtonMouseListener extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
mouseInNewButton = true;
}
public void mouseExited(MouseEvent e) {
mouseInNewButton = false;
}
}
/*****************************************************************************/
/**
* This is the second part of the elaborate focus hack. Whenever the New
* toolbar button gets focus, we check to see if the mouse position is
* inside the New button's bounds. If it isn't, then we assume that the
* event was fired as a result of the situation described in
* NewButtonMouseListener's blurb, and so we give focus back to the current
* text area.
*/
private class NewButtonFocusListener extends FocusAdapter {
public void focusGained(FocusEvent e) {
RTextEditorPane textArea = owner.getMainView().currentTextArea;
if (!mouseInNewButton && textArea!=null)
textArea.requestFocusInWindow();
}
public void focusLost(FocusEvent e) {
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -