Skip to content

Commit

Permalink
feat: ESC key selects parent of selected node (gluonhq#389)
Browse files Browse the repository at this point in the history
* Implementation for gluonhq#388
* CHG setOnKeyPressed() instead of addEventListener() to handle ESC key
* Update ContentPanelController.java
* Update HierarchyTreeViewController.java
  • Loading branch information
luca-domenichini authored Sep 30, 2024
1 parent 2f2efc5 commit d426938
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import javafx.scene.control.TreeTableView;
import javafx.scene.image.Image;
import javafx.scene.input.InputEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
Expand Down Expand Up @@ -911,6 +912,20 @@ protected void controllerDidLoadFxml() {
= getEditorController().getContextMenuController();
scrollPane.setContextMenu(contextMenuController.getContextMenu());

scrollPane.setOnKeyPressed(e -> {

if (e.getCode() == KeyCode.ESCAPE) {
// on ESC we select the parent of current selected item
Selection selection = getEditorController().getSelection();
FXOMObject parent = selection.getAncestor();

if (parent != null) {
selection.clear();
selection.select(parent);
}
}
});

// Setup default workspace background
setWorkspaceBackground(ImageUtils.getImage(getDefaultWorkspaceBackgroundURL()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2016, 2022 Gluon and/or its affiliates.
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -46,11 +47,14 @@
import javafx.scene.Node;
import javafx.scene.control.Cell;
import javafx.scene.control.Control;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.input.KeyCode;

/**
* Hierarchy panel controller based on the TreeView control.
Expand Down Expand Up @@ -85,6 +89,22 @@ protected void initializePanel() {
// We do not use the platform editing feature because
// editing is started on selection + simple click instead of double click
treeView.setEditable(false);

treeView.setOnKeyPressed(event -> {

if (event.getCode() == KeyCode.ESCAPE) {
// on ESC we select the parent of current selected item
MultipleSelectionModel<TreeItem<HierarchyItem>> selectionModel = treeView.getSelectionModel();
TreeItem<HierarchyItem> item = selectionModel.getSelectedItem();
if (item != null) {
TreeItem<HierarchyItem> parent = item.getParent();
if (parent != null) {
selectionModel.clearSelection();
selectionModel.select(parent);
}
}
}
});
}

@Override
Expand Down

0 comments on commit d426938

Please sign in to comment.