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 various copy to clipboard issues #5340

Merged
merged 14 commits into from
Oct 6, 2019
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an exception which occured when trying to open a non existing file from the "Recent files"-menu [#5334](https://github.com/JabRef/jabref/issues/5334)
- The context menu for fields in the entry editor is back. [#5254](https://github.com/JabRef/jabref/issues/5254)
- 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 various issues (including [#5263](https://github.com/JabRef/jabref/issues/5263)) related to copying entries to the clipboard

### Removed

Expand Down
35 changes: 29 additions & 6 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 @@ -73,10 +79,9 @@ public void execute() {
selectedExporter.ifPresent(exporter -> BackgroundTask.wrap(() -> exportToClipboard(exporter))
.onSuccess(this::setContentToClipboard)
.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,7 +107,7 @@ 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);
} finally {
Expand All @@ -115,12 +120,20 @@ private String exportToClipboard(Exporter exporter) {
}
}
}
return "";
throw new Exception("export failed.");
matthiasgeiger marked this conversation as resolved.
Show resolved Hide resolved
}

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 +148,14 @@ private String readFileToString(Path tmp) throws IOException {
return reader.lines().collect(Collectors.joining(OS.NEWLINE));
}
}

private class ExportResult {
final String content;
matthiasgeiger marked this conversation as resolved.
Show resolved Hide resolved
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