Skip to content

Commit

Permalink
Activate context menu on key press (#4004)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez authored May 1, 2018
1 parent 1a7f968 commit 9437ed8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import java.util.function.BiConsumer;
import java.util.function.Function;

import javafx.geometry.Bounds;
import javafx.geometry.Point2D;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeTableCell;
import javafx.scene.input.DragEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
Expand Down Expand Up @@ -101,6 +105,23 @@ public TableRow<S> call(TableView<S> tableView) {
}
event.consume();
});

// Activate context menu if user presses the "context menu" key
tableView.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
boolean rowFocused = !row.isEmpty() && tableView.getFocusModel().getFocusedIndex() == row.getIndex();
if (event.getCode() == KeyCode.CONTEXT_MENU && rowFocused) {
// Get center of focused cell
Bounds anchorBounds = row.getBoundsInParent();
double x = anchorBounds.getMinX() + anchorBounds.getWidth() / 2;
double y = anchorBounds.getMinY() + anchorBounds.getHeight() / 2;
Point2D screenPosition = row.getParent().localToScreen(x, y);

if (row.getContextMenu() == null) {
row.setContextMenu(contextMenuFactory.apply(row.getItem()));
}
row.getContextMenu().show(row, screenPosition.getX(), screenPosition.getY());
}
});
}

if (toOnDragDetected != null) {
Expand Down

0 comments on commit 9437ed8

Please sign in to comment.