From cd6f6569de4992079aff6d677ea560115068cd00 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Mon, 23 Dec 2019 10:37:17 +0100 Subject: [PATCH] Fixes StringIndexOutOfBoundsException when cutting text (#5776) * Fixes StringIndexOutOfBoundsException when cutting text * Incldue workaround as proposed at https://bugs.openjdk.java.net/browse/JDK-8176270 * Try to get right text * Access input value the right way * Fix checkstyle Co-authored-by: Oliver Kopp --- .../java/org/jabref/gui/ClipBoardManager.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/jabref/gui/ClipBoardManager.java b/src/main/java/org/jabref/gui/ClipBoardManager.java index 340fb61ea1a..80f66951882 100644 --- a/src/main/java/org/jabref/gui/ClipBoardManager.java +++ b/src/main/java/org/jabref/gui/ClipBoardManager.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Optional; +import javafx.application.Platform; import javafx.scene.control.TextInputControl; import javafx.scene.input.Clipboard; import javafx.scene.input.ClipboardContent; @@ -57,20 +58,24 @@ public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard pri } /** - * Add X11 clipboard support to a text input control. - * It is necessary to call this method in every input where you want to use it: - * {@code ClipBoardManager.addX11Support(TextInputControl input);}. + * Add X11 clipboard support to a text input control. It is necessary to call this method in every input where you + * want to use it: {@code ClipBoardManager.addX11Support(TextInputControl input);}. * * @param input the TextInputControl (e.g., TextField, TextArea, and children) where adding this functionality. - * @see Short summary for X11 clipboards - * @see Longer text over clipboards + * @see Short summary for X11 + * clipboards + * @see Longer + * text over clipboards */ public static void addX11Support(TextInputControl input) { - input.selectedTextProperty().addListener((observable, oldValue, newValue) -> { - if (!newValue.isEmpty() && primary != null) { - primary.setContents(new StringSelection(newValue), null); - } - }); + input.selectedTextProperty().addListener( + // using InvalidationListener because of https://bugs.openjdk.java.net/browse/JDK-8176270 + observable -> Platform.runLater(() -> { + String newValue = input.getSelectedText(); + if (!newValue.isEmpty() && primary != null) { + primary.setContents(new StringSelection(newValue), null); + } + })); input.setOnMouseClicked(event -> { if (event.getButton() == MouseButton.MIDDLE) { input.insertText(input.getCaretPosition(), getContentsPrimary());