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

Added missing executable bindings to various commands #8342

Merged
merged 3 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed "The library has been modified by another program" showing up when line breaks change [#4877](https://github.com/JabRef/jabref/issues/4877)
- The default directory of the "LaTeX Citations" tab is now the directory of the currently opened database (and not the directory chosen at the last open file dialog or the last database save) [koppor#538](https://github.com/koppor/jabref/issues/538)
- When writing a bib file, the `NegativeArraySizeException` should not occur [#8231](https://github.com/JabRef/jabref/issues/8231) [#8265](https://github.com/JabRef/jabref/issues/8265)
- We fixed an issue where the export menu entries were available when they should not be. [#4795](https://github.com/JabRef/jabref/issues/4795)
- We fixed an issue where some menu entries were available without entries selected. [#4795](https://github.com/JabRef/jabref/issues/4795)
- We fixed an issue where right-clicking on a tab and selecting close will close the focused tab even if it is not the tab we right-clicked [#8193](https://github.com/JabRef/jabref/pull/8193)
- We fixed an issue where selecting a citation style in the preferences would sometimes produce an exception [#7860](https://github.com/JabRef/jabref/issues/7860)
- We fixed an issue where an exception would occur when clicking on a DOI link in the preview pane [#7706](https://github.com/JabRef/jabref/issues/7706)
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 @@ -745,7 +745,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, Globals.getClipboardManager(), prefs)),
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.getClipboardManager(), prefs.getPreviewPreferences())),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService, Globals.exportFactory, Globals.getClipboardManager(), Globals.TASK_EXECUTOR, prefs))),
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(dialogService, Globals.exportFactory, stateManager, Globals.getClipboardManager(), Globals.TASK_EXECUTOR, prefs))),

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

Expand Down
81 changes: 39 additions & 42 deletions src/main/java/org/jabref/gui/exporter/ExportToClipboardAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -15,8 +17,8 @@

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
Expand All @@ -37,43 +39,42 @@ public class ExportToClipboardAction extends SimpleCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(ExportToClipboardAction.class);

// Only text based exporters can be used
private static final Set<FileType> SUPPORTED_FILETYPES = Set.of(StandardFileType.TXT, StandardFileType.RTF, StandardFileType.RDF, StandardFileType.XML, StandardFileType.HTML, StandardFileType.CSV, StandardFileType.RIS);
private static final Set<FileType> SUPPORTED_FILETYPES = Set.of(
StandardFileType.TXT,
StandardFileType.RTF,
StandardFileType.RDF,
StandardFileType.XML,
StandardFileType.HTML,
StandardFileType.CSV,
StandardFileType.RIS);

private JabRefFrame frame;
private final DialogService dialogService;
private LibraryTab panel;
private final List<BibEntry> entries = new ArrayList<>();
private final ExporterFactory exporterFactory;
private final ClipBoardManager clipBoardManager;
private final TaskExecutor taskExecutor;
private final PreferencesService preferences;

public ExportToClipboardAction(JabRefFrame frame, DialogService dialogService, ExporterFactory exporterFactory, ClipBoardManager clipBoardManager, TaskExecutor taskExecutor, PreferencesService prefs) {
this.frame = frame;
this.dialogService = dialogService;
this.exporterFactory = exporterFactory;
this.clipBoardManager = clipBoardManager;
this.taskExecutor = taskExecutor;
this.preferences = prefs;
}

