TreeWrapper.java
上传用户:hltofo
上传日期:2021-08-24
资源大小:21k
文件大小:8k
- package net.antonioshome.swing.treewrapper.example;
- import java.awt.datatransfer.StringSelection;
- import java.awt.dnd.DnDConstants;
- import java.awt.dnd.DragGestureEvent;
- import java.awt.dnd.DragGestureListener;
- import java.awt.dnd.DragGestureRecognizer;
- import java.awt.dnd.DragSource;
- import java.awt.dnd.DragSourceDragEvent;
- import java.awt.dnd.DragSourceDropEvent;
- import java.awt.dnd.DragSourceEvent;
- import java.awt.dnd.DragSourceListener;
- import java.awt.dnd.DropTarget;
- import java.awt.dnd.DropTargetDragEvent;
- import java.awt.dnd.DropTargetDropEvent;
- import java.awt.dnd.DropTargetEvent;
- import java.awt.dnd.DropTargetListener;
- import javax.swing.JTree;
- import javax.swing.tree.DefaultTreeModel;
- import javax.swing.tree.MutableTreeNode;
- import javax.swing.tree.TreeNode;
- import javax.swing.tree.TreePath;
- public class TreeWrapper {
- private JTree tree;
- private DragSource dragSource;
- private DnDNode dndNode = new DnDNode();
- private MutableTreeNode dropNode;
- public TreeWrapper(JTree aTree) {
- tree = aTree;
- initHandler();
- }
- private void initHandler() {
- dragSource = new DragSource();
- DragGestureRecognizer draGestureRecognizer = dragSource
- .createDefaultDragGestureRecognizer(tree,
- DnDConstants.ACTION_COPY_OR_MOVE,
- new TreeDragGestureListener());
- DropTarget dropTarget = new DropTarget(tree,
- DnDConstants.ACTION_COPY_OR_MOVE, new TreeDropTargetListener());
- }
- class TreeDragGestureListener implements DragGestureListener {
- public void dragGestureRecognized(DragGestureEvent dge) {
- if (!tree.isEnabled())
- return;
- TreePath draggedPath = tree.getClosestPathForLocation(dge
- .getDragOrigin().x, dge.getDragOrigin().y);
- if (draggedPath == null)
- return;
- TreeNode node = (TreeNode) draggedPath.getLastPathComponent();
- if (node instanceof MutableTreeNode && (node.getParent() != null)
- && (node.getParent() instanceof MutableTreeNode))
- ;
- else
- return;
- TransferableTreeNode transferableNode = new TransferableTreeNode(
- tree, (MutableTreeNode) node, tree.isExpanded(draggedPath));
- dndNode.setDraggedNode(node);
-
- dragSource.startDrag(dge, null, transferableNode,
- new TreeDragSourceListener());
- }
- }
- class TreeDragSourceListener implements DragSourceListener {
- public void dragDropEnd(DragSourceDropEvent arg0) {
- resetDragAndDrop();
- }
- public void dragEnter(DragSourceDragEvent arg0) {
- }
- public void dragExit(DragSourceEvent arg0) {
- }
- public void dragOver(DragSourceDragEvent arg0) {
- }
- public void dropActionChanged(DragSourceDragEvent arg0) {
- }
- }
- class TreeDropTargetListener implements DropTargetListener {
- private TreeNode lastDragOverNode;
- public void dragOver(DropTargetDragEvent dtde) {
- System.out.println("DragOver----------------");
- if (!tree.isEnabled()) {
- dtde.rejectDrag();
- return;
- }
- TreePath dropPath = tree.getClosestPathForLocation(dtde
- .getLocation().x, dtde.getLocation().y);
- TreeNode currentDropNode = (TreeNode) dropPath
- .getLastPathComponent();
- if (dropPath == null || currentDropNode == null
- || currentDropNode.equals(lastDragOverNode))
- return;
- else
- lastDragOverNode = currentDropNode;
- boolean mayDropHere = false;
- try {
- //这里取得传输数据是为了判断要放的位置合不合法
- TransferableTreeNode transferableNode = (TransferableTreeNode) dtde
- .getTransferable().getTransferData(
- TransferableTreeNode
- .getJavaJVMLocalObjectFlavor());
- JTree sourceTree = transferableNode.getSourceTree();
- MutableTreeNode sourceNode = (MutableTreeNode) transferableNode
- .getSourceNode();
- if (mayDropHere(sourceTree, sourceNode, dropPath)) {
- dropNode = (MutableTreeNode) dropPath
- .getLastPathComponent();
- if (!tree.isExpanded(dropPath))
- tree.expandPath(dropPath);
- mayDropHere = true;
- } else {
- dropNode = null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- dropNode = null;
- }
- dndNode.setDropAllowed(mayDropHere);
- dndNode.setDropNode((TreeNode) dropPath.getLastPathComponent());
- tree.repaint();
- if (!mayDropHere)
- dtde.rejectDrag();
- else
- dtde.acceptDrag(dtde.getDropAction());
- tree.repaint();
- }
- public boolean mayDropHere(JTree sourceTree,
- MutableTreeNode sourceNode, TreePath dropPath) {
- if (dropPath == null)
- return false;
- return mayDropHere(sourceTree, sourceNode, (TreeNode) dropPath
- .getLastPathComponent());
- }
- public boolean mayDropHere(JTree sourceTree,
- MutableTreeNode sourceNode, TreeNode aNode) {
- boolean mayDropHere = (aNode != sourceNode)
- && (aNode instanceof MutableTreeNode)
- && ((aNode.getParent() == null) || (aNode.getParent() instanceof MutableTreeNode))
- && (tree.getModel() instanceof DefaultTreeModel)
- && !((tree == sourceTree) && isAncestorOf(sourceNode, aNode));
- return mayDropHere;
- }
- private boolean isAncestorOf(TreeNode aPossibleParent, TreeNode aNode) {
- if (aPossibleParent == null || aNode.getParent() == null)
- return false;
- else if (aNode.getParent() == aPossibleParent)//把父节点拖放都子节点上时
- return true;
- else
- return isAncestorOf(aPossibleParent, aNode.getParent());
- }
- //鼠标点击结束时才会执行
- public void drop(DropTargetDropEvent dtde) {
- System.out.println("drag-------------------");
- TreePath dropPath = tree.getClosestPathForLocation(dtde
- .getLocation().x, dtde.getLocation().y);
- if (!tree.isEnabled() || dropPath == null) {
- dtde.rejectDrop();
- dtde.dropComplete(false);
- resetDragAndDrop();
- return;
- }
- dropNode = (MutableTreeNode) dropPath.getLastPathComponent();
- try {
- dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
- TransferableTreeNode ttn = (TransferableTreeNode) dtde
- .getTransferable().getTransferData(
- TransferableTreeNode
- .getJavaJVMLocalObjectFlavor());
- JTree sourceTree = ttn.getSourceTree();
- MutableTreeNode sourceNode = (MutableTreeNode) ttn
- .getSourceNode();
- if (mayDropHere(sourceTree, sourceNode, dropNode)) {
- dtde
- .dropComplete(dropNodes(
- ttn.getSourceTree(),
- ttn.getSourceNode(),
- tree,
- dropNode,
- (dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0));
- if (ttn.isWasExpand()) {
- DefaultTreeModel targetModel = (DefaultTreeModel) tree
- .getModel();
- System.out.println("树中该节点处的深度:" + targetModel.getPathToRoot(ttn.getSourceNode()).length);
- //如果从根节点到该节点的路径上的所有节点都不是叶子节点则都要把它展开,也就是能看到最后一个节点的子节点
- tree.expandPath(new TreePath(targetModel
- .getPathToRoot(ttn.getSourceNode())));
- }
- resetDragAndDrop();
- } else {
- try {
- dtde.rejectDrop();
- } catch (Exception e) {
- }
- dtde.dropComplete(false);
- resetDragAndDrop();
- return;
- }
- } catch (Exception e) {
- e.printStackTrace();
- dtde.rejectDrop();
- dtde.dropComplete(true);
- resetDragAndDrop();
- return;
- }
- }
- private boolean dropNodes(JTree aSourceTree,
- TreeNode treeNode, JTree aTargetTree,
- MutableTreeNode aDropNode, boolean move) {
- DefaultTreeModel sourceModel = (DefaultTreeModel) aSourceTree
- .getModel();
- DefaultTreeModel targetModel = (DefaultTreeModel) aTargetTree
- .getModel();
- MutableTreeNode sourceNodeCopy = (MutableTreeNode) treeNode;
- if (move) {
- sourceModel.removeNodeFromParent((MutableTreeNode) treeNode);
- }
- MutableTreeNode parent = (MutableTreeNode) aDropNode.getParent();
- if (aDropNode.isLeaf() && parent != null) {
- int index = parent.getIndex(aDropNode);
- // 把拖动的节点放在目标节点的下一个位置
- targetModel.insertNodeInto(sourceNodeCopy, parent, index + 1);
- } else {
- targetModel.insertNodeInto(sourceNodeCopy, aDropNode, 0);// aDropNode.getChildCount()
- // );
- }
- return true;
- }
-
- public void dropActionChanged(DropTargetDragEvent dtde) {
- }
- public void dragEnter(DropTargetDragEvent dtde) {
- dragOver(dtde);
- }
- public void dragExit(DropTargetEvent dte) {
- dropNode = null;
- dndNode.setDropNode(null);
- tree.repaint();
- }
- }
- public void resetDragAndDrop() {
- dropNode = null;
- dndNode.setDraggedNode(null);
- dndNode.setDropNode(null);
- dndNode.setDropAllowed(false);
- tree.repaint();
- }
- }