⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtextmdiview.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 04/16/2004
 *
 * RTextMDIView.java - A multi-document interface implementation.
 * Copyright (C) 2004 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.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import org.fife.ui.rtextarea.RTextScrollPane;


/**
 * An implementation of a multi-document interface.
 *
 * @author Robert Futrell
 * @version 1.0
 */
class RTextMDIView extends AbstractMainView implements InternalFrameListener {

	private static final int CASCADE_X_INCREMENT		= 30;
	private static final int CASCADE_Y_INCREMENT		= 30;

	private static int openFrameCount = 0;

	private JDesktopPane desktopPane;
	private ArrayList frames;

	private JPopupMenu popupMenu;

	// Whether documentList should be in the top, left, bottom, or right pane.
	private int documentSelectionPlacement;


/*****************************************************************************/


	/**
	 * Creates a new <code>RTextMDIView</code>.
	 *
	 * @param owner The <code>RText</code> that this view sits in.
	 * @param filesToOpen Array of strings representing files to open.  If
	 *        this parameter is null, a single file with a default name is
	 *        opened.
	 * @param properties A properties object used to initialize some fields on
	 *        this view.
	 */
	public RTextMDIView(RText owner, String[] filesToOpen,
									RTextPreferences properties) {

		super();

		frames = new ArrayList(5);

		setLayout(new GridLayout(1,1));
		desktopPane = new JDesktopPane();
		desktopPane.setBackground(Color.GRAY);
		desktopPane.addMouseListener(new MDIMouseListener());
		add(desktopPane);

		// Add transfer handler to listen for files being drag-and-dropped
		// into this main view.
		TransferHandler th = new MainPanelTransferHandler(this);
		desktopPane.setTransferHandler(th);

		// Set everything up.
		initialize(owner, filesToOpen, properties);

	}


/*****************************************************************************/


	/**
	 * Adds a new document to the desktop pae and places a number beside documents
	 * opened multiple times.
	 *
	 * @param title The "display name" for the document.
	 * @param component The scroll pane containing the text editor to add.
	 * @param fileFullPath The full path to the document being added.
	 */
	public void addDocument(String title, Component component, String fileFullPath) {

		// "Physically" add the frame.
		InternalFrame frame = new InternalFrame(title, component);
		frame.setVisible(true);	// Necessary.
		frame.addInternalFrameListener(this);
		desktopPane.add(frame);
		frames.add(frame);

		// Loop through all tabs (documents) except the last (the one just added).
		int numDocuments = getNumDocuments();
		for (int i=0; i<numDocuments-1; i++) {

			// If any of them is the same physical file as the just added one, do the numbering.
			if (getRTextEditorPaneAt(i).getFileFullPath().equals(fileFullPath)) {
				int count = 0;
				for (int j=i; j<numDocuments; j++) {
					RTextEditorPane pane = getRTextEditorPaneAt(j);
					if (pane.getFileFullPath().equals(fileFullPath)) {
						String newTitle = title + " (" + (++count) + ")";
						if (pane.isModified())
							newTitle = newTitle + "*";
						try {
							setDocumentDisplayNameAt(j, newTitle);
						} catch (Exception e) { System.err.println("Exception: " + e); }
					}
				}
				break;
			}

		}

		// Do any extra stuff.
		// This updates currentTextArea and shifts focus too.
		setSelectedIndex(numDocuments-1);
		if (currentTextArea.isModified())
			owner.setMessages(fileFullPath + "*", "Opened document '" + fileFullPath + "'");
		else
			owner.setMessages(fileFullPath, "Opened document '" + fileFullPath + "'");
		
		// RText's listeners will be updated by stateChanged() for all addDocument() calls.
		
	}


/*****************************************************************************/


