Skip to content

Commit

Permalink
Fix various copy to clipboard issues (#5340)
Browse files Browse the repository at this point in the history
* fix plain text copy in preview viewer

* put textual representation also to clipboard

* fix tests

* improve "Export to Clipboard" function

* fix checkstyle

* fix plain text copy in preview viewer (a bit more intelligent ;-))

* filter exporter list to supported exporters only

* update CHANGELOG.md

* fix checkstyle

* improve exception handling

* checkstyle
  • Loading branch information
matthiasgeiger authored and tobiasdiez committed Oct 6, 2019
1 parent 9c7f7f6 commit 9117fff
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 32 additions & 8 deletions src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<String> SUPPORTED_FILETYPES = Arrays.asList("txt", "rtf", "rdf", "xml", "html", "htm", "csv", "ris");

private JabRefFrame frame;
private final DialogService dialogService;
private BasePanel panel;
Expand Down Expand Up @@ -59,6 +64,7 @@ public void execute() {

List<Exporter> 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
Expand All @@ -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!)
Expand All @@ -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)) {
Expand All @@ -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<String> 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());
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ private List<String> 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<String> citations) {
return String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations);
protected static ClipboardContent processPreview(List<String> citations) {
ClipboardContent content = new ClipboardContent();
content.putHtml(String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations));
content.putString(String.join(CitationStyleOutputFormat.HTML.getLineSeparator(), citations));
return content;
}

/**
Expand All @@ -108,6 +111,7 @@ protected static ClipboardContent processRtf(List<String> citations) {
String.join(CitationStyleOutputFormat.RTF.getLineSeparator(), citations) +
"}";
ClipboardContent content = new ClipboardContent();
content.putString(result);
content.putRtf(result);
return content;
}
Expand All @@ -134,6 +138,7 @@ protected static ClipboardContent processXslFo(List<String> citations) {
"</fo:root>" + OS.NEWLINE;

ClipboardContent content = new ClipboardContent();
content.putString(result);
content.put(ClipBoardManager.XML, result);
return content;
}
Expand All @@ -155,14 +160,15 @@ protected static ClipboardContent processHtml(List<String> citations) {
"</html>" + OS.NEWLINE;

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

private void setClipBoardContent(List<String> 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;
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -91,8 +92,8 @@ void processPreviewHtml() throws Exception {
"</dd>" + OS.NEWLINE +
"<p></p></font>";

String actual = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));

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

Expand Down

0 comments on commit 9117fff

Please sign in to comment.