/** * Handles mouse moved events. * * @param e the mouse event */ public void mouseMoved(MouseEvent e) { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path == null) return; if (e.getX() > tree.getPathBounds(path).x + hotspot - 3 || e.getX() < tree.getPathBounds(path).x + 2) tree.setCursor(Cursor.getDefaultCursor()); else { tree.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } }
@Nullable public static Object getTagAtForRenderer( CheckboxTree.CheckboxTreeCellRenderer renderer, MouseEvent e) { JTree tree = (JTree) e.getSource(); Object tag = null; final TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path != null) { final Rectangle rectangle = tree.getPathBounds(path); assert rectangle != null; int dx = e.getX() - rectangle.x; final TreeNode treeNode = (TreeNode) path.getLastPathComponent(); final int row = tree.getRowForLocation(e.getX(), e.getY()); tree.getCellRenderer() .getTreeCellRendererComponent(tree, treeNode, false, false, true, row, true); if (treeNode instanceof RepositoryNode) { RepositoryNode repositoryNode = (RepositoryNode) treeNode; int checkBoxWidth = repositoryNode.isCheckboxVisible() ? renderer.getCheckbox().getWidth() : 0; tag = renderer.getTextRenderer().getFragmentTagAt(dx - checkBoxWidth); } else { tag = renderer.getTextRenderer().getFragmentTagAt(dx); } } return tag; }
/** * Handles mouse click events. * * @param e the mouse event */ public void mouseClicked(MouseEvent e) { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path == null) return; if (e.getX() > tree.getPathBounds(path).x + hotspot) return; boolean selected = selectionModel.isAncestorSelected(path); try { ignoreEvents = true; if (selected) { selectionModel.removeSelectionPath(path); } else { selectionModel.addSelectionPath(path); } } finally { ignoreEvents = false; tree.treeDidChange(); } }
public static RelativePoint getPointForPath(JTree aTree, TreePath path) { final Rectangle rowBounds = aTree.getPathBounds(path); rowBounds.x += 20; return getPointForBounds(aTree, rowBounds); }