Skip to content

Commit

Permalink
Fix a bug that threw a NPE when using middle mouse click on Windows (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidemdot authored and koppor committed Oct 28, 2019
1 parent fa25ee9 commit 7f9b73f
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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);
}
});
Expand All @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit 7f9b73f

Please sign in to comment.