|
3 | 3 | import java.util.Locale; |
4 | 4 | import java.util.Optional; |
5 | 5 |
|
| 6 | +import javafx.application.Platform; |
6 | 7 | import javafx.fxml.FXML; |
7 | 8 | import javafx.scene.control.ButtonType; |
8 | 9 | import javafx.scene.control.ListView; |
9 | 10 | import javafx.scene.control.ScrollPane; |
10 | 11 | import javafx.scene.control.ToggleButton; |
11 | 12 | import javafx.scene.input.KeyCode; |
| 13 | +import javafx.scene.input.KeyEvent; |
12 | 14 |
|
13 | 15 | import org.jabref.gui.DialogService; |
14 | 16 | import org.jabref.gui.icon.IconTheme; |
@@ -115,6 +117,58 @@ private void initialize() { |
115 | 117 | memoryStickMode.selectedProperty().bindBidirectional(viewModel.getMemoryStickProperty()); |
116 | 118 |
|
117 | 119 | viewModel.setValues(); |
| 120 | + // Delay execution until JavaFX has completed layout and the Scene object is attached to the DialogPane |
| 121 | + Platform.runLater(this::installGlobalCloseBehavior); |
| 122 | + } |
| 123 | + |
| 124 | + private void installGlobalCloseBehavior() { |
| 125 | + var scene = getDialogPane().getScene(); |
| 126 | + |
| 127 | + // Capture events BEFORE child nodes |
| 128 | + scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> { |
| 129 | + if (!preferences.getKeyBindingRepository().checkKeyCombinationEquality(KeyBinding.CLOSE, e)) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + // If any cell-based control is editing (ListView/TableView/TreeView/TreeTableView), |
| 134 | + // record that the close input is for canceling edit; DO NOT consume, let the control cancel. |
| 135 | + if (isAnyCellControlEditing()) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + // No editing in progress --> close dialog |
| 140 | + closeDialog(); |
| 141 | + e.consume(); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + private boolean isAnyCellControlEditing() { |
| 146 | + var root = getDialogPane(); |
| 147 | + |
| 148 | + for (var n : root.lookupAll(".list-view")) { |
| 149 | + if (n instanceof javafx.scene.control.ListView<?> lv && lv.getEditingIndex() != -1) { |
| 150 | + return true; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + for (var n : root.lookupAll(".table-view")) { |
| 155 | + if (n instanceof javafx.scene.control.TableView<?> tv && tv.getEditingCell() != null) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + for (var n : root.lookupAll(".tree-view")) { |
| 161 | + if (n instanceof javafx.scene.control.TreeView<?> trv && trv.getEditingItem() != null) { |
| 162 | + return true; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + for (var n : root.lookupAll(".tree-table-view")) { |
| 167 | + if (n instanceof javafx.scene.control.TreeTableView<?> ttv && ttv.getEditingCell() != null) { |
| 168 | + return true; |
| 169 | + } |
| 170 | + } |
| 171 | + return false; |
118 | 172 | } |
119 | 173 |
|
120 | 174 | @FXML |
|
0 commit comments