📄 jtreedroptarget.java
字号:
/* * JRemCntl - Copyright (C) 2007 Filippo Di Vattimo <fildiv@gmail.com> * See COPYING */package fildiv.jremcntl.server.gui.util.swing;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.dnd.DnDConstants;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetDragEvent;import java.awt.dnd.DropTargetDropEvent;import java.awt.dnd.DropTargetEvent;import javax.swing.JTree;import javax.swing.tree.TreeCellRenderer;import javax.swing.tree.TreePath;public class JTreeDropTarget extends DropTarget { /** * */ private static final long serialVersionUID = 1L; private Insets autoscrollInsets = new Insets(20, 20, 20, 20); private TreeCellRenderer prevCellRender; public synchronized void dragEnter(DropTargetDragEvent dtde) { JTree tree = (JTree) dtde.getDropTargetContext().getComponent(); prevCellRender = tree.getCellRenderer(); tree.setCellRenderer(new JTreeDropCellRender()); super.dragEnter(dtde); } public void dragOver(DropTargetDragEvent dtde) { JTree tree = (JTree) dtde.getDropTargetContext().getComponent(); Point loc = dtde.getLocation(); manageDragOver(tree, loc, dtde); autoscroll(tree, loc); super.dragOver(dtde); } public synchronized void drop(DropTargetDropEvent dtde) { JTree tree = (JTree) dtde.getDropTargetContext().getComponent(); onDragFinish(tree); super.drop(dtde); } public synchronized void dragExit(DropTargetEvent dtde) { JTree tree = (JTree) dtde.getDropTargetContext().getComponent(); onDragFinish(tree); super.dragExit(dtde);} private void onDragFinish(JTree tree) { tree.setCellRenderer(prevCellRender); } private void autoscroll(JTree tree, Point cursorLocation) { Insets insets = autoscrollInsets; Rectangle outer = tree.getVisibleRect(); Rectangle inner = new Rectangle(outer.x + insets.left, outer.y + insets.top, outer.width - (insets.left + insets.right), outer.height - (insets.top + insets.bottom)); if (!inner.contains(cursorLocation)) { Rectangle scrollRect = new Rectangle( cursorLocation.x - insets.left, cursorLocation.y - insets.top, insets.left + insets.right, insets.top + insets.bottom); tree.scrollRectToVisible(scrollRect); } } public void manageDragOver(JTree tree, Point location, DropTargetDragEvent dtde) { int row = tree.getRowForPath(tree.getClosestPathForLocation(location.x, location.y)); TreePath path = tree.getPathForRow(row); if (path != null) { Object nodeOver = markNode(tree, location); if (nodeOver != null) { boolean acceptDrop = acceptDrop(tree, nodeOver); if (!acceptDrop) dtde.rejectDrag(); else dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); } } } private Object markNode(JTree tree, Point location) { Object nodeOver = null; TreePath path = tree.getClosestPathForLocation(location.x, location.y); if (path != null) { tree.setSelectionPath(path); if (canExpandPath(tree, nodeOver)) tree.expandPath(path); nodeOver = path.getLastPathComponent(); } return nodeOver; } protected boolean acceptDrop(JTree tree, Object nodeOver) { return true; } protected boolean canExpandPath(JTree tree, Object nodeOver) { return true; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -