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

📄 rtexteditorpane.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
字号:
/*
 * 11/14/2003
 *
 * RTextEditorPane.java - the text editor 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.Font;
import java.awt.Graphics;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.io.File;
import java.nio.charset.Charset;
import javax.swing.JComponent;

import org.fife.print.RPrintUtilities;
import org.fife.ui.rtextarea.RTATextTransferHandler;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;


/**
 * An extension of {@link org.fife.ui.rsyntaxtextarea.RSyntaxTextArea}
 * that adds information about the file being edited, such as:
 *
 * <ul>
 *   <li>Its name and location.</li>
 *   <li>Is it dirty (modified)?</li>
 *   <li>Is it read-only?</li>
 *   <li>The time it was last modified.</li>
 *   <li>The file's encoding on disk.</li>
 * </ul>
 *
 * @author Robert Futrell
 * @version 1.1
 */
public class RTextEditorPane extends RSyntaxTextArea implements Printable {

	public static final String FULL_PATH_PROPERTY	= "RTextEditorPane.fileFullPath";
	public static final String MODIFIED_PROPERTY		= "RTextEditorPane.isModified";
	public static final String READ_ONLY_PROPERTY	= "RTextEditorPane.isReadOnly";

	private static final String commonImagePath = "org/fife/rtext/graphics/common_icons/";

	private RText rtext;


	private File file;				// File on disk we're editing.
	private boolean isModified;		// Whether or not file has been modified.
	private boolean isReadOnly;		// Whether or not this file is read-only.
	private long lastModified;		// The last-modified timestamp.
	private String charSet;			// The name of a charset to encode/decode this file.


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


	/**
	 * Creates a new <code>RTextEditorPane</code>.  The file will be given
	 * a default name and placed in the current working directory with no
	 * syntax highlighting.
	 *
	 * @param rtext The owning RText instance.
	 * @param wordWrapEnabled Whether or not to use word wrap in this pane.
	 * @param textMode Either <code>INSERT_MODE</code> or
	 *                 <code>OVERWRITE_MODE</code>.
	 */
	public RTextEditorPane(RText rtext, boolean wordWrapEnabled,
						int textMode) {
		this(rtext, wordWrapEnabled, textMode,
					new File(rtext.getNewFileName()));
	}


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


	/**
	 * Creates a new <code>RTextEditorPane</code>.  Syntax highlighting will be
	 * selected as follows:  filenames ending in <code>".java"</code> default
	 * to Java syntax highlighting; all others default to no syntax
	 * highlighting.
	 *
	 * @param rtext The owning RText instance.
	 * @param wordWrapEnabled Whether or not to use word wrap in this pane.
	 * @param textMode Either <code>INSERT_MODE</code> or
	 *                 <code>OVERWRITE_MODE</code>.
	 * @param fileFullPath Full path to the file you wish to open in this
	 *                     editor.  If it's just a file name, file will be in
	 *                     the current directory.
	 */
	public RTextEditorPane(RText rtext, boolean wordWrapEnabled,
						int textMode, String fileFullPath) {
		this(rtext, wordWrapEnabled, textMode, new File(fileFullPath));
	}


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


	/**
	 * Creates a new <code>RTextEditorPane</code>.  Syntax highlighting will
	 * be selected as follows:  filenames ending in <code>".java"</code>
	 * default to Java syntax highlighting; all others default to no syntax
	 * highlighting.
	 *
	 * @param rtext The owning RText instance.
	 * @param wordWrapEnabled Whether or not to use word wrap in this pane.
	 * @param textMode Either <code>INSERT_MODE</code> or
	 *                 <code>OVERWRITE_MODE</code>.
	 * @param file The file to open in this text area.
	 */
	public RTextEditorPane(RText rtext, boolean wordWrapEnabled,
						int textMode, File file) {

		super(wordWrapEnabled, textMode);
		this.rtext = rtext;

		// Store all the data that used to be in RTextDocument.
		this.file = file;
		if (!file.exists())
			setReadOnly(false);
		else {
			setReadOnly(!file.canWrite());
			this.lastModified = file.lastModified();
		}
		isModified = false;

		// Change the transfer handler to one that recognizes drag-and-dropped
		// files as needing to be opened in the parent main view.
		setTransferHandler(new RTextEditorPaneTransferHandler());

	}


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


	/**
	 * Returns the encoding used when reading/writing this file to/from disk.
	 *
	 * @return The encoding.
	 * @see #setEncoding
	 */
	public String getEncoding() {
		return charSet;
	}


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


