Skip to content

Commit b71360b

Browse files
committed
Refactored SendAsEMailAction
1 parent f664d10 commit b71360b

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import org.jabref.gui.undo.UndoableInsertEntries;
5757
import org.jabref.gui.undo.UndoableRemoveEntries;
5858
import org.jabref.gui.util.DefaultTaskExecutor;
59-
import org.jabref.gui.worker.SendAsEMailAction;
6059
import org.jabref.logic.citationstyle.CitationStyleCache;
6160
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
6261
import org.jabref.logic.l10n.Localization;
@@ -335,7 +334,7 @@ private void setupActions() {
335334
entryEditor.previousPreviewStyle();
336335
}); */
337336

338-
actions.put(Actions.SEND_AS_EMAIL, new SendAsEMailAction(frame));
337+
// actions.put(Actions.SEND_AS_EMAIL, new SendAsEMailAction(frame));
339338

340339
// actions.put(Actions.WRITE_XMP, new WriteXMPAction(this)::execute);
341340

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
import org.jabref.gui.undo.CountingUndoManager;
105105
import org.jabref.gui.util.BackgroundTask;
106106
import org.jabref.gui.util.DefaultTaskExecutor;
107+
import org.jabref.gui.worker.SendAsEMailAction;
107108
import org.jabref.logic.autosaveandbackup.AutosaveManager;
108109
import org.jabref.logic.autosaveandbackup.BackupManager;
109110
import org.jabref.logic.importer.IdFetcher;
@@ -780,7 +781,7 @@ private MenuBar createMenu() {
780781

781782
factory.createMenuItem(StandardActions.GENERATE_CITE_KEYS, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, stateManager)),
782783
factory.createMenuItem(StandardActions.REPLACE_ALL, new OldDatabaseCommandWrapper(Actions.REPLACE_ALL, this, stateManager)),
783-
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new OldDatabaseCommandWrapper(Actions.SEND_AS_EMAIL, this, stateManager)),
784+
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsEMailAction(dialogService, stateManager)),
784785
pushToApplicationMenuItem,
785786

786787
factory.createSubMenu(StandardActions.ABBREVIATE,

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.jabref.gui.mergeentries.MergeEntriesAction;
2020
import org.jabref.gui.mergeentries.MergeWithFetchedEntryAction;
2121
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
22+
import org.jabref.gui.worker.SendAsEMailAction;
2223
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
2324
import org.jabref.logic.citationstyle.PreviewLayout;
2425
import org.jabref.model.entry.field.SpecialField;
@@ -40,7 +41,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit
4041

4142
contextMenu.getItems().add(new SeparatorMenuItem());
4243

43-
contextMenu.getItems().add(factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new OldCommandWrapper(Actions.SEND_AS_EMAIL, panel)));
44+
contextMenu.getItems().add(factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsEMailAction(dialogService, stateManager)));
4445

4546
contextMenu.getItems().add(new SeparatorMenuItem());
4647

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

+22-20
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
import java.util.List;
1010

1111
import org.jabref.Globals;
12-
import org.jabref.gui.BasePanel;
13-
import org.jabref.gui.JabRefFrame;
14-
import org.jabref.gui.actions.BaseAction;
12+
import org.jabref.gui.DialogService;
13+
import org.jabref.gui.StateManager;
14+
import org.jabref.gui.actions.ActionHelper;
15+
import org.jabref.gui.actions.SimpleCommand;
1516
import org.jabref.gui.desktop.JabRefDesktop;
1617
import org.jabref.gui.util.BackgroundTask;
1718
import org.jabref.logic.bibtex.BibEntryWriter;
1819
import org.jabref.logic.bibtex.FieldWriter;
1920
import org.jabref.logic.l10n.Localization;
2021
import org.jabref.logic.util.io.FileUtil;
22+
import org.jabref.model.database.BibDatabaseContext;
2123
import org.jabref.model.entry.BibEntry;
2224
import org.jabref.preferences.JabRefPreferences;
2325

@@ -34,49 +36,50 @@
3436
* are opened. This feature is disabled by default and can be switched on at
3537
* preferences/external programs
3638
*/
37-
public class SendAsEMailAction implements BaseAction {
39+
public class SendAsEMailAction extends SimpleCommand {
3840

3941
private static final Logger LOGGER = LoggerFactory.getLogger(SendAsEMailAction.class);
40-
private final JabRefFrame frame;
42+
private DialogService dialogService;
43+
private StateManager stateManager;
4144

42-
public SendAsEMailAction(JabRefFrame frame) {
43-
this.frame = frame;
45+
public SendAsEMailAction(DialogService dialogService, StateManager stateManager) {
46+
this.dialogService = dialogService;
47+
this.stateManager = stateManager;
48+
49+
this.executable.bind(ActionHelper.needsEntriesSelected(stateManager));
4450
}
4551

4652
@Override
47-
public void action() {
53+
public void execute() {
4854
BackgroundTask.wrap(this::sendEmail)
49-
.onSuccess(frame.getDialogService()::notify)
55+
.onSuccess(dialogService::notify)
5056
.onFailure(e -> {
5157
String message = Localization.lang("Error creating email");
5258
LOGGER.warn(message, e);
53-
frame.getDialogService().notify(message);
59+
dialogService.notify(message);
5460
})
5561
.executeWith(Globals.TASK_EXECUTOR);
5662
}
5763

5864
private String sendEmail() throws Exception {
59-
if (!Desktop.isDesktopSupported()) {
65+
if (!Desktop.isDesktopSupported() || stateManager.getActiveDatabase().isEmpty()) {
6066
return Localization.lang("Error creating email");
6167
}
6268

63-
BasePanel panel = frame.getCurrentBasePanel();
64-
if (panel == null) {
65-
throw new IllegalStateException("Base panel is not available.");
66-
}
67-
if (panel.getSelectedEntries().isEmpty()) {
69+
if (stateManager.getSelectedEntries().isEmpty()) {
6870
return Localization.lang("This operation requires one or more entries to be selected.");
6971
}
7072

7173
StringWriter sw = new StringWriter();
72-
List<BibEntry> bes = panel.getSelectedEntries();
74+
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();
75+
List<BibEntry> bes = stateManager.getSelectedEntries();
7376

7477
// write the entries using sw, which is used later to form the email content
7578
BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new FieldWriter(Globals.prefs.getFieldWriterPreferences()), Globals.entryTypesManager);
7679

7780
for (BibEntry entry : bes) {
7881
try {
79-
bibtexEntryWriter.write(entry, sw, panel.getBibDatabaseContext().getMode());
82+
bibtexEntryWriter.write(entry, sw, databaseContext.getMode());
8083
} catch (IOException e) {
8184
LOGGER.warn("Problem creating BibTeX file for mailing.", e);
8285
}
@@ -88,8 +91,7 @@ private String sendEmail() throws Exception {
8891
// the unofficial "mailto:attachment" property
8992
boolean openFolders = JabRefPreferences.getInstance().getBoolean(JabRefPreferences.OPEN_FOLDERS_OF_ATTACHED_FILES);
9093

91-
List<Path> fileList = FileUtil.getListOfLinkedFiles(bes, frame.getCurrentBasePanel().getBibDatabaseContext()
92-
.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()));
94+
List<Path> fileList = FileUtil.getListOfLinkedFiles(bes, databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()));
9395
for (Path f : fileList) {
9496
attachments.add(f.toAbsolutePath().toString());
9597
if (openFolders) {

0 commit comments

Comments
 (0)