📄 rtextmdiview.java
字号:
/**
* Returns the currently active component.
*
* @return The component.
*/
public Component getSelectedComponent() {
return desktopPane.getSelectedFrame();
}
/*****************************************************************************/
/**
* Returns the currently selected document's index.
*
* @return The index of the currently selected document.
*/
public int getSelectedIndex() {
return frames.indexOf(desktopPane.getSelectedFrame());
}
/*****************************************************************************/
/**
* Called when an internal frame is activated.
*/
public void internalFrameActivated(InternalFrameEvent e) {
currentTextArea = getRTextEditorPaneAt(getSelectedIndex());
// Update RText's title bar and status bar.
String title = currentTextArea.getFileFullPath() +
(currentTextArea.isModified() ? "*" : "");
owner.setMessages(title, null);
updateStatusBar(); // Updates read-only indicator and line/column.
// currentTextArea.requestFocusInWindow();
currentTextArea.getDocument().addDocumentListener(owner);
currentTextArea.addCaretListener(owner);
currentTextArea.addKeyListener(owner);
// Trick the parent RText into updating the row/column indicator.
owner.caretUpdate(null); // Null because caretUpdate doesn't actually use the caret event.
// Let any listeners know that the current document changed.
firePropertyChange(CURRENT_DOCUMENT_PROPERTY, -1, getSelectedIndex());
fireCurrentTextAreaEvent(CurrentTextAreaEvent.TEXT_AREA_CHANGED,
null, currentTextArea);
}
/*****************************************************************************/
/**
* Called when an internal frame is closed.
*/
public void internalFrameClosed(InternalFrameEvent e) {
}
/*****************************************************************************/
/**
* Called when an internal frame is closing.
*/
public void internalFrameClosing(InternalFrameEvent e) {
// We must ensure that the frame closing is the selected frame, because
// the user can click the "x" button of an internal frame that isn't the
// currently active frame.
setSelectedIndex(frames.indexOf(e.getInternalFrame()));
getAction(CLOSE_ACTION).actionPerformed(null);
}
/*****************************************************************************/
/**
* Called when an internal frame is deactivated.
*/
public void internalFrameDeactivated(InternalFrameEvent e) {
currentTextArea.getDocument().removeDocumentListener(owner);
currentTextArea.removeCaretListener(owner);
currentTextArea.removeKeyListener(owner);
}
/*****************************************************************************/
/**
* Called when an internal frame is de-iconified.
*/
public void internalFrameDeiconified(InternalFrameEvent e) {
}
/*****************************************************************************/
/**
* Called when an internal frame is iconified.
*/
public void internalFrameIconified(InternalFrameEvent e) {
}
/*****************************************************************************/
/**
* Called when an internal frame is opened.
*/
public void internalFrameOpened(InternalFrameEvent e) {
}
/*****************************************************************************/
/**
* Repaints the display names for open documents.
*/
public void refreshDisplayNames() {
/*
Color defaultForeground = (Color)UIManager.get("tabbedpane.foreground");
Color modifiedColor = getModifiedDocumentDisplayNamesColor();
int numDocuments = getNumDocuments();
if (highlightModifiedDocumentDisplayNames()==true) {
for (int i=0; i<numDocuments; i++) {
if (getRTextEditorPaneAt(i).isModified()==true)
tabbedPane.setForegroundAt(i, modifiedColor);
else
tabbedPane.setForegroundAt(i, defaultForeground);
}
}
else {
for (int i=0; i<numDocuments; i++)
tabbedPane.setForegroundAt(i, defaultForeground);
}
*/
}
/*****************************************************************************/
/**
* Removes a component from this container. Note that this method does not
* update currentTextArea, you must do that yourself.
*/
protected void removeComponentAt(int index) {
if (index>=0 && index<getNumDocuments()) {
((JInternalFrame)frames.get(index)).dispose();
frames.remove(index);
//tabbedPane.removeTabAt(index);
}
}
/*****************************************************************************/
/**
* Sets the name of the document displayed on the document's tab.
*
* @param index The index of the document whose name you want to change.
* If this value is invalid, this method does nothing.
* @param displayName The name to display.
* @see #getDocumentDisplayNameAt
*/
public void setDocumentDisplayNameAt(int index, String displayName) {
if (index>=0 && index<getNumDocuments()) {
((JInternalFrame)frames.get(index)).setTitle(displayName);
}
}
/*****************************************************************************/
/**
* Changes the location of the document selection area of this component.
*
* @param location The location to use; (<code>TOP</code>,
* <code>LEFT</code>, <code>BOTTOM</code>, or <code>RIGHT</code>.
* If this value is invalid, nothing happens.
*/
public void setDocumentSelectionPlacement(int location) {
if (location==DOCUMENT_SELECT_TOP || location==DOCUMENT_SELECT_LEFT ||
location==DOCUMENT_SELECT_BOTTOM || location==DOCUMENT_SELECT_RIGHT)
documentSelectionPlacement = location;
}
/*****************************************************************************/
/**
* Sets the currently active document. This updates currentTextArea.
*
* @param index The index of the document to make the active document. If this
* value is invalid, nothing happens.
*/
public void setSelectedIndex(int index) {
if (index>=0 && index<getNumDocuments()) {
JInternalFrame frame = (JInternalFrame)frames.get(index);
try {
frame.setSelected(true); // Updates currentTextArea via internalFrameActivated.
} catch (PropertyVetoException e) { }
//frame.toFront();
desktopPane.setSelectedFrame(frame);
}
}
/*****************************************************************************/
/**
* Tiles all open internal frames horizontally.
*/
public void tileWindowsHorizontally() {
int numFrames = frames.size();
int numToResize = 0;
for (int i=0; i<numFrames; i++) {
JInternalFrame frame = (JInternalFrame)frames.get(i);
if (frame.isVisible() && !frame.isIcon()) {
// Make sure any frames that can't be resized don't
// cover all our stuff up.
if (!frame.isResizable()) {
try {
frame.setMaximum(false);
} catch (PropertyVetoException e) { }
}
else
numToResize++;
} // End of if (frame.isVisible() && !frame.isIcon()).
} // End of for (int i=0; i<numFrames; i++).
if (numToResize>0) {
Rectangle desktopBounds = desktopPane.getBounds();
int desktopWidth = desktopBounds.width;
int desktopHeight = desktopBounds.height;
int fHeight = desktopHeight / numToResize;
int yPos = 0;
for (int i=0; i<numFrames; i++) {
JInternalFrame frame = (JInternalFrame)frames.get(i);
if (frame.isVisible() && frame.isResizable() && !frame.isIcon()) {
frame.setSize(desktopWidth, fHeight);
frame.setLocation(0, yPos);
yPos += fHeight;
}
} // End of for (int i=0; i<numFrames; i++).
} // End of if (numToResize>0).
}
/*****************************************************************************/
/**
* Tiles all open internal frames horizontally.
*/
public void tileWindowsVertically() {
int numFrames = frames.size();
int numToResize = 0;
for (int i=0; i<numFrames; i++) {
JInternalFrame frame = (JInternalFrame)frames.get(i);
if (frame.isVisible() && !frame.isIcon()) {
// Make sure any frames that can't be resized don't
// cover all our stuff up.
if (!frame.isResizable()) {
try {
frame.setMaximum(false);
} catch (PropertyVetoException e) { }
}
else
numToResize++;
} // End of if (frame.isVisible() && !frame.isIcon()).
} // End of for (int i=0; i<numFrames; i++).
if (numToResize>0) {
Rectangle desktopBounds = desktopPane.getBounds();
int desktopWidth = desktopBounds.width;
int desktopHeight = desktopBounds.height;
int fWidth = desktopWidth / numToResize;
int xPos = 0;
for (int i=0; i<numFrames; i++) {
JInternalFrame frame = (JInternalFrame)frames.get(i);
if (frame.isVisible() && frame.isResizable() && !frame.isIcon()) {
frame.setBounds(xPos,0, fWidth,desktopHeight);
xPos += fWidth;
}
} // End of for (int i=0; i<numFrames; i++).
} // End of if (numToResize>0).
}
/*****************************************************************************/
/**
* Overridden so we can update the right-click popup menu.
*/
public void updateUI() {
super.updateUI();
if (popupMenu!=null)
SwingUtilities.updateComponentTreeUI(popupMenu);
}
/*****************************************************************************/
/********************** PRIVATE INNER CLASSES ********************************/
/*****************************************************************************/
/**
* The internal frames used.
*/
class InternalFrame extends JInternalFrame {
private final int xOffset = 30;
private final int yOffset = 30;
public InternalFrame(String title, Component component) {
super(title, true, true, true, true);
this.setFrameIcon(getIconFor((RTextScrollPane)component));
java.awt.Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1,1));
contentPane.add(component);
++openFrameCount;
if (openFrameCount>8)
openFrameCount = 0;
setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
pack();
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}
}
/*****************************************************************************/
/**
* Listens for mouse events in the desktop pane.
*/
class MDIMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
public void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
if (popupMenu==null)
createPopupMenu();
popupMenu.show(desktopPane, e.getX(), e.getY());
}
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -