Skip to content

Commit e1a6e78

Browse files
committed
Refactored CitationStyleToClipboardWorkerTest to CopyCitationAction
1 parent 18a3439 commit e1a6e78

File tree

6 files changed

+43
-44
lines changed

6 files changed

+43
-44
lines changed

src/main/java/org/jabref/gui/BasePanel.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.jabref.gui.journals.UnabbreviateAction;
4747
import org.jabref.gui.maintable.MainTable;
4848
import org.jabref.gui.maintable.MainTableDataModel;
49-
import org.jabref.gui.preview.CitationStyleToClipboardWorker;
5049
import org.jabref.gui.specialfields.SpecialFieldDatabaseChangeListener;
5150
import org.jabref.gui.specialfields.SpecialFieldValueViewModel;
5251
import org.jabref.gui.specialfields.SpecialFieldViewModel;
@@ -57,7 +56,6 @@
5756
import org.jabref.gui.undo.UndoableRemoveEntries;
5857
import org.jabref.gui.util.DefaultTaskExecutor;
5958
import org.jabref.logic.citationstyle.CitationStyleCache;
60-
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
6159
import org.jabref.logic.l10n.Localization;
6260
import org.jabref.logic.layout.Layout;
6361
import org.jabref.logic.layout.LayoutHelper;
@@ -283,11 +281,13 @@ private void setupActions() {
283281
// The action for copying the BibTeX key and the title for the first selected entry
284282
actions.put(Actions.COPY_KEY_AND_TITLE, this::copyKeyAndTitle);
285283

284+
/*
286285
actions.put(Actions.COPY_CITATION_ASCII_DOC, () -> copyCitationToClipboard(CitationStyleOutputFormat.ASCII_DOC));
287286
actions.put(Actions.COPY_CITATION_XSLFO, () -> copyCitationToClipboard(CitationStyleOutputFormat.XSL_FO));
288287
actions.put(Actions.COPY_CITATION_HTML, () -> copyCitationToClipboard(CitationStyleOutputFormat.HTML));
289288
actions.put(Actions.COPY_CITATION_RTF, () -> copyCitationToClipboard(CitationStyleOutputFormat.RTF));
290289
actions.put(Actions.COPY_CITATION_TEXT, () -> copyCitationToClipboard(CitationStyleOutputFormat.TEXT));
290+
*/
291291

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

349-
/**
350-
* Generates and copies citations based on the selected entries to the clipboard
351-
*
352-
* @param outputFormat the desired {@link CitationStyleOutputFormat}
353-
*/
354-
private void copyCitationToClipboard(CitationStyleOutputFormat outputFormat) {
355-
CitationStyleToClipboardWorker worker = new CitationStyleToClipboardWorker(this, outputFormat, dialogService, Globals.clipboardManager, Globals.prefs.getPreviewPreferences());
356-
worker.copyCitationStyleToClipboard(Globals.TASK_EXECUTOR);
357-
}
358-
359349
/**
360350
* Removes the selected entries from the database
361351
*

src/main/java/org/jabref/gui/JabRefFrame.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import org.jabref.gui.metadata.BibtexStringEditorAction;
9595
import org.jabref.gui.metadata.PreambleEditor;
9696
import org.jabref.gui.preferences.ShowPreferencesAction;
97+
import org.jabref.gui.preview.CopyCitationAction;
9798
import org.jabref.gui.protectedterms.ManageProtectedTermsAction;
9899
import org.jabref.gui.push.PushToApplicationAction;
99100
import org.jabref.gui.push.PushToApplicationsManager;
@@ -104,9 +105,9 @@
104105
import org.jabref.gui.undo.CountingUndoManager;
105106
import org.jabref.gui.util.BackgroundTask;
106107
import org.jabref.gui.util.DefaultTaskExecutor;
107-
import org.jabref.gui.worker.SendAsEMailAction;
108108
import org.jabref.logic.autosaveandbackup.AutosaveManager;
109109
import org.jabref.logic.autosaveandbackup.BackupManager;
110+
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
110111
import org.jabref.logic.importer.IdFetcher;
111112
import org.jabref.logic.importer.ParserResult;
112113
import org.jabref.logic.importer.WebFetchers;
@@ -706,7 +707,7 @@ private MenuBar createMenu() {
706707
factory.createMenuItem(StandardActions.COPY_CITE_KEY, new OldDatabaseCommandWrapper(Actions.COPY_CITE_KEY, this, stateManager)),
707708
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new OldDatabaseCommandWrapper(Actions.COPY_KEY_AND_TITLE, this, stateManager)),
708709
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new OldDatabaseCommandWrapper(Actions.COPY_KEY_AND_LINK, this, stateManager)),
709-
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new OldDatabaseCommandWrapper(Actions.COPY_CITATION_HTML, this, stateManager)),
710+
factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, prefs.getPreviewPreferences())),
710711
factory.createMenuItem(StandardActions.EXPORT_SELECTED_TO_CLIPBOARD, new ExportToClipboardAction(this, dialogService))),
711712

712713
factory.createMenuItem(StandardActions.PASTE, new EditAction(Actions.PASTE)),

src/main/java/org/jabref/gui/worker/SendAsEMailAction.java src/main/java/org/jabref/gui/SendAsEMailAction.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.jabref.gui.worker;
1+
package org.jabref.gui;
22

33
import java.awt.Desktop;
44
import java.io.IOException;
@@ -9,8 +9,6 @@
99
import java.util.List;
1010

1111
import org.jabref.Globals;
12-
import org.jabref.gui.DialogService;
13-
import org.jabref.gui.StateManager;
1412
import org.jabref.gui.actions.ActionHelper;
1513
import org.jabref.gui.actions.SimpleCommand;
1614
import org.jabref.gui.desktop.JabRefDesktop;

src/main/java/org/jabref/gui/maintable/RightClickMenu.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.jabref.Globals;
88
import org.jabref.gui.BasePanel;
99
import org.jabref.gui.DialogService;
10+
import org.jabref.gui.SendAsEMailAction;
1011
import org.jabref.gui.StateManager;
1112
import org.jabref.gui.actions.ActionFactory;
1213
import org.jabref.gui.actions.Actions;
@@ -18,8 +19,9 @@
1819
import org.jabref.gui.menus.ChangeEntryTypeMenu;
1920
import org.jabref.gui.mergeentries.MergeEntriesAction;
2021
import org.jabref.gui.mergeentries.MergeWithFetchedEntryAction;
22+
import org.jabref.gui.preview.CopyCitationAction;
2123
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
22-
import org.jabref.gui.worker.SendAsEMailAction;
24+
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
2325
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
2426
import org.jabref.logic.citationstyle.PreviewLayout;
2527
import org.jabref.model.entry.field.SpecialField;
@@ -34,7 +36,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
3436
ActionFactory factory = new ActionFactory(keyBindingRepository);
3537

3638
contextMenu.getItems().add(factory.createMenuItem(StandardActions.COPY, new OldCommandWrapper(Actions.COPY, panel)));
37-
contextMenu.getItems().add(createCopySubMenu(panel, factory, dialogService));
39+
contextMenu.getItems().add(createCopySubMenu(panel, factory, dialogService, stateManager));
3840
contextMenu.getItems().add(factory.createMenuItem(StandardActions.PASTE, new OldCommandWrapper(Actions.PASTE, panel)));
3941
contextMenu.getItems().add(factory.createMenuItem(StandardActions.CUT, new OldCommandWrapper(Actions.CUT, panel)));
4042
contextMenu.getItems().add(factory.createMenuItem(StandardActions.DELETE, new OldCommandWrapper(Actions.DELETE, panel)));
@@ -70,7 +72,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
7072
return contextMenu;
7173
}
7274

73-
private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, DialogService dialogService) {
75+
private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, DialogService dialogService, StateManager stateManager) {
7476
Menu copySpecialMenu = factory.createMenu(StandardActions.COPY_MORE);
7577
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_TITLE, new OldCommandWrapper(Actions.COPY_TITLE, panel)));
7678
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_KEY, new OldCommandWrapper(Actions.COPY_KEY, panel)));
@@ -82,15 +84,15 @@ private static Menu createCopySubMenu(BasePanel panel, ActionFactory factory, Di
8284
PreviewPreferences previewPreferences = Globals.prefs.getPreviewPreferences();
8385
PreviewLayout style = previewPreferences.getCurrentPreviewStyle();
8486
if (style instanceof CitationStylePreviewLayout) {
85-
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_HTML, new OldCommandWrapper(Actions.COPY_CITATION_HTML, panel)));
87+
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_HTML, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
8688
Menu copyCitationMenu = factory.createMenu(StandardActions.COPY_CITATION_MORE);
87-
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new OldCommandWrapper(Actions.COPY_CITATION_TEXT, panel)));
88-
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_RTF, new OldCommandWrapper(Actions.COPY_CITATION_RTF, panel)));
89-
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_ASCII_DOC, new OldCommandWrapper(Actions.COPY_CITATION_ASCII_DOC, panel)));
90-
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_XSLFO, new OldCommandWrapper(Actions.COPY_CITATION_XSLFO, panel)));
89+
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_TEXT, new CopyCitationAction(CitationStyleOutputFormat.TEXT, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
90+
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_RTF, new CopyCitationAction(CitationStyleOutputFormat.RTF, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
91+
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_ASCII_DOC, new CopyCitationAction(CitationStyleOutputFormat.ASCII_DOC, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
92+
copyCitationMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_XSLFO, new CopyCitationAction(CitationStyleOutputFormat.XSL_FO, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
9193
copySpecialMenu.getItems().add(copyCitationMenu);
9294
} else {
93-
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new OldCommandWrapper(Actions.COPY_CITATION_HTML, panel)));
95+
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_PREVIEW, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, Globals.clipboardManager, previewPreferences)));
9496
}
9597

9698
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.EXPORT_TO_CLIPBOARD, new ExportToClipboardAction(panel, dialogService)));

src/main/java/org/jabref/gui/preview/CitationStyleToClipboardWorker.java src/main/java/org/jabref/gui/preview/CopyCitationAction.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import java.io.IOException;
44
import java.io.StringReader;
55
import java.util.ArrayList;
6+
import java.util.Collections;
67
import java.util.List;
78

89
import javafx.scene.input.ClipboardContent;
910

1011
import org.jabref.Globals;
11-
import org.jabref.gui.BasePanel;
1212
import org.jabref.gui.ClipBoardManager;
1313
import org.jabref.gui.DialogService;
14+
import org.jabref.gui.StateManager;
15+
import org.jabref.gui.actions.ActionHelper;
16+
import org.jabref.gui.actions.SimpleCommand;
1417
import org.jabref.gui.util.BackgroundTask;
15-
import org.jabref.gui.util.TaskExecutor;
1618
import org.jabref.logic.citationstyle.CitationStyleGenerator;
1719
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
1820
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
@@ -32,33 +34,35 @@
3234
* Copies the selected entries and formats them with the selected citation style (or preview), then it is copied to the clipboard.
3335
* This worker cannot be reused.
3436
*/
35-
public class CitationStyleToClipboardWorker {
37+
public class CopyCitationAction extends SimpleCommand {
3638

37-
private static final Logger LOGGER = LoggerFactory.getLogger(CitationStyleToClipboardWorker.class);
39+
private static final Logger LOGGER = LoggerFactory.getLogger(CopyCitationAction.class);
3840

39-
private final BasePanel basePanel;
4041
private final List<BibEntry> selectedEntries;
42+
private StateManager stateManager;
4143
private final PreviewLayout style;
4244
private final String previewStyle;
4345
private final CitationStyleOutputFormat outputFormat;
4446
private final DialogService dialogService;
4547
private final ClipBoardManager clipBoardManager;
4648

47-
public CitationStyleToClipboardWorker(BasePanel basePanel, CitationStyleOutputFormat outputFormat, DialogService dialogService, ClipBoardManager clipBoardManager, PreviewPreferences previewPreferences) {
48-
this.basePanel = basePanel;
49-
this.selectedEntries = basePanel.getSelectedEntries();
49+
public CopyCitationAction(CitationStyleOutputFormat outputFormat, DialogService dialogService, StateManager stateManager, ClipBoardManager clipBoardManager, PreviewPreferences previewPreferences) {
50+
this.selectedEntries = stateManager.getSelectedEntries();
5051
this.style = previewPreferences.getCurrentPreviewStyle();
5152
this.previewStyle = previewPreferences.getPreviewStyle();
5253
this.outputFormat = outputFormat;
5354
this.clipBoardManager = clipBoardManager;
5455
this.dialogService = dialogService;
56+
this.stateManager = stateManager;
57+
58+
this.executable.bind(ActionHelper.needsEntriesSelected(stateManager));
5559
}
5660

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

6468
private List<String> generateCitations() throws IOException {
@@ -71,13 +75,17 @@ private List<String> generateCitations() throws IOException {
7175
if (styleSource != null) {
7276
return CitationStyleGenerator.generateCitations(selectedEntries, styleSource, outputFormat);
7377
} else {
78+
if (stateManager.getActiveDatabase().isEmpty()) {
79+
return Collections.emptyList();
80+
}
81+
7482
StringReader sr = new StringReader(previewStyle.replace("__NEWLINE__", "\n"));
7583
LayoutFormatterPreferences layoutFormatterPreferences = Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
7684
Layout layout = new LayoutHelper(sr, layoutFormatterPreferences).getLayoutFromText();
7785

7886
List<String> citations = new ArrayList<>(selectedEntries.size());
7987
for (BibEntry entry : selectedEntries) {
80-
citations.add(layout.doLayout(entry, basePanel.getDatabase()));
88+
citations.add(layout.doLayout(entry, stateManager.getActiveDatabase().get().getDatabase()));
8189
}
8290
return citations;
8391
}

src/test/java/org/jabref/gui/preview/CitationStyleToClipboardWorkerTest.java src/test/java/org/jabref/gui/preview/CopyCitationActionTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

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

14-
class CitationStyleToClipboardWorkerTest {
14+
class CopyCitationActionTest {
1515

1616
@Test
1717
void processPreviewText() throws Exception {
@@ -39,7 +39,7 @@ void processPreviewText() throws Exception {
3939
OS.NEWLINE +
4040
"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. ";
4141

42-
ClipboardContent clipboardContent = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));
42+
ClipboardContent clipboardContent = CopyCitationAction.processPreview(Arrays.asList(citation, citation));
4343
String actual = clipboardContent.getString();
4444

4545
assertEquals(expected, actual);
@@ -92,7 +92,7 @@ void processPreviewHtml() throws Exception {
9292
"</dd>" + OS.NEWLINE +
9393
"<p></p></font>";
9494

95-
ClipboardContent clipboardContent = CitationStyleToClipboardWorker.processPreview(Arrays.asList(citation, citation));
95+
ClipboardContent clipboardContent = CopyCitationAction.processPreview(Arrays.asList(citation, citation));
9696
String actual = clipboardContent.getString();
9797
assertEquals(expected, actual);
9898
}
@@ -103,7 +103,7 @@ void processText() throws Exception {
103103
"[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;
104104

105105
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;
106-
ClipboardContent textTransferable = CitationStyleToClipboardWorker.processText(Arrays.asList(citation, citation));
106+
ClipboardContent textTransferable = CopyCitationAction.processText(Arrays.asList(citation, citation));
107107

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

120120
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;
121-
ClipboardContent content = CitationStyleToClipboardWorker.processRtf(Arrays.asList(citation, citation));
121+
ClipboardContent content = CopyCitationAction.processRtf(Arrays.asList(citation, citation));
122122

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

194-
ClipboardContent xmlTransferable = CitationStyleToClipboardWorker.processXslFo(Arrays.asList(citation, citation));
194+
ClipboardContent xmlTransferable = CopyCitationAction.processXslFo(Arrays.asList(citation, citation));
195195

196196
Object actual = xmlTransferable.get(ClipBoardManager.XML);
197197
assertEquals(expected, actual);
@@ -221,7 +221,7 @@ void processHtmlAsHtml() throws Exception {
221221
String citation = " <div class=\"csl-entry\">" + OS.NEWLINE +
222222
" <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 +
223223
" </div>" + OS.NEWLINE;
224-
ClipboardContent htmlTransferable = CitationStyleToClipboardWorker.processHtml(Arrays.asList(citation, citation));
224+
ClipboardContent htmlTransferable = CopyCitationAction.processHtml(Arrays.asList(citation, citation));
225225

226226
Object actual = htmlTransferable.getHtml();
227227
assertEquals(expected, actual);

0 commit comments

Comments
 (0)