public ExportToClipboardAction(LibraryTab panel, DialogService dialogService, ExporterFactory exporterFactory, ClipBoardManager clipBoardManager, TaskExecutor taskExecutor, PreferencesService prefs) {
this.panel = panel;
private final StateManager stateManager;

public ExportToClipboardAction(DialogService dialogService,
ExporterFactory exporterFactory,
StateManager stateManager,
ClipBoardManager clipBoardManager,
TaskExecutor taskExecutor,
PreferencesService prefs) {
this.dialogService = dialogService;
this.exporterFactory = exporterFactory;
this.clipBoardManager = clipBoardManager;
this.taskExecutor = taskExecutor;
this.preferences = prefs;
this.stateManager = stateManager;

this.executable.bind(ActionHelper.needsEntriesSelected(stateManager));
}

@Override
public void execute() {
if (panel == null) {
panel = frame.getCurrentLibraryTab();
}

if (panel.getSelectedEntries().isEmpty()) {
if (stateManager.getSelectedEntries().isEmpty()) {
dialogService.notify(Localization.lang("This operation requires one or more entries to be selected."));
return;
}
Expand Down Expand Up @@ -106,8 +107,9 @@ 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!)
preferences.storeFileDirforDatabase(panel.getBibDatabaseContext()
.getFileDirectories(preferences.getFilePreferences()));
preferences.storeFileDirforDatabase(stateManager.getActiveDatabase()
.map(db -> db.getFileDirectories(preferences.getFilePreferences()))
.orElse(List.of(preferences.getFilePreferences().getWorkingDirectory())));

// Add chosen export type to last used preference, to become default
preferences.getImportExportPreferences().setLastExportExtension(exporter.getName());
Expand All @@ -118,14 +120,14 @@ private ExportResult exportToClipboard(Exporter exporter) throws Exception {
// file, and read the contents afterwards:
tmp = Files.createTempFile("jabrefCb", ".tmp");

entries.addAll(panel.getSelectedEntries());
entries.addAll(stateManager.getSelectedEntries());

// Write to file:
exporter.export(panel.getBibDatabaseContext(), tmp,
panel.getBibDatabaseContext()
.getMetaData()
.getEncoding()
.orElse(preferences.getGeneralPreferences().getDefaultEncoding()),
exporter.export(stateManager.getActiveDatabase().get(), tmp,
stateManager.getActiveDatabase().get()
.getMetaData()
.getEncoding()
.orElse(preferences.getGeneralPreferences().getDefaultEncoding()),
entries);
// Read the file and put the contents on the clipboard:

Expand Down Expand Up @@ -159,22 +161,17 @@ private void setContentToClipboard(ExportResult result) {
}

private String readFileToString(Path tmp) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(tmp, panel.getBibDatabaseContext()
.getMetaData()
.getEncoding()
.orElse(preferences.getGeneralPreferences().getDefaultEncoding()))) {
Charset defaultEncoding = Objects.requireNonNull(preferences.getGeneralPreferences().getDefaultEncoding());
try (BufferedReader reader = Files.newBufferedReader(tmp, stateManager.getActiveDatabase()
.map(db -> db.getMetaData()
.getEncoding()
.orElse(defaultEncoding))
.orElse(defaultEncoding))) {
return reader.lines().collect(Collectors.joining(OS.NEWLINE));
}
}

private static class ExportResult {

final String content;
final FileType fileType;
private record ExportResult(String content, FileType fileType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wait with this new Syntax elements. We are still at jdk 16

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Records are already a stable language feature since jdk 16.
https://openjdk.java.net/jeps/395


ExportResult(String content, FileType fileType) {
this.content = content;
this.fileType = fileType;
}
}
}
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 @@ -110,7 +110,7 @@ private static Menu createCopySubMenu(LibraryTab libraryTab,
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, previewPreferences)));
}

copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.EXPORT_TO_CLIPBOARD, new ExportToClipboardAction(libraryTab, dialogService, Globals.exportFactory, clipBoardManager, Globals.TASK_EXECUTOR, preferencesService)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.EXPORT_TO_CLIPBOARD, new ExportToClipboardAction(dialogService, Globals.exportFactory, stateManager, clipBoardManager, Globals.TASK_EXECUTOR, preferencesService)));
return copySpecialMenu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.util.CurrentThreadTaskExecutor;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.exporter.Exporter;
Expand All @@ -30,6 +30,7 @@
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.metadata.MetaData;
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.GeneralPreferences;
import org.jabref.preferences.ImportExportPreferences;
import org.jabref.preferences.PreferencesService;
Expand All @@ -50,43 +51,42 @@
public class ExportToClipboardActionTest {

private ExportToClipboardAction exportToClipboardAction;
private final LibraryTab libraryTab = mock(LibraryTab.class);
private final JabRefFrame jabRefFrame = mock(JabRefFrame.class);
private final DialogService dialogService = spy(DialogService.class);
private ExporterFactory exporterFactory;
private final ClipBoardManager clipBoardManager = mock(ClipBoardManager.class);
private TaskExecutor taskExecutor;
private List<BibEntry> selectedEntries;
private final BibDatabaseContext databaseContext = mock(BibDatabaseContext.class);
private final PreferencesService preferences = spy(PreferencesService.class);
private final ImportExportPreferences importExportPrefs = mock(ImportExportPreferences.class);
private final StateManager stateManager = mock(StateManager.class);

private ExporterFactory exporterFactory;
private TaskExecutor taskExecutor;
private ObservableList<BibEntry> selectedEntries;

@BeforeEach
public void setUp() {
taskExecutor = new CurrentThreadTaskExecutor();

List<TemplateExporter> customFormats = new ArrayList<>();
LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS);
SavePreferences savePreferences = mock(SavePreferences.class);
XmpPreferences xmpPreferences = mock(XmpPreferences.class);
BibEntryTypesManager entryTypesManager = mock(BibEntryTypesManager.class);
exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences, xmpPreferences, BibDatabaseMode.BIBTEX, entryTypesManager);
exportToClipboardAction = new ExportToClipboardAction(libraryTab, dialogService, exporterFactory, clipBoardManager, taskExecutor, preferences);

