Skip to content

Commit

Permalink
Merge pull request #6518 from JabRef/refactor_tableprefs
Browse files Browse the repository at this point in the history
Refactor main table preferences
  • Loading branch information
Siedlerchr committed May 28, 2020
2 parents d43bcfa + c1867e5 commit 41678ce
Show file tree
Hide file tree
Showing 17 changed files with 902 additions and 751 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We improved the error message for invalid jstyles. [#6303](https://github.com/JabRef/jabref/issues/6303)
- We changed the section name of 'Advanced' to 'Network' in the preferences and removed some obsolete options.[#6489](https://github.com/JabRef/jabref/pull/6489)
- We improved the context menu of the column "Linked identifiers" of the main table, by truncating their texts, if they are too long. [#6499](https://github.com/JabRef/jabref/issues/6499)
- We merged the main table tabs in the preferences dialog. [#6518](https://github.com/JabRef/jabref/pull/6518)

### Fixed

Expand Down Expand Up @@ -76,6 +77,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where brackets in regular expressions were not working. [6469](https://github.com/JabRef/jabref/pull/6469)
- We fixed an issue where LaTeX citations for specific commands (\autocites) of biblatex-mla were not recognized. [#6476](https://github.com/JabRef/jabref/issues/6476)
- We fixed an issue where drag and drop was not working on empty database. [#6487](https://github.com/JabRef/jabref/issues/6487)
- We fixed an issue where the name fields were not updated after the preferences changed. [#6515](https://github.com/JabRef/jabref/issues/6515)
- We fixed an issue where "null" appeared in generated BibTeX keys. [#6459](https://github.com/JabRef/jabref/issues/6459)
- We fixed an issue where the authors' names were incorrectly displayed in the authors' column when they were bracketed. [#6465](https://github.com/JabRef/jabref/issues/6465) [#6459](https://github.com/JabRef/jabref/issues/6459)

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas
bibDatabaseContext.getMetaData().registerListener(this);

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

citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, Globals.prefs.getFilePreferences());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jabref.gui.maintable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
Expand All @@ -32,14 +34,14 @@
public class BibEntryTableViewModel {
private final BibEntry entry;
private final BibDatabase database;
private final MainTableNameFormatter nameFormatter;
private final ObservableValue<MainTableNameFormatter> nameFormatter;
private final Map<OrFields, ObservableValue<String>> fieldValues = new HashMap<>();
private final Map<SpecialField, OptionalBinding<SpecialFieldValueViewModel>> specialFieldValues = new HashMap<>();
private final EasyBinding<List<LinkedFile>> linkedFiles;
private final EasyBinding<Map<Field, String>> linkedIdentifiers;
private final ObservableValue<List<AbstractGroup>> matchedGroups;

public BibEntryTableViewModel(BibEntry entry, BibDatabaseContext database, MainTableNameFormatter nameFormatter) {
public BibEntryTableViewModel(BibEntry entry, BibDatabaseContext database, ObservableValue<MainTableNameFormatter> nameFormatter) {
this.entry = entry;
this.database = database.getDatabase();
this.nameFormatter = nameFormatter;
Expand Down Expand Up @@ -117,13 +119,16 @@ public ObservableValue<String> getFields(OrFields fields) {
return value;
}

ArrayList<Observable> observables = new ArrayList<>(List.of(entry.getObservables()));
observables.add(nameFormatter);

value = Bindings.createStringBinding(() -> {
for (Field field : fields) {
if (field.getProperties().contains(FieldProperty.PERSON_NAMES)) {
Optional<String> name = entry.getResolvedFieldOrAlias(field, database);

if (name.isPresent()) {
return nameFormatter.formatNameLatexFree(name.get());
return nameFormatter.getValue().formatNameLatexFree(name.get());
}
} else {
Optional<String> content = entry.getResolvedFieldOrAliasLatexFree(field, database);
Expand All @@ -134,7 +139,7 @@ public ObservableValue<String> getFields(OrFields fields) {
}
}
return "";
}, entry.getObservables());
}, observables.toArray(Observable[]::new));
fieldValues.put(fields, value);
return value;
}
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/org/jabref/gui/maintable/MainTableDataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;

import org.jabref.Globals;
import org.jabref.gui.StateManager;
import org.jabref.gui.groups.GroupViewMode;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.logic.search.SearchQuery;
Expand All @@ -19,31 +21,35 @@
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.search.matchers.MatcherSet;
import org.jabref.model.search.matchers.MatcherSets;
import org.jabref.preferences.PreferencesService;

import com.tobiasdiez.easybind.EasyBind;

public class MainTableDataModel {
private final FilteredList<BibEntryTableViewModel> entriesFiltered;
private final SortedList<BibEntryTableViewModel> entriesSorted;
private final GroupViewMode groupViewMode;
private final ObjectProperty<MainTableNameFormatter> nameFormatter;
private final PreferencesService preferencesService;

public MainTableDataModel(BibDatabaseContext context) {
ObservableList<BibEntry> allEntries = BindingsHelper.forUI(context.getDatabase().getEntries());
public MainTableDataModel(BibDatabaseContext context, PreferencesService preferencesService, StateManager stateManager) {
this.nameFormatter = new SimpleObjectProperty<>(new MainTableNameFormatter(preferencesService));
this.preferencesService = preferencesService;

MainTableNameFormatter nameFormatter = new MainTableNameFormatter(Globals.prefs);
ObservableList<BibEntry> allEntries = BindingsHelper.forUI(context.getDatabase().getEntries());
ObservableList<BibEntryTableViewModel> entriesViewModel = EasyBind.mapBacked(allEntries, entry -> new BibEntryTableViewModel(entry, context, nameFormatter));

entriesFiltered = new FilteredList<>(entriesViewModel);
entriesFiltered.predicateProperty().bind(
EasyBind.combine(Globals.stateManager.activeGroupProperty(), Globals.stateManager.activeSearchQueryProperty(), (groups, query) -> entry -> isMatched(groups, query, entry))
EasyBind.combine(stateManager.activeGroupProperty(), stateManager.activeSearchQueryProperty(), (groups, query) -> entry -> isMatched(groups, query, entry))
);

IntegerProperty resultSize = new SimpleIntegerProperty();
resultSize.bind(Bindings.size(entriesFiltered));
Globals.stateManager.setActiveSearchResultSize(context, resultSize);
stateManager.setActiveSearchResultSize(context, resultSize);
// We need to wrap the list since otherwise sorting in the table does not work
entriesSorted = new SortedList<>(entriesFiltered);
groupViewMode = Globals.prefs.getGroupViewMode();
groupViewMode = preferencesService.getGroupViewMode();
}

private boolean isMatched(ObservableList<GroupTreeNode> groups, Optional<SearchQuery> query, BibEntryTableViewModel entry) {
Expand Down Expand Up @@ -78,4 +84,8 @@ private Optional<MatcherSet> createGroupMatcher(List<GroupTreeNode> selectedGrou
public SortedList<BibEntryTableViewModel> getEntriesFilteredAndSorted() {
return entriesSorted;
}

public void refresh() {
this.nameFormatter.setValue(new MainTableNameFormatter(preferencesService));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jabref.gui.maintable;

public class MainTableNameFormatPreferences {

public enum DisplayStyle {
NATBIB, AS_IS, FIRSTNAME_LASTNAME, LASTNAME_FIRSTNAME
}

public enum AbbreviationStyle {
NONE, LASTNAME_ONLY, FULL
}

private final DisplayStyle displayStyle;
private final AbbreviationStyle abbreviationStyle;

public MainTableNameFormatPreferences(DisplayStyle displayStyle,
AbbreviationStyle abbreviationStyle) {

this.displayStyle = displayStyle;
this.abbreviationStyle = abbreviationStyle;
}

public DisplayStyle getDisplayStyle() {
return displayStyle;
}

public AbbreviationStyle getAbbreviationStyle() {
return abbreviationStyle;
}
}
51 changes: 29 additions & 22 deletions src/main/java/org/jabref/gui/maintable/MainTableNameFormatter.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package org.jabref.gui.maintable;

import org.jabref.model.entry.AuthorList;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

public class MainTableNameFormatter {

private final boolean namesNatbib;
private final boolean namesLastOnly;
private final boolean namesAsIs;
private final boolean namesFf;
private final boolean abbrAuthorNames;

MainTableNameFormatter(JabRefPreferences preferences) {
namesNatbib = preferences.getBoolean(JabRefPreferences.NAMES_NATBIB);
namesLastOnly = preferences.getBoolean(JabRefPreferences.NAMES_LAST_ONLY);
namesAsIs = preferences.getBoolean(JabRefPreferences.NAMES_AS_IS);
namesFf = preferences.getBoolean(JabRefPreferences.NAMES_FIRST_LAST);
abbrAuthorNames = preferences.getBoolean(JabRefPreferences.ABBR_AUTHOR_NAMES);
private final PreferencesService preferencesService;

MainTableNameFormatter(PreferencesService preferences) {
this.preferencesService = preferences;
}

/**
Expand All @@ -30,18 +22,33 @@ public String formatNameLatexFree(final String nameToFormat) {
if (nameToFormat == null) {
return null;
}

MainTableNameFormatPreferences nameFormatPreferences = preferencesService.getMainTableNameFormatPreferences();
MainTableNameFormatPreferences.DisplayStyle displayStyle = nameFormatPreferences.getDisplayStyle();
MainTableNameFormatPreferences.AbbreviationStyle abbreviationStyle = nameFormatPreferences.getAbbreviationStyle();

AuthorList authors = AuthorList.parse(nameToFormat);

if (namesAsIs) {
return nameToFormat;
} else if (namesNatbib) {
return authors.getAsNatbibLatexFree();
} else if (namesLastOnly) {
if (((displayStyle == MainTableNameFormatPreferences.DisplayStyle.FIRSTNAME_LASTNAME)
|| (displayStyle == MainTableNameFormatPreferences.DisplayStyle.LASTNAME_FIRSTNAME))
&& abbreviationStyle == MainTableNameFormatPreferences.AbbreviationStyle.LASTNAME_ONLY) {
return authors.getAsLastNamesLatexFree(false);
} else if (namesFf) {
return authors.getAsFirstLastNamesLatexFree(abbrAuthorNames, false);
} else {
return authors.getAsLastFirstNamesLatexFree(abbrAuthorNames, false);
}

switch (nameFormatPreferences.getDisplayStyle()) {
case AS_IS:
return nameToFormat;
case NATBIB:
return authors.getAsNatbibLatexFree();
case FIRSTNAME_LASTNAME:
return authors.getAsFirstLastNamesLatexFree(
abbreviationStyle == MainTableNameFormatPreferences.AbbreviationStyle.FULL,
false);
default:
case LASTNAME_FIRSTNAME:
return authors.getAsLastFirstNamesLatexFree(
abbreviationStyle == MainTableNameFormatPreferences.AbbreviationStyle.FULL,
false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

import javafx.beans.InvalidationListener;

import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

/**
* Keep track of changes made to the columns (reordering, resorting, resizing).
*/
public class PersistenceVisualStateTable {

private final MainTable mainTable;
private final JabRefPreferences preferences;
private final PreferencesService preferences;

public PersistenceVisualStateTable(final MainTable mainTable, JabRefPreferences preferences) {
public PersistenceVisualStateTable(final MainTable mainTable, PreferencesService preferences) {
this.mainTable = mainTable;
this.preferences = preferences;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public PreferencesDialogViewModel(DialogService dialogService, JabRefFrame frame
new GeneralTabView(preferences),
new FileTabView(preferences),
new TableTabView(preferences),
new TableColumnsTabView(preferences),
new PreviewTabView(preferences),
new ExternalTabView(preferences, frame.getPushToApplicationsManager()),
new GroupsTabView(preferences),
Expand Down Expand Up @@ -135,6 +134,11 @@ public void resetPreferences() {
* Reloads the JabRefPreferences into the UI
*/
private void updateAfterPreferenceChanges() {
// Reload internal preferences cache
preferences.updateEntryEditorTabList();
preferences.updateGlobalBibtexKeyPattern();
preferences.updateMainTableColumns();

setValues();

List<TemplateExporter> customExporters = preferences.getCustomExportFormats(Globals.journalAbbreviationRepository);
Expand All @@ -147,8 +151,7 @@ private void updateAfterPreferenceChanges() {
PushToApplicationsManager manager = frame.getPushToApplicationsManager();
manager.updateApplicationAction(manager.getApplicationByName(externalApplicationsPreferences.getPushToApplicationName()));

preferences.updateEntryEditorTabList();
preferences.updateGlobalBibtexKeyPattern();
frame.getBasePanelList().forEach(panel -> panel.getMainTable().getTableModel().refresh());
}

/**
Expand Down Expand Up @@ -191,6 +194,8 @@ public void storeAllSettings() {
frame.setupAllTables();
frame.getGlobalSearchBar().updateHintVisibility();
dialogService.notify(Localization.lang("Preferences recorded."));

updateAfterPreferenceChanges();
}

/**
Expand Down
Loading

0 comments on commit 41678ce

Please sign in to comment.