diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a4e50fdea6..2f9f21900f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,10 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We fixed an exception which occurred when trying to open a non existing file from the "Recent files"-menu [#5334](https://github.com/JabRef/jabref/issues/5334) - We fixed a problem where the "editor" information has been duplicated during saving a .bib-Database. [#5359](https://github.com/JabRef/jabref/issues/5359) - We re-introduced the feature to switch between different preview styles. [#5221](https://github.com/JabRef/jabref/issues/5221) +- We fixed various issues (including [#5263](https://github.com/JabRef/jabref/issues/5263)) related to copying entries to the clipboard + + + ### Removed diff --git a/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java b/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java index 122b5baf610..94b63150d15 100644 --- a/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java +++ b/src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java @@ -5,6 +5,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Optional; @@ -20,6 +21,7 @@ import org.jabref.gui.util.BackgroundTask; import org.jabref.logic.exporter.Exporter; import org.jabref.logic.l10n.Localization; +import org.jabref.logic.util.FileType; import org.jabref.logic.util.OS; import org.jabref.model.entry.BibEntry; import org.jabref.preferences.JabRefPreferences; @@ -31,6 +33,9 @@ public class ExportToClipboardAction extends SimpleCommand { private static final Logger LOGGER = LoggerFactory.getLogger(ExportToClipboardAction.class); + // Only text based exporters can be used + private static final List SUPPORTED_FILETYPES = Arrays.asList("txt", "rtf", "rdf", "xml", "html", "htm", "csv", "ris"); + private JabRefFrame frame; private final DialogService dialogService; private BasePanel panel; @@ -59,6 +64,7 @@ public void execute() { List exporters = Globals.exportFactory.getExporters().stream() .sorted(Comparator.comparing(Exporter::getName)) + .filter(exporter -> SUPPORTED_FILETYPES.containsAll(exporter.getFileType().getExtensions())) .collect(Collectors.toList()); //Find default choice, if any @@ -71,12 +77,12 @@ public void execute() { Localization.lang("Export"), defaultChoice, exporters); selectedExporter.ifPresent(exporter -> BackgroundTask.wrap(() -> exportToClipboard(exporter)) - .onSuccess(this::setContentToClipboard) - .executeWith(Globals.TASK_EXECUTOR)); - + .onSuccess(this::setContentToClipboard) + .onFailure(ex -> { /* swallow as already logged */ }) + .executeWith(Globals.TASK_EXECUTOR)); } - private String exportToClipboard(Exporter exporter) { + private ExportResult exportToClipboard(Exporter exporter) throws Exception { // Set the global variable for this database's file directory before exporting, // so formatters can resolve linked files correctly. // (This is an ugly hack!) @@ -102,9 +108,10 @@ private String exportToClipboard(Exporter exporter) { entries); // Read the file and put the contents on the clipboard: - return readFileToString(tmp); + return new ExportResult(readFileToString(tmp), exporter.getFileType()); } catch (Exception e) { LOGGER.error("Error exporting to clipboard", e); + throw new Exception("Rethrow ", e); } finally { // Clean up: if ((tmp != null) && Files.exists(tmp)) { @@ -115,12 +122,19 @@ private String exportToClipboard(Exporter exporter) { } } } - return ""; } - private void setContentToClipboard(String content) { + private void setContentToClipboard(ExportResult result) { ClipboardContent clipboardContent = new ClipboardContent(); - clipboardContent.putRtf(content); + List extensions = result.fileType.getExtensions(); + if (extensions.contains("html")) { + clipboardContent.putHtml(result.content); + } else if (extensions.contains("rtf")) { + clipboardContent.putRtf(result.content); + } else if (extensions.contains("rdf")) { + clipboardContent.putRtf(result.content); + } + clipboardContent.putString(result.content); Globals.clipboardManager.setContent(clipboardContent); dialogService.notify(Localization.lang("Entries exported to clipboard") + ": " + entries.size()); @@ -135,4 +149,14 @@ private String readFileToString(Path tmp) throws IOException { return reader.lines().collect(Collectors.joining(OS.NEWLINE)); } } + + private class ExportResult { + final String content; + final FileType fileType; + + ExportResult(String content, FileType fileType) { + this.content = content; + this.fileType = fileType; + } + } } diff --git a/src/main/java/org/jabref/gui/preview/CitationStyleToClipboardWorker.java b/src/main/java/org/jabref/gui/preview/CitationStyleToClipboardWorker.java index 80299068046..23a4f056d36 100644 --- a/src/main/java/org/jabref/gui/preview/CitationStyleToClipboardWorker.java +++ b/src/main/java/org/jabref/gui/preview/CitationStyleToClipboardWorker.java @@ -87,8 +87,11 @@ private List generateCitations() throws IOException { * Generates a plain text string out of the preview and copies it additionally to the html to the clipboard * (WYSIWYG Editors use the HTML, plain text editors the text) */ - protected static String processPreview(List citations) { - return String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations); + protected static ClipboardContent processPreview(List citations) { + ClipboardContent content = new ClipboardContent(); + content.putHtml(String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations)); + content.putString(String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations)); + return content; } /** @@ -108,6 +111,7 @@ protected static ClipboardContent processRtf(List citations) { String.join(CitationStyleOutputFormat.RTF.getLineSeparator(), citations) + "}"; ClipboardContent content = new ClipboardContent(); + content.putString(result); content.putRtf(result); return content; } @@ -134,6 +138,7 @@ protected static ClipboardContent processXslFo(List citations) { "" + OS.NEWLINE; ClipboardContent content = new ClipboardContent(); + content.putString(result); content.put(ClipBoardManager.XML, result); return content; } @@ -155,6 +160,7 @@ protected static ClipboardContent processHtml(List citations) { "" + OS.NEWLINE; ClipboardContent content = new ClipboardContent(); + content.putString(result); content.putHtml(result); return content; } @@ -162,7 +168,7 @@ protected static ClipboardContent processHtml(List citations) { private void setClipBoardContent(List citations) { // if it's not a citation style take care of the preview if (!(style instanceof CitationStylePreviewLayout)) { - clipBoardManager.setHtmlContent(processPreview(citations)); + clipBoardManager.setContent(processPreview(citations)); } else { // if it's generated by a citation style take care of each output format ClipboardContent content; diff --git a/src/main/java/org/jabref/gui/preview/PreviewViewer.java b/src/main/java/org/jabref/gui/preview/PreviewViewer.java index 8811d398b88..c3d0ec4b405 100644 --- a/src/main/java/org/jabref/gui/preview/PreviewViewer.java +++ b/src/main/java/org/jabref/gui/preview/PreviewViewer.java @@ -29,8 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; /** * Displays an BibEntry using the given layout format. @@ -175,16 +173,8 @@ public void copyPreviewToClipBoard() { StringBuilder previewStringContent = new StringBuilder(); Document document = previewView.getEngine().getDocument(); - NodeList nodeList = document.getElementsByTagName("html"); - - //Nodelist does not implement iterable - for (int i = 0; i < nodeList.getLength(); i++) { - Element element = (Element) nodeList.item(i); - previewStringContent.append(element.getTextContent()); - } - ClipboardContent content = new ClipboardContent(); - content.putString(previewStringContent.toString()); + content.putString(document.getElementById("content").getTextContent()); content.putHtml((String) previewView.getEngine().executeScript("document.documentElement.outerHTML")); clipBoardManager.setContent(content); diff --git a/src/test/java/org/jabref/gui/preview/CitationStyleToClipboardWorkerTest.java b/src/test/java/org/jabref/gui/preview/CitationStyleToClipboardWorkerTest.java index 0ac88a6d323..82501d0e182 100644 --- a/src/test/java/org/jabref/gui/preview/CitationStyleToClipboardWorkerTest.java +++ b/src/test/java/org/jabref/gui/preview/CitationStyleToClipboardWorkerTest.java @@ -39,7 +39,8 @@ void processPreviewText() throws Exception { OS.NEWLINE + "Abstract: This entry describes a test scenario which may be useful in JabRef. By providing a test entry it is possible to see how certain things will look in this graphical BIB-file mananger. "; - String actual = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation)); + ClipboardContent clipboardContent = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation)); + String actual = clipboardContent.getString(); assertEquals(expected, actual); } @@ -91,8 +92,8 @@ void processPreviewHtml() throws Exception { "" + OS.NEWLINE + "

"; - String actual = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation)); - + ClipboardContent clipboardContent = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation)); + String actual = clipboardContent.getString(); assertEquals(expected, actual); }