BibEntry entry = new BibEntry(StandardEntryType.Misc)
.withField(StandardField.AUTHOR, "Souti Chattopadhyay and Nicholas Nelson and Audrey Au and Natalia Morales and Christopher Sanchez and Rahul Pandita and Anita Sarma")
.withField(StandardField.TITLE, "A tale from the trenches")
.withField(StandardField.YEAR, "2020")
.withField(StandardField.DOI, "10.1145/3377811.3380330")
.withField(StandardField.SUBTITLE, "cognitive biases and software development");

selectedEntries = new ArrayList<>();
selectedEntries.add(entry);
selectedEntries = FXCollections.observableArrayList(entry);
when(stateManager.getSelectedEntries()).thenReturn(selectedEntries);

taskExecutor = new CurrentThreadTaskExecutor();
List<TemplateExporter> customFormats = new ArrayList<>();
LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS);
SavePreferences savePreferences = mock(SavePreferences.class);
XmpPreferences xmpPreferences = mock(XmpPreferences.class);
BibEntryTypesManager entryTypesManager = mock(BibEntryTypesManager.class);
exporterFactory = ExporterFactory.create(customFormats, layoutPreferences, savePreferences, xmpPreferences, BibDatabaseMode.BIBTEX, entryTypesManager);
exportToClipboardAction = new ExportToClipboardAction(dialogService, exporterFactory, stateManager, clipBoardManager, taskExecutor, preferences);
}

@Test
public void testExecuteIfNoSelectedEntries() {
when(libraryTab.getSelectedEntries()).thenReturn(Collections.EMPTY_LIST);
when(stateManager.getSelectedEntries()).thenReturn(FXCollections.emptyObservableList());

exportToClipboardAction.execute();
verify(dialogService, times(1)).notify(Localization.lang("This operation requires one or more entries to be selected."));
Expand All @@ -97,27 +97,30 @@ public void testExecuteOnSuccess() {

Exporter selectedExporter = new Exporter("html", "HTML", StandardFileType.HTML) {
@Override
public void export(BibDatabaseContext databaseContext, Path file, Charset encoding, List<BibEntry> entries) throws Exception {
public void export(BibDatabaseContext databaseContext, Path file, Charset encoding, List<BibEntry> entries) {
}
};

when(importExportPrefs.getLastExportExtension()).thenReturn("HTML");
when(preferences.getImportExportPreferences()).thenReturn(importExportPrefs);
GeneralPreferences generalPreferences = mock(GeneralPreferences.class, Answers.RETURNS_DEEP_STUBS);
FilePreferences filePreferences = mock(FilePreferences.class, Answers.RETURNS_DEEP_STUBS);
when(generalPreferences.getDefaultEncoding()).thenReturn(StandardCharsets.UTF_8);
when(preferences.getFilePreferences()).thenReturn(filePreferences);
when(preferences.getGeneralPreferences()).thenReturn(generalPreferences);
when(libraryTab.getSelectedEntries()).thenReturn(selectedEntries);
when(libraryTab.getBibDatabaseContext()).thenReturn(databaseContext);
when(databaseContext.getFileDirectories(preferences.getFilePreferences())).thenReturn(new ArrayList<>(Arrays.asList(Path.of("path"))));
when(stateManager.getSelectedEntries()).thenReturn(selectedEntries);
when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext));
// noinspection ConstantConditions since databaseContext is mocked
when(databaseContext.getFileDirectories(preferences.getFilePreferences())).thenReturn(new ArrayList<>(List.of(Path.of("path"))));
when(databaseContext.getMetaData()).thenReturn(new MetaData());
when(dialogService.showChoiceDialogAndWait(
eq(Localization.lang("Export")), eq(Localization.lang("Select export format")),
eq(Localization.lang("Export")), any(Exporter.class), anyCollection())).thenReturn(Optional.of(selectedExporter));

exportToClipboardAction.execute();
verify(dialogService, times(1)).showChoiceDialogAndWait(
eq(Localization.lang("Export")), eq(Localization.lang("Select export format")),
eq(Localization.lang("Export")), eq(Localization.lang("Select export format")),
eq(Localization.lang("Export")), any(Exporter.class), anyCollection());
verify(dialogService, times(1)).notify(Localization.lang("Entries exported to clipboard") + ": " + selectedEntries.size());
}
}
}