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

📄 rtatexttransferhandler.java~1~

📁 具有不同语法高亮的编辑器实例
💻 JAVA~1~
📖 第 1 页 / 共 2 页
字号:
	}


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


	/**
	 * This method causes a transfer to a component from a clipboard or a 
	 * DND drop operation.  The Transferable represents the data to be
	 * imported into the component.  
	 *
	 * @param comp  The component to receive the transfer.  This
	 *  argument is provided to enable sharing of TransferHandlers by
	 *  multiple components.
	 * @param t The data to import
	 * @return <code>true</code> iff the data was inserted into the component.
	 */
	public boolean importData(JComponent comp, Transferable t) {

		JTextComponent c = (JTextComponent)comp;

		// if we are importing to the same component that we exported from
		// then don't actually do anything if the drop location is inside
		// the drag location and set shouldRemove to false so that exportDone
		// knows not to remove any data
		if (c==exportComp && c.getCaretPosition()>=p0 && c.getCaretPosition()<=p1) {
			shouldRemove = false;
			return true;
		}

		boolean imported = false;
		DataFlavor importFlavor = getImportFlavor(t.getTransferDataFlavors(), c);
		if (importFlavor != null) {
			try {
				InputContext ic = c.getInputContext();
				if (ic != null)
					ic.endComposition();
				Reader r = importFlavor.getReaderForText(t);
				handleReaderImport(r, c);
				imported = true;
			} catch (UnsupportedFlavorException ufe) {
			} catch (BadLocationException ble) {
			} catch (IOException ioe) {
			}
		}

		return imported;

	}

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


	/**
	 * This method indicates if a component would accept an import of the
	 * given set of data flavors prior to actually attempting to import it. 
	 *
	 * @param comp The component to receive the transfer.  This argument is
	 *        provided to enable sharing of TransferHandlers by multiple
	 *        components.
	 * @param flavors The data formats available.
	 * @return <code>true</code> iff the data can be inserted.
	 */
	public boolean canImport(JComponent comp, DataFlavor[] flavors) {
		JTextComponent c = (JTextComponent)comp;
		if (!(c.isEditable() && c.isEnabled()))
			return false;
		return (getImportFlavor(flavors, c) != null);
	}


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


	/**
	 * A possible implementation of the Transferable interface for RTextAreas.
	 */
	static class TextTransferable  implements Transferable {

		Position p0;
		Position p1;
		String mimeType;
		String richText;
		JTextComponent c;

		protected String plainData;

		private static DataFlavor[] stringFlavors;
		private static DataFlavor[] plainFlavors;

		TextTransferable(JTextComponent c, int start, int end) {
			this.c = c;
			Document doc = c.getDocument();
			try {
				p0 = doc.createPosition(start);
				p1 = doc.createPosition(end);
				plainData = c.getSelectedText();
			} catch (BadLocationException ble) {
			}
		}

		/**
		 * Fetch the data in a text/plain format.
		 */
		protected String getPlainData() {
			return plainData;
		}

		/**
		 * Returns an object which represents the data to be transferred.  The class 
		 * of the object returned is defined by the representation class of the flavor.
		 *
		 * @param flavor the requested flavor for the data
		 * @see DataFlavor#getRepresentationClass
		 * @exception IOException                if the data is no longer available
		 *              in the requested flavor.
		 * @exception UnsupportedFlavorException if the requested data flavor is
		 *              not supported.
		 */
		public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
			if (isPlainFlavor(flavor)) {
				String data = getPlainData();
				data = (data == null) ? "" : data;
				if (String.class.equals(flavor.getRepresentationClass())) {
					return data;
				} else if (Reader.class.equals(flavor.getRepresentationClass())) {
					return new StringReader(data);
				} else if (InputStream.class.equals(flavor.getRepresentationClass())) {
					return new StringBufferInputStream(data);
				}
					// fall through to unsupported
			} else if (isStringFlavor(flavor)) {
				String data = getPlainData();
				data = (data == null) ? "" : data;
				return data;
			}
			throw new UnsupportedFlavorException(flavor);
		}

		/**
		 * Returns an array of DataFlavor objects indicating the flavors the data 
		 * can be provided in.  The array should be ordered according to preference
		 * for providing the data (from most richly descriptive to least descriptive).
		 * @return an array of data flavors in which this data can be transferred
		 */
		public DataFlavor[] getTransferDataFlavors() {

			int nPlain = (isPlainSupported()) ? plainFlavors.length: 0;
			int nString = (isPlainSupported()) ? stringFlavors.length : 0;
			int nFlavors = nPlain + nString;
			DataFlavor[] flavors = new DataFlavor[nFlavors];
	
			// fill in the array
			int nDone = 0;
			if (nPlain > 0) {
				System.arraycopy(plainFlavors, 0, flavors, nDone, nPlain);
				nDone += nPlain;
			}
			if (nString > 0) {
				System.arraycopy(stringFlavors, 0, flavors, nDone, nString);
				nDone += nString;
			}
			return flavors;

		}

		/**
		 * Returns whether or not the specified data flavor is supported for
		 * this object.
		 * @param flavor the requested flavor for the data
		 * @return boolean indicating whether or not the data flavor is supported
		 */
		public boolean isDataFlavorSupported(DataFlavor flavor) {
			DataFlavor[] flavors = getTransferDataFlavors();
			for (int i = 0; i < flavors.length; i++) {
				if (flavors[i].equals(flavor))
					return true;
			}
			return false;
		}

		/**
		 * Returns whether or not the specified data flavor is an plain flavor that
		 * is supported.
		 * @param flavor the requested flavor for the data
		 * @return boolean indicating whether or not the data flavor is supported
		 */
		protected boolean isPlainFlavor(DataFlavor flavor) {
			DataFlavor[] flavors = plainFlavors;
			for (int i = 0; i < flavors.length; i++) {
				if (flavors[i].equals(flavor))
					return true;
			}
			return false;
		}

		/**
		 * Should the plain text flavors be offered?  If so, the method
		 * getPlainData should be implemented to provide something reasonable.
		 */
		protected boolean isPlainSupported() {
			return plainData != null;
		}

		/**
		 * Returns whether or not the specified data flavor is a String flavor that
		 * is supported.
		 * @param flavor the requested flavor for the data
		 * @return boolean indicating whether or not the data flavor is supported
		 */
		protected boolean isStringFlavor(DataFlavor flavor) {
			DataFlavor[] flavors = stringFlavors;
			for (int i = 0; i < flavors.length; i++) {
				if (flavors[i].equals(flavor))
					return true;
			}
			return false;
		}

		void removeText() {
			if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
				try {
					Document doc = c.getDocument();
					doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
				} catch (BadLocationException e) {
				}
			}
		}

		// Initialization of supported flavors.
		static {
			try {

				plainFlavors = new DataFlavor[3];
				plainFlavors[0] = new DataFlavor("text/plain;class=java.lang.String");
				plainFlavors[1] = new DataFlavor("text/plain;class=java.io.Reader");
				plainFlavors[2] = new DataFlavor("text/plain;charset=unicode;class=java.io.InputStream");

				stringFlavors = new DataFlavor[2];
				stringFlavors[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class=java.lang.String");
				stringFlavors[1] = DataFlavor.stringFlavor;
 
			} catch (ClassNotFoundException cle) {
				System.err.println("Error initializing org.fife.ui.RTATextTransferHandler");
			}
		}

	}


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

}

⌨️ 快捷键说明

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