Skip to content

Commit

Permalink
More explicit access to preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Dec 25, 2019
1 parent 053392f commit 5d55379
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 72 deletions.
46 changes: 33 additions & 13 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.swing.undo.CannotRedoException;
Expand Down Expand Up @@ -49,6 +51,7 @@
import org.jabref.gui.journals.AbbreviateAction;
import org.jabref.gui.journals.AbbreviationType;
import org.jabref.gui.journals.UnabbreviateAction;
import org.jabref.gui.maintable.ColumnPreferences;
import org.jabref.gui.maintable.MainTable;
import org.jabref.gui.maintable.MainTableDataModel;
import org.jabref.gui.mergeentries.MergeEntriesAction;
Expand All @@ -66,12 +69,15 @@
import org.jabref.gui.worker.SendAsEMailAction;
import org.jabref.logic.citationstyle.CitationStyleCache;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutHelper;
import org.jabref.logic.pdf.FileAnnotationCache;
import org.jabref.logic.preferences.TimestampPreferences;
import org.jabref.logic.search.SearchQuery;
import org.jabref.logic.util.UpdateField;
import org.jabref.logic.util.UpdateFieldPreferences;
import org.jabref.logic.util.io.FileFinder;
import org.jabref.logic.util.io.FileFinders;
import org.jabref.logic.util.io.FileUtil;
Expand All @@ -96,6 +102,7 @@
import org.jabref.model.entry.field.SpecialFieldValue;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.metadata.FilePreferences;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;
Expand Down Expand Up @@ -127,7 +134,7 @@ public class BasePanel extends StackPane {
private final EntryEditor entryEditor;
private final DialogService dialogService;
private MainTable mainTable;
private BasePanelPreferences preferences;
private BasePanelPreferences basePanelPreferences;
// To contain instantiated entry editors. This is to save time
// As most enums, this must not be null
private BasePanelMode mode = BasePanelMode.SHOWING_NOTHING;
Expand All @@ -147,8 +154,8 @@ public class BasePanel extends StackPane {
private Optional<DatabaseChangeMonitor> changeMonitor = Optional.empty();
private JabRefExecutorService executorService;

public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabaseContext bibDatabaseContext, ExternalFileTypes externalFileTypes, GroupViewMode groupViewMode, FilePreferences filePreferences) {
this.preferences = Objects.requireNonNull(preferences);
public BasePanel(JabRefFrame frame, BasePanelPreferences basePanelPreferences, BibDatabaseContext bibDatabaseContext, ExternalFileTypes externalFileTypes, GroupViewMode groupViewMode, FilePreferences filePreferences, ImportFormatPreferences importFormatPreferences, UpdateFieldPreferences updateFieldPreferences, FileUpdateMonitor fileUpdateMonitor, StateManager stateManager, Consumer<ColumnPreferences> updateColumnPreferences, Supplier<TimestampPreferences> timestampPreferencesSupplier) {
this.basePanelPreferences = Objects.requireNonNull(basePanelPreferences);
this.frame = Objects.requireNonNull(frame);
this.executorService = JabRefExecutorService.INSTANCE;
this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext);
Expand All @@ -165,7 +172,7 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas
citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, filePreferences);

setupMainPanel();
setupMainPanel(filePreferences, importFormatPreferences, updateFieldPreferences, fileUpdateMonitor, stateManager, updateColumnPreferences, basePanelPreferences);

setupActions();

Expand All @@ -177,7 +184,7 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas
// ensure that all entry changes mark the panel as changed
this.bibDatabaseContext.getDatabase().registerListener(this);

this.getDatabase().registerListener(new UpdateTimestampListener(Globals.prefs));
this.getDatabase().registerListener(new UpdateTimestampListener(timestampPreferencesSupplier));

this.entryEditor = new EntryEditor(this, externalFileTypes);
// Open entry editor for first entry on start up.
Expand Down Expand Up @@ -647,10 +654,23 @@ public void updateTableFont() {
mainTable.updateFont();
}

private void createMainTable() {
private void createMainTable(FilePreferences filePreferences, ImportFormatPreferences importFormatPreferences, UpdateFieldPreferences updateFieldPreferences, FileUpdateMonitor fileUpdateMonitor, StateManager stateManager, Consumer<ColumnPreferences> updateColumnPreferences) {
bibDatabaseContext.getDatabase().registerListener(SpecialFieldDatabaseChangeListener.INSTANCE);

mainTable = new MainTable(tableModel, frame, this, bibDatabaseContext, preferences.getTablePreferences(), externalFileTypes, preferences.getKeyBindings());
mainTable = new MainTable(
tableModel,
frame,
this,
bibDatabaseContext,
basePanelPreferences.getTablePreferences(),
externalFileTypes,
basePanelPreferences.getKeyBindings(),
filePreferences,
importFormatPreferences,
updateFieldPreferences,
fileUpdateMonitor,
stateManager,
updateColumnPreferences);

mainTable.updateFont();

Expand Down Expand Up @@ -728,14 +748,14 @@ public void actionPerformed(ActionEvent e) {
*/
}

public void setupMainPanel() {
preferences = BasePanelPreferences.from(Globals.prefs);
public void setupMainPanel(FilePreferences filePreferences, ImportFormatPreferences importFormatPreferences, UpdateFieldPreferences updateFieldPreferences, FileUpdateMonitor fileUpdateMonitor, StateManager stateManager, Consumer<ColumnPreferences> updateColumnPreferences, BasePanelPreferences basePanelPreferences) {
this.basePanelPreferences = basePanelPreferences;

splitPane = new SplitPane();
splitPane.setOrientation(Orientation.VERTICAL);
adjustSplitter(); // restore last splitting state (before mainTable is created as creation affects the stored size of the entryEditors)

createMainTable();
createMainTable(filePreferences, importFormatPreferences, updateFieldPreferences, fileUpdateMonitor, stateManager, updateColumnPreferences);

splitPane.getItems().add(mainTable);

Expand Down Expand Up @@ -771,7 +791,7 @@ public void setupMainPanel() {
* Set up auto completion for this database
*/
private void setupAutoCompletion() {
AutoCompletePreferences autoCompletePreferences = preferences.getAutoCompletePreferences();
AutoCompletePreferences autoCompletePreferences = basePanelPreferences.getAutoCompletePreferences();
if (autoCompletePreferences.shouldAutoComplete()) {
suggestionProviders = new SuggestionProviders(autoCompletePreferences, Globals.journalAbbreviationLoader);
suggestionProviders.indexDatabase(getDatabase());
Expand All @@ -797,7 +817,7 @@ private void instantiateSearchAutoCompleter() {

private void adjustSplitter() {
if (mode == BasePanelMode.SHOWING_EDITOR) {
splitPane.setDividerPositions(preferences.getEntryEditorDividerPosition());
splitPane.setDividerPositions(basePanelPreferences.getEntryEditorDividerPosition());
}
}

Expand Down Expand Up @@ -968,7 +988,7 @@ private void saveDividerLocation(Number position) {
}

if (mode == BasePanelMode.SHOWING_EDITOR) {
preferences.setEntryEditorDividerPosition(position.doubleValue());
basePanelPreferences.setEntryEditorDividerPosition(position.doubleValue());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public void setupAllTables() {

// Update tables:
if (bf.getDatabase() != null) {
DefaultTaskExecutor.runInJavaFXThread(bf::setupMainPanel);
DefaultTaskExecutor.runInJavaFXThread(() -> bf.setupMainPanel(Globals.prefs.getFilePreferences(), Globals.prefs.getImportFormatPreferences(), Globals.prefs.getUpdateFieldPreferences(), Globals.getFileUpdateMonitor(), Globals.stateManager, columnPreferences -> Globals.prefs.storeColumnPreferences(columnPreferences), BasePanelPreferences.from(Globals.prefs)));
}
}
}
Expand Down Expand Up @@ -1016,7 +1016,7 @@ private void trackOpenNewDatabase(BasePanel basePanel) {
public BasePanel addTab(BibDatabaseContext databaseContext, boolean raisePanel) {
Objects.requireNonNull(databaseContext);

BasePanel bp = new BasePanel(this, BasePanelPreferences.from(Globals.prefs), databaseContext, ExternalFileTypes.getInstance(), Globals.prefs.getGroupViewMode(), Globals.prefs.getFilePreferences());
BasePanel bp = new BasePanel(this, BasePanelPreferences.from(Globals.prefs), databaseContext, ExternalFileTypes.getInstance(), Globals.prefs.getGroupViewMode(), Globals.prefs.getFilePreferences(), Globals.prefs.getImportFormatPreferences(), Globals.prefs.getUpdateFieldPreferences(), Globals.getFileUpdateMonitor(), Globals.stateManager, columnPreferences -> Globals.prefs.storeColumnPreferences(columnPreferences), () -> Globals.prefs.getTimestampPreferences());
addTab(bp, raisePanel);
return bp;
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/jabref/gui/UpdateTimestampListener.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package org.jabref.gui;

import java.util.function.Supplier;

import org.jabref.logic.preferences.TimestampPreferences;
import org.jabref.model.entry.event.EntryChangedEvent;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;

/**
* Updates the timestamp of changed entries if the feature is enabled
*/
class UpdateTimestampListener {
private final JabRefPreferences jabRefPreferences;
private final Supplier<TimestampPreferences> timestampPreferencesSupplier;

/**
* The jabRefPreferences are required, because they are queried at each call. The user can change the preferences
* and this listener behaves differently. This implementation seems to be less complex then re-registering this
* listener or reconfiguring this listener if the preferences changed.
*
* @param jabRefPreferences the global JabRef preferences.
* The timestampPreferencesSupplier is required, because timestampPreferences are queried at each call. The user can
* change the preferences and this listener behaves differently. This implementation seems to be less complex then
* re-registering this listener or reconfiguring this listener if the preferences changed.
*/
UpdateTimestampListener(JabRefPreferences jabRefPreferences) {
this.jabRefPreferences = jabRefPreferences;
UpdateTimestampListener(Supplier<TimestampPreferences> timestampPreferencesSupplier) {
this.timestampPreferencesSupplier = timestampPreferencesSupplier;
}

@Subscribe
public void listen(EntryChangedEvent event) {
if (jabRefPreferences.getTimestampPreferences().includeTimestamps()) {
event.getBibEntry().setField(jabRefPreferences.getTimestampPreferences().getTimestampField(),
jabRefPreferences.getTimestampPreferences().now());
TimestampPreferences timestampPreferences = timestampPreferencesSupplier.get();
if (timestampPreferences.includeTimestamps()) {
event.getBibEntry().setField(timestampPreferences.getTimestampField(), timestampPreferences.now());
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/auximport/FromAuxDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public FromAuxDialog(JabRefFrame frame) {
setResultConverter(button -> {
if (button == generateButtonType) {
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
BasePanel bp = new BasePanel(frame, BasePanelPreferences.from(Globals.prefs), new BibDatabaseContext(auxParserResult.getGeneratedBibDatabase(), defaults), ExternalFileTypes.getInstance(), Globals.prefs.getGroupViewMode(), Globals.prefs.getFilePreferences());
BasePanel bp = new BasePanel(frame, BasePanelPreferences.from(Globals.prefs), new BibDatabaseContext(auxParserResult.getGeneratedBibDatabase(), defaults), ExternalFileTypes.getInstance(), Globals.prefs.getGroupViewMode(), Globals.prefs.getFilePreferences(), Globals.prefs.getImportFormatPreferences(), Globals.prefs.getUpdateFieldPreferences(), Globals.getFileUpdateMonitor(), Globals.stateManager, columnPreferences -> Globals.prefs.storeColumnPreferences(columnPreferences), () -> Globals.prefs.getTimestampPreferences());
frame.addTab(bp, true);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;

import org.jabref.Globals;
import org.jabref.gui.AbstractViewModel;
import org.jabref.gui.BasePanel;
import org.jabref.gui.BasePanelPreferences;
import org.jabref.gui.DialogService;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.field.Field;
Expand Down Expand Up @@ -177,7 +179,7 @@ void saveChanges() {
List<Field> fieldNamesToRemove = filterFieldsToRemove();
fieldNamesToRemove.forEach(metaData::clearContentSelectors);

basePanel.setupMainPanel();
basePanel.setupMainPanel(Globals.prefs.getFilePreferences(), Globals.prefs.getImportFormatPreferences(), Globals.prefs.getUpdateFieldPreferences(), Globals.getFileUpdateMonitor(), Globals.stateManager, columnPreferences -> Globals.prefs.storeColumnPreferences(columnPreferences), BasePanelPreferences.from(Globals.prefs));
basePanel.markNonUndoableBaseChanged();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private BasePanel addNewDatabase(ParserResult result, final Path file, boolean r
ParserResultWarningDialog.showParserResultWarningDialog(result, frame);
}

BasePanel basePanel = new BasePanel(frame, BasePanelPreferences.from(Globals.prefs), result.getDatabaseContext(), ExternalFileTypes.getInstance(), Globals.prefs.getGroupViewMode(), Globals.prefs.getFilePreferences());
BasePanel basePanel = new BasePanel(frame, BasePanelPreferences.from(Globals.prefs), result.getDatabaseContext(), ExternalFileTypes.getInstance(), Globals.prefs.getGroupViewMode(), Globals.prefs.getFilePreferences(), Globals.prefs.getImportFormatPreferences(), Globals.prefs.getUpdateFieldPreferences(), Globals.getFileUpdateMonitor(), Globals.stateManager, columnPreferences -> Globals.prefs.storeColumnPreferences(columnPreferences), () -> Globals.prefs.getTimestampPreferences());
frame.addTab(basePanel, raisePanel);
return basePanel;

Expand Down
Loading

0 comments on commit 5d55379

Please sign in to comment.