Skip to content

Commit

Permalink
Fix entry preview copy exception
Browse files Browse the repository at this point in the history
Fixes #4714
  • Loading branch information
Siedlerchr committed Mar 5, 2019
1 parent bdf7aa6 commit 02b7876
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ private void setupActions() {
* @param outputFormat the desired {@link CitationStyleOutputFormat}
*/
private void copyCitationToClipboard(CitationStyleOutputFormat outputFormat) {
new CitationStyleToClipboardWorker(this, outputFormat).execute();
new CitationStyleToClipboardWorker(this, outputFormat, dialogService, Globals.clipboardManager, Globals.prefs.getPreviewPreferences());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.jabref.gui.worker;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.SwingWorker;

import javafx.scene.input.ClipboardContent;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.citationstyle.CitationStyle;
import org.jabref.logic.citationstyle.CitationStyleGenerator;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
Expand All @@ -30,7 +30,7 @@
* Copies the selected entries and formats them with the selected citation style (or preview), then it is copied to the clipboard.
* This worker cannot be reused.
*/
public class CitationStyleToClipboardWorker extends SwingWorker<List<String>, Void> {
public class CitationStyleToClipboardWorker {

private static final Logger LOGGER = LoggerFactory.getLogger(CitationStyleToClipboardWorker.class);

Expand All @@ -39,29 +39,33 @@ public class CitationStyleToClipboardWorker extends SwingWorker<List<String>, Vo
private final String style;
private final String previewStyle;
private final CitationStyleOutputFormat outputFormat;
private final DialogService dialogService;
private final ClipBoardManager clipBoardManager;


public CitationStyleToClipboardWorker(BasePanel basePanel, CitationStyleOutputFormat outputFormat) {
public CitationStyleToClipboardWorker(BasePanel basePanel, CitationStyleOutputFormat outputFormat, DialogService dialogService, ClipBoardManager clipBoardManager, PreviewPreferences previewPreferences) {
this.basePanel = basePanel;
this.selectedEntries = basePanel.getSelectedEntries();
PreviewPreferences previewPreferences = Globals.prefs.getPreviewPreferences();
this.style = previewPreferences.getPreviewCycle().get(previewPreferences.getPreviewCyclePosition());
this.previewStyle = Globals.prefs.getPreviewPreferences().getPreviewStyle();
this.previewStyle = previewPreferences.getPreviewStyle();
this.outputFormat = outputFormat;
this.clipBoardManager = clipBoardManager;

basePanel.frame().setStatus(Localization.lang("Copying..."));
this.dialogService = dialogService;
dialogService.notify(Localization.lang("Copying..."));

BackgroundTask.wrap(this::loadCitationStyles).onFailure(ex -> LOGGER.error("Error while copying citations to the clipboard", ex))
.onSuccess(this::done);
}

@Override
protected List<String> doInBackground() throws Exception {
private List<String> loadCitationStyles() throws IOException {
// This worker stored the style as filename. The CSLAdapter and the CitationStyleCache store the source of the
// style. Therefore, we extract the style source from the file.
String styleSource = null;
if (CitationStyle.isCitationStyleFile(style)) {
styleSource = CitationStyle.createCitationStyleFromFile(style)
.filter(citationStyleFromFile -> !citationStyleFromFile.getSource().isEmpty())
.map(CitationStyle::getSource)
.orElse(null);
.filter(citationStyleFromFile -> !citationStyleFromFile.getSource().isEmpty())
.map(CitationStyle::getSource)
.orElse(null);
}
if (styleSource != null) {
return CitationStyleGenerator.generateCitations(selectedEntries, styleSource, outputFormat);
Expand Down Expand Up @@ -100,8 +104,8 @@ protected static ClipboardContent processText(List<String> citations) {
*/
protected static ClipboardContent processRtf(List<String> citations) {
String result = "{\\rtf" + OS.NEWLINE +
String.join(CitationStyleOutputFormat.RTF.getLineSeparator(), citations) +
"}";
String.join(CitationStyleOutputFormat.RTF.getLineSeparator(), citations) +
"}";
ClipboardContent content = new ClipboardContent();
content.putRtf(result);
return content;
Expand All @@ -112,21 +116,21 @@ protected static ClipboardContent processRtf(List<String> citations) {
*/
protected static ClipboardContent processXslFo(List<String> citations) {
String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + OS.NEWLINE +
"<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">" + OS.NEWLINE +
" <fo:layout-master-set>" + OS.NEWLINE +
" <fo:simple-page-master master-name=\"citations\">" + OS.NEWLINE +
" <fo:region-body/>" + OS.NEWLINE +
" </fo:simple-page-master>" + OS.NEWLINE +
" </fo:layout-master-set>" + OS.NEWLINE +
" <fo:page-sequence master-reference=\"citations\">" + OS.NEWLINE +
" <fo:flow flow-name=\"xsl-region-body\">" + OS.NEWLINE + OS.NEWLINE;
"<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">" + OS.NEWLINE +
" <fo:layout-master-set>" + OS.NEWLINE +
" <fo:simple-page-master master-name=\"citations\">" + OS.NEWLINE +
" <fo:region-body/>" + OS.NEWLINE +
" </fo:simple-page-master>" + OS.NEWLINE +
" </fo:layout-master-set>" + OS.NEWLINE +
" <fo:page-sequence master-reference=\"citations\">" + OS.NEWLINE +
" <fo:flow flow-name=\"xsl-region-body\">" + OS.NEWLINE + OS.NEWLINE;

result += String.join(CitationStyleOutputFormat.XSL_FO.getLineSeparator(), citations);

result += OS.NEWLINE +
" </fo:flow>" + OS.NEWLINE +
" </fo:page-sequence>" + OS.NEWLINE +
"</fo:root>" + OS.NEWLINE;
" </fo:flow>" + OS.NEWLINE +
" </fo:page-sequence>" + OS.NEWLINE +
"</fo:root>" + OS.NEWLINE;

ClipboardContent content = new ClipboardContent();
content.put(ClipBoardManager.XML, result);
Expand All @@ -138,59 +142,52 @@ protected static ClipboardContent processXslFo(List<String> citations) {
*/
protected static ClipboardContent processHtml(List<String> citations) {
String result = "<!DOCTYPE html>" + OS.NEWLINE +
"<html>" + OS.NEWLINE +
" <head>" + OS.NEWLINE +
" <meta charset=\"utf-8\">" + OS.NEWLINE +
" </head>" + OS.NEWLINE +
" <body>" + OS.NEWLINE + OS.NEWLINE;
"<html>" + OS.NEWLINE +
" <head>" + OS.NEWLINE +
" <meta charset=\"utf-8\">" + OS.NEWLINE +
" </head>" + OS.NEWLINE +
" <body>" + OS.NEWLINE + OS.NEWLINE;

result += String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations);

result += OS.NEWLINE +
" </body>" + OS.NEWLINE +
"</html>" + OS.NEWLINE;
" </body>" + OS.NEWLINE +
"</html>" + OS.NEWLINE;

ClipboardContent content = new ClipboardContent();
content.putHtml(result);
return content;
}

@Override
public void done() {
try {
List<String> citations = get();

// if it's not a citation style take care of the preview
if (!CitationStyle.isCitationStyleFile(style)) {
Globals.clipboardManager.setHtmlContent(processPreview(citations));
} else {
// if it's generated by a citation style take care of each output format
ClipboardContent content;
switch (outputFormat) {
case HTML:
content = processHtml(citations);
break;
case RTF:
content = processRtf(citations);
break;
case XSL_FO:
content = processXslFo(citations);
break;
case ASCII_DOC:
case TEXT:
content = processText(citations);
break;
default:
LOGGER.warn("unknown output format: '" + outputFormat + "', processing it via the default.");
content = processText(citations);
break;
}
Globals.clipboardManager.setContent(content);
public void done(List<String> citations) {
// if it's not a citation style take care of the preview
if (!CitationStyle.isCitationStyleFile(style)) {
clipBoardManager.setHtmlContent(processPreview(citations));
} else {
// if it's generated by a citation style take care of each output format
ClipboardContent content;
switch (outputFormat) {
case HTML:
content = processHtml(citations);
break;
case RTF:
content = processRtf(citations);
break;
case XSL_FO:
content = processXslFo(citations);
break;
case ASCII_DOC:
case TEXT:
content = processText(citations);
break;
default:
LOGGER.warn("unknown output format: '" + outputFormat + "', processing it via the default.");
content = processText(citations);
break;
}

basePanel.frame().setStatus(Localization.lang("Copied %0 citations.", String.valueOf(selectedEntries.size())));
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("Error while copying citations to the clipboard", e);
clipBoardManager.setContent(content);
}

dialogService.notify(Localization.lang("Copied %0 citations.", String.valueOf(selectedEntries.size())));
}

}

0 comments on commit 02b7876

Please sign in to comment.