From d5aee591b8eca5ea610417b17cec8e9c43fc1910 Mon Sep 17 00:00:00 2001 From: Kirill Kalishev Date: Mon, 4 Jul 2011 18:29:39 +0400 Subject: [PATCH] tree collapsing/expanding fix for mac --- .../util/src/com/intellij/util/ui/UIUtil.java | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/platform/util/src/com/intellij/util/ui/UIUtil.java b/platform/util/src/com/intellij/util/ui/UIUtil.java index 2ad443d00860..ce6032fdd32c 100644 --- a/platform/util/src/com/intellij/util/ui/UIUtil.java +++ b/platform/util/src/com/intellij/util/ui/UIUtil.java @@ -1778,6 +1778,10 @@ public class UIUtil { protected void installKeyboardActions() { super.installKeyboardActions(); + if (Boolean.TRUE.equals(tree.getClientProperty("MacTreeUi.actionsInstalled"))) return; + + tree.putClientProperty("MacTreeUi.actionsInstalled", Boolean.TRUE); + final InputMap inputMap = tree.getInputMap(JComponent.WHEN_FOCUSED); inputMap.put(KeyStroke.getKeyStroke("pressed LEFT"), "collapse_or_move_up"); inputMap.put(KeyStroke.getKeyStroke("pressed RIGHT"), "expand"); @@ -1795,26 +1799,30 @@ public class UIUtil { int toScroll = -1; JTree tree = (JTree)source; int selectionRow = tree.getLeadSelectionRow(); - if (selectionRow == -1) return; + if (selectionRow != -1) { + TreePath selectionPath = tree.getPathForRow(selectionRow); + if (selectionPath != null) { + boolean leaf = tree.getModel().isLeaf(selectionPath.getLastPathComponent()); + if (!leaf && tree.isExpanded(selectionRow)) { + if (selectionRow + 1 < tree.getRowCount()) { + toSelect = selectionRow + 1; + toScroll = toSelect; + } + } else if (leaf) { + toScroll = selectionRow; + } - if (!isLeaf(selectionRow) && tree.isExpanded(selectionRow)) { - if (selectionRow + 1 < tree.getRowCount()) { - toSelect = selectionRow + 1; - toScroll = toSelect; + if (toSelect != -1) { + tree.setSelectionInterval(toSelect, toSelect); + } + + if (toScroll != -1) { + tree.scrollRowToVisible(toScroll); + } + + if (toSelect != -1 || toScroll != -1) return; } - } else if (isLeaf(selectionRow)) { - toScroll = selectionRow; } - - if (toSelect != -1) { - tree.setSelectionInterval(toSelect, toSelect); - } - - if (toScroll != -1) { - tree.scrollRowToVisible(toScroll); - } - - if (toSelect != -1 || toScroll != -1) return; } @@ -1831,7 +1839,10 @@ public class UIUtil { int selectionRow = tree.getLeadSelectionRow(); if (selectionRow == -1) return; - if (isLeaf(selectionRow) || tree.isCollapsed(selectionRow)) { + TreePath selectionPath = tree.getPathForRow(selectionRow); + if (selectionPath == null) return; + + if (tree.getModel().isLeaf(selectionPath.getLastPathComponent()) || tree.isCollapsed(selectionRow)) { final TreePath parentPath = tree.getPathForRow(selectionRow).getParentPath(); if (parentPath != null) { if (parentPath.getParentPath() != null || tree.isRootVisible()) {