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

📄 rtatexttransferhandler.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
📖 第 1 页 / 共 2 页
字号:
/*
 * 07/29/2004
 *
 * RTATextTransferHandler.java - Handles the transfer of data to/from an
 *                 RTextArea via drag-and-drop.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program 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.
 *
 * This program 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.ui.rtextarea;

import java.awt.datatransfer.*;
import java.awt.im.InputContext;
import java.io.InputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import javax.swing.*;
import javax.swing.text.*;


/**
 * Handles the transfer of data to/from an <code>RTextArea</code> via
 * drag-and-drop.  This class is pretty much ripped off from a subclass of
 * <code>BasicTextUI</code>.  In the future, it will include the ability to
 * drag-and-drop files into <code>RTextArea</code>s (i.e., the text will be
 * inserted into the text area).
 *
 * @author Robert Futrell
 * @version 0.1
 */
public class RTATextTransferHandler extends TransferHandler {
        
	/**
	 * 
	 */
	private static final long serialVersionUID = 8299135968359055298L;
	private JTextComponent exportComp;
	private boolean shouldRemove;
	private int p0;
	private int p1;


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


	/**
	 * Try to find a flavor that can be used to import a Transferable to a
	 * specified text component.  
	 * The set of usable flavors are tried in the following order:
	 * <ol>
	 *     <li>First, an attempt is made to find a flavor matching the content
	 *         tyep of the EditorKit for the component.
	 *     <li>Second, an attempt to find a text/plain flavor is made.
	 *     <li>Third, an attempt to find a flavor representing a String
	 *         reference in the same VM is made.
	 *     <li>Lastly, DataFlavor.stringFlavor is searched for.
	 * </ol>
	 *
	 * @param flavors The flavors to check if c will accept them.
	 * @param c The text component to see whether it will accept any of the
	 *          specified data flavors as input.
	 */
	 protected DataFlavor getImportFlavor(DataFlavor[] flavors,
	 								JTextComponent c) {

		DataFlavor refFlavor = null;
		DataFlavor stringFlavor = null;
            
		for (int i = 0; i < flavors.length; i++) {

			String mime = flavors[i].getMimeType();
			if (mime.startsWith("text/plain")) {
				return flavors[i];
			}
			else if (refFlavor == null
				&& mime.startsWith("application/x-java-jvm-local-objectref")
				&& flavors[i].getRepresentationClass() == String.class)
			{
				refFlavor = flavors[i];
			}
			else if (stringFlavor==null &&
				flavors[i].equals(DataFlavor.stringFlavor))
			{
				stringFlavor = flavors[i];
			}

		}

		if (refFlavor != null)
			return refFlavor;
		else if (stringFlavor != null)
			return stringFlavor;

		return null;

	}


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


	/**
	 * Import the given stream data into the text component.
	 */
	protected void handleReaderImport(Reader in, JTextComponent c)
							throws BadLocationException, IOException {

		char[] buff = new char[1024];
		int nch;
		boolean lastWasCR = false;
		int last;
		StringBuffer sbuff = null;
                
		// Read in a block at a time, mapping \r\n to \n, as well as single
		// \r to \n.
		while ((nch = in.read(buff, 0, buff.length)) != -1) {

			if (sbuff == null) {
				sbuff = new StringBuffer(nch);
                   }
			last = 0;

			for (int counter = 0; counter < nch; counter++) {

				switch (buff[counter]) {
					case '\r':
						if (lastWasCR) {
							if (counter == 0)
								sbuff.append('\n');
							else
								buff[counter - 1] = '\n';
						}
						else
							lastWasCR = true;
						break;
					case '\n':
						if (lastWasCR) {
							if (counter > (last + 1))
								sbuff.append(buff, last, counter - last - 1);
							// else nothing to do, can skip \r, next write will
							// write \n
							lastWasCR = false;
							last = counter;
						}
						break;
					default:
						if (lastWasCR) {
							if (counter == 0)
								sbuff.append('\n');
							else
								buff[counter - 1] = '\n';
							lastWasCR = false;
						}
						break;

				} // End fo switch (buff[counter]).

			} // End of for (int counter = 0; counter < nch; counter++).

			if (last < nch) {
				if (lastWasCR) {
					if (last < (nch - 1))
						sbuff.append(buff, last, nch - last - 1);
				}
					else
					sbuff.append(buff, last, nch - last);
			}

		} // End of while ((nch = in.read(buff, 0, buff.length)) != -1).

		if (lastWasCR)
			sbuff.append('\n');
		c.replaceSelection(sbuff != null ? sbuff.toString() : "");

	}


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


	/**
	 * This is the type of transfer actions supported by the source.  Some
	 * models are not mutable, so a transfer operation of COPY only should
	 * be advertised in that case.
	 * 
	 * @param c  The component holding the data to be transfered.  This
	 *  argument is provided to enable sharing of TransferHandlers by
	 *  multiple components.
	 * @return If the text component is editable, COPY_OR_MOVE is returned,
	 *         otherwise just COPY is allowed.
	 */
	public int getSourceActions(JComponent c) {
		if (((JTextComponent)c).isEditable())
			return COPY_OR_MOVE;
		else
			return COPY;
	}


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


	/**
	 * Create a Transferable to use as the source for a data transfer.
	 *
	 * @param comp  The component holding the data to be transfered.  This
	 *  argument is provided to enable sharing of TransferHandlers by
	 *  multiple components.
	 * @return  The representation of the data to be transfered. 
	 *  
	 */
	protected Transferable createTransferable(JComponent comp) {
		exportComp = (JTextComponent)comp;
		shouldRemove = true;
		p0 = exportComp.getSelectionStart();
		p1 = exportComp.getSelectionEnd();
		return (p0 != p1) ? (new TextTransferable(exportComp, p0, p1)) : null;
	}


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


	/**
	 * This method is called after data has been exported.  This method should
	 * remove the data that was transfered if the action was MOVE.
	 *
	 * @param source The component that was the source of the data.
	 * @param data   The data that was transferred or possibly null
      *               if the action is <code>NONE</code>.
	 * @param action The actual action that was performed.  
	 */
	protected void exportDone(JComponent source, Transferable data, int action) {
		// only remove the text if shouldRemove has not been set to
		// false by importData and only if the action is a move
		if (shouldRemove && action == MOVE) {
			TextTransferable t = (TextTransferable)data;
			t.removeText();
		}
		exportComp = null;

⌨️ 快捷键说明

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