	/**
	 * Returns the full path to this document.
	 *
	 * @return The full path to the document.
	 */
	public String getFileFullPath() {
		return file.getAbsolutePath();
	}


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


	/**
	 * Returns the file name of this document.
	 *
	 * @return The file name.
	 */
	public String getFileName() {
		return file.getName();
	}


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


	/**
	 * Returns the "last modified" date of the file this editor pane
	 * represents.
	 *
	 * @return The last-modified timestamp for this file.
	 * @see #setLastModified
	 */
	public long getLastModified() {
		return lastModified;
	}


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


	/**
	 * Returns whether or not the document is modified.
	 *
	 * @return Whether or not the document is modified.
	 */
	public boolean isModified() {
		return isModified;
	}


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


	/**
	 * Returns whether or not the document is read-only.
	 *
	 * @return Whether or not the document is read-only.
	 */
	public boolean isReadOnly() {
		return isReadOnly;
	}


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


	/**
	 * Method called when it's time to print this badboy (the old-school, AWT
	 * way).  This method overrides <code>RTextArea</code>'s <code>print</code>
	 * method so that we can use the font specified in RText when printing.
	 *
	 * @param g The context into which the page is drawn.
	 * @param pageFormat The size and orientation of the page being drawn.
	 * @param pageIndex The zero based index of the page to be drawn.
	 */
	public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
		Font printWithMeFont = rtext.getMainView().getPrintFont();
		if (printWithMeFont==null)	// null => print with the current font.
			printWithMeFont = this.getFont();
		return RPrintUtilities.printDocumentWordWrap(g, this,
				printWithMeFont, pageIndex, pageFormat, this.getTabSize());
	}


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


	/**
	 * Sets the encoding used when reading/writing this file to/from disk.
	 *
	 * @param encoding The new encoding.  If this is invalid,
	 *        <code>US-ASCII</code> is used.
	 */
	public void setEncoding(String encoding) {
		if (!Charset.isSupported(encoding)) {
			System.err.println("Unsupported encoding!  Using ASCII.");
			encoding = "US-ASCII";
		}
		if (charSet==null || !charSet.equals(encoding))
			charSet = encoding;
	}


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


	/**
	 * Sets the full path to this document.  This fires a property change
	 * event of type <code>FULL_PATH_PROPERTY</code>.
	 *
	 * @param fileFullPath The full path to the document.
	 */
	public void setFileFullPath(String fileFullPath) {
		String old = getFileFullPath();
		file = new File(fileFullPath);
		firePropertyChange(FULL_PATH_PROPERTY, old, fileFullPath);
	}


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


	/**
	 * Sets the "last modified" date of the file this editor pane represents.
	 *
	 * @param lastModified The new last-modified timestamp for this document.
	 *        This should accurately reflect the actual file's last-modified
	 *        timestamp.
	 * @see #getLastModified
	 */
	public void setLastModified(long lastModified) {
		this.lastModified = lastModified;
	}


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


	/**
	 * Sets whether or not this document has been modified.  This fires a
	 * property change event of type <code>MODIFIED_PROPERTY</code>.
	 *
	 * @param modified Whether or not the document has beeen modified.
	 */
	public void setModified(boolean modified) {
		if (this.isModified!=modified) {
			firePropertyChange(MODIFIED_PROPERTY, this.isModified,
											modified);
			isModified = modified;
		}
	}


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


	/**
	 * Sets whether or not this document is read-only.  This fires a
	 * property change event of type <code>READ_ONLY_PROPERTY</code>.
	 *
	 * @param readOnly Whether or not the document is read-only.
	 */
	public void setReadOnly(boolean readOnly) {
		if (this.isReadOnly!=readOnly) {
			isReadOnly = readOnly;
			setEditable(!isReadOnly);
			firePropertyChange(READ_ONLY_PROPERTY, !isReadOnly, isReadOnly);
		}
	}


/*****************************************************************************/
/*********************** INNER CLASSES ***************************************/
/*****************************************************************************/


	/**
	 * Transfer handler for editor panes.  Overrides the default transfer
	 * handler so we can drag-and-drop files into a text area, and know to
	 * open it in the parent main view.
	 */
	class RTextEditorPaneTransferHandler extends RTATextTransferHandler {

		public boolean canImport(JComponent c, DataFlavor[] flavors) {
			return MainPanelTransferHandler.hasFileFlavor(flavors) ||
					super.canImport(c, flavors);
		}

		public boolean importData(JComponent c, Transferable t) {
			return MainPanelTransferHandler.
				importDataImpl(rtext.getMainView(), c, t) ||
						super.importData(c, t);
		}

	}


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

}

⌨️ 快捷键说明

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