TransferableTreeNode.java
上传用户:hltofo
上传日期:2021-08-24
资源大小:21k
文件大小:3k
- package net.antonioshome.swing.treewrapper.example;
- import java.awt.datatransfer.DataFlavor;
- import java.awt.datatransfer.Transferable;
- import java.awt.datatransfer.UnsupportedFlavorException;
- import java.io.IOException;
- import javax.swing.JTree;
- import javax.swing.tree.MutableTreeNode;
- import javax.swing.tree.TreeNode;
- public class TransferableTreeNode implements Transferable{
- private JTree sourceTree;//拖动节点的所在的树
- private MutableTreeNode sourceNode;//拖动的源节点
- private boolean wasExpand;//拖动的节点(如果不是叶子节点)是不是展开状态
- public TransferableTreeNode(JTree sourceTree, MutableTreeNode sourceNode, boolean wasExpand) {
- this.sourceTree = sourceTree;
- this.sourceNode = sourceNode;
- this.wasExpand = wasExpand;
- }
- private static DataFlavor javaJVMLocalObjectFlavor;
- private static DataFlavor[] supportedDataFlavors;
- public static DataFlavor getJavaJVMLocalObjectFlavor() {
- if (javaJVMLocalObjectFlavor == null) {
- try {
- javaJVMLocalObjectFlavor = new DataFlavor(
- DataFlavor.javaJVMLocalObjectMimeType);
- } catch (ClassNotFoundException cnfe) {
- System.err.println("Cannot create JVM Local Object Flavor "
- + cnfe.getMessage());
- }
- }
- return javaJVMLocalObjectFlavor;
- }
- private static DataFlavor[] getSupportedDataFlavors() {
- if (supportedDataFlavors == null) {
- DataFlavor localJVMFlavor = getJavaJVMLocalObjectFlavor();
- supportedDataFlavors = localJVMFlavor == null ? new DataFlavor[] { DataFlavor.stringFlavor }
- : new DataFlavor[] { localJVMFlavor,
- DataFlavor.stringFlavor };
- }
- return supportedDataFlavors;
- }
- public JTree getSourceTree() {
- return sourceTree;
- }
- public void setSourceTree(JTree sourceTree) {
- this.sourceTree = sourceTree;
- }
- public TreeNode getSourceNode() {
- return sourceNode;
- }
- public void setSourceNode(MutableTreeNode sourceNode) {
- this.sourceNode = sourceNode;
- }
- public boolean isWasExpand() {
- return wasExpand;
- }
- public boolean isDataFlavorSupported(DataFlavor flavor) {
- DataFlavor[] flavors = getSupportedDataFlavors();
- for (int i = 0; i < flavors.length; i++) {
- if (flavor.equals(flavors[i]))
- return true;
- }
- return false;
- }
- public Object getTransferData(DataFlavor flavor)
- throws UnsupportedFlavorException, IOException {
- if (flavor.equals(javaJVMLocalObjectFlavor)) {
- return this;
-
- } //如果传递的是String类型,用以下操作
- /*else if (flavor.equals(DataFlavor.stringFlavor)) {
- return getSourceNode().toString();
- } */
- else
- throw new UnsupportedFlavorException(flavor);
- }
- public DataFlavor[] getTransferDataFlavors() {
- return getSupportedDataFlavors();
- }
- }