Skip to content

Commit

Permalink
Refactored SendAsEMailAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Feb 18, 2020
1 parent f664d10 commit b71360b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.worker.SendAsEMailAction;
import org.jabref.logic.citationstyle.CitationStyleCache;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -335,7 +334,7 @@ private void setupActions() {
entryEditor.previousPreviewStyle();
}); */

actions.put(Actions.SEND_AS_EMAIL, new SendAsEMailAction(frame));
// actions.put(Actions.SEND_AS_EMAIL, new SendAsEMailAction(frame));

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

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
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.importer.IdFetcher;
Expand Down Expand Up @@ -780,7 +781,7 @@ private MenuBar createMenu() {

factory.createMenuItem(StandardActions.GENERATE_CITE_KEYS, new OldDatabaseCommandWrapper(Actions.MAKE_KEY, this, stateManager)),
factory.createMenuItem(StandardActions.REPLACE_ALL, new OldDatabaseCommandWrapper(Actions.REPLACE_ALL, this, stateManager)),
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new OldDatabaseCommandWrapper(Actions.SEND_AS_EMAIL, this, stateManager)),
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsEMailAction(dialogService, stateManager)),
pushToApplicationMenuItem,

factory.createSubMenu(StandardActions.ABBREVIATE,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jabref.gui.mergeentries.MergeEntriesAction;
import org.jabref.gui.mergeentries.MergeWithFetchedEntryAction;
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
import org.jabref.gui.worker.SendAsEMailAction;
import org.jabref.logic.citationstyle.CitationStylePreviewLayout;
import org.jabref.logic.citationstyle.PreviewLayout;
import org.jabref.model.entry.field.SpecialField;
Expand All @@ -40,7 +41,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, KeyBindingReposit

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

contextMenu.getItems().add(factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new OldCommandWrapper(Actions.SEND_AS_EMAIL, panel)));
contextMenu.getItems().add(factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsEMailAction(dialogService, stateManager)));

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

Expand Down
42 changes: 22 additions & 20 deletions src/main/java/org/jabref/gui/worker/SendAsEMailAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
import java.util.List;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.actions.BaseAction;
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;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.FieldWriter;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;

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

private static final Logger LOGGER = LoggerFactory.getLogger(SendAsEMailAction.class);
private final JabRefFrame frame;
private DialogService dialogService;
private StateManager stateManager;

public SendAsEMailAction(JabRefFrame frame) {
this.frame = frame;
public SendAsEMailAction(DialogService dialogService, StateManager stateManager) {
this.dialogService = dialogService;
this.stateManager = stateManager;

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

@Override
public void action() {
public void execute() {
BackgroundTask.wrap(this::sendEmail)
.onSuccess(frame.getDialogService()::notify)
.onSuccess(dialogService::notify)
.onFailure(e -> {
String message = Localization.lang("Error creating email");
LOGGER.warn(message, e);
frame.getDialogService().notify(message);
dialogService.notify(message);
})
.executeWith(Globals.TASK_EXECUTOR);
}

private String sendEmail() throws Exception {
if (!Desktop.isDesktopSupported()) {
if (!Desktop.isDesktopSupported() || stateManager.getActiveDatabase().isEmpty()) {
return Localization.lang("Error creating email");
}

BasePanel panel = frame.getCurrentBasePanel();
if (panel == null) {
throw new IllegalStateException("Base panel is not available.");
}
if (panel.getSelectedEntries().isEmpty()) {
if (stateManager.getSelectedEntries().isEmpty()) {
return Localization.lang("This operation requires one or more entries to be selected.");
}

StringWriter sw = new StringWriter();
List<BibEntry> bes = panel.getSelectedEntries();
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get();
List<BibEntry> bes = stateManager.getSelectedEntries();

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

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

List<Path> fileList = FileUtil.getListOfLinkedFiles(bes, frame.getCurrentBasePanel().getBibDatabaseContext()
.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()));
List<Path> fileList = FileUtil.getListOfLinkedFiles(bes, databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()));
for (Path f : fileList) {
attachments.add(f.toAbsolutePath().toString());
if (openFolders) {
Expand Down

0 comments on commit b71360b

Please sign in to comment.