use of org.jabref.collab.ChangeScanner in project jabref by JabRef.
the class BasePanel method fileUpdated.
@Override
public void fileUpdated() {
if (saving) {
// If not, we'll handle it on the next polling.
return;
}
updatedExternally = true;
final ChangeScanner scanner = new ChangeScanner(frame, BasePanel.this, getBibDatabaseContext().getDatabaseFile().orElse(null));
// Test: running scan automatically in background
if ((getBibDatabaseContext().getDatabaseFile().isPresent()) && !FileBasedLock.waitForFileLock(getBibDatabaseContext().getDatabaseFile().get().toPath())) {
// The file is locked even after the maximum wait. Do nothing.
LOGGER.error("File updated externally, but change scan failed because the file is locked.");
// Perturb the stored timestamp so successive checks are made:
Globals.getFileUpdateMonitor().perturbTimestamp(getFileMonitorHandle());
return;
}
JabRefExecutorService.INSTANCE.executeInterruptableTaskAndWait(scanner);
// Adding the sidepane component is Swing work, so we must do this in the Swing
// thread:
Runnable t = () -> {
// Check if there is already a notification about external
// changes:
boolean hasAlready = sidePaneManager.hasComponent(FileUpdatePanel.class);
if (hasAlready) {
sidePaneManager.hideComponent(FileUpdatePanel.class);
sidePaneManager.unregisterComponent(FileUpdatePanel.class);
}
FileUpdatePanel pan = new FileUpdatePanel(BasePanel.this, sidePaneManager, getBibDatabaseContext().getDatabaseFile().orElse(null), scanner);
sidePaneManager.register(pan);
sidePaneManager.show(FileUpdatePanel.class);
};
if (scanner.changesFound()) {
SwingUtilities.invokeLater(t);
} else {
setUpdatedExternally(false);
}
}
use of org.jabref.collab.ChangeScanner in project jabref by JabRef.
the class SaveDatabaseAction method checkExternalModification.
/**
* Check whether or not the external database has been modified. If so need to alert the user to accept external updates prior to
* saving the database. This is necessary to avoid overwriting other users work when using a multiuser database file.
*
* @return true if the external database file has been modified and the user must choose to accept the changes and false if no modifications
* were found or there is no requested protection for the database file.
*/
private boolean checkExternalModification() {
// Check for external modifications:
if (panel.isUpdatedExternally() || Globals.getFileUpdateMonitor().hasBeenModified(panel.getFileMonitorHandle())) {
String[] opts = new String[] { Localization.lang("Review changes"), Localization.lang("Save"), Localization.lang("Cancel") };
int answer = JOptionPane.showOptionDialog(panel.frame(), Localization.lang("File has been updated externally. " + "What do you want to do?"), Localization.lang("File updated externally"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, opts, opts[0]);
if (answer == JOptionPane.CANCEL_OPTION) {
canceled = true;
return true;
} else if (answer == JOptionPane.YES_OPTION) {
canceled = true;
JabRefExecutorService.INSTANCE.execute(() -> {
if (!FileBasedLock.waitForFileLock(panel.getBibDatabaseContext().getDatabaseFile().get().toPath())) {
// TODO: GUI handling of the situation when the externally modified file keeps being locked.
LOGGER.error("File locked, this will be trouble.");
}
ChangeScanner scanner = new ChangeScanner(panel.frame(), panel, panel.getBibDatabaseContext().getDatabaseFile().get());
JabRefExecutorService.INSTANCE.executeInterruptableTaskAndWait(scanner);
if (scanner.changesFound()) {
scanner.displayResult(resolved -> {
if (resolved) {
panel.setUpdatedExternally(false);
SwingUtilities.invokeLater(() -> panel.getSidePaneManager().hide(FileUpdatePanel.class));
} else {
canceled = true;
}
});
}
});
return true;
} else {
// User indicated to store anyway.
if (panel.getBibDatabaseContext().getMetaData().isProtected()) {
JOptionPane.showMessageDialog(frame, Localization.lang("Library is protected. Cannot save until external changes have been reviewed."), Localization.lang("Protected library"), JOptionPane.ERROR_MESSAGE);
canceled = true;
} else {
panel.setUpdatedExternally(false);
panel.getSidePaneManager().hide(FileUpdatePanel.class);
}
}
}
// Return false as either no external database file modifications have been found or overwrite is requested any way
return false;
}
Aggregations