From 5ca728555858541a665d1a4f43af9660ba88d4c6 Mon Sep 17 00:00:00 2001 From: Skylot Date: Wed, 12 Jan 2022 15:40:51 +0000 Subject: [PATCH] fix(gui): correct handling for tree row click (#1324) --- .../src/main/java/jadx/gui/ui/MainWindow.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java b/jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java index 041d1761c6f..3190cbaf67c 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java @@ -9,6 +9,7 @@ import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.HeadlessException; +import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTarget; @@ -704,7 +705,7 @@ private void nodeClickAction(@Nullable Object obj) { } private void treeRightClickAction(MouseEvent e) { - JNode obj = getJNodeUnderMouse(e, false); + JNode obj = getJNodeUnderMouse(e); if (obj instanceof JPackage) { JPackagePopupMenu menu = new JPackagePopupMenu(this, (JPackage) obj); menu.show(e.getComponent(), e.getX(), e.getY()); @@ -718,17 +719,26 @@ private void treeRightClickAction(MouseEvent e) { } @Nullable - private JNode getJNodeUnderMouse(MouseEvent mouseEvent, boolean trySelection) { - TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY()); - if (path == null && trySelection) { - // maybe click on node row (mouse pressed event should select this node in tree) - path = tree.getSelectionPath(); + private JNode getJNodeUnderMouse(MouseEvent mouseEvent) { + TreePath path = tree.getClosestPathForLocation(mouseEvent.getX(), mouseEvent.getY()); + if (path == null) { + return null; } - if (path != null) { - Object obj = path.getLastPathComponent(); - if (obj instanceof JNode) { - return (JNode) obj; + // allow 'closest' path only at the right of the item row + Rectangle pathBounds = tree.getPathBounds(path); + if (pathBounds != null) { + int y = mouseEvent.getY(); + if (y < pathBounds.y || y > (pathBounds.y + pathBounds.height)) { + return null; } + if (mouseEvent.getX() < pathBounds.x) { + // exclude expand/collapse events + return null; + } + } + Object obj = path.getLastPathComponent(); + if (obj instanceof JNode) { + return (JNode) obj; } return null; } @@ -1081,16 +1091,10 @@ private void initUI() { ToolTipManager.sharedInstance().registerComponent(tree); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addMouseListener(new MouseAdapter() { - - @Override - public void mousePressed(MouseEvent e) { - super.mousePressed(e); - } - @Override public void mouseClicked(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { - nodeClickAction(getJNodeUnderMouse(e, true)); + nodeClickAction(getJNodeUnderMouse(e)); } else if (SwingUtilities.isRightMouseButton(e)) { treeRightClickAction(e); }