📄 rtextmenubar.java
字号:
/*
* 11/14/2003
*
* RTextMenuBar.java - Menu bar 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.Component;
import java.awt.FontMetrics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.Action;
import org.fife.RUtilities;
import org.fife.ui.app.GUIApplication;
import org.fife.ui.app.MenuBar;
import org.fife.ui.app.PluginMenu;
import org.fife.ui.rtextarea.RTextArea;
import org.fife.ui.rtextarea.RTextAreaEditorKit;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit;
/**
* The menu bar used by rtext. The menu bar includes a "file history" feature,
* where it can remember any number of recent files and display them as options
* in the File menu.
*
* @author Robert Futrell
* @version 0.5
*/
class RTextMenuBar extends MenuBar implements PropertyChangeListener,
PopupMenuListener {
// These items correspond to actions belonging to RTextEditorPanes, and are
// changed in disableEditorActions() below, so we need to remember them.
private JMenuItem newItem;
private JMenuItem openItem;
private JMenuItem openInNewWindowItem;
private JMenuItem openRemoteItem;
private JMenuItem saveItem;
private JMenuItem saveAsItem;
private JMenuItem saveAsWebPageItem;
private JMenuItem saveAllItem;
private JMenuItem closeItem;
private JMenuItem closeAllItem;
private JMenuItem printItem;
private JMenuItem printPreviewItem;
private JMenuItem exitItem;
private JMenuItem undoItem;
private JMenuItem redoItem;
private JMenuItem cutItem;
private JMenuItem copyItem;
private JMenuItem pasteItem;
private JMenuItem deleteItem;
private JMenuItem findItem;
private JMenuItem findNextItem;
private JMenuItem replaceItem;
private JMenuItem replaceNextItem;
private JMenuItem findInFilesItem;
private JMenuItem goToItem;
private JMenuItem selectAllItem;
private JMenuItem timeDateItem;
private JMenuItem optionsItem;
private JCheckBoxMenuItem searchToolbarMenuItem;
private JCheckBoxMenuItem lineNumbersItem;
private JMenuItem increaseFontSizesItem;
private JMenuItem decreaseFontSizesItem;
private JMenuItem helpItem;
private JMenuItem aboutItem;
private JMenu fileMenu;
private JMenu pluginMenu;
private JMenu windowMenu;
private JMenu recentFilesMenu;
private JMenu savedMacroMenu;
private RText rtext;
private int maxFileHistorySize; // Number of files to remember in the "file history."
private int numFilesInHistory; // Number of files currently in the file history.
private ArrayList fileHistory; // Strings representing full paths of file history.
/**
* Approximate maximum length, in pixels, of a File History entry.
* Note that this is only GUIDELINE, and some filenames
* can (and will) exceed this limit.
*/
private final int MAX_FILE_PATH_LENGTH = 250;
/*****************************************************************************/
/**
* Creates an instance of the menu bar.
*
* @param rtext The instance of the <code>RText</code> editor that this
* menu bar belongs to.
* @param mouseListener The <code>org.fife.rtext.StatusBar</code>
* of <code>rtext</code>.
* @param lnfName The name for a look and feel; should be obtained from
* <code>UIManager.getLookAndFeel().getName()</code>.
* @param properties The properties we'll be using to initialize the menu
* bar.
*/
public RTextMenuBar(final RText rtext, StatusBar mouseListener,
String lnfName, RTextPreferences properties) {
// Initialize some private variables.
this.rtext = rtext;
// Variables to create the menu.
JMenu menu;
JMenuItem menuItem;
JCheckBoxMenuItem cbMenuItem;
ResourceBundle msg = rtext.getResourceBundle();
ResourceBundle menuMsg = ResourceBundle.getBundle(
"org.fife.rtext.MenuBar");
AbstractMainView mainView = rtext.getMainView();
// File submenu.
fileMenu = RUtilities.createMenu(menuMsg, "MenuFile", "MenuFileMnemonic");
add(fileMenu);
// File submenu's items.
newItem = createMenuItem(rtext.getAction(RText.NEW_ACTION), null,
msg.getString("DescNew"), mouseListener);
fileMenu.add(newItem);
openItem = createMenuItem(rtext.getAction(RText.OPEN_ACTION), null,
msg.getString("DescOpen"), mouseListener);
fileMenu.add(openItem);
openInNewWindowItem = createMenuItem(rtext.getAction(RText.OPEN_NEWWIN_ACTION),
null, msg.getString("DescOpenInNewWindow"), mouseListener);
fileMenu.add(openInNewWindowItem);
/*
openRemoteItem = createMenuItem(rtext.getAction(RText.OPEN_REMOTE_ACTION),
null, msg.getString("DescOpenRemote"), mouseListener);
fileMenu.add(openRemoteItem);
*/
closeItem = createMenuItem(mainView.getAction(AbstractMainView.CLOSE_ACTION),
null, msg.getString("DescClose"), mouseListener);
fileMenu.add(closeItem);
closeAllItem = createMenuItem(mainView.getAction(AbstractMainView.CLOSE_ALL_ACTION),
null, msg.getString("DescCloseAll"), mouseListener);
fileMenu.add(closeAllItem);
fileMenu.addSeparator();
saveItem = createMenuItem(rtext.getAction(RText.SAVE_ACTION), null,
msg.getString("DescSave"), mouseListener);
fileMenu.add(saveItem);
saveAsItem = createMenuItem(rtext.getAction(RText.SAVE_AS_ACTION),
null, msg.getString("DescSaveAs"), mouseListener);
fileMenu.add(saveAsItem);
saveAsWebPageItem = createMenuItem(rtext.getAction(RText.SAVE_WEBPAGE_ACTION),
null, msg.getString("DescSaveAsWebPage"), mouseListener);
fileMenu.add(saveAsWebPageItem);
saveAllItem = createMenuItem(rtext.getAction(RText.SAVE_ALL_ACTION),
null, msg.getString("DescSaveAll"), mouseListener);
fileMenu.add(saveAllItem);
fileMenu.addSeparator();
printItem = createMenuItem(mainView.getAction(AbstractMainView.PRINT_ACTION),
null, msg.getString("DescPrint"), mouseListener);
fileMenu.add(printItem);
printPreviewItem = createMenuItem(mainView.getAction(AbstractMainView.PRINT_PREVIEW_ACTION),
null, msg.getString("DescPrintPreview"), mouseListener);
fileMenu.add(printPreviewItem);
fileMenu.addSeparator();
recentFilesMenu = new JMenu(menuMsg.getString("RecentFiles"));
fileMenu.add(recentFilesMenu);
// Add file history, if any.
this.maxFileHistorySize = 20;//maxFileHistorySize; <-- FIXME: Make this configurable from the Options dialog!!
fileHistory = new ArrayList(maxFileHistorySize);
if (properties.fileHistoryString != null) {
StringTokenizer st = new StringTokenizer(properties.fileHistoryString, "<");
String token;
while (st.hasMoreTokens()) {
token = st.nextToken();
addFileToFileHistory(token);
}
}
// 1.5.2004/pwy: On OS X the Exit menu item is in the standard application menu
// and is always generated by the system. No need to have an additional Exit in
// the menu.
if (rtext.getOS()!=rtext.OS_MAC_OSX) {
exitItem = RUtilities.createMenuItem(menuMsg,
"ExitItemName", "ExitItemMnemonic");
exitItem.getAccessibleContext().
setAccessibleDescription(
msg.getString("DescExit"));
exitItem.setActionCommand("Exit");
exitItem.addActionListener(rtext);
exitItem.addMouseListener(mouseListener);
fileMenu.addSeparator();
fileMenu.add(exitItem);
}
// Edit submenu.
menu = RUtilities.createMenu(menuMsg, "MenuEdit", "MenuEditMnemonic");
add(menu);
// Edit submenu's items.
undoItem = createMenuItem(RTextArea.getAction(RTextArea.UNDO_ACTION),
null, msg.getString("DescUndo"), mouseListener);
menu.add(undoItem);
redoItem = createMenuItem(RTextArea.getAction(RTextArea.REDO_ACTION),
null, msg.getString("DescRedo"), mouseListener);
menu.add(redoItem);
menu.addSeparator();
cutItem = createMenuItem(RTextArea.getAction(RTextArea.CUT_ACTION),
null, msg.getString("DescCut"), mouseListener);
menu.add(cutItem);
copyItem = createMenuItem(RTextArea.getAction(RTextArea.COPY_ACTION),
null, msg.getString("DescCopy"), mouseListener);
menu.add(copyItem);
pasteItem = createMenuItem(RTextArea.getAction(RTextArea.PASTE_ACTION),
null, msg.getString("DescPaste"), mouseListener);
menu.add(pasteItem);
deleteItem = createMenuItem(RTextArea.getAction(RTextArea.DELETE_ACTION),
null, msg.getString("DescDelete"), mouseListener);
menu.add(deleteItem);
menu.addSeparator();
selectAllItem = createMenuItem(RTextArea.getAction(RTextArea.SELECT_ALL_ACTION),
null, msg.getString("DescSelectAll"), mouseListener);
menu.add(selectAllItem);
timeDateItem = createMenuItem(rtext.getAction(RText.TIME_DATE_ACTION),
null, msg.getString("DescTimeDate"), mouseListener);
menu.add(timeDateItem);
menu.addSeparator();
// The "text" menu. Note that keystrokes are okay here, because
// these actions are also owned by RSyntaxTextArea and thus we
// do not want to be able to change them.
JMenu textMenu = RUtilities.createMenu(menuMsg, "MenuText", "MenuTextMnemonic");
int defaultModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
menuItem = RUtilities.createMenuItem(
new RTextAreaEditorKit.DeleteRestOfLineAction(),
menuMsg, "DeleteLineRemainder", "DeleteLineRemainderMnemonic",
KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, defaultModifier));
RUtilities.setDescription(menuItem, menuMsg, "DescDeleteLineRemainder");
menuItem.addMouseListener(mouseListener);
textMenu.add(menuItem);
menuItem = RUtilities.createMenuItem(
new RSyntaxTextAreaEditorKit.JoinLinesAction(),
menuMsg, "JoinLines", "JoinLinesMnemonic",
KeyStroke.getKeyStroke(KeyEvent.VK_J, defaultModifier));
RUtilities.setDescription(menuItem, menuMsg, "DescJoinLines");
menuItem.addMouseListener(mouseListener);
textMenu.add(menuItem);
textMenu.addSeparator();
menuItem = RUtilities.createMenuItem(
new RTextAreaEditorKit.UpperSelectionCaseAction(),
menuMsg, "UpperCase", "UpperCaseMnemonic");
RUtilities.setDescription(menuItem, menuMsg, "DescUpperCase");
menuItem.addMouseListener(mouseListener);
textMenu.add(menuItem);
menuItem = RUtilities.createMenuItem(
new RTextAreaEditorKit.LowerSelectionCaseAction(),
menuMsg, "LowerCase", "LowerCaseMnemonic");
RUtilities.setDescription(menuItem, menuMsg, "DescLowerCase");
menuItem.addMouseListener(mouseListener);
textMenu.add(menuItem);
menuItem = RUtilities.createMenuItem(
new RTextAreaEditorKit.InvertSelectionCaseAction(),
menuMsg, "InvertCase", "InvertCaseMnemonic");
RUtilities.setDescription(menuItem, menuMsg, "DescInvertCase");
menuItem.addMouseListener(mouseListener);
textMenu.add(menuItem);
menu.add(textMenu);
// The "indent" menu. Note that keystrokes are okay here, because
// these actions are also owned by the RSyntaxTextArea and thus we do
// not want to be able to change them.
JMenu indentMenu = RUtilities.createMenu(menuMsg, "MenuIndent", "MenuIndentMnemonic");
final int shift = InputEvent.SHIFT_MASK;
menuItem = RUtilities.createMenuItem(
//new RSyntaxTextAreaEditorKit.IncreaseIndentAction(),
new RSyntaxTextAreaEditorKit.InsertTabAction(),
menuMsg, "IncreaseIndent", "IncreaseIndentMnemonic",
KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
RUtilities.setDescription(menuItem, menuMsg, "DescIncreaseIndent");
menuItem.addMouseListener(mouseListener);
indentMenu.add(menuItem);
menuItem = RUtilities.createMenuItem(
new RSyntaxTextAreaEditorKit.DecreaseIndentAction(),
menuMsg, "DecreaseIndent", "DecreaseIndentMnemonic",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -