9
9
import java .util .List ;
10
10
11
11
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 ;
15
16
import org .jabref .gui .desktop .JabRefDesktop ;
16
17
import org .jabref .gui .util .BackgroundTask ;
17
18
import org .jabref .logic .bibtex .BibEntryWriter ;
18
19
import org .jabref .logic .bibtex .FieldWriter ;
19
20
import org .jabref .logic .l10n .Localization ;
20
21
import org .jabref .logic .util .io .FileUtil ;
22
+ import org .jabref .model .database .BibDatabaseContext ;
21
23
import org .jabref .model .entry .BibEntry ;
22
24
import org .jabref .preferences .JabRefPreferences ;
23
25
34
36
* are opened. This feature is disabled by default and can be switched on at
35
37
* preferences/external programs
36
38
*/
37
- public class SendAsEMailAction implements BaseAction {
39
+ public class SendAsEMailAction extends SimpleCommand {
38
40
39
41
private static final Logger LOGGER = LoggerFactory .getLogger (SendAsEMailAction .class );
40
- private final JabRefFrame frame ;
42
+ private DialogService dialogService ;
43
+ private StateManager stateManager ;
41
44
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 ));
44
50
}
45
51
46
52
@ Override
47
- public void action () {
53
+ public void execute () {
48
54
BackgroundTask .wrap (this ::sendEmail )
49
- .onSuccess (frame . getDialogService () ::notify )
55
+ .onSuccess (dialogService ::notify )
50
56
.onFailure (e -> {
51
57
String message = Localization .lang ("Error creating email" );
52
58
LOGGER .warn (message , e );
53
- frame . getDialogService () .notify (message );
59
+ dialogService .notify (message );
54
60
})
55
61
.executeWith (Globals .TASK_EXECUTOR );
56
62
}
57
63
58
64
private String sendEmail () throws Exception {
59
- if (!Desktop .isDesktopSupported ()) {
65
+ if (!Desktop .isDesktopSupported () || stateManager . getActiveDatabase (). isEmpty () ) {
60
66
return Localization .lang ("Error creating email" );
61
67
}
62
68
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 ()) {
68
70
return Localization .lang ("This operation requires one or more entries to be selected." );
69
71
}
70
72
71
73
StringWriter sw = new StringWriter ();
72
- List <BibEntry > bes = panel .getSelectedEntries ();
74
+ BibDatabaseContext databaseContext = stateManager .getActiveDatabase ().get ();
75
+ List <BibEntry > bes = stateManager .getSelectedEntries ();
73
76
74
77
// write the entries using sw, which is used later to form the email content
75
78
BibEntryWriter bibtexEntryWriter = new BibEntryWriter (new FieldWriter (Globals .prefs .getFieldWriterPreferences ()), Globals .entryTypesManager );
76
79
77
80
for (BibEntry entry : bes ) {
78
81
try {
79
- bibtexEntryWriter .write (entry , sw , panel . getBibDatabaseContext () .getMode ());
82
+ bibtexEntryWriter .write (entry , sw , databaseContext .getMode ());
80
83
} catch (IOException e ) {
81
84
LOGGER .warn ("Problem creating BibTeX file for mailing." , e );
82
85
}
@@ -88,8 +91,7 @@ private String sendEmail() throws Exception {
88
91
// the unofficial "mailto:attachment" property
89
92
boolean openFolders = JabRefPreferences .getInstance ().getBoolean (JabRefPreferences .OPEN_FOLDERS_OF_ATTACHED_FILES );
90
93
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 ()));
93
95
for (Path f : fileList ) {
94
96
attachments .add (f .toAbsolutePath ().toString ());
95
97
if (openFolders ) {
0 commit comments