Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup preferences #8036

Merged
merged 5 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public class JabRefFrame extends BorderPane {

private final SplitPane splitPane = new SplitPane();
private final PreferencesService prefs = Globals.prefs;
private final GlobalSearchBar globalSearchBar = new GlobalSearchBar(this, Globals.stateManager, prefs);
private final GlobalSearchBar globalSearchBar;

private final FileHistoryMenu fileHistory;

Expand All @@ -180,6 +180,7 @@ public JabRefFrame(Stage mainStage) {
this.dialogService = new JabRefDialogService(mainStage, this, prefs);
this.stateManager = Globals.stateManager;
this.pushToApplicationsManager = new PushToApplicationsManager(dialogService, stateManager, prefs);
this.globalSearchBar = new GlobalSearchBar(this, stateManager, prefs);
this.undoManager = Globals.undoManager;
this.fileHistory = new FileHistoryMenu(prefs, dialogService, getOpenDatabaseAction());
this.taskExecutor = Globals.TASK_EXECUTOR;
Expand Down Expand Up @@ -288,7 +289,7 @@ private void initKeyBindings() {
}

private void initShowTrackingNotification() {
if (Globals.prefs.getTelemetryPreferences().shouldAskToCollectTelemetry()) {
if (prefs.getTelemetryPreferences().shouldAskToCollectTelemetry()) {
JabRefExecutorService.INSTANCE.submit(new TimerTask() {

@Override
Expand All @@ -300,7 +301,7 @@ public void run() {
}

private void showTrackingNotification() {
TelemetryPreferences telemetryPreferences = Globals.prefs.getTelemetryPreferences();
TelemetryPreferences telemetryPreferences = prefs.getTelemetryPreferences();
boolean shouldCollect = telemetryPreferences.shouldCollectTelemetry();

if (!telemetryPreferences.shouldCollectTelemetry()) {
Expand All @@ -311,7 +312,7 @@ private void showTrackingNotification() {
Localization.lang("Don't share"));
}

Globals.prefs.storeTelemetryPreferences(telemetryPreferences.withCollectTelemetry(shouldCollect)
prefs.storeTelemetryPreferences(telemetryPreferences.withCollectTelemetry(shouldCollect)
.withAskToCollectTelemetry(false));
}

Expand Down Expand Up @@ -478,8 +479,8 @@ private Node createToolbar() {

new HBox(
factory.createIconButton(StandardActions.NEW_LIBRARY, new NewDatabaseAction(this, prefs)),
factory.createIconButton(StandardActions.OPEN_LIBRARY, new OpenDatabaseAction(this, prefs, dialogService)),
factory.createIconButton(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, this, stateManager))),
factory.createIconButton(StandardActions.OPEN_LIBRARY, new OpenDatabaseAction(this, prefs, dialogService, stateManager)),
factory.createIconButton(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, this, prefs, stateManager))),

leftSpacer,

Expand Down Expand Up @@ -607,15 +608,13 @@ public void init() {
* cut/paste/copy operations would some times occur in the wrong tab.
*/
EasyBind.subscribe(tabbedPane.getSelectionModel().selectedItemProperty(), tab -> {
if ((tab == null) || (!(tab instanceof LibraryTab))) {
if (!(tab instanceof LibraryTab libraryTab)) {
stateManager.setSelectedEntries(Collections.emptyList());
mainStage.titleProperty().unbind();
mainStage.setTitle(FRAME_TITLE);
return;
}

LibraryTab libraryTab = (LibraryTab) tab;

// Poor-mans binding to global state
stateManager.setSelectedEntries(libraryTab.getSelectedEntries());

Expand Down Expand Up @@ -678,9 +677,9 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.NEW_LIBRARY, new NewDatabaseAction(this, prefs)),
factory.createMenuItem(StandardActions.OPEN_LIBRARY, getOpenDatabaseAction()),
fileHistory,
factory.createMenuItem(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, this, stateManager)),
factory.createMenuItem(StandardActions.SAVE_LIBRARY_AS, new SaveAction(SaveAction.SaveMethod.SAVE_AS, this, stateManager)),
factory.createMenuItem(StandardActions.SAVE_ALL, new SaveAllAction(this)),
factory.createMenuItem(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, this, prefs, stateManager)),
factory.createMenuItem(StandardActions.SAVE_LIBRARY_AS, new SaveAction(SaveAction.SaveMethod.SAVE_AS, this, prefs, stateManager)),
factory.createMenuItem(StandardActions.SAVE_ALL, new SaveAllAction(this, prefs)),

new SeparatorMenuItem(),

Expand All @@ -691,7 +690,7 @@ private MenuBar createMenu() {
factory.createSubMenu(StandardActions.EXPORT,
factory.createMenuItem(StandardActions.EXPORT_ALL, new ExportCommand(this, false, prefs)),
factory.createMenuItem(StandardActions.EXPORT_SELECTED, new ExportCommand(this, true, prefs)),
factory.createMenuItem(StandardActions.SAVE_SELECTED_AS_PLAIN_BIBTEX, new SaveAction(SaveAction.SaveMethod.SAVE_SELECTED, this, stateManager))),
factory.createMenuItem(StandardActions.SAVE_SELECTED_AS_PLAIN_BIBTEX, new SaveAction(SaveAction.SaveMethod.SAVE_SELECTED, this, prefs, stateManager))),

new SeparatorMenuItem(),

Expand Down Expand Up @@ -811,16 +810,16 @@ private MenuBar createMenu() {

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.WRITE_XMP, new WriteXMPAction(stateManager, dialogService)),
factory.createMenuItem(StandardActions.COPY_LINKED_FILES, new CopyFilesAction(stateManager, this.getDialogService())),
factory.createMenuItem(StandardActions.WRITE_XMP, new WriteXMPAction(stateManager, dialogService, prefs)),
factory.createMenuItem(StandardActions.COPY_LINKED_FILES, new CopyFilesAction(dialogService, prefs, stateManager)),

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsEMailAction(dialogService, this.prefs, stateManager)),
pushToApplicationMenuItem,
new SeparatorMenuItem(),
factory.createMenuItem(StandardActions.START_NEW_STUDY, new StartNewStudyAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs)),
factory.createMenuItem(StandardActions.SEARCH_FOR_EXISTING_STUDY, new ExistingStudySearchAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs)),
factory.createMenuItem(StandardActions.START_NEW_STUDY, new StartNewStudyAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs, stateManager)),
factory.createMenuItem(StandardActions.SEARCH_FOR_EXISTING_STUDY, new ExistingStudySearchAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs, stateManager)),

new SeparatorMenuItem(),

Expand All @@ -845,7 +844,7 @@ private MenuBar createMenu() {

factory.createMenuItem(StandardActions.SHOW_PDF_VIEWER, new ShowDocumentViewerAction(stateManager, prefs)),
factory.createMenuItem(StandardActions.EDIT_ENTRY, new OpenEntryEditorAction(this, stateManager)),
factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OpenConsoleAction(stateManager))
factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OpenConsoleAction(stateManager, prefs))
);

