Skip to content

Commit

Permalink
Tree: Adhere to the default behaviour of left/right key navigation.
Browse files Browse the repository at this point in the history
Navigating left should:
- Collapse the current hierarchy if the node is expanded.
- Select the parent node if existent and the current node is a leaf or not expanded
- Select the first root node otherwise.

Navigating right should:
- Expand the current node if it isn't a leaf.
- Navigate to the first child if the current node is expanded.
- Navigate to the next node otherwise (Darklaf addition)

This fixes #228
  • Loading branch information
weisJ committed Apr 10, 2021
1 parent 9fc4dc1 commit 6406f4a
Showing 1 changed file with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ protected void uninstallListeners() {
treeExpansionAnimationListener = null;
}

@Override
protected void uninstallKeyboardActions() {
super.uninstallKeyboardActions();
TreeUIAction.uninstallActions(tree);
}

@Override
public void paint(final Graphics g, final JComponent c) {
if (tree != c) {
Expand Down Expand Up @@ -625,13 +631,27 @@ public void actionPerformed(final ActionEvent e) {
actionListener.actionPerformed(e);
}

protected static void uninstallActions(final JTree tree) {
tree.putClientProperty(KEY_MAC_ACTIONS_INSTALLED, Boolean.FALSE);
final InputMap inputMap = tree.getInputMap(JComponent.WHEN_FOCUSED);
uninstallInputMap(inputMap);

final ActionMap actionMap = tree.getActionMap();
uninstallActionMap(actionMap);
}

protected static void installActions(final JTree tree) {
if (PropertyUtil.getBooleanProperty(tree, KEY_MAC_ACTIONS_INSTALLED)) return;
tree.putClientProperty(KEY_MAC_ACTIONS_INSTALLED, Boolean.TRUE);

installInputMap(tree.getInputMap(JComponent.WHEN_FOCUSED));
final InputMap inputMap = tree.getInputMap(JComponent.WHEN_FOCUSED);
installInputMap(inputMap);

final ActionMap actionMap = tree.getActionMap();
installActionMap(tree, actionMap);
}

private static void installActionMap(final JTree tree, final ActionMap actionMap) {
actionMap.put("expand_or_move_down", new TreeUIAction(e -> {
JTree target = getTree(e);
if (target == null) return;
Expand All @@ -653,12 +673,19 @@ protected static void installActions(final JTree tree) {
int selectionRow = target.getLeadSelectionRow();
if (selectionRow == -1) return;

if (isLeaf(target, selectionRow) || target.isCollapsed(selectionRow)) {
if (!isLeaf(target, selectionRow) && target.isExpanded(selectionRow)) {
target.collapseRow(selectionRow);
} else {
if (!PropertyUtil.getBooleanProperty(target, KEY_IS_TABLE_TREE)) {
moveTo(target, selectionRow - 1);
TreePath parentPath = target.getPathForRow(selectionRow).getParentPath();
if (parentPath == null) {
// No node above. Move to the first node.
moveTo(target, 0);
} else {

moveTo(target, tree.getRowForPath(parentPath));
}
}
} else {
target.collapseRow(selectionRow);
}
target.repaint();
}));
Expand All @@ -667,6 +694,14 @@ protected static void installActions(final JTree tree) {
actionMap.put("toggle_edit", new TreeUIAction(e -> toggleEdit(getTree(e))));
}

protected static void uninstallActionMap(final ActionMap actionMap) {
remove(actionMap, "expand_or_move_down");
remove(actionMap, "collapse_or_move_up");
remove(actionMap, "move_down");
remove(actionMap, "move_up");
remove(actionMap, "toggle_edit");
}

protected static void installInputMap(final InputMap inputMap) {
inputMap.put(KeyStroke.getKeyStroke("pressed LEFT"), "collapse_or_move_up");
inputMap.put(KeyStroke.getKeyStroke("pressed RIGHT"), "expand_or_move_down");
Expand All @@ -675,6 +710,28 @@ protected static void installInputMap(final InputMap inputMap) {
inputMap.put(KeyStroke.getKeyStroke("pressed ENTER"), "toggle_edit");
}

protected static void uninstallInputMap(final InputMap inputMap) {
remove(inputMap, KeyStroke.getKeyStroke("pressed LEFT"));
remove(inputMap, KeyStroke.getKeyStroke("pressed RIGHT"));
remove(inputMap, KeyStroke.getKeyStroke("pressed DOWN"));
remove(inputMap, KeyStroke.getKeyStroke("pressed UP"));
remove(inputMap, KeyStroke.getKeyStroke("pressed ENTER"));
}

private static void remove(final InputMap inputMap, final KeyStroke ks) {
Object key = inputMap.get(ks);
if (key == null || key instanceof UIResource) {
inputMap.remove(ks);
}
}

private static void remove(final ActionMap actionMap, final String key) {
Object action = actionMap.get(key);
if (action == null || action instanceof UIResource) {
actionMap.remove(key);
}
}

protected static JTree getTree(final ActionEvent e) {
return DarkUIUtil.nullableCast(JTree.class, e.getSource());
}
Expand All @@ -699,6 +756,7 @@ protected static void move(final JTree tree, final int offset) {

protected static void moveTo(final JTree tree, final int row) {
int newRow = Math.max(Math.min(row, tree.getRowCount() - 1), 0);
if (newRow < 0) return;
tree.setSelectionRow(newRow);
scrollRowToVisible(tree, newRow);
tree.repaint();
Expand Down

0 comments on commit 6406f4a

Please sign in to comment.