	/**
	 * Cascades the windows in this MDI view.
	 */
	public void cascadeWindows() {

		int x = 0;
		int y = 0;
		int numRows = 0;
		Dimension desktopSize = desktopPane.getSize();

		int size = frames.size();
		int selectedIndex = getSelectedIndex();
		for (int i=0; i<size; i++) {
			if (i==selectedIndex)
				continue;
			JInternalFrame frame = (JInternalFrame)frames.get(i);
			frame.setBounds(x,y, 300,300);
			frame.toFront();
			x += CASCADE_X_INCREMENT;
			y += CASCADE_Y_INCREMENT;
			if (y + 300 >= desktopSize.height) {
				x = (CASCADE_X_INCREMENT*2) * ++numRows + CASCADE_X_INCREMENT/2;
				y = 0;
			}
		}

		// Make it so current text area is "last" to be cascaded by swapping its place
		// with the last frame's place.
		JInternalFrame currentFrame = (JInternalFrame)frames.get(selectedIndex);
		currentFrame.setBounds(x,y, 300,300);
		currentFrame.toFront();

	}


/*****************************************************************************/


	/**
	 * Attempts to close the current document.  This method is synchronized so
	 * it doesn't interfere with the thread checking for files being modified
	 * outside of the editor.
	 *
	 * @return JOptionPane.YES_OPTION/NO_OPTION/CANCEL_OPTION, depending on
	 *         what the user does.
	 */
	public synchronized int closeCurrentDocument() {

		ResourceBundle msg = owner.getResourceBundle();

		// Return code for if the user is prompted to save; returns yes for
		// closeAllDocuments().
		int rc = JOptionPane.YES_OPTION;

		// If the current document has been modified, prompt them to save it.
		if (currentTextArea.isModified() == true) {

			// We must get it as a regular expression because replaceFirst
			// expects one.
			String temp = RTextUtilities.getRegularExpressionForLine(currentTextArea.getFileName(), true);
			if (temp!=null)
				temp = msg.getString("SaveChangesPrompt").replaceFirst("%file%", temp);
			else
				temp = msg.getString("SaveChangesPrompt"); // Just leave the %file% in there.

			// The prompting dialog.
			rc = JOptionPane.showConfirmDialog(null, temp,
					msg.getString("ConfDialogTitle"), JOptionPane.YES_NO_CANCEL_OPTION);

			// If they decide to save...
			if (rc == JOptionPane.YES_OPTION) {

				boolean rc2 = saveCurrentDocument(false);
				if (rc2==false)
					return JOptionPane.CANCEL_OPTION;

			} // End of if (rc == JOptionPane.YES_OPTION).

			// If they choose to Cancel (NOT "No" to saving), quit the whole schibang.
			else if (rc == JOptionPane.CANCEL_OPTION)
				return rc;

		} // End of if ( ((RTextDocument)currentTextArea).isModified() == true).

		// Remove the document from this tabbed pane.
		removeComponentAt(getSelectedIndex());

		// If there are open documents, make sure any duplicates are numbered correctly.
		// If there are no open documents, add a new empty one.
		if (getNumDocuments()>0) {
			renumberDisplayNames();
			JInternalFrame frame = (JInternalFrame)frames.get(0);
			desktopPane.setSelectedFrame(frame);
			try {
				frame.setSelected(true); // Updates currentTextArea.
			} catch (PropertyVetoException e) { }
			frame.toFront();
		}
		else
			addNewEmptyUntitledTextFile();

		// Update the RText's status bar.
		updateStatusBar();

		// Update RText's title and the status bar message.
		if (currentTextArea.isModified())
			owner.setMessages(currentTextArea.getFileFullPath() + "*", msg.getString("Ready"));
		else
			owner.setMessages(currentTextArea.getFileFullPath(), msg.getString("Ready"));

		// Return JOptionPane.YES_OPTION or JOptionPane.NO_OPTION.
		return rc;

	}


/*****************************************************************************/


