Skip to content

Commit

Permalink
Observable preferences I (Internal [formerly Version], Groups, Xmp, A…
Browse files Browse the repository at this point in the history
…utoComplete) (#8336)

* Refactored VersionPreferences to InternalPreferences and converted GroupsPreferences to new prefs model

* Converted XmpPreferences to new observable prefs pattern

* Converted AutoCompletePreferences to new observable prefs pattern

* Fixed tests

* Fixed tests

* Fixed tests
  • Loading branch information
calixtus authored Dec 26, 2021
1 parent cd0137f commit ba5beb2
Show file tree
Hide file tree
Showing 31 changed files with 482 additions and 341 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static void startTelemetryClient() {
telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.getContext().getProperties().put("JabRef version", Globals.BUILD_INFO.version.toString());
telemetryClient.getContext().getProperties().put("Java version", StandardSystemProperty.JAVA_VERSION.value());
telemetryClient.getContext().getUser().setId(Globals.prefs.getOrCreateUserId());
telemetryClient.getContext().getUser().setId(Globals.prefs.getTelemetryPreferences().getUserId());
telemetryClient.getContext().getSession().setId(UUID.randomUUID().toString());
telemetryClient.getContext().getDevice().setOperatingSystem(StandardSystemProperty.OS_NAME.value());
telemetryClient.getContext().getDevice().setOperatingSystemVersion(StandardSystemProperty.OS_VERSION.value());
Expand Down
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 @@ -888,7 +888,7 @@ private MenuBar createMenu() {
new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.DONATE, new OpenBrowserAction("https://donations.jabref.org")),
factory.createMenuItem(StandardActions.SEARCH_FOR_UPDATES, new SearchForUpdateAction(Globals.BUILD_INFO, prefs.getVersionPreferences(), dialogService, Globals.TASK_EXECUTOR)),
factory.createMenuItem(StandardActions.SEARCH_FOR_UPDATES, new SearchForUpdateAction(Globals.BUILD_INFO, prefs.getInternalPreferences(), dialogService, Globals.TASK_EXECUTOR)),
factory.createSubMenu(StandardActions.WEB_MENU,
factory.createMenuItem(StandardActions.OPEN_WEBPAGE, new OpenBrowserAction("https://jabref.org/")),
factory.createMenuItem(StandardActions.OPEN_BLOG, new OpenBrowserAction("https://blog.jabref.org/")),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public JabRefGUI(Stage mainStage, List<ParserResult> databases, boolean isBlank,
new VersionWorker(Globals.BUILD_INFO.version,
mainFrame.getDialogService(),
Globals.TASK_EXECUTOR,
preferencesService.getVersionPreferences())
preferencesService.getInternalPreferences())
.checkForNewVersionDelayed();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import java.util.Set;

import org.jabref.logic.journals.JournalAbbreviationPreferences;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;

import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;

Expand All @@ -12,49 +18,70 @@ public enum NameFormat {
LAST_FIRST, FIRST_LAST, BOTH
}

private final boolean shouldAutoComplete;
private final AutoCompleteFirstNameMode firstNameMode;
private final NameFormat nameFormat;
private final Set<Field> completeFields;
private final JournalAbbreviationPreferences journalAbbreviationPreferences;
private final BooleanProperty shouldAutoComplete;
private final ObjectProperty<AutoCompleteFirstNameMode> firstNameMode;
private final ObjectProperty<NameFormat> nameFormat;
private final ObservableSet<Field> completeFields;

public AutoCompletePreferences(boolean shouldAutoComplete, AutoCompleteFirstNameMode firstNameMode, NameFormat nameFormat, Set<Field> completeFields, JournalAbbreviationPreferences journalAbbreviationPreferences) {
this.shouldAutoComplete = shouldAutoComplete;
this.firstNameMode = firstNameMode;
this.nameFormat = nameFormat;
this.completeFields = completeFields;
this.journalAbbreviationPreferences = journalAbbreviationPreferences;
public AutoCompletePreferences(boolean shouldAutoComplete,
AutoCompleteFirstNameMode firstNameMode,
NameFormat nameFormat,
Set<Field> completeFields) {
this.shouldAutoComplete = new SimpleBooleanProperty(shouldAutoComplete);
this.firstNameMode = new SimpleObjectProperty<>(firstNameMode);
this.nameFormat = new SimpleObjectProperty<>(nameFormat);
this.completeFields = FXCollections.observableSet(completeFields);
}

public boolean shouldAutoComplete() {
return shouldAutoComplete.get();
}

public BooleanProperty autoCompleteProperty() {
return shouldAutoComplete;
}

public void setAutoComplete(boolean shouldAutoComplete) {
this.shouldAutoComplete.set(shouldAutoComplete);
}

/**
* Returns how the first names are handled.
*/
public AutoCompleteFirstNameMode getFirstNameMode() {
return firstNameMode.get();
}

public ObjectProperty<AutoCompleteFirstNameMode> firstNameModeProperty() {
return firstNameMode;
}

public void setFirstNameMode(AutoCompleteFirstNameMode firstNameMode) {
this.firstNameMode.set(firstNameMode);
}

public NameFormat getNameFormat() {
return nameFormat.get();
}

public ObjectProperty<NameFormat> nameFormatProperty() {
return nameFormat;
}

public void setNameFormat(NameFormat nameFormat) {
this.nameFormat.set(nameFormat);
}

/**
* Returns the list of fields for which autocomplete is enabled
*
* @return List of field names
*/
public Set<Field> getCompleteFields() {
public ObservableSet<Field> getCompleteFields() {
return completeFields;
}

public String getCompleteNamesAsString() {
return FieldFactory.serializeFieldsList(completeFields);
}

public JournalAbbreviationPreferences getJournalAbbreviationPreferences() {
return journalAbbreviationPreferences;
}
}
8 changes: 3 additions & 5 deletions src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager state

displayName = new LatexToUnicodeFormatter().format(groupNode.getName());
isRoot = groupNode.isRoot();
if (groupNode.getGroup() instanceof AutomaticGroup) {
AutomaticGroup automaticGroup = (AutomaticGroup) groupNode.getGroup();

if (groupNode.getGroup() instanceof AutomaticGroup automaticGroup) {
children = automaticGroup.createSubgroups(this.databaseContext.getDatabase().getEntries())
.stream()
.map(this::toViewModel)
Expand All @@ -87,7 +85,7 @@ public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager state
}
hasChildren = new SimpleBooleanProperty();
hasChildren.bind(Bindings.isNotEmpty(children));
updateMatchedEntries();
EasyBind.subscribe(preferencesService.getGroupsPreferences().displayGroupCountProperty(), shouldDisplay -> updateMatchedEntries());
expandedProperty.set(groupNode.getGroup().isExpanded());
expandedProperty.addListener((observable, oldValue, newValue) -> groupNode.getGroup().setExpanded(newValue));

Expand Down Expand Up @@ -267,7 +265,7 @@ private void updateMatchedEntries() {
// We calculate the new hit value
// We could be more intelligent and try to figure out the new number of hits based on the entry change
// for example, a previously matched entry gets removed -> hits = hits - 1
if (preferencesService.getDisplayGroupCount()) {
if (preferencesService.getGroupsPreferences().shouldDisplayGroupCount()) {
BackgroundTask
.wrap(() -> groupNode.findMatches(databaseContext.getDatabase()))
.onSuccess(entries -> {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/org/jabref/gui/groups/GroupTreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public class GroupTreeView extends BorderPane {
* Note: This panel is deliberately not created in FXML, since parsing of this took about 500 msecs. In an attempt
* to speed up the startup time of JabRef, this has been rewritten to plain java.
*/
public GroupTreeView(TaskExecutor taskExecutor, StateManager stateManager, PreferencesService preferencesService, DialogService dialogService) {
public GroupTreeView(TaskExecutor taskExecutor,
StateManager stateManager,
PreferencesService preferencesService,
DialogService dialogService) {
this.taskExecutor = taskExecutor;
this.stateManager = stateManager;
this.preferencesService = preferencesService;
Expand Down Expand Up @@ -199,9 +202,17 @@ private void initialize() {
group.allSelectedEntriesMatchedProperty());
}
Text text = new Text();
if (preferencesService.getDisplayGroupCount()) {
text.textProperty().bind(group.getHits().asString());
}
EasyBind.subscribe(preferencesService.getGroupsPreferences().displayGroupCountProperty(),
(newValue) -> {
if (text.textProperty().isBound()) {
text.textProperty().unbind();
text.setText("");
}

if (newValue) {
text.textProperty().bind(group.getHits().asString());
}
});
text.getStyleClass().setAll("text");
node.getChildren().add(text);
node.setMaxWidth(Control.USE_PREF_SIZE);
Expand Down Expand Up @@ -296,9 +307,7 @@ private void initialize() {
}
event.consume();
});
row.setOnDragExited(event -> {
ControlHelper.removeDroppingPseudoClasses(row);
});
row.setOnDragExited(event -> ControlHelper.removeDroppingPseudoClasses(row));

row.setOnDragDropped(event -> {
Dragboard dragboard = event.getDragboard();
Expand Down
55 changes: 46 additions & 9 deletions src/main/java/org/jabref/gui/groups/GroupsPreferences.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,73 @@
package org.jabref.gui.groups;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;

public class GroupsPreferences {

private final GroupViewMode groupViewMode;
private final boolean shouldAutoAssignGroup;
private final boolean shouldDisplayGroupCount;
private final Character keywordSeparator;
private final ObjectProperty<GroupViewMode> groupViewMode;
private final BooleanProperty shouldAutoAssignGroup;
private final BooleanProperty shouldDisplayGroupCount;
private final ObjectProperty<Character> keywordSeparator;

public GroupsPreferences(GroupViewMode groupViewMode,
boolean shouldAutoAssignGroup,
boolean shouldDisplayGroupCount,
Character keywordSeparator) {
ObjectProperty<Character> keywordSeparator) {

this.groupViewMode = groupViewMode;
this.shouldAutoAssignGroup = shouldAutoAssignGroup;
this.shouldDisplayGroupCount = shouldDisplayGroupCount;
this.groupViewMode = new SimpleObjectProperty<>(groupViewMode);
this.shouldAutoAssignGroup = new SimpleBooleanProperty(shouldAutoAssignGroup);
this.shouldDisplayGroupCount = new SimpleBooleanProperty(shouldDisplayGroupCount);
this.keywordSeparator = keywordSeparator;
}

public GroupViewMode getGroupViewMode() {
return groupViewMode.getValue();
}

public ObjectProperty<GroupViewMode> groupViewModeProperty() {
return groupViewMode;
}

public void setGroupViewMode(GroupViewMode groupViewMode) {
this.groupViewMode.set(groupViewMode);
}

public boolean shouldAutoAssignGroup() {
return shouldAutoAssignGroup.getValue();
}

public BooleanProperty autoAssignGroupProperty() {
return shouldAutoAssignGroup;
}

public void setAutoAssignGroup(boolean shouldAutoAssignGroup) {
this.shouldAutoAssignGroup.set(shouldAutoAssignGroup);
}

public boolean shouldDisplayGroupCount() {
return shouldDisplayGroupCount.getValue();
}

public BooleanProperty displayGroupCountProperty() {
return shouldDisplayGroupCount;
}

public Character getKeywordDelimiter() {
public void setDisplayGroupCount(boolean shouldDisplayGroupCount) {
this.shouldDisplayGroupCount.set(shouldDisplayGroupCount);
}

public Character getKeywordSeparator() {
return keywordSeparator.getValue();
}

public ObjectProperty<Character> keywordSeparatorProperty() {
return keywordSeparator;
}

public void setKeywordSeparator(Character keywordSeparator) {
this.keywordSeparator.set(keywordSeparator);
}
}
10 changes: 5 additions & 5 deletions src/main/java/org/jabref/gui/help/SearchForUpdateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.util.BuildInfo;
import org.jabref.preferences.VersionPreferences;
import org.jabref.preferences.InternalPreferences;

public class SearchForUpdateAction extends SimpleCommand {

private final BuildInfo buildInfo;
private final VersionPreferences versionPreferences;
private final InternalPreferences internalPreferences;
private final DialogService dialogService;
private final TaskExecutor taskExecutor;

public SearchForUpdateAction(BuildInfo buildInfo, VersionPreferences versionPreferences, DialogService dialogService, TaskExecutor taskExecutor) {
public SearchForUpdateAction(BuildInfo buildInfo, InternalPreferences internalPreferences, DialogService dialogService, TaskExecutor taskExecutor) {
this.buildInfo = buildInfo;
this.versionPreferences = versionPreferences;
this.internalPreferences = internalPreferences;
this.dialogService = dialogService;
this.taskExecutor = taskExecutor;
}

@Override
public void execute() {
new VersionWorker(buildInfo.version, dialogService, taskExecutor, versionPreferences)
new VersionWorker(buildInfo.version, dialogService, taskExecutor, internalPreferences)
.checkForNewVersionAsync();
}
}
12 changes: 6 additions & 6 deletions src/main/java/org/jabref/gui/help/VersionWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.Version;
import org.jabref.preferences.VersionPreferences;
import org.jabref.preferences.InternalPreferences;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,17 +35,17 @@ public class VersionWorker {

private final DialogService dialogService;
private final TaskExecutor taskExecutor;
private final VersionPreferences versionPreferences;
private final InternalPreferences internalPreferences;

public VersionWorker(Version installedVersion,

DialogService dialogService,
TaskExecutor taskExecutor,
VersionPreferences versionPreferences) {
InternalPreferences internalPreferences) {
this.installedVersion = Objects.requireNonNull(installedVersion);
this.dialogService = Objects.requireNonNull(dialogService);
this.taskExecutor = Objects.requireNonNull(taskExecutor);
this.versionPreferences = versionPreferences;
this.internalPreferences = internalPreferences;
}

/**
Expand Down Expand Up @@ -89,14 +89,14 @@ private void showConnectionError(Exception exception, boolean manualExecution) {
*/
private void showUpdateInfo(Optional<Version> newerVersion, boolean manualExecution) {
// no new version could be found, only respect the ignored version on automated version checks
if (newerVersion.isEmpty() || (newerVersion.get().equals(versionPreferences.getIgnoredVersion()) && !manualExecution)) {
if (newerVersion.isEmpty() || (newerVersion.get().equals(internalPreferences.getIgnoredVersion()) && !manualExecution)) {
if (manualExecution) {
dialogService.notify(Localization.lang("JabRef is up-to-date."));
}
} else {
// notify the user about a newer version
if (dialogService.showCustomDialogAndWait(new NewVersionDialog(installedVersion, newerVersion.get())).orElse(true)) {
versionPreferences.setIgnoredVersion(newerVersion.get());
internalPreferences.setIgnoredVersion(newerVersion.get());
}
}
}
Expand Down
Loading

0 comments on commit ba5beb2

Please sign in to comment.