Skip to content

Commit

Permalink
further refactoring, problem persists
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Sep 27, 2020
1 parent 3ea16a6 commit 3a9f00d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ public void addTab(BasePanel basePanel, boolean raisePanel) {
basePanel.getUndoManager().registerListener(new UndoRedoEventManager());

BibDatabaseContext context = basePanel.getBibDatabaseContext();
GlobalSaveManager manager = GlobalSaveManager.create(basePanel, prefs, Globals.entryTypesManager);
GlobalSaveManager manager = GlobalSaveManager.create(stateManager, prefs, Globals.entryTypesManager);

if (readyForAutosave(context)) {
AutosaveManager autosaver = AutosaveManager.start(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
public class AutosaveUiManager {

private static final Logger LOGGER = LoggerFactory.getLogger(AutosaveUiManager.class);

private final BasePanel panel;

public AutosaveUiManager(BasePanel panel) {
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.dialogs.AutosaveUiManager;
import org.jabref.gui.util.BackgroundTask;
Expand All @@ -35,7 +36,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.ChangePropagation;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -54,19 +55,22 @@ public class SaveDatabaseAction {
private final BasePanel panel;
private final JabRefFrame frame;
private final DialogService dialogService;
private final JabRefPreferences preferences;
private final PreferencesService preferences;
private final BibEntryTypesManager entryTypesManager;

private final GlobalSaveManager saveManager;

public enum SaveDatabaseMode {
SILENT, NORMAL
}

public SaveDatabaseAction(BasePanel panel, JabRefPreferences preferences, BibEntryTypesManager entryTypesManager) {
public SaveDatabaseAction(BasePanel panel, PreferencesService preferences, BibEntryTypesManager entryTypesManager) {
this.panel = panel;
this.frame = panel.frame();
this.dialogService = frame.getDialogService();
this.preferences = preferences;
this.entryTypesManager = entryTypesManager;
this.saveManager = GlobalSaveManager.create(Globals.stateManager, preferences, entryTypesManager);
}

public boolean save() {
Expand Down Expand Up @@ -157,7 +161,7 @@ private Optional<Path> askForSavePath() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.BIBTEX_DB)
.withDefaultExtension(StandardFileType.BIBTEX_DB)
.withInitialDirectory(preferences.get(JabRefPreferences.WORKING_DIRECTORY))
.withInitialDirectory(preferences.getWorkingDir())
.build();
Optional<Path> selectedPath = dialogService.showFileSaveDialog(fileDialogConfiguration);
selectedPath.ifPresent(path -> preferences.setWorkingDir(path.getParent()));
Expand Down Expand Up @@ -216,15 +220,15 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {
}

private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType) throws SaveException {
GlobalSaveManager manager = GlobalSaveManager.create(panel, preferences, entryTypesManager);


SavePreferences preferences = this.preferences.getSavePreferences()
.withEncoding(encoding)
.withSaveType(saveType);

Consumer<List<FieldChange>> consumer = fieldChanges -> panel.registerUndoableChanges(fieldChanges);

var future = manager.save(file, selectedOnly, encoding, saveType, panel.getBibDatabaseContext(), consumer);
var future = saveManager.save(file, selectedOnly, encoding, saveType, consumer);
Set<Character> characters;
try {
characters = future.get();
Expand Down Expand Up @@ -268,10 +272,10 @@ private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset

private boolean readyForAutosave(BibDatabaseContext context) {
return ((context.getLocation() == DatabaseLocation.SHARED) ||
((context.getLocation() == DatabaseLocation.LOCAL)
&& preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)))
(((context.getLocation() == DatabaseLocation.LOCAL)
&& preferences.getShouldAutosave())
&&
context.getDatabasePath().isPresent();
context.getDatabasePath().isPresent()));
}

private boolean readyForBackup(BibDatabaseContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
Expand All @@ -43,12 +43,12 @@ public class BackupManager {
private static Set<BackupManager> runningInstances = new HashSet<>();

private final BibDatabaseContext bibDatabaseContext;
private final JabRefPreferences preferences;
private final PreferencesService preferences;
private final DelayTaskThrottler<Void> throttler;
private final CoarseChangeFilter changeFilter;
private final BibEntryTypesManager entryTypesManager;

private BackupManager(BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, JabRefPreferences preferences) {
private BackupManager(BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, PreferencesService preferences) {
this.bibDatabaseContext = bibDatabaseContext;
this.entryTypesManager = entryTypesManager;
this.preferences = preferences;
Expand All @@ -68,7 +68,7 @@ static Path getBackupPath(Path originalPath) {
*
* @param bibDatabaseContext Associated {@link BibDatabaseContext}
*/
public static BackupManager start(BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, JabRefPreferences preferences) {
public static BackupManager start(BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager, PreferencesService preferences) {
BackupManager backupManager = new BackupManager(bibDatabaseContext, entryTypesManager, preferences);
backupManager.startBackupTask();
runningInstances.add(backupManager);
Expand Down
41 changes: 21 additions & 20 deletions src/main/java/org/jabref/logic/exporter/GlobalSaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
import java.util.concurrent.Future;
import java.util.function.Consumer;

import org.jabref.gui.BasePanel;
import org.jabref.gui.Globals;
import org.jabref.gui.StateManager;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.DelayTaskThrottler;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

public class GlobalSaveManager {

private static Set<GlobalSaveManager> runningInstances = new HashSet<>();

private final DelayTaskThrottler<Set<Character>> throttler = new DelayTaskThrottler<>(1500);
private final BibDatabaseContext bibDatabaseContext;
private final List<BibEntry> selectedEntries;
private final JabRefPreferences preferences;
private final BibEntryTypesManager entryTypesManager;

private GlobalSaveManager(BasePanel panel, JabRefPreferences preferences, BibEntryTypesManager entryTypesManager) {
this.bibDatabaseContext = panel.getBibDatabaseContext();
this.selectedEntries = panel.getSelectedEntries();
this.preferences = preferences;
this.entryTypesManager = entryTypesManager;
private final StateManager stateManager;

private final PreferencesService preferencesService;

private GlobalSaveManager(StateManager stateManager, PreferencesService preferencesService, BibEntryTypesManager entryTypesManager) {
this.bibDatabaseContext= stateManager.getActiveDatabase().get();
this.stateManager = stateManager;
this.preferencesService = preferencesService;

}

Expand All @@ -43,8 +43,8 @@ public static void shutdown(BibDatabaseContext context) {
runningInstances.removeIf(instance -> instance.bibDatabaseContext == context);
}

public static GlobalSaveManager create(BasePanel panel, JabRefPreferences preferences, BibEntryTypesManager entryTypesManager) {
GlobalSaveManager saveAction = new GlobalSaveManager(panel, preferences, entryTypesManager);
public static GlobalSaveManager create(StateManager stateManager, PreferencesService preferencesService, BibEntryTypesManager entryTypesManager) {
GlobalSaveManager saveAction = new GlobalSaveManager(stateManager, preferencesService, entryTypesManager);
runningInstances.add(saveAction);
return saveAction;
}
Expand All @@ -54,22 +54,23 @@ private void shutdown() {

}

public Future<Set<Character>> save(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType, BibDatabaseContext context, Consumer<List<FieldChange>> consumeFieldChanges) {
return throttler.scheduleTask(() -> saveThrotteld(file, selectedOnly, encoding, saveType, context, consumeFieldChanges));
public Future<Set<Character>> save(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType, Consumer<List<FieldChange>> consumeFieldChanges) {
return throttler.scheduleTask(() -> saveThrottled(file, selectedOnly, encoding, saveType, consumeFieldChanges));
}

private Set<Character> saveThrotteld(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType, BibDatabaseContext context, Consumer<List<FieldChange>> consumeFieldChanges) throws SaveException {
SavePreferences savePrefs = this.preferences.getSavePreferences()
private Set<Character> saveThrottled(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType, Consumer<List<FieldChange>> consumeFieldChanges) throws SaveException {

SavePreferences savePrefs = this.preferencesService.getSavePreferences()
.withEncoding(encoding)
.withSaveType(saveType);

try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, savePrefs.getEncoding(), savePrefs.shouldMakeBackup())) {
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, savePrefs, entryTypesManager);
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, savePrefs, Globals.entryTypesManager);

if (selectedOnly) {
databaseWriter.savePartOfDatabase(context, selectedEntries);
databaseWriter.savePartOfDatabase(this.bibDatabaseContext, this.stateManager.getSelectedEntries());
} else {
databaseWriter.saveDatabase(context);
databaseWriter.saveDatabase(this.bibDatabaseContext);
}

consumeFieldChanges.accept(databaseWriter.getSaveActionsFieldChanges());
Expand Down

0 comments on commit 3a9f00d

Please sign in to comment.