	/**
	 * Creates the popup menu for the desktop pane.
	 */
	protected void createPopupMenu() {

		popupMenu = new JPopupMenu();

		JMenuItem menuItem = new JMenuItem(new AbstractAction() {
								public void actionPerformed(java.awt.event.ActionEvent e) {
									tileWindowsVertically();
								}
							});
		menuItem.setText("Tile Vertically");
		popupMenu.add(menuItem);

		menuItem = new JMenuItem(new AbstractAction() {
								public void actionPerformed(java.awt.event.ActionEvent e) {
									tileWindowsHorizontally();
								}
							});
		menuItem.setText("Tile Horizontally");
		popupMenu.add(menuItem);

		menuItem = new JMenuItem(new AbstractAction() {
								public void actionPerformed(java.awt.event.ActionEvent e) {
									cascadeWindows();
								}
							});
		menuItem.setText("Cascade");
		popupMenu.add(menuItem);

	}


/*****************************************************************************/


	/**
	 * Returns the name being displayed for the document.  For example, in a tabbed
	 * pane, this could be the text on the tab for this document.
	 *
	 * @param index The index at which to find the name.  If the index is invalid,
	 *              <code>null</code> is returned.
	 * @return The name being displayed for this document.
	 */
	public String getDocumentDisplayNameAt(int index) {
		if (index>=0 && index<getNumDocuments()) {
			return ((JInternalFrame)frames.get(index)).getTitle();
		}
		return null;
	}


/*****************************************************************************/


	/**
	 * Returns the location of the document selection area of this component.
	 * Note that this value currently has no effect on an an instance of
	 * <code>RTextMDIView</code>.
	 *
	 * @return The location of the document selection area.
	 */
	public int getDocumentSelectionPlacement() {
		return documentSelectionPlacement;
	}


/*****************************************************************************/


	/**
	 * Returns the number of documents open in this container.
	 *
	 * @return The number of open documents.
	 */
	public int getNumDocuments() {
		return frames.size();
	}


/*****************************************************************************/


	/**
	 * Returns the preferred size of this MDI view.
	 *
	 * @return The preferred size of this view.
	 */
	public Dimension getPreferredSize() {
		Dimension preferredSize = new Dimension(300,300); // Default value.
		int numDocuments = frames.size();
		for (int i=0; i<numDocuments; i++) {
			Dimension framePreferredSize = ((JInternalFrame)frames.get(i)).getPreferredSize();
			preferredSize.width = Math.max(framePreferredSize.width, preferredSize.width);
			preferredSize.height = Math.max(framePreferredSize.height, preferredSize.height);
		}
		return preferredSize;
	}


/*****************************************************************************/


	/**
	 * Returns the <code>org.fife.rtext.RTextEditorPane</code> on the specified tab.
	 * This is a convenience method for
	 * <code>((RTextEditorPane) ((RTextScrollPane)getComponentAt(i)).textArea)</code>
	 *
	 * @param index The tab for which you want to get the <code>org.fife.rtext.RTextEditorPane</code>.
	 * @return The corresponding <code>org.fife.rtext.RTextEditorPane</code>.
	 */
	public RTextEditorPane getRTextEditorPaneAt(int index) {
		if (index<0 || index>=getNumDocuments())
			//throw new IndexOutOfBoundsException();
			return null;
		return (RTextEditorPane)((RTextScrollPane)((JInternalFrame)frames.get(index)).getContentPane().getComponent(0)).textArea;
	}


/*****************************************************************************/


	/**
	 * Returns the scroll pane at the specified tab.
	 *
	 * @param index The tab for which you want to get the scroll pane.
	 * @return The scroll pane.
	 */
	public RTextScrollPane getRTextScrollPaneAt(int index) {
		if (index<0 || index>=getNumDocuments())
			//throw new IndexOutOfBoundsException();
			return null;
		return (RTextScrollPane)((JInternalFrame)frames.get(index)).getContentPane().getComponent(0);
	}


/*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -