Skip to content

Commit

Permalink
Refactored CitationStyleToClipboardWorkerTest to CopyCitationAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Feb 18, 2020
1 parent 18a3439 commit e1a6e78
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
14 changes: 2 additions & 12 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.jabref.gui.journals.UnabbreviateAction;
import org.jabref.gui.maintable.MainTable;
import org.jabref.gui.maintable.MainTableDataModel;
import org.jabref.gui.preview.CitationStyleToClipboardWorker;
import org.jabref.gui.specialfields.SpecialFieldDatabaseChangeListener;
import org.jabref.gui.specialfields.SpecialFieldValueViewModel;
import org.jabref.gui.specialfields.SpecialFieldViewModel;
Expand All @@ -57,7 +56,6 @@
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.citationstyle.CitationStyleCache;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutHelper;
Expand Down Expand Up @@ -283,11 +281,13 @@ private void setupActions() {
// The action for copying the BibTeX key and the title for the first selected entry
actions.put(Actions.COPY_KEY_AND_TITLE, this::copyKeyAndTitle);

/*
actions.put(Actions.COPY_CITATION_ASCII_DOC, () -> copyCitationToClipboard(CitationStyleOutputFormat.ASCII_DOC));
actions.put(Actions.COPY_CITATION_XSLFO, () -> copyCitationToClipboard(CitationStyleOutputFormat.XSL_FO));
actions.put(Actions.COPY_CITATION_HTML, () -> copyCitationToClipboard(CitationStyleOutputFormat.HTML));
actions.put(Actions.COPY_CITATION_RTF, () -> copyCitationToClipboard(CitationStyleOutputFormat.RTF));
actions.put(Actions.COPY_CITATION_TEXT, () -> copyCitationToClipboard(CitationStyleOutputFormat.TEXT));
*/

// The action for copying the BibTeX keys as hyperlinks to the urls of the selected entries
actions.put(Actions.COPY_KEY_AND_LINK, new CopyBibTeXKeyAndLinkAction(mainTable, Globals.clipboardManager));
Expand Down Expand Up @@ -346,16 +346,6 @@ private void setupActions() {
actions.put(Actions.DOWNLOAD_FULL_TEXT, new DownloadFullTextAction(this)::execute);
}

/**
* Generates and copies citations based on the selected entries to the clipboard
*
* @param outputFormat the desired {@link CitationStyleOutputFormat}
*/
private void copyCitationToClipboard(CitationStyleOutputFormat outputFormat) {
CitationStyleToClipboardWorker worker = new CitationStyleToClipboardWorker(this, outputFormat, dialogService, Globals.clipboardManager, Globals.prefs.getPreviewPreferences());
worker.copyCitationStyleToClipboard(Globals.TASK_EXECUTOR);
}

/**
* Removes the selected entries from the database
*
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.jabref.gui.metadata.BibtexStringEditorAction;
import org.jabref.gui.metadata.PreambleEditor;
import org.jabref.gui.preferences.ShowPreferencesAction;
import org.jabref.gui.preview.CopyCitationAction;
import org.jabref.gui.protectedterms.ManageProtectedTermsAction;
import org.jabref.gui.push.PushToApplicationAction;
import org.jabref.gui.push.PushToApplicationsManager;
Expand All @@ -104,9 +105,9 @@
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.worker.SendAsEMailAction;
import org.jabref.logic.autosaveandbackup.AutosaveManager;
import org.jabref.logic.autosaveandbackup.BackupManager;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.importer.IdFetcher;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.WebFetchers;
Expand Down Expand Up @@ -706,7 +707,7 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.COPY_CITE_KEY, new OldDatabaseCommandWrapper(Actions.COPY_CITE_KEY, this, stateManager)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new OldDatabaseCommandWrapper(Actions.COPY_KEY_AND_TITLE, this, stateManager)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new OldDatabaseCommandWrapper(Actions.COPY_KEY_AND_LINK, this, stateManager)),
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new OldDatabaseCommandWrapper(Actions.COPY_CITATION_HTML, this, stateManager)),
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.PASTE, new EditAction(Actions.PASTE)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.gui.worker;
package org.jabref.gui;

import java.awt.Desktop;
import java.io.IOException;
Expand All @@ -9,8 +9,6 @@
import java.util.List;

import org.jabref.Globals;
import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.desktop.JabRefDesktop;
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.SendAsEMailAction;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.Actions;
Expand All @@ -18,8 +19,9 @@
import org.jabref.gui.menus.ChangeEntryTypeMenu;
import org.jabref.gui.mergeentries.MergeEntriesAction;
import org.jabref.gui.mergeentries.MergeWithFetchedEntryAction;
import org.jabref.gui.preview.CopyCitationAction;
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
import org.jabref.gui.worker.SendAsEMailAction;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
import org.jabref.logic.citationstyle.PreviewLayout;
import org.jabref.model.entry.field.SpecialField;
Expand All @@ -34,7 +36,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
ActionFactory factory = new ActionFactory(keyBindingRepository);

contextMenu.getItems().add(factory.createMenuItem(StandardActions.COPY, new OldCommandWrapper(Actions.COPY, panel)));
contextMenu.getItems().add(createCopySubMenu(panel, factory, dialogService));
contextMenu.getItems().add(createCopySubMenu(panel, factory, dialogService, stateManager));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.PASTE, new OldCommandWrapper(Actions.PASTE, panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.CUT, new OldCommandWrapper(Actions.CUT, panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.DELETE, new OldCommandWrapper(Actions.DELETE, panel)));
Expand Down Expand Up @@ -70,7 +72,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
return contextMenu;
}

private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, DialogService dialogService) {
private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, DialogService dialogService, StateManager stateManager) {
Menu copySpecialMenu = factory.createMenu(StandardActions.COPY_MORE);
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_TITLE, new OldCommandWrapper(Actions.COPY_TITLE, panel)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_KEY, new OldCommandWrapper(Actions.COPY_KEY, panel)));
Expand All @@ -82,15 +84,15 @@ private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, Di
PreviewPreferences previewPreferences = Globals.prefs.getPreviewPreferences();
PreviewLayout style = previewPreferences.getCurrentPreviewStyle();
if (style instanceof CitationStylePreviewLayout) {
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_HTML, new OldCommandWrapper(Actions.COPY_CITATION_HTML, panel)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_HTML, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
Menu copyCitationMenu = factory.createMenu(StandardActions.COPY_CITATION_MORE);
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new OldCommandWrapper(Actions.COPY_CITATION_TEXT, panel)));
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_RTF, new OldCommandWrapper(Actions.COPY_CITATION_RTF, panel)));
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_ASCII_DOC, new OldCommandWrapper(Actions.COPY_CITATION_ASCII_DOC, panel)));
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_XSLFO, new OldCommandWrapper(Actions.COPY_CITATION_XSLFO, panel)));
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new CopyCitationAction(CitationStyleOutputFormat.TEXT, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_RTF, new CopyCitationAction(CitationStyleOutputFormat.RTF, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_ASCII_DOC, new CopyCitationAction(CitationStyleOutputFormat.ASCII_DOC, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_XSLFO, new CopyCitationAction(CitationStyleOutputFormat.XSL_FO, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
copySpecialMenu.getItems().add(copyCitationMenu);
} else {
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new OldCommandWrapper(Actions.COPY_CITATION_HTML, panel)));
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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javafx.scene.input.ClipboardContent;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
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;
import org.jabref.logic.citationstyle.CitationStyleGenerator;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
Expand All @@ -32,33 +34,35 @@
* Copies the selected entries and formats them with the selected citation style (or preview), then it is copied to the clipboard.
* This worker cannot be reused.
*/
public class CitationStyleToClipboardWorker {
public class CopyCitationAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(CitationStyleToClipboardWorker.class);
private static final Logger LOGGER = LoggerFactory.getLogger(CopyCitationAction.class);

private final BasePanel basePanel;
private final List<BibEntry> selectedEntries;
private StateManager stateManager;
private final PreviewLayout style;
private final String previewStyle;
private final CitationStyleOutputFormat outputFormat;
private final DialogService dialogService;
private final ClipBoardManager clipBoardManager;

public CitationStyleToClipboardWorker(BasePanel basePanel, CitationStyleOutputFormat outputFormat, DialogService dialogService, ClipBoardManager clipBoardManager, PreviewPreferences previewPreferences) {
this.basePanel = basePanel;
this.selectedEntries = basePanel.getSelectedEntries();
public CopyCitationAction(CitationStyleOutputFormat outputFormat, DialogService dialogService, StateManager stateManager, ClipBoardManager clipBoardManager, PreviewPreferences previewPreferences) {
this.selectedEntries = stateManager.getSelectedEntries();
this.style = previewPreferences.getCurrentPreviewStyle();
this.previewStyle = previewPreferences.getPreviewStyle();
this.outputFormat = outputFormat;
this.clipBoardManager = clipBoardManager;
this.dialogService = dialogService;
this.stateManager = stateManager;

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

public void copyCitationStyleToClipboard(TaskExecutor taskExecutor) {
public void execute() {
BackgroundTask.wrap(this::generateCitations)
.onFailure(ex -> LOGGER.error("Error while copying citations to the clipboard", ex))
.onSuccess(this::setClipBoardContent)
.executeWith(taskExecutor);
.executeWith(Globals.TASK_EXECUTOR);
}

private List<String> generateCitations() throws IOException {
Expand All @@ -71,13 +75,17 @@ private List<String> generateCitations() throws IOException {
if (styleSource != null) {
return CitationStyleGenerator.generateCitations(selectedEntries, styleSource, outputFormat);
} else {
if (stateManager.getActiveDatabase().isEmpty()) {
return Collections.emptyList();
}

StringReader sr = new StringReader(previewStyle.replace("__NEWLINE__", "\n"));
LayoutFormatterPreferences layoutFormatterPreferences = Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
Layout layout = new LayoutHelper(sr, layoutFormatterPreferences).getLayoutFromText();

List<String> citations = new ArrayList<>(selectedEntries.size());
for (BibEntry entry : selectedEntries) {
citations.add(layout.doLayout(entry, basePanel.getDatabase()));
citations.add(layout.doLayout(entry, stateManager.getActiveDatabase().get().getDatabase()));
}
return citations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

class CitationStyleToClipboardWorkerTest {
class CopyCitationActionTest {

@Test
void processPreviewText() throws Exception {
Expand Down Expand Up @@ -39,7 +39,7 @@ 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. ";

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

assertEquals(expected, actual);
Expand Down Expand Up @@ -92,7 +92,7 @@ void processPreviewHtml() throws Exception {
"</dd>" + OS.NEWLINE +
"<p></p></font>";

ClipboardContent clipboardContent = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));
ClipboardContent clipboardContent = CopyCitationAction.processPreview(Arrays.asList(citation, citation));
String actual = clipboardContent.getString();
assertEquals(expected, actual);
}
Expand All @@ -103,7 +103,7 @@ void processText() throws Exception {
"[1]B. Smith, B. Jones, and J. Williams, “Title of the test entry,” BibTeX Journal, vol. 34, no. 3, pp. 45–67, Jul. 2016." + OS.NEWLINE;

String citation = "[1]B. Smith, B. Jones, and J. Williams, “Title of the test entry,” BibTeX Journal, vol. 34, no. 3, pp. 45–67, Jul. 2016." + OS.NEWLINE;
ClipboardContent textTransferable = CitationStyleToClipboardWorker.processText(Arrays.asList(citation, citation));
ClipboardContent textTransferable = CopyCitationAction.processText(Arrays.asList(citation, citation));

String actual = textTransferable.getString();
assertEquals(expected, actual);
Expand All @@ -118,7 +118,7 @@ void processRtf() throws Exception {
"}";

String citation = "[1]\\tab B. Smith, B. Jones, and J. Williams, \\uc0\\u8220{}Title of the test entry,\\uc0\\u8221{} {\\i{}BibTeX Journal}, vol. 34, no. 3, pp. 45\\uc0\\u8211{}67, Jul. 2016." + OS.NEWLINE;
ClipboardContent content = CitationStyleToClipboardWorker.processRtf(Arrays.asList(citation, citation));
ClipboardContent content = CopyCitationAction.processRtf(Arrays.asList(citation, citation));

Object actual = content.getRtf();
assertEquals(expected, actual);
Expand Down Expand Up @@ -191,7 +191,7 @@ void processXslFo() throws Exception {
" </fo:table>" + OS.NEWLINE +
"</fo:block>" + OS.NEWLINE;

ClipboardContent xmlTransferable = CitationStyleToClipboardWorker.processXslFo(Arrays.asList(citation, citation));
ClipboardContent xmlTransferable = CopyCitationAction.processXslFo(Arrays.asList(citation, citation));

Object actual = xmlTransferable.get(ClipBoardManager.XML);
assertEquals(expected, actual);
Expand Down Expand Up @@ -221,7 +221,7 @@ void processHtmlAsHtml() throws Exception {
String citation = " <div class=\"csl-entry\">" + OS.NEWLINE +
" <div class=\"csl-left-margin\">[1]</div><div class=\"csl-right-inline\">B. Smith, B. Jones, and J. Williams, “Title of the test entry,” <i>BibTeX Journal</i>, vol. 34, no. 3, pp. 45–67, Jul. 2016.</div>" + OS.NEWLINE +
" </div>" + OS.NEWLINE;
ClipboardContent htmlTransferable = CitationStyleToClipboardWorker.processHtml(Arrays.asList(citation, citation));
ClipboardContent htmlTransferable = CopyCitationAction.processHtml(Arrays.asList(citation, citation));

Object actual = htmlTransferable.getHtml();
assertEquals(expected, actual);
Expand Down

0 comments on commit e1a6e78

Please sign in to comment.