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 selecting custom export for copy to clipboard with uppercase file ext #6290

Merged
merged 9 commits into from
May 1, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue when importing into current library an erroneous message "import cancelled" is displayed even though import is successful. [#6266](https://github.com/JabRef/jabref/issues/6266)
- We fixed an issue where custom jstyles for Open/LibreOffice where not saved correctly. [#6170](https://github.com/JabRef/jabref/issues/6170)
- We fixed an issue where the INSPIRE fetcher was no longer working [#6229](https://github.com/JabRef/jabref/issues/6229)
- We fixed an issue where custom exports with an uppercase file extension could not be selected for "Copy...-> Export to Clipboard" [#6285](https://github.com/JabRef/jabref/issues/6285)


### Removed
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, Globals.clipboardManager, prefs)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, Globals.clipboardManager, prefs)),
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, prefs.getPreviewPreferences())),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService))),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService, Globals.exportFactory))),

factory.createMenuItem(StandardActions.PASTE, new EditAction(StandardActions.PASTE, this, stateManager)),

Expand Down
30 changes: 18 additions & 12 deletions src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
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;
import java.util.Set;
import java.util.stream.Collectors;

import javafx.scene.input.ClipboardContent;
Expand All @@ -20,6 +20,7 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.exporter.Exporter;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.FileType;
import org.jabref.logic.util.OS;
Expand All @@ -34,21 +35,24 @@ 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 static final Set<String> SUPPORTED_FILETYPES = Set.of("txt", "rtf", "rdf", "xml", "html", "htm", "csv", "ris");
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved

private JabRefFrame frame;
private final DialogService dialogService;
private BasePanel panel;
private final List<BibEntry> entries = new ArrayList<>();
private final ExporterFactory exporterFactory;

public ExportToClipboardAction(JabRefFrame frame, DialogService dialogService) {
public ExportToClipboardAction(JabRefFrame frame, DialogService dialogService, ExporterFactory exporterFactory) {
this.frame = frame;
this.dialogService = dialogService;
this.exporterFactory = exporterFactory;
}

public ExportToClipboardAction(BasePanel panel, DialogService dialogService) {
public ExportToClipboardAction(BasePanel panel, DialogService dialogService, ExporterFactory exporterFactory) {
this.panel = panel;
this.dialogService = dialogService;
this.exporterFactory = exporterFactory;
}

@Override
Expand All @@ -62,10 +66,10 @@ public void execute() {
return;
}

List<Exporter> exporters = Globals.exportFactory.getExporters().stream()
.sorted(Comparator.comparing(Exporter::getName))
.filter(exporter -> SUPPORTED_FILETYPES.containsAll(exporter.getFileType().getExtensions()))
.collect(Collectors.toList());
List<Exporter> exporters = exporterFactory.getExporters().stream()
.sorted(Comparator.comparing(Exporter::getName))
.filter(exporter -> SUPPORTED_FILETYPES.containsAll(exporter.getFileType().getExtensionsLowerCase()))
.collect(Collectors.toList());

// Find default choice, if any
Exporter defaultChoice = exporters.stream()
Expand All @@ -74,12 +78,13 @@ public void execute() {
.orElse(null);

Optional<Exporter> selectedExporter = dialogService.showChoiceDialogAndWait(Localization.lang("Export"), Localization.lang("Select export format"),
Localization.lang("Export"), defaultChoice, exporters);
Localization.lang("Export"), defaultChoice, exporters);

selectedExporter.ifPresent(exporter -> BackgroundTask.wrap(() -> exportToClipboard(exporter))
.onSuccess(this::setContentToClipboard)
.onFailure(ex -> { /* swallow as already logged */ })
.executeWith(Globals.TASK_EXECUTOR));
.onSuccess(this::setContentToClipboard)
.onFailure(ex -> {
/* swallow as already logged */ })
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
.executeWith(Globals.TASK_EXECUTOR));
}

private ExportResult exportToClipboard(Exporter exporter) throws Exception {
Expand Down Expand Up @@ -151,6 +156,7 @@ private String readFileToString(Path tmp) throws IOException {
}

private static class ExportResult {

final String content;
final FileType fileType;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, Di
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
}

copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.EXPORT_TO_CLIPBOARD, new ExportToClipboardAction(panel, dialogService)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.EXPORT_TO_CLIPBOARD, new ExportToClipboardAction(panel, dialogService, Globals.exportFactory)));
return copySpecialMenu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public CopyCitationAction(CitationStyleOutputFormat outputFormat, DialogService
this.executable.bind(ActionHelper.needsEntriesSelected(stateManager));
}

@Override
public void execute() {
BackgroundTask.wrap(this::generateCitations)
.onFailure(ex -> LOGGER.error("Error while copying citations to the clipboard", ex))
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/jabref/logic/util/FileType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jabref.logic.util;

import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand All @@ -15,5 +17,9 @@ default List<String> getExtensionsWithDot() {
.collect(Collectors.toList());
}

default Set<String> getExtensionsLowerCase() {
return getExtensions().stream().map(ext -> ext.toLowerCase(Locale.ROOT)).collect(Collectors.toSet());
}

List<String> getExtensions();
}