Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug that threw a NPE when using middle mouse click on Windows #5533

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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