@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 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)); } }
public static void dropSelectionButUnderPoint(JTree tree, Point treePoint) { final TreePath toRetain = tree.getPathForLocation(treePoint.x, treePoint.y); if (toRetain == null) return; TreePath[] selection = tree.getSelectionModel().getSelectionPaths(); selection = selection == null ? new TreePath[0] : selection; for (TreePath each : selection) { if (toRetain.equals(each)) continue; tree.getSelectionModel().removeSelectionPath(each); } }
/** * 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 boolean isOverSelection(final JTree tree, final Point point) { TreePath path = tree.getPathForLocation(point.x, point.y); return path != null && tree.getSelectionModel().isPathSelected(path); }