📄 rtextmenubar.java
字号:
while (tempFirstPartLength+endPieceLength < MAX_FILE_PATH_LENGTH) {
firstPart = tempFirstPart;
separatorPos = displayString.indexOf(separator, separatorPos+1);
if (separatorPos==-1)
endPieceLength = 9999999;
else {
tempFirstPart = displayString.substring(0, separatorPos+1);
tempFirstPartLength = getTextWidth(tempFirstPart, fontMetrics);
}
}
return firstPart+endPiece;
}
}
/*****************************************************************************/
/**
* Returns a string representing all files in the file history separated by
* '<' characters. This character was chosen as the separator because it
* is a character that cannot be used in filenames in both Windows and
* UNIX/Linux.
*
* @return A <code>String</code> representing all files in the file
* history, separated by '<' characters. If no files are in the
* file history, then <code>null</code> is returned.
* @see #addFileToFileHistory
*/
public String getFileHistoryString() {
String retVal = null;
for (int i=numFilesInHistory-1; i>=0; i--) {
if (i==numFilesInHistory-1)
retVal = (String)fileHistory.get(i) + "<";
else
retVal += (String)fileHistory.get(i) + "<";
}
if (retVal!=null)
retVal = retVal.substring(0, retVal.length()-1); // Remove trailing '>'.
return retVal;
}
/*****************************************************************************/
/**
* Returns the maximum number of files the file history in the File menu
* will remember.
*
* @return The maximum size of the file history.
*/
public int getMaximumFileHistorySize() {
return maxFileHistorySize;
}
/*****************************************************************************/
/**
* Determines the width of the given <code>String</code> containing no
* tabs. Note that this is simply a trimmed-down version of
* <code>javax.swing.text.getTextWidth</code> that has been
* optimized for our use.
*
* @param s the source of the text
* @param metrics the font metrics to use for the calculation
* @return the width of the text
*/
private final int getTextWidth(String s, FontMetrics metrics) {
int textWidth = 0;
char[] txt = s.toCharArray();
int n = txt.length;
for (int i=0; i<n; i++) {
// Ignore newlines, they take up space and we shouldn't be
// counting them.
if(txt[i] != '\n')
textWidth += metrics.charWidth(txt[i]);
}
return textWidth;
}
/*****************************************************************************/
/**
* Thanks to Java Bug ID 5026829, JMenuItems (among other Swing components)
* don't update their accelerators, etc. when the properties on which they
* were created update them. Thus, we have to do this manually. This is
* still broken as of 1.5.
*/
protected void menuItemAcceleratorWorkaround() {
AbstractMainView mainView = rtext.getMainView();
newItem.setAction(null);
newItem.setAction(rtext.getAction(RText.NEW_ACTION));
openItem.setAction(null);
openItem.setAction(rtext.getAction(RText.OPEN_ACTION));
openInNewWindowItem.setAction(null);
openInNewWindowItem.setAction(rtext.getAction(RText.OPEN_NEWWIN_ACTION));
/* openRemoteItem.setAction(null);
openRemoteItem.setAction(rtext.getAction(RText.OPEN_REMOTE_ACTION));
*/ closeItem.setAction(null);
closeItem.setAction(mainView.getAction(AbstractMainView.CLOSE_ACTION));
closeAllItem.setAction(null);
closeAllItem.setAction(mainView.getAction(AbstractMainView.CLOSE_ALL_ACTION));
saveItem.setAction(null);
saveItem.setAction(rtext.getAction(RText.SAVE_ACTION));
saveAsItem.setAction(null);
saveAsItem.setAction(rtext.getAction(RText.SAVE_AS_ACTION));
saveAsWebPageItem.setAction(null);
saveAsWebPageItem.setAction(rtext.getAction(RText.SAVE_WEBPAGE_ACTION));
saveAllItem.setAction(null);
saveAllItem.setAction(rtext.getAction(RText.SAVE_ALL_ACTION));
printItem.setAction(null);
printItem.setAction(mainView.getAction(AbstractMainView.PRINT_ACTION));
printPreviewItem.setAction(null);
printPreviewItem.setAction(mainView.getAction(AbstractMainView.PRINT_PREVIEW_ACTION));
findItem.setAction(null);
findItem.setAction(mainView.getAction(AbstractMainView.FIND_ACTION));
findNextItem.setAction(null);
findNextItem.setAction(mainView.getAction(AbstractMainView.FIND_NEXT_ACTION));
replaceItem.setAction(null);
replaceItem.setAction(mainView.getAction(AbstractMainView.REPLACE_ACTION));
replaceNextItem.setAction(null);
replaceNextItem.setAction(mainView.getAction(AbstractMainView.REPLACE_NEXT_ACTION));
findInFilesItem.setAction(null);
findInFilesItem.setAction(mainView.getAction(AbstractMainView.FIND_IN_FILES_ACTION));
goToItem.setAction(null);
goToItem.setAction(mainView.getAction(AbstractMainView.GOTO_ACTION));
timeDateItem.setAction(null);
timeDateItem.setAction(rtext.getAction(RText.TIME_DATE_ACTION));
if (rtext.getOS()!=RText.OS_MAC_OSX) {
optionsItem.setAction(null);
optionsItem.setAction(rtext.getAction(RText.OPTIONS_ACTION));
}
lineNumbersItem.setAction(null);
lineNumbersItem.setAction(rtext.getAction(RText.LINE_NUMBER_ACTION));
helpItem.setAction(null);
helpItem.setAction(rtext.getAction(RText.HELP_ACTION));
aboutItem.setAction(null);
aboutItem.setAction(rtext.getAction(RText.ABOUT_ACTION));
}
/*****************************************************************************/
/**
* Called whenever a property changes on a component we're listening to.
*/
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (propertyName.equals(AbstractMainView.NEW_FILE_ADDED_PROPERTY) ||
propertyName.equals(AbstractMainView.OLD_FILE_ADDED_PROPERTY)) {
addFileToFileHistory((String)e.getNewValue());
}
}
/*****************************************************************************/
public void popupMenuCanceled(PopupMenuEvent e) {
}
/*****************************************************************************/
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
/*****************************************************************************/
/**
* Called when one of the popup menus is about to become visible.
*
* @param e The popup menu event.
*/
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
Object source = e.getSource();
// If the "window" menu is becoming visible (MDI view only)...
if (source==windowMenu.getPopupMenu()) {
JPopupMenu popupMenu = windowMenu.getPopupMenu();
int count = popupMenu.getComponentCount() - 4; // Cascade, Tile Vertically & horizontally and the separator.
// Remove the old menu items for each open document.
for (int i=0; i<count; i++)
windowMenu.remove(4);
// Since we only listen for the "Window" menu, and the Window
// menu is only available when the AbstractMainView is the MDI
// view...
if (!(rtext.getMainView() instanceof RTextMDIView))
return;
final RTextMDIView mdiView = (RTextMDIView)rtext.getMainView();
// Add a menu item for each open document.
JMenu currentMenu = windowMenu; // So our menu doesn't get too long.
int i = 0;
count = mdiView.getNumDocuments();
int selectedIndex = mdiView.getSelectedIndex();
while (i<count) {
if ((i+1)%15 == 0) {
currentMenu.add(new JMenu("More..."));
currentMenu = (JMenu)currentMenu.getItem(currentMenu.getItemCount()-1);
}
String text = (i+1) + " " + getDisplayPath(
mdiView.getRTextEditorPaneAt(i).getFileFullPath());
final int index = i;
JRadioButtonMenuItem menuItem =
new JRadioButtonMenuItem(
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
mdiView.setSelectedIndex(index);
}
});
menuItem.setText(text);
menuItem.setSelected(i==selectedIndex);
currentMenu.add(menuItem);
i++;
} // End of while (i<count).
currentMenu = null;
}
// If the "Macros" popup is becoming visible...
else if (source==savedMacroMenu.getPopupMenu()) {
// Clear old macros from menu (some may have been added/removed).
savedMacroMenu.removeAll();
// Get all saved macros (guaranteed non-null array).
File[] files = RTextUtilities.getSavedMacroFiles();
// Add all saved macros to the popup menu.
int count = files.length;
for (int i=0; i<count; i++) {
String name = RTextUtilities.getMacroName(files[i]);
final File f = files[i];
AbstractAction a = new AbstractAction(name) {
public void actionPerformed(ActionEvent e) {
rtext.getMainView().loadMacro(f);
}
};
savedMacroMenu.add(new JMenuItem(a));
}
}
}
/*****************************************************************************/
/**
* Sets the maximum number of files this <code>RText</code> will remember
* for you in it's "file history" in the File menu.
*
* @param newSize The new size of the file history.
*/
public void setMaximumFileHistorySize(int newSize) {
if (newSize<0)
return;
// If the new size is smaller than the current size, we need to remove
// some files from the history.
if (newSize<maxFileHistorySize) {
for (int i=newSize; i<maxFileHistorySize; i++)
fileMenu.remove(fileMenu.getItemCount()-3); // The last file in the history.
}
// Remember the new size.
maxFileHistorySize = newSize;
// Adjust the known number of files in the history, if we removed any.
if (maxFileHistorySize < numFilesInHistory)
numFilesInHistory = maxFileHistorySize;
}
/*****************************************************************************/
/**
* Sets whether the "QuickSearch toolbar" menu item is selected.
*
* @param selected Whether the QuickSearch toolbar menu item is
* selected.
*/
public void setSearchToolbarMenuItemSelected(boolean selected) {
searchToolbarMenuItem.setSelected(selected);
}
/*****************************************************************************/
/**
* Sets whether or not the "Window" menu is visible. This menu should
* only be visible on the MDI view.
*
* @param visible Whether or not the menu should be visible.
*/
public void setWindowMenuVisible(boolean visible) {
if (visible)
add(windowMenu, 4);
else
remove(windowMenu);
validate(); // Must call validate() to repaint menu bar.
}
/*****************************************************************************/
/**
* Overridden to make sure that the "Window" menu gets its look-and-feel
* updated too, even if it currently isn't visible.
*/
public void updateUI() {
super.updateUI();
// Update the Window menu only if we're NOT in MDI view (otherwise, it
// would have been updated by super.updateUI(ui)). We must also check
// windowMenu for null as this is called during initialization.
if (rtext!=null && rtext.getMainViewStyle()!=RText.MDI_VIEW &&
windowMenu!=null)
SwingUtilities.updateComponentTreeUI(windowMenu);
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -