Skip to content

Commit 6431099

Browse files
authored
Improve things arround change detection (#5770)
* Dismiss changes hides change message * Show added entries in change dialog * Improve message when current value is null * Remove unused reference file * Apply post-load actions before comparing with currently load database
1 parent 44fdfa7 commit 6431099

14 files changed

+64
-90
lines changed

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

-8
Original file line numberDiff line numberDiff line change
@@ -991,10 +991,6 @@ public BibDatabaseContext getBibDatabaseContext() {
991991
return this.bibDatabaseContext;
992992
}
993993

994-
public void markExternalChangesAsResolved() {
995-
changeMonitor.ifPresent(DatabaseChangeMonitor::markExternalChangesAsResolved);
996-
}
997-
998994
public SidePaneManager getSidePaneManager() {
999995
return sidePaneManager;
1000996
}
@@ -1059,10 +1055,6 @@ public void resetChangeMonitorAndChangePane() {
10591055
this.getChildren().setAll(changePane);
10601056
}
10611057

1062-
public void updateTimeStamp() {
1063-
changeMonitor.ifPresent(DatabaseChangeMonitor::markAsSaved);
1064-
}
1065-
10661058
public void copy() {
10671059
mainTable.copy();
10681060
}

src/main/java/org/jabref/gui/collab/ChangeDisplayDialog.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ public ChangeDisplayDialog(BibDatabaseContext database, List<DatabaseChangeViewM
4242
Label rootInfo = new Label(Localization.lang("Select the tree nodes to view and accept or reject changes") + '.');
4343
infoPanel.setCenter(rootInfo);
4444

45+
ButtonType dismissChanges = new ButtonType(Localization.lang("Dismiss changes"), ButtonBar.ButtonData.CANCEL_CLOSE);
4546
getDialogPane().getButtonTypes().setAll(
4647
new ButtonType(Localization.lang("Accept changes"), ButtonBar.ButtonData.APPLY),
47-
ButtonType.CANCEL
48+
dismissChanges
4849
);
4950

5051
setResultConverter(button -> {
51-
if (button == ButtonType.CANCEL) {
52+
if (button == dismissChanges) {
5253
return false;
5354
} else {
5455
// Perform all accepted changes

src/main/java/org/jabref/gui/collab/ChangeScanner.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import org.jabref.logic.bibtex.comparator.BibEntryDiff;
1111
import org.jabref.logic.bibtex.comparator.BibStringDiff;
1212
import org.jabref.logic.importer.ImportFormatPreferences;
13+
import org.jabref.logic.importer.OpenDatabase;
1314
import org.jabref.logic.importer.ParserResult;
14-
import org.jabref.logic.importer.fileformat.BibtexImporter;
1515
import org.jabref.model.database.BibDatabaseContext;
1616
import org.jabref.model.util.DummyFileUpdateMonitor;
1717

@@ -36,9 +36,9 @@ public List<DatabaseChangeViewModel> scanForChanges() {
3636
List<DatabaseChangeViewModel> changes = new ArrayList<>();
3737

3838
// Parse the modified file
39+
// Important: apply all post-load actions
3940
ImportFormatPreferences importFormatPreferences = Globals.prefs.getImportFormatPreferences();
40-
ParserResult result = new BibtexImporter(importFormatPreferences, new DummyFileUpdateMonitor())
41-
.importDatabase(database.getDatabasePath().get(), importFormatPreferences.getEncoding());
41+
ParserResult result = OpenDatabase.loadDatabase(database.getDatabasePath().get(), importFormatPreferences, new DummyFileUpdateMonitor());
4242
BibDatabaseContext databaseOnDisk = result.getDatabaseContext();
4343

4444
// Start looking at changes.

src/main/java/org/jabref/gui/collab/DatabaseChangeMonitor.java

-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package org.jabref.gui.collab;
22

33
import java.io.IOException;
4-
import java.nio.file.Files;
5-
import java.nio.file.Path;
64
import java.util.ArrayList;
75
import java.util.List;
86

97
import org.jabref.gui.util.BackgroundTask;
108
import org.jabref.gui.util.TaskExecutor;
11-
import org.jabref.logic.util.io.FileUtil;
129
import org.jabref.model.database.BibDatabaseContext;
1310
import org.jabref.model.util.FileUpdateListener;
1411
import org.jabref.model.util.FileUpdateMonitor;
@@ -22,7 +19,6 @@ public class DatabaseChangeMonitor implements FileUpdateListener {
2219
private final BibDatabaseContext database;
2320
private final FileUpdateMonitor fileMonitor;
2421
private final List<DatabaseChangeListener> listeners;
25-
private Path referenceFile;
2622
private TaskExecutor taskExecutor;
2723

2824
public DatabaseChangeMonitor(BibDatabaseContext database, FileUpdateMonitor fileMonitor, TaskExecutor taskExecutor) {
@@ -34,9 +30,6 @@ public DatabaseChangeMonitor(BibDatabaseContext database, FileUpdateMonitor file
3430
this.database.getDatabasePath().ifPresent(path -> {
3531
try {
3632
fileMonitor.addListenerForFile(path, this);
37-
referenceFile = Files.createTempFile("jabref", ".bib");
38-
referenceFile.toFile().deleteOnExit();
39-
setAsReference(path);
4033
} catch (IOException e) {
4134
LOGGER.error("Error while trying to monitor " + path, e);
4235
}
@@ -64,15 +57,4 @@ public void unregister() {
6457
database.getDatabasePath().ifPresent(file -> fileMonitor.removeListener(file, this));
6558
}
6659

67-
public void markExternalChangesAsResolved() {
68-
markAsSaved();
69-
}
70-
71-
public void markAsSaved() {
72-
database.getDatabasePath().ifPresent(this::setAsReference);
73-
}
74-
75-
private void setAsReference(Path file) {
76-
FileUtil.copyFile(file, referenceFile, true);
77-
}
7860
}

src/main/java/org/jabref/gui/collab/DatabaseChangePane.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ public DatabaseChangePane(Node parent, BibDatabaseContext database, DatabaseChan
3030
private void onDatabaseChanged(List<DatabaseChangeViewModel> changes) {
3131
this.getActions().setAll(
3232
new Action(Localization.lang("Dismiss changes"), event -> {
33-
monitor.markExternalChangesAsResolved();
3433
this.hide();
3534
}),
3635
new Action(Localization.lang("Review changes"), event -> {
3736
ChangeDisplayDialog changeDialog = new ChangeDisplayDialog(database, changes);
38-
boolean changesHandled = changeDialog.showAndWait().orElse(false);
39-
if (changesHandled) {
40-
monitor.markExternalChangesAsResolved();
41-
this.hide();
42-
}
37+
changeDialog.showAndWait();
38+
39+
this.hide();
4340
}));
4441
this.show();
4542
}

src/main/java/org/jabref/gui/collab/EntryAddChangeViewModel.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@
1313

1414
class EntryAddChangeViewModel extends DatabaseChangeViewModel {
1515

16-
private final BibEntry diskEntry;
16+
private final BibEntry entry;
1717

18-
public EntryAddChangeViewModel(BibEntry diskEntry) {
19-
super(Localization.lang("Added entry"));
20-
this.diskEntry = diskEntry;
18+
public EntryAddChangeViewModel(BibEntry entry) {
19+
super();
20+
this.name = entry.getCiteKeyOptional()
21+
.map(key -> Localization.lang("Added entry") + ": '" + key + '\'')
22+
.orElse(Localization.lang("Added entry"));
23+
this.entry = entry;
2124
}
2225

2326
@Override
2427
public void makeChange(BibDatabaseContext database, NamedCompound undoEdit) {
25-
database.getDatabase().insertEntry(diskEntry);
26-
undoEdit.addEdit(new UndoableInsertEntry(database.getDatabase(), diskEntry));
28+
database.getDatabase().insertEntry(entry);
29+
undoEdit.addEdit(new UndoableInsertEntry(database.getDatabase(), entry));
2730
}
2831

2932
@Override
3033
public Node description() {
3134
PreviewViewer previewViewer = new PreviewViewer(new BibDatabaseContext(), JabRefGUI.getMainFrame().getDialogService(), Globals.stateManager);
32-
previewViewer.setEntry(diskEntry);
35+
previewViewer.setLayout(Globals.prefs.getPreviewPreferences().getCurrentPreviewStyle());
36+
previewViewer.setEntry(entry);
3337
return previewViewer;
3438
}
3539
}

src/main/java/org/jabref/gui/collab/EntryChangeViewModel.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ public Node description() {
116116
container.getChildren().add(new Label(Localization.lang("Value cleared externally")));
117117
}
118118

119-
container.getChildren().add(new Label(Localization.lang("Current value") + ": " + value));
119+
if (StringUtil.isNotBlank(value)) {
120+
container.getChildren().add(new Label(Localization.lang("Current value") + ": " + value));
121+
} else {
122+
container.getChildren().add(new Label(Localization.lang("Current value") + ": " + Localization.lang("empty")));
123+
}
120124

121125
return container;
122126
}

src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java

-2
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,12 @@ private boolean doSave() {
135135
SavePreferences.DatabaseSaveType.ALL);
136136

137137
if (success) {
138-
panel.updateTimeStamp();
139138
panel.getUndoManager().markUnchanged();
140139
// (Only) after a successful save the following
141140
// statement marks that the base is unchanged
142141
// since last save:
143142
panel.setNonUndoableChange(false);
144143
panel.setBaseChanged(false);
145-
panel.markExternalChangesAsResolved();
146144

147145
// Reset title of tab
148146
frame.setTabTitle(panel, panel.getTabTitle(),

src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private String openIt(Path file, boolean importEntries, boolean importStrings, b
179179
boolean importSelectorWords) throws IOException, KeyCollisionException {
180180
Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, file.getParent().toString());
181181
// Should this be done _after_ we know it was successfully opened?
182-
ParserResult parserResult = OpenDatabase.loadDatabase(file.toFile(),
182+
ParserResult parserResult = OpenDatabase.loadDatabase(file,
183183
Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor());
184184
AppendDatabaseAction.mergeFromBibtex(panel, parserResult, importEntries, importStrings, importGroups,
185185
importSelectorWords);

src/main/java/org/jabref/gui/preview/PreviewViewer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void setEntry(BibEntry newEntry) {
173173
}
174174

175175
private void update() {
176-
if (!entry.isPresent() || layout == null) {
176+
if (entry.isEmpty() || layout == null) {
177177
// Nothing to do
178178
return;
179179
}

src/main/java/org/jabref/logic/importer/ImportFormatReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public UnknownFormatImport importUnknownFormat(Path filePath, FileUpdateMonitor
170170

171171
// First, see if it is a BibTeX file:
172172
try {
173-
ParserResult parserResult = OpenDatabase.loadDatabase(filePath.toFile(), importFormatPreferences, fileMonitor);
173+
ParserResult parserResult = OpenDatabase.loadDatabase(filePath, importFormatPreferences, fileMonitor);
174174
if (parserResult.getDatabase().hasEntries() || !parserResult.getDatabase().hasNoStrings()) {
175175
parserResult.setFile(filePath.toFile());
176176
return new UnknownFormatImport(ImportFormatReader.BIBTEX_FORMAT, parserResult);

src/main/java/org/jabref/logic/importer/OpenDatabase.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.jabref.logic.importer;
22

3-
import java.io.File;
43
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
57
import java.util.Arrays;
68
import java.util.List;
79

@@ -29,31 +31,26 @@ private OpenDatabase() {
2931
*
3032
* @param name Name of the BIB-file to open
3133
* @return ParserResult which never is null
34+
* @deprecated use {@link #loadDatabase(Path, ImportFormatPreferences, FileUpdateMonitor)} instead
3235
*/
36+
@Deprecated
3337
public static ParserResult loadDatabase(String name, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor) {
34-
File file = new File(name);
35-
LOGGER.info("Opening: " + name);
38+
LOGGER.debug("Opening: " + name);
39+
Path file = Paths.get(name);
3640

37-
if (!file.exists()) {
41+
if (!Files.exists(file)) {
3842
ParserResult pr = ParserResult.fromErrorMessage(Localization.lang("File not found"));
39-
pr.setFile(file);
43+
pr.setFile(file.toFile());
4044

4145
LOGGER.error(Localization.lang("Error") + ": " + Localization.lang("File not found"));
4246
return pr;
4347
}
4448

4549
try {
46-
ParserResult pr = OpenDatabase.loadDatabase(file, importFormatPreferences, fileMonitor);
47-
pr.setFile(file);
48-
if (pr.hasWarnings()) {
49-
for (String aWarn : pr.warnings()) {
50-
LOGGER.warn(aWarn);
51-
}
52-
}
53-
return pr;
50+
return OpenDatabase.loadDatabase(file, importFormatPreferences, fileMonitor);
5451
} catch (IOException ex) {
5552
ParserResult pr = ParserResult.fromError(ex);
56-
pr.setFile(file);
53+
pr.setFile(file.toFile());
5754
LOGGER.error("Problem opening .bib-file", ex);
5855
return pr;
5956
}
@@ -62,9 +59,9 @@ public static ParserResult loadDatabase(String name, ImportFormatPreferences imp
6259
/**
6360
* Opens a new database.
6461
*/
65-
public static ParserResult loadDatabase(File fileToOpen, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor)
62+
public static ParserResult loadDatabase(Path fileToOpen, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor)
6663
throws IOException {
67-
ParserResult result = new BibtexImporter(importFormatPreferences, fileMonitor).importDatabase(fileToOpen.toPath(),
64+
ParserResult result = new BibtexImporter(importFormatPreferences, fileMonitor).importDatabase(fileToOpen,
6865
importFormatPreferences.getEncoding());
6966

7067
if (importFormatPreferences.isKeywordSyncEnabled()) {

src/main/resources/l10n/JabRef_en.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -2094,4 +2094,4 @@ Reset\ All=Reset All
20942094
Column\ type\ %0\ is\ unknown.=Column type %0 is unknown.
20952095
Linked\ identifiers=Linked identifiers
20962096
Special\ field\ type\ %0\ is\ unknown.\ Using\ normal\ column\ type.=Special field type %0 is unknown. Using normal column type.
2097-
2097+
empty=empty

0 commit comments

Comments
 (0)