options.getItems().addAll(
Expand Down Expand Up @@ -1004,7 +1003,7 @@ private ContextMenu createTabContextMenu(KeyBindingRepository keyBindingReposito

contextMenu.getItems().addAll(
factory.createMenuItem(StandardActions.OPEN_DATABASE_FOLDER, new OpenDatabaseFolder()),
factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OpenConsoleAction(stateManager)),
factory.createMenuItem(StandardActions.OPEN_CONSOLE, new OpenConsoleAction(stateManager, prefs)),
new SeparatorMenuItem(),
factory.createMenuItem(StandardActions.CLOSE_LIBRARY, new CloseDatabaseAction()),
factory.createMenuItem(StandardActions.CLOSE_OTHER_LIBRARIES, new CloseOthersDatabaseAction()),
Expand Down Expand Up @@ -1053,14 +1052,14 @@ private void trackOpenNewDatabase(LibraryTab libraryTab) {
public LibraryTab addTab(BibDatabaseContext databaseContext, boolean raisePanel) {
Objects.requireNonNull(databaseContext);

LibraryTab libraryTab = new LibraryTab(this, prefs, databaseContext, ExternalFileTypes.getInstance());
LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, databaseContext, ExternalFileTypes.getInstance());
addTab(libraryTab, raisePanel);
return libraryTab;
}

private boolean readyForAutosave(BibDatabaseContext context) {
return ((context.getLocation() == DatabaseLocation.SHARED) ||
((context.getLocation() == DatabaseLocation.LOCAL) && Globals.prefs.shouldAutosave()))
((context.getLocation() == DatabaseLocation.LOCAL) && prefs.shouldAutosave()))
&&
context.getDatabasePath().isPresent();
}
Expand Down Expand Up @@ -1162,7 +1161,7 @@ public void closeCurrentTab() {
}

public OpenDatabaseAction getOpenDatabaseAction() {
return new OpenDatabaseAction(this, prefs, dialogService);
return new OpenDatabaseAction(this, prefs, dialogService, stateManager);
}

public SidePaneManager getSidePaneManager() {
Expand Down Expand Up @@ -1238,7 +1237,7 @@ private class OpenDatabaseFolder extends SimpleCommand {
public void execute() {
stateManager.getActiveDatabase().flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> {
try {
JabRefDesktop.openFolderAndSelectFile(path);
JabRefDesktop.openFolderAndSelectFile(path, prefs);
} catch (IOException e) {
LOGGER.info("Could not open folder", e);
}
Expand Down
37 changes: 20 additions & 17 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class LibraryTab extends Tab {
private final ExternalFileTypes externalFileTypes;
private final DialogService dialogService;
private final PreferencesService preferencesService;
private final StateManager stateManager;
private final BooleanProperty changedProperty = new SimpleBooleanProperty(false);
private final BooleanProperty nonUndoableChangeProperty = new SimpleBooleanProperty(false);
private BibDatabaseContext bibDatabaseContext;
Expand Down Expand Up @@ -112,6 +113,7 @@ public class LibraryTab extends Tab {

public LibraryTab(JabRefFrame frame,
PreferencesService preferencesService,
StateManager stateManager,
BibDatabaseContext bibDatabaseContext,
ExternalFileTypes externalFileTypes) {
this.frame = Objects.requireNonNull(frame);
Expand All @@ -120,12 +122,13 @@ public LibraryTab(JabRefFrame frame,
this.undoManager = frame.getUndoManager();
this.dialogService = frame.getDialogService();
this.preferencesService = Objects.requireNonNull(preferencesService);
this.stateManager = Objects.requireNonNull(stateManager);

bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);

this.sidePaneManager = frame.getSidePaneManager();
this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferencesService, Globals.stateManager);
this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferencesService, stateManager);

citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, preferencesService.getFilePreferences());
Expand All @@ -148,7 +151,7 @@ public LibraryTab(JabRefFrame frame,

Platform.runLater(() -> {
EasyBind.subscribe(changedProperty, this::updateTabTitle);
Globals.stateManager.getOpenDatabases().addListener((ListChangeListener<BibDatabaseContext>) c ->
stateManager.getOpenDatabases().addListener((ListChangeListener<BibDatabaseContext>) c ->
updateTabTitle(changedProperty.getValue()));
});
}
Expand Down Expand Up @@ -201,7 +204,7 @@ public void onDatabaseLoadingSucceed(ParserResult result) {

feedData(context);
// a temporary workaround to update groups pane
Globals.stateManager.activeDatabaseProperty().bind(
stateManager.activeDatabaseProperty().bind(
EasyBind.map(frame.getTabbedPane().getSelectionModel().selectedItemProperty(),
selectedTab -> Optional.ofNullable(selectedTab)
.filter(tab -> tab instanceof LibraryTab)
Expand All @@ -224,7 +227,7 @@ public void feedData(BibDatabaseContext bibDatabaseContext) {
bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);

this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferencesService, Globals.stateManager);
this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferencesService, stateManager);
citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, preferencesService.getFilePreferences());

Expand All @@ -245,7 +248,7 @@ public void feedData(BibDatabaseContext bibDatabaseContext) {

Platform.runLater(() -> {
EasyBind.subscribe(changedProperty, this::updateTabTitle);
Globals.stateManager.getOpenDatabases().addListener((ListChangeListener<BibDatabaseContext>) c ->
stateManager.getOpenDatabases().addListener((ListChangeListener<BibDatabaseContext>) c ->
updateTabTitle(changedProperty.getValue()));
});

Expand All @@ -254,7 +257,7 @@ public void feedData(BibDatabaseContext bibDatabaseContext) {
autoSaver.registerListener(new AutosaveUiManager(this));
}

BackupManager.start(this.bibDatabaseContext, Globals.entryTypesManager, Globals.prefs);
BackupManager.start(this.bibDatabaseContext, Globals.entryTypesManager, preferencesService);
}

private boolean isDatabaseReadyForAutoSave(BibDatabaseContext context) {
Expand Down Expand Up @@ -348,11 +351,11 @@ public void updateTabTitle(boolean isChanged) {

private List<String> collectAllDatabasePaths() {
List<String> list = new ArrayList<>();
Globals.stateManager.getOpenDatabases().stream()
.map(BibDatabaseContext::getDatabasePath)
.forEachOrdered(pathOptional -> pathOptional.ifPresentOrElse(
path -> list.add(path.toAbsolutePath().toString()),
() -> list.add("")));
stateManager.getOpenDatabases().stream()
.map(BibDatabaseContext::getDatabasePath)
.forEachOrdered(pathOptional -> pathOptional.ifPresentOrElse(
path -> list.add(path.toAbsolutePath().toString()),
() -> list.add("")));
return list;
}

Expand Down Expand Up @@ -480,12 +483,12 @@ private void createMainTable() {
bibDatabaseContext,
preferencesService,
dialogService,
Globals.stateManager,
stateManager,
externalFileTypes,
Globals.getKeyPrefs());

// Add the listener that binds selection to state manager (TODO: should be replaced by proper JavaFX binding as soon as table is implemented in JavaFX)
mainTable.addSelectionListener(listEvent -> Globals.stateManager.setSelectedEntries(mainTable.getSelectedEntries()));
mainTable.addSelectionListener(listEvent -> stateManager.setSelectedEntries(mainTable.getSelectedEntries()));

// Update entry editor and preview according to selected entries
mainTable.addSelectionListener(event -> mainTable.getSelectedEntries()
Expand Down Expand Up @@ -750,7 +753,7 @@ public FileAnnotationCache getAnnotationCache() {

public void resetChangeMonitorAndChangePane() {
changeMonitor.ifPresent(DatabaseChangeMonitor::unregister);
changeMonitor = Optional.of(new DatabaseChangeMonitor(bibDatabaseContext, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, preferencesService));
changeMonitor = Optional.of(new DatabaseChangeMonitor(bibDatabaseContext, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, preferencesService, stateManager));

changePane = new DatabaseChangePane(splitPane, bibDatabaseContext, changeMonitor.get());

Expand Down Expand Up @@ -796,11 +799,11 @@ public void resetChangedProperties() {
}

public static class Factory {
public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, Path file, BackgroundTask<ParserResult> dataLoadingTask) {
public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, StateManager stateManager, Path file, BackgroundTask<ParserResult> dataLoadingTask) {
BibDatabaseContext context = new BibDatabaseContext();
context.setDatabasePath(file);

LibraryTab newTab = new LibraryTab(frame, preferencesService, context, ExternalFileTypes.getInstance());
LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, context, ExternalFileTypes.getInstance());
newTab.setDataLoadingTask(dataLoadingTask);

dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted)
Expand All @@ -823,7 +826,7 @@ public void listen(EntriesAddedEvent addedEntriesEvent) {

// Automatically add new entries to the selected group (or set of groups)
if (preferencesService.getGroupsPreferences().shouldAutoAssignGroup()) {
Globals.stateManager.getSelectedGroup(bibDatabaseContext).forEach(
stateManager.getSelectedGroup(bibDatabaseContext).forEach(
selectedGroup -> selectedGroup.addEntriesToGroup(addedEntriesEvent.getBibEntries()));
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/jabref/gui/OpenConsoleAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -14,9 +15,11 @@ public class OpenConsoleAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(OpenConsoleAction.class);
private final StateManager stateManager;
private final PreferencesService preferencesService;

public OpenConsoleAction(StateManager stateManager) {
public OpenConsoleAction(StateManager stateManager, PreferencesService preferencesService) {
this.stateManager = stateManager;
this.preferencesService = preferencesService;

this.executable.bind(ActionHelper.needsDatabase(stateManager));
}
Expand All @@ -25,7 +28,7 @@ public OpenConsoleAction(StateManager stateManager) {
public void execute() {
stateManager.getActiveDatabase().flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> {
try {
JabRefDesktop.openConsole(path.toFile());
JabRefDesktop.openConsole(path.toFile(), preferencesService);
} catch (IOException e) {
LOGGER.info("Could not open console", e);
}
Expand Down
Loading