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

📄 rtexttabbedpaneview.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 11/14/2003
 *
 * RTextTabbedPaneView.java - A JTabbedPane containing multiple
 * text documents.
 * 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.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.SystemColor;
import java.awt.event.ContainerListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.ResourceBundle;
import javax.imageio.ImageIO;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputAdapter;
import javax.swing.plaf.TabbedPaneUI;

import org.fife.ui.DrawDnDIndicatorTabbedPane;
import org.fife.ui.RFileChooser;
import org.fife.ui.TabbedPaneTransferHandler;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxHighlightingColorScheme;


/**
 * An extension of <code>JTabbedPane</code> that contains documents being
 * edited by rtext.
 *
 * @author Robert Futrell
 * @version 1.0
 */
class RTextTabbedPaneView extends AbstractMainView implements ChangeListener {

	private TabbedPane tabbedPane;	// The actual visible component.

	private boolean inCloseCurrentDocument;

	private final TabbedPaneViewTransferHandler transferHandler =
								new TabbedPaneViewTransferHandler(this);


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


	/**
	 * Creates a new <code>RTextTabbedPaneView</code>.
	 *
	 * @param owner The <code>RText</code> that this tabbed pane 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 tabbed pane.
	 */
	public RTextTabbedPaneView(RText owner, String[] filesToOpen,
							RTextPreferences properties) {

		super();

		setLayout(new GridLayout(1,1));
		tabbedPane = new TabbedPane();
		tabbedPane.addChangeListener(this);
		//tabbedPane.setUI(new org.fife.RTextTabbedPaneViewUI());
		add(tabbedPane);

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

		// Set up our drag-and-drop handler.
		tabbedPane.setTransferHandler(transferHandler);
		try {
			tabbedPane.getDropTarget().addDropTargetListener(transferHandler);
		} catch (java.util.TooManyListenersException tmle) {
			owner.displayException(tmle);
		}
		TabDragListener tdl = new TabDragListener();
		tabbedPane.addMouseListener(tdl);
		tabbedPane.addMouseMotionListener(tdl);

	}


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


	/**
	 * Adds a change listener.
	 *
	 * @param listener The change listener.
	 */
	public void addChangeListener(ChangeListener listener) {
		tabbedPane.addChangeListener(listener);
	}


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


	/**
	 * Adds a container listener.
	 *
	 * @param listener The container listener.
	 */
	public void addContainerListener(ContainerListener listener) {
		tabbedPane.addContainerListener(listener);
	}


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


	/**
	 * Adds a tab to the tabbed pane, and places a number beside documents
	 * opened multiple times.  In rtext, this is the preferred way to add a tab
	 * (as opposed to <code>JTextPane</code>'s <code>addTab</code> methods).
	 *
	 * @param title The "display name" to use on the tab of the document.
	 * @param component The scroll pane containing the text editor to add.
	 * @param fileFullPath The path to the file this editor contains.
	 */
	public void addDocument(String title, Component component, String fileFullPath) {
		
		// "Physically" add the tab.
		tabbedPane.addTab(title, getIconFor((RTextScrollPane)component),
						component);
		currentTextArea = getRTextEditorPaneAt(getSelectedIndex());

		// Loop through all tabs (documents) except the last (the one just added).
		int tabCount = getNumDocuments();
		for (int i=0; i<tabCount-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<tabCount; 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) {
							owner.displayException(e);
						}
					}
				}
				break;
			}

		}

		// Do any extra stuff.
		// This updates currentTextArea and shifts focus too.
		setSelectedIndex(tabCount-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.
		
	}


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


	/**
	 * 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.
			else if (rc == JOptionPane.CANCEL_OPTION)
				return rc;

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

inCloseCurrentDocument = true;

		// Remove listeners from current text area IFF stateChanged() won't do it
		// (i.e., we're removing any document except the "rightmost" document).
//		boolean removingLastDocument = (getSelectedIndex()==getNumDocuments()-1);
//		if (removingLastDocument==false) {
			currentTextArea.getDocument().removeDocumentListener(owner);
			currentTextArea.removeCaretListener(owner);
			currentTextArea.removeKeyListener(owner);
//		}

		// 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();
		else
			addNewEmptyUntitledTextFile();

		// Request focus in the window for the new currentTextArea.
		// Note that this may also be done by stateChanged() but we need to
		// do it here too because code below relies on currentTextArea being
		// up-to-date.
		RTextEditorPane oldTextArea = currentTextArea;
		currentTextArea = getRTextEditorPaneAt(getSelectedIndex());
		// MUST be done by SwingUtilities.invokeLater(), I think because
		// currentTextArea is not yet visible on this line of code, so
		// calling requestFocusInWindow() now would do nothing.
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				currentTextArea.requestFocusInWindow();
			}
		});

		// Update RText's listeners IFF the active document number doesn't
		// change (i.e., they closed any document except the "rightmost" one).
		// Closing the "rightmost" document means stateChanged() will handle
		// the listeners.
//		if (removingLastDocument==false) {
			currentTextArea.getDocument().addDocumentListener(owner);
			currentTextArea.addCaretListener(owner);
			currentTextArea.addKeyListener(owner);
//		}

inCloseCurrentDocument = false;

		// Let any listeners know that the current document changed.
		firePropertyChange(CURRENT_DOCUMENT_PROPERTY, -1, getSelectedIndex());
		fireCurrentTextAreaEvent(CurrentTextAreaEvent.TEXT_AREA_CHANGED,
							oldTextArea, currentTextArea);

		// 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;

	}


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


	/**
	 * Returns the name being displayed for the specified 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<tabbedPane.getTabCount()) {
			return tabbedPane.getTitleAt(index);
		}
		return null;
	}


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


	/**
	 * Returns the location of the document selection area of this component.
	 *
	 * @return The location of the document selection area.
	 */
	public int getDocumentSelectionPlacement() {
		return tabbedPane.getTabPlacement();
	}


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


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


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


	/**
	 * Returns the <code>org.fife.rtext.RTextEditorPane</code> on the
	 * specified tab.  This is a convenience method for
	 * <code>(RTextEditorPane)getRTextScrollPaneAt(i).textArea</code>.

⌨️ 快捷键说明

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