Verified Owner
Top Inners Fields Constructors Methods
org.jabref.gui.collab

public Class DatabaseChangeMonitor

extends Object
implements FileUpdateListener
Class Inheritance
Imports
java.io.IOException, java.nio.file.Path, java.util.ArrayList, .List, .Optional, org.jabref.gui.DialogService, .LibraryTab, .Notifications, .StateManager, org.jabref.gui.preferences.GuiPreferences, org.jabref.logic.l10n.Localization, org.jabref.logic.undo.UndoManager, org.jabref.logic.util.BackgroundTask, .TaskExecutor, org.jabref.logic.util.io.FileSnapshot, org.jabref.model.database.BibDatabaseContext, org.jabref.model.util.FileUpdateListener, .FileUpdateMonitor, com.dlsc.gemsfx.infocenter.NotificationAction, org.jspecify.annotations.Nullable, org.slf4j.Logger, .LoggerFactory

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private @Nullable DatabaseChangeMonitor.ExternalLibraryChangeNotification
private boolean
private final BibDatabaseContext
private final DialogService
private final FileUpdateMonitor
private @Nullable FileSnapshot
knownDiskState

State of the monitored file as of the last scan or the last point where the in-memory library was known to match the disk (library load, successful save, all external changes merged).

private final LibraryTab
private final List<DatabaseChangeListener>
private static final Logger
private final Optional<Path>
private final GuiPreferences
private final StateManager
private final TaskExecutor
private final UndoManager

Constructor Summary

AccessConstructor and Description
public
DatabaseChangeMonitor(BibDatabaseContext database, FileUpdateMonitor fileMonitor, TaskExecutor taskExecutor, DialogService dialogService, GuiPreferences preferences, UndoManager undoManager, StateManager stateManager, LibraryTab libraryTab)

Method Summary

Modifier and TypeMethod and Description
public void
pack-priv void
applyResolvedChanges(List<DatabaseChange>
the externally resolved changes to apply to the in-memory database
resolvedChanges
,
boolean
true if the accepted result now matches the file on disk, so the library can be marked clean; false if the resolved result differs from disk and still needs saving
resolvedChangesMatchDisk
)

Applies the accepted external changes and updates the library's dirty state.

private void
public void
public void
markConsistentWithDisk(@Nullable FileSnapshot
the matching on-disk state, ideally as reported by the writer that committed it (captured right after the commit, this leaves no window in which a concurrent write could be mistaken for the consistent state); null to read the current state from the file instead, which is subject to such a window — its worst case is a delayed notification, never a lost update, since lost-update protection lives in org.jabref.logic.exporter.AtomicFileOutputStream
diskState
)

Records the given file state as consistent with the in-memory library.

pack-priv void
public void
private void
scanForChanges()

Looks for notable changes of the file on disk compared to the in-memory library and notifies listeners in case there are such changes.

private void
scanIfFileChanged()

A full scan parses the whole library file, so it is skipped when size and modification time show that the file has not actually changed since the last known-consistent state — e.g. for events caused by JabRef's own save.

public void
suspendChangeDetection()

Defers handling of file events while keeping the watcher registered, so that events arriving during JabRef's own save are neither acted upon (the file may be half-written) nor lost.

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

activeNotificationback to summary
private @Nullable DatabaseChangeMonitor.ExternalLibraryChangeNotification activeNotification
changeDetectionSuspendedback to summary
private boolean changeDetectionSuspended
databaseback to summary
private final BibDatabaseContext database
dialogServiceback to summary
private final DialogService dialogService
fileMonitorback to summary
private final FileUpdateMonitor fileMonitor
knownDiskStateback to summary
private @Nullable FileSnapshot knownDiskState

State of the monitored file as of the last scan or the last point where the in-memory library was known to match the disk (library load, successful save, all external changes merged). Guarded by synchronized (database); null when unknown, in which case the next event triggers a full scan.

libraryTabback to summary
private final LibraryTab libraryTab
listenersback to summary
private final List<DatabaseChangeListener> listeners
LOGGERback to summary
private static final Logger LOGGER
monitoredPathback to summary
private final Optional<Path> monitoredPath
preferencesback to summary
private final GuiPreferences preferences
stateManagerback to summary
private final StateManager stateManager
taskExecutorback to summary
private final TaskExecutor taskExecutor
undoManagerback to summary
private final UndoManager undoManager

Constructor Detail

DatabaseChangeMonitorback to summary
public DatabaseChangeMonitor(BibDatabaseContext database, FileUpdateMonitor fileMonitor, TaskExecutor taskExecutor, DialogService dialogService, GuiPreferences preferences, UndoManager undoManager, StateManager stateManager, LibraryTab libraryTab)

Method Detail

addListenerback to summary
public void addListener(DatabaseChangeListener listener)
applyResolvedChangesback to summary
pack-priv void applyResolvedChanges(List<DatabaseChange> resolvedChanges, boolean resolvedChangesMatchDisk)

Applies the accepted external changes and updates the library's dirty state.

Parameters
resolvedChanges:List<DatabaseChange>

the externally resolved changes to apply to the in-memory database

resolvedChangesMatchDisk:boolean

true if the accepted result now matches the file on disk, so the library can be marked clean; false if the resolved result differs from disk and still needs saving

clearActiveNotificationback to summary
private void clearActiveNotification(DatabaseChangeMonitor.ExternalLibraryChangeNotification notification)
fileUpdatedback to summary
public void fileUpdated()
Annotations
@Override
markConsistentWithDiskback to summary
public void markConsistentWithDisk(@Nullable FileSnapshot diskState)

Records the given file state as consistent with the in-memory library. To be called whenever the two are known to match (successful save, all external changes merged), so that watcher events reflecting that very state do not trigger a scan.

Parameters
diskState:@Nullable FileSnapshot

the matching on-disk state, ideally as reported by the writer that committed it (captured right after the commit, this leaves no window in which a concurrent write could be mistaken for the consistent state); null to read the current state from the file instead, which is subject to such a window — its worst case is a delayed notification, never a lost update, since lost-update protection lives in org.jabref.logic.exporter.AtomicFileOutputStream

notifyExternalChangesback to summary
pack-priv void notifyExternalChanges(List<DatabaseChange> changes)
resumeChangeDetectionback to summary
public void resumeChangeDetection()
scanForChangesback to summary
private void scanForChanges()

Looks for notable changes of the file on disk compared to the in-memory library and notifies listeners in case there are such changes.

scanIfFileChangedback to summary
private void scanIfFileChanged()

A full scan parses the whole library file, so it is skipped when size and modification time show that the file has not actually changed since the last known-consistent state — e.g. for events caused by JabRef's own save.

suspendChangeDetectionback to summary
public void suspendChangeDetection()

Defers handling of file events while keeping the watcher registered, so that events arriving during JabRef's own save are neither acted upon (the file may be half-written) nor lost.

unregisterback to summary
public void unregister()
org.jabref.gui.collab back to summary

private Class DatabaseChangeMonitor.ExternalLibraryChangeNotification

extends FileNotification
Class Inheritance

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

ExternalLibraryChangeNotificationback to summary
public ExternalLibraryChangeNotification(List<DatabaseChange> changes)