Skip to content

Commit 05eaa64

Browse files
tobiasdiezSiedlerchr
authored andcommitted
Convert AutoLinkFilesAction to JavaFX (#4822)
* Convert AutoLinkFilesAction to JavaFX Fixes #4819. * Convert a few more dialogs to JavaFX
1 parent 85e684b commit 05eaa64

File tree

11 files changed

+153
-297
lines changed

11 files changed

+153
-297
lines changed

src/main/java/org/jabref/JabRefMain.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import java.net.Authenticator;
44

5-
import javax.swing.JFrame;
6-
import javax.swing.JOptionPane;
7-
85
import javafx.application.Application;
96
import javafx.application.Platform;
7+
import javafx.scene.control.Alert;
108
import javafx.stage.Stage;
119

1210
import org.jabref.cli.ArgumentProcessor;
11+
import org.jabref.gui.FXDialog;
1312
import org.jabref.gui.remote.JabRefMessageHandler;
1413
import org.jabref.logic.journals.JournalAbbreviationLoader;
1514
import org.jabref.logic.l10n.Localization;
@@ -119,9 +118,10 @@ private static void ensureCorrectJavaVersion() {
119118
versionError.append("\n");
120119
versionError.append(Localization.lang("Note that currently, JabRef does not run with Java 9."));
121120
}
122-
final JFrame frame = new JFrame();
123-
JOptionPane.showMessageDialog(null, versionError, Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
124-
frame.dispose();
121+
122+
FXDialog alert = new FXDialog(Alert.AlertType.ERROR, Localization.lang("Error"), true);
123+
alert.setHeaderText(null);
124+
alert.setContentText(versionError.toString());
125125

126126
// We exit on Java 9 error since this will definitely not work
127127
if (java9Fail) {

src/main/java/org/jabref/cli/ArgumentProcessor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
import org.jabref.Globals;
1515
import org.jabref.JabRefException;
16-
import org.jabref.gui.externalfiles.AutoSetLinks;
16+
import org.jabref.gui.externalfiles.AutoSetFileLinksUtil;
17+
import org.jabref.gui.externalfiletype.ExternalFileTypes;
18+
import org.jabref.gui.undo.NamedCompound;
1719
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
1820
import org.jabref.logic.exporter.AtomicFileWriter;
1921
import org.jabref.logic.exporter.BibDatabaseWriter;
@@ -496,7 +498,8 @@ private void automaticallySetFileLinks(List<ParserResult> loaded) {
496498
for (ParserResult parserResult : loaded) {
497499
BibDatabase database = parserResult.getDatabase();
498500
LOGGER.info(Localization.lang("Automatically setting file links"));
499-
AutoSetLinks.autoSetLinks(database.getEntries(), parserResult.getDatabaseContext());
501+
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(parserResult.getDatabaseContext(), Globals.prefs.getFilePreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
502+
util.linkAssociatedFiles(database.getEntries(), new NamedCompound(""));
500503
}
501504
}
502505

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import org.jabref.JabRefExecutorService;
5757
import org.jabref.gui.actions.ActionFactory;
5858
import org.jabref.gui.actions.Actions;
59-
import org.jabref.gui.actions.AutoLinkFilesAction;
6059
import org.jabref.gui.actions.BibtexKeyPatternAction;
6160
import org.jabref.gui.actions.ConnectToSharedDatabaseCommand;
6261
import org.jabref.gui.actions.CopyFilesAction;
@@ -87,6 +86,7 @@
8786
import org.jabref.gui.exporter.ExportToClipboardAction;
8887
import org.jabref.gui.exporter.SaveAllAction;
8988
import org.jabref.gui.exporter.SaveDatabaseAction;
89+
import org.jabref.gui.externalfiles.AutoLinkFilesAction;
9090
import org.jabref.gui.externalfiles.FindUnlinkedFilesAction;
9191
import org.jabref.gui.externalfiletype.ExternalFileTypes;
9292
import org.jabref.gui.help.AboutAction;
@@ -112,7 +112,6 @@
112112
import org.jabref.logic.autosaveandbackup.BackupManager;
113113
import org.jabref.logic.importer.IdFetcher;
114114
import org.jabref.logic.importer.OpenDatabase;
115-
import org.jabref.logic.importer.OutputPrinter;
116115
import org.jabref.logic.importer.ParserResult;
117116
import org.jabref.logic.importer.WebFetchers;
118117
import org.jabref.logic.l10n.Localization;
@@ -147,7 +146,7 @@
147146
/**
148147
* The main window of the application.
149148
*/
150-
public class JabRefFrame extends BorderPane implements OutputPrinter {
149+
public class JabRefFrame extends BorderPane {
151150

152151
// Frame titles.
153152
public static final String FRAME_TITLE = "JabRef";
@@ -819,7 +818,7 @@ private MenuBar createMenu() {
819818

820819
new SeparatorMenuItem(),
821820

822-
factory.createMenuItem(StandardActions.SET_FILE_LINKS, new AutoLinkFilesAction())
821+
factory.createMenuItem(StandardActions.SET_FILE_LINKS, new AutoLinkFilesAction(this, prefs))
823822
);
824823

825824
PushToApplicationButton pushToExternal = new PushToApplicationButton(this, pushApplications.getApplications());
@@ -1249,17 +1248,6 @@ private boolean isExistURLorDOI(List<BibEntry> selectEntryList) {
12491248
return false;
12501249
}
12511250

1252-
@Override
1253-
public void showMessage(String message, String title, int msgType) {
1254-
JOptionPane.showMessageDialog(null, message, title, msgType);
1255-
}
1256-
1257-
@Override
1258-
public void setStatus(String s) {
1259-
output(s);
1260-
}
1261-
1262-
@Override
12631251
public void showMessage(String message) {
12641252
JOptionPane.showMessageDialog(null, message);
12651253
}
@@ -1438,15 +1426,15 @@ private void setDefaultTableFontSize() {
14381426
for (BasePanel basePanel : getBasePanelList()) {
14391427
basePanel.updateTableFont();
14401428
}
1441-
setStatus(Localization.lang("Table font size is %0", String.valueOf(GUIGlobals.currentFont.getSize())));
1429+
dialogService.notify(Localization.lang("Table font size is %0", String.valueOf(GUIGlobals.currentFont.getSize())));
14421430
}
14431431

14441432
private void increaseTableFontSize() {
14451433
GUIGlobals.setFont(GUIGlobals.currentFont.getSize() + 1);
14461434
for (BasePanel basePanel : getBasePanelList()) {
14471435
basePanel.updateTableFont();
14481436
}
1449-
setStatus(Localization.lang("Table font size is %0", String.valueOf(GUIGlobals.currentFont.getSize())));
1437+
dialogService.notify(Localization.lang("Table font size is %0", String.valueOf(GUIGlobals.currentFont.getSize())));
14501438
}
14511439

14521440
private void decreaseTableFontSize() {
@@ -1458,7 +1446,7 @@ private void decreaseTableFontSize() {
14581446
for (BasePanel basePanel : getBasePanelList()) {
14591447
basePanel.updateTableFont();
14601448
}
1461-
setStatus(Localization.lang("Table font size is %0", String.valueOf(GUIGlobals.currentFont.getSize())));
1449+
dialogService.notify(Localization.lang("Table font size is %0", String.valueOf(GUIGlobals.currentFont.getSize())));
14621450
}
14631451

14641452
private class CloseDatabaseAction extends SimpleCommand {

src/main/java/org/jabref/gui/actions/AutoLinkFilesAction.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.jabref.gui.externalfiles;
2+
3+
import java.util.List;
4+
5+
import javafx.concurrent.Task;
6+
7+
import org.jabref.gui.DialogService;
8+
import org.jabref.gui.JabRefFrame;
9+
import org.jabref.gui.actions.SimpleCommand;
10+
import org.jabref.gui.externalfiletype.ExternalFileTypes;
11+
import org.jabref.gui.undo.NamedCompound;
12+
import org.jabref.logic.l10n.Localization;
13+
import org.jabref.model.entry.BibEntry;
14+
import org.jabref.preferences.JabRefPreferences;
15+
16+
/**
17+
* This Action may only be used in a menu or button.
18+
* Never in the entry editor. FileListEditor and EntryEditor have other ways to update the file links
19+
*/
20+
public class AutoLinkFilesAction extends SimpleCommand {
21+
22+
private final DialogService dialogService;
23+
private final JabRefFrame frame;
24+
private final JabRefPreferences preferences;
25+
26+
public AutoLinkFilesAction(JabRefFrame frame, JabRefPreferences preferences) {
27+
this.frame = frame;
28+
this.dialogService = frame.getDialogService();
29+
this.preferences = preferences;
30+
}
31+
32+
@Override
33+
public void execute() {
34+
List<BibEntry> entries = frame.getCurrentBasePanel().getSelectedEntries();
35+
if (entries.isEmpty()) {
36+
dialogService.notify(Localization.lang("This operation requires one or more entries to be selected."));
37+
return;
38+
}
39+
40+
final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));
41+
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(frame.getCurrentBasePanel().getBibDatabaseContext(), preferences.getFilePreferences(), preferences.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
42+
Task<List<BibEntry>> linkFilesTask = new Task<List<BibEntry>>() {
43+
@Override
44+
protected List<BibEntry> call() {
45+
return util.linkAssociatedFiles(entries, nc);
46+
}
47+
48+
@Override
49+
protected void succeeded() {
50+
if (!getValue().isEmpty()) {
51+
if (nc.hasEdits()) {
52+
nc.end();
53+
frame.getCurrentBasePanel().getUndoManager().addEdit(nc);
54+
}
55+
dialogService.notify(Localization.lang("Finished automatically setting external links."));
56+
} else {
57+
dialogService.notify(Localization.lang("Finished automatically setting external links.") + " " + Localization.lang("No files found."));
58+
}
59+
}
60+
};
61+
62+
dialogService.showProgressDialogAndWait(
63+
Localization.lang("Automatically setting file links"),
64+
Localization.lang("Searching for files"),
65+
linkFilesTask
66+
);
67+
}
68+
}

src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
import org.jabref.gui.externalfiletype.ExternalFileType;
1212
import org.jabref.gui.externalfiletype.ExternalFileTypes;
1313
import org.jabref.gui.externalfiletype.UnknownExternalFileType;
14+
import org.jabref.gui.undo.NamedCompound;
15+
import org.jabref.gui.undo.UndoableFieldChange;
16+
import org.jabref.gui.util.DefaultTaskExecutor;
1417
import org.jabref.logic.util.io.AutoLinkPreferences;
1518
import org.jabref.logic.util.io.FileFinder;
1619
import org.jabref.logic.util.io.FileFinders;
1720
import org.jabref.logic.util.io.FileUtil;
1821
import org.jabref.model.database.BibDatabaseContext;
1922
import org.jabref.model.entry.BibEntry;
23+
import org.jabref.model.entry.FieldName;
24+
import org.jabref.model.entry.FileFieldWriter;
2025
import org.jabref.model.entry.LinkedFile;
2126
import org.jabref.model.metadata.FilePreferences;
2227
import org.jabref.model.util.FileHelper;
@@ -26,7 +31,7 @@
2631

2732
public class AutoSetFileLinksUtil {
2833

29-
private static final Logger LOGGER = LoggerFactory.getLogger(AutoSetLinks.class);
34+
private static final Logger LOGGER = LoggerFactory.getLogger(AutoSetFileLinksUtil.class);
3035
private List<Path> directories;
3136
private AutoLinkPreferences autoLinkPreferences;
3237
private ExternalFileTypes externalFileTypes;
@@ -35,12 +40,44 @@ public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences
3540
this(databaseContext.getFileDirectoriesAsPaths(filePreferences), autoLinkPreferences, externalFileTypes);
3641
}
3742

38-
public AutoSetFileLinksUtil(List<Path> directories, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
43+
private AutoSetFileLinksUtil(List<Path> directories, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
3944
this.directories = directories;
4045
this.autoLinkPreferences = autoLinkPreferences;
4146
this.externalFileTypes = externalFileTypes;
4247
}
4348

49+
public List<BibEntry> linkAssociatedFiles(List<BibEntry> entries, NamedCompound ce) {
50+
List<BibEntry> changedEntries = new ArrayList<>();
51+
for (BibEntry entry : entries) {
52+
53+
List<LinkedFile> linkedFiles = new ArrayList<>();
54+
try {
55+
linkedFiles = findAssociatedNotLinkedFiles(entry);
56+
} catch (IOException e) {
57+
LOGGER.error("Problem finding files", e);
58+
}
59+
60+
if (ce != null) {
61+
for (LinkedFile linkedFile : linkedFiles) {
62+
// store undo information
63+
String newVal = FileFieldWriter.getStringRepresentation(linkedFile);
64+
65+
String oldVal = entry.getField(FieldName.FILE).orElse(null);
66+
67+
UndoableFieldChange fieldChange = new UndoableFieldChange(entry, FieldName.FILE, oldVal, newVal);
68+
ce.addEdit(fieldChange);
69+
70+
DefaultTaskExecutor.runInJavaFXThread(() -> {
71+
entry.addFile(linkedFile);
72+
});
73+
}
74+
75+
changedEntries.add(entry);
76+
}
77+
}
78+
return changedEntries;
79+
}
80+
4481
public List<LinkedFile> findAssociatedNotLinkedFiles(BibEntry entry) throws IOException {
4582
List<LinkedFile> linkedFiles = new ArrayList<>();
4683

@@ -53,22 +90,23 @@ public List<LinkedFile> findAssociatedNotLinkedFiles(BibEntry entry) throws IOEx
5390
// Collect the found files that are not yet linked
5491
for (Path foundFile : result) {
5592
boolean fileAlreadyLinked = entry.getFiles().stream()
56-
.map(file -> file.findIn(directories))
57-
.anyMatch(file -> {
58-
try {
59-
return file.isPresent() && Files.isSameFile(file.get(), foundFile);
60-
} catch (IOException e) {
61-
LOGGER.error("Problem with isSameFile", e);
62-
}
63-
return false;
64-
});
93+
.map(file -> file.findIn(directories))
94+
.anyMatch(file -> {
95+
try {
96+
return file.isPresent() && Files.isSameFile(file.get(), foundFile);
97+
} catch (IOException e) {
98+
LOGGER.error("Problem with isSameFile", e);
99+
}
100+
return false;
101+
});
102+
65103
if (!fileAlreadyLinked) {
66104
Optional<ExternalFileType> type = FileHelper.getFileExtension(foundFile)
67-
.map(externalFileTypes::getExternalFileTypeByExt)
68-
.orElse(Optional.of(new UnknownExternalFileType("")));
105+
.map(externalFileTypes::getExternalFileTypeByExt)
106+
.orElse(Optional.of(new UnknownExternalFileType("")));
69107

70108
String strType = type.isPresent() ? type.get().getName() : "";
71-
String relativeFilePath = FileUtil.relativize(foundFile, directories).toString();
109+
Path relativeFilePath = FileUtil.relativize(foundFile, directories);
72110
LinkedFile linkedFile = new LinkedFile("", relativeFilePath, strType);
73111
linkedFiles.add(linkedFile);
74112
}

0 commit comments

Comments
 (0)