Skip to content

Commit 7f9b73f

Browse files
davidemdotkoppor
authored andcommitted
Fix a bug that threw a NPE when using middle mouse click on Windows (#5533)
1 parent fa25ee9 commit 7f9b73f

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/main/java/org/jabref/gui/ClipBoardManager.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ public class ClipBoardManager {
4646
private static java.awt.datatransfer.Clipboard primary;
4747
private static ImportFormatReader importFormatReader;
4848

49+
public ClipBoardManager() {
50+
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER);
51+
}
52+
4953
public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, ImportFormatReader importFormatReader) {
5054
ClipBoardManager.clipboard = clipboard;
5155
ClipBoardManager.primary = primary;
5256
ClipBoardManager.importFormatReader = importFormatReader;
5357
}
5458

55-
public ClipBoardManager() {
56-
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER);
57-
}
58-
5959
/**
6060
* Add X11 clipboard support to a text input control.
6161
* It is necessary to call this method in every input where you want to use it:
@@ -67,7 +67,7 @@ public ClipBoardManager() {
6767
*/
6868
public static void addX11Support(TextInputControl input) {
6969
input.selectedTextProperty().addListener((observable, oldValue, newValue) -> {
70-
if (!newValue.isEmpty()) {
70+
if (!newValue.isEmpty() && primary != null) {
7171
primary.setContents(new StringSelection(newValue), null);
7272
}
7373
});
@@ -92,17 +92,19 @@ public static String getContents() {
9292
}
9393

9494
/**
95-
* Get the String residing on the primary clipboard.
95+
* Get the String residing on the primary clipboard (if it exists).
9696
*
9797
* @return any text found on the primary Clipboard; if none found, try with the system clipboard.
9898
*/
9999
public static String getContentsPrimary() {
100-
Transferable contents = primary.getContents(null);
101-
if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
102-
try {
103-
return (String) contents.getTransferData(DataFlavor.stringFlavor);
104-
} catch (UnsupportedFlavorException | IOException e) {
105-
LOGGER.warn(e.getMessage());
100+
if (primary != null) {
101+
Transferable contents = primary.getContents(null);
102+
if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
103+
try {
104+
return (String) contents.getTransferData(DataFlavor.stringFlavor);
105+
} catch (UnsupportedFlavorException | IOException e) {
106+
LOGGER.warn(e.getMessage());
107+
}
106108
}
107109
}
108110
return getContents();
@@ -119,12 +121,14 @@ public void setContent(ClipboardContent content) {
119121
}
120122

121123
/**
122-
* Puts content onto the primary clipboard.
124+
* Puts content onto the primary clipboard (if it exists).
123125
*
124126
* @param content the ClipboardContent to set as current value of the primary clipboard.
125127
*/
126128
public void setPrimaryClipboardContent(ClipboardContent content) {
127-
primary.setContents(new StringSelection(content.getString()), null);
129+
if (primary != null) {
130+
primary.setContents(new StringSelection(content.getString()), null);
131+
}
128132
}
129133

130134
public void setHtmlContent(String html) {

0 commit comments

Comments
 (0)