From dc8b085ad6e783c7d7422c35ff0e80770d1f571c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=A9ndez?= Date: Sun, 27 Oct 2019 23:16:45 +0100 Subject: [PATCH] Fix a bug that threw a NPE when using middle mouse click on Windows --- .../java/org/jabref/gui/ClipBoardManager.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jabref/gui/ClipBoardManager.java b/src/main/java/org/jabref/gui/ClipBoardManager.java index 64bc6617533..340fb61ea1a 100644 --- a/src/main/java/org/jabref/gui/ClipBoardManager.java +++ b/src/main/java/org/jabref/gui/ClipBoardManager.java @@ -46,16 +46,16 @@ public class ClipBoardManager { private static java.awt.datatransfer.Clipboard primary; private static ImportFormatReader importFormatReader; + public ClipBoardManager() { + this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER); + } + public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, ImportFormatReader importFormatReader) { ClipBoardManager.clipboard = clipboard; ClipBoardManager.primary = primary; ClipBoardManager.importFormatReader = importFormatReader; } - public ClipBoardManager() { - this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER); - } - /** * 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: @@ -67,7 +67,7 @@ public ClipBoardManager() { */ public static void addX11Support(TextInputControl input) { input.selectedTextProperty().addListener((observable, oldValue, newValue) -> { - if (!newValue.isEmpty()) { + if (!newValue.isEmpty() && primary != null) { primary.setContents(new StringSelection(newValue), null); } }); @@ -92,17 +92,19 @@ public static String getContents() { } /** - * Get the String residing on the primary clipboard. + * Get the String residing on the primary clipboard (if it exists). * * @return any text found on the primary Clipboard; if none found, try with the system clipboard. */ public static String getContentsPrimary() { - Transferable contents = primary.getContents(null); - if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) { - try { - return (String) contents.getTransferData(DataFlavor.stringFlavor); - } catch (UnsupportedFlavorException | IOException e) { - LOGGER.warn(e.getMessage()); + if (primary != null) { + Transferable contents = primary.getContents(null); + if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) { + try { + return (String) contents.getTransferData(DataFlavor.stringFlavor); + } catch (UnsupportedFlavorException | IOException e) { + LOGGER.warn(e.getMessage()); + } } } return getContents(); @@ -119,12 +121,14 @@ public void setContent(ClipboardContent content) { } /** - * Puts content onto the primary clipboard. + * Puts content onto the primary clipboard (if it exists). * * @param content the ClipboardContent to set as current value of the primary clipboard. */ public void setPrimaryClipboardContent(ClipboardContent content) { - primary.setContents(new StringSelection(content.getString()), null); + if (primary != null) { + primary.setContents(new StringSelection(content.getString()), null); + } } public void setHtmlContent(String html) {