Search in sources :

Example 1 with ChangeScanner

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);
    }
}
Also used : ChangeScanner(org.jabref.collab.ChangeScanner) FileUpdatePanel(org.jabref.collab.FileUpdatePanel)

Example 2 with ChangeScanner

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;
}
Also used : SavePreferences(org.jabref.logic.exporter.SavePreferences) AutosaveManager(org.jabref.logic.autosaveandbackup.AutosaveManager) ChangeScanner(org.jabref.collab.ChangeScanner) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) AbstractWorker(org.jabref.gui.worker.AbstractWorker) DialogService(org.jabref.gui.DialogService) JabRefPreferences(org.jabref.preferences.JabRefPreferences) BasePanel(org.jabref.gui.BasePanel) SwingUtilities(javax.swing.SwingUtilities) FileExtensions(org.jabref.logic.util.FileExtensions) JabRefFrame(org.jabref.gui.JabRefFrame) Charset(java.nio.charset.Charset) DBMSConnectionProperties(org.jabref.shared.DBMSConnectionProperties) Encodings(org.jabref.logic.l10n.Encodings) FileUpdatePanel(org.jabref.collab.FileUpdatePanel) SaveException(org.jabref.logic.exporter.SaveException) Localization(org.jabref.logic.l10n.Localization) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Path(java.nio.file.Path) FormBuilder(com.jgoodies.forms.builder.FormBuilder) FileBasedLock(org.jabref.logic.util.io.FileBasedLock) SharedDatabasePreferences(org.jabref.shared.prefs.SharedDatabasePreferences) SaveSession(org.jabref.logic.exporter.SaveSession) FileDialogConfiguration(org.jabref.gui.util.FileDialogConfiguration) BibEntry(org.jabref.model.entry.BibEntry) IOException(java.io.IOException) FXDialogService(org.jabref.gui.FXDialogService) JOptionPane(javax.swing.JOptionPane) File(java.io.File) Globals(org.jabref.Globals) DefaultTaskExecutor(org.jabref.gui.util.DefaultTaskExecutor) AutosaveUIManager(org.jabref.gui.autosaveandbackup.AutosaveUIManager) FormLayout(com.jgoodies.forms.layout.FormLayout) DatabaseLocation(org.jabref.model.database.DatabaseLocation) Optional(java.util.Optional) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) Log(org.apache.commons.logging.Log) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) JTextArea(javax.swing.JTextArea) BackupManager(org.jabref.logic.autosaveandbackup.BackupManager) LogFactory(org.apache.commons.logging.LogFactory) ChangePropagation(org.jabref.model.database.event.ChangePropagation) JabRefExecutorService(org.jabref.JabRefExecutorService) ChangeScanner(org.jabref.collab.ChangeScanner) FileUpdatePanel(org.jabref.collab.FileUpdatePanel)

Aggregations

ChangeScanner (org.jabref.collab.ChangeScanner)2 FileUpdatePanel (org.jabref.collab.FileUpdatePanel)2 FormBuilder (com.jgoodies.forms.builder.FormBuilder)1 FormLayout (com.jgoodies.forms.layout.FormLayout)1 File (java.io.File)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 Path (java.nio.file.Path)1 Optional (java.util.Optional)1 JOptionPane (javax.swing.JOptionPane)1 JTextArea (javax.swing.JTextArea)1 SwingUtilities (javax.swing.SwingUtilities)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Globals (org.jabref.Globals)1 JabRefExecutorService (org.jabref.JabRefExecutorService)1 BasePanel (org.jabref.gui.BasePanel)1 DialogService (org.jabref.gui.DialogService)1 FXDialogService (org.jabref.gui.FXDialogService)1