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

Fix save order preferences #651

Merged
merged 25 commits into from
Aug 28, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We changed the behavior of group addition/edit, so that sorting by alphabetical order is not performed by default after the modification [#10017](https://github.com/JabRef/jabref/issues/10017)
- We fixed an issue with spacing in the cleanup dialogue. [#10081](https://github.com/JabRef/jabref/issues/10081)
- The GVK fetcher now uses the new [K10plus](https://www.bszgbv.de/services/k10plus/) database [#10189](https://github.com/JabRef/jabref/pull/10189)
- The SLR feature adds new entries sorted into the new bib file (and not at the end).

### Fixed

Expand Down Expand Up @@ -134,6 +135,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where the ACM Portal search sometimes would not return entries for some search queries when the article author had no given name [#10107](https://github.com/JabRef/jabref/issues/10107)
- We fixed an issue that caused high CPU usage and a zombie process after quitting JabRef because of author names autocompletion [#10159](https://github.com/JabRef/jabref/pull/10159)
- We fixed an issue where files with illegal characters in the filename could be added to JabRef. [#10182](https://github.com/JabRef/jabref/issues/10182)
- We fixed an issue with JabRef respecting the save order. [#9869](https://github.com/JabRef/jabref/issues/9869)

### Removed

Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ dependencies {

implementation group: 'org.jooq', name: 'jool', version: '0.9.15'

compileOnly 'org.jspecify:jspecify:0.3.0'
testCompileOnly 'org.jspecify:jspecify:0.3.0'

testImplementation 'io.github.classgraph:classgraph:4.8.162'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testImplementation 'org.junit.platform:junit-platform-launcher:1.10.0'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,6 @@
requires org.eclipse.jgit;
uses org.eclipse.jgit.transport.SshSessionFactory;
uses org.eclipse.jgit.lib.GpgSigner;

requires static org.jspecify;
}
2 changes: 0 additions & 2 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,7 @@ private void saveDatabase(BibDatabase newBase, String subName) {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), StandardCharsets.UTF_8)) {
BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE);
SaveConfiguration saveConfiguration = new SaveConfiguration()
.withMetadataSaveOrder(true)
.withReformatOnSave(preferencesService.getLibraryPreferences().shouldAlwaysReformatOnSave());

BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(
bibWriter,
saveConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* The action concerned with generate a new (sub-)database from latex AUX file.
*
* A new library is created by {@link org.jabref.gui.importer.NewDatabaseAction}
*/
public class NewSubLibraryAction extends SimpleCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javafx.collections.FXCollections;

import org.jabref.model.entry.field.Field;
import org.jabref.model.metadata.SaveOrder;

public class SaveOrderConfigPanelViewModel {

Expand All @@ -24,7 +23,7 @@ public SaveOrderConfigPanelViewModel() {
}

public void addCriterion() {
selectedSortCriteriaProperty.add(new SortCriterionViewModel(new SaveOrder.SortCriterion()));
selectedSortCriteriaProperty.add(new SortCriterionViewModel());
}

public void removeCriterion(SortCriterionViewModel sortCriterionViewModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.beans.property.SimpleObjectProperty;

import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.metadata.SaveOrder;

public class SortCriterionViewModel {
Expand All @@ -18,6 +19,11 @@ public SortCriterionViewModel(SaveOrder.SortCriterion criterion) {
this.descendingProperty.setValue(criterion.descending);
}

public SortCriterionViewModel() {
this.fieldProperty.setValue(StandardField.AUTHOR);
this.descendingProperty.setValue(false);
}

public ObjectProperty<Field> fieldProperty() {
return fieldProperty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ExporterViewModel saveExporter() {
layoutFile.get(),
extension.get(),
preferences.getLayoutFormatterPreferences(),
preferences.getExportConfiguration());
preferences.getExportConfiguration().getSaveOrder());
format.setCustomExport(true);
return new ExporterViewModel(format);
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.ChangePropagation;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
Expand Down Expand Up @@ -79,7 +80,7 @@ public boolean save(SaveDatabaseMode mode) {
}

/**
* Asks the user for the path and saves afterwards
* Asks the user for the path and saves afterward
*/
public void saveAs() {
askForSavePath().ifPresent(this::saveAs);
Expand All @@ -92,7 +93,8 @@ public boolean saveAs(Path file) {
public void saveSelectedAsPlain() {
askForSavePath().ifPresent(path -> {
try {
saveDatabase(path, true, StandardCharsets.UTF_8, BibDatabaseWriter.SaveType.PLAIN_BIBTEX);
saveDatabase(path, true, StandardCharsets.UTF_8, BibDatabaseWriter.SaveType.PLAIN_BIBTEX,
libraryTab.getBibDatabaseContext().getMetaData().getSaveOrder().orElse(SaveOrder.getDefaultSaveOrder()));
frame.getFileHistory().newFile(path);
dialogService.notify(Localization.lang("Saved selected to '%0'.", path.toString()));
} catch (SaveException ex) {
Expand Down Expand Up @@ -210,7 +212,8 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {
libraryTab.getBibDatabaseContext().getMetaData().setEncoding(encoding, ChangePropagation.DO_NOT_POST_EVENT);

// Save the database
boolean success = saveDatabase(targetPath, false, encoding, BibDatabaseWriter.SaveType.ALL);
boolean success = saveDatabase(targetPath, false, encoding, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA,
libraryTab.getBibDatabaseContext().getMetaData().getSaveOrder().orElse(SaveOrder.getDefaultSaveOrder()));

if (success) {
libraryTab.getUndoManager().markUnchanged();
Expand All @@ -228,12 +231,12 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) {
}
}

private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, BibDatabaseWriter.SaveType saveType) throws SaveException {
private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, BibDatabaseWriter.SaveType saveType, SaveOrder saveOrder) throws SaveException {
// if this code is adapted, please also adapt org.jabref.logic.autosaveandbackup.BackupManager.performBackup

SaveConfiguration saveConfiguration = new SaveConfiguration()
.withSaveType(saveType)
.withMetadataSaveOrder(true)
.withSaveOrder(saveOrder)
.withReformatOnSave(preferences.getLibraryPreferences().shouldAlwaysReformatOnSave());
BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext();
synchronized (bibDatabaseContext) {
Expand All @@ -255,7 +258,7 @@ private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding,
libraryTab.registerUndoableChanges(databaseWriter.getSaveActionsFieldChanges());

if (fileWriter.hasEncodingProblems()) {
saveWithDifferentEncoding(file, selectedOnly, encoding, fileWriter.getEncodingProblems(), saveType);
saveWithDifferentEncoding(file, selectedOnly, encoding, fileWriter.getEncodingProblems(), saveType, saveOrder);
}
} catch (UnsupportedCharsetException ex) {
throw new SaveException(Localization.lang("Character encoding '%0' is not supported.", encoding.displayName()), ex);
Expand All @@ -266,7 +269,7 @@ private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding,
}
}

private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset encoding, Set<Character> encodingProblems, BibDatabaseWriter.SaveType saveType) throws SaveException {
private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset encoding, Set<Character> encodingProblems, BibDatabaseWriter.SaveType saveType, SaveOrder saveOrder) throws SaveException {
DialogPane pane = new DialogPane();
VBox vbox = new VBox();
vbox.getChildren().addAll(
Expand All @@ -288,7 +291,7 @@ private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset
// Make sure to remember which encoding we used.
libraryTab.getBibDatabaseContext().getMetaData().setEncoding(newEncoding.get(), ChangePropagation.DO_NOT_POST_EVENT);

saveDatabase(file, selectedOnly, newEncoding.get(), saveType);
saveDatabase(file, selectedOnly, newEncoding.get(), saveType, saveOrder);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.libraryproperties.saving;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

Expand All @@ -18,13 +19,22 @@
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.metadata.MetaData;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.preferences.CleanupPreferences;
import org.jabref.preferences.PreferencesService;

public class SavingPropertiesViewModel implements PropertiesTabViewModel {

private static final SaveOrder UI_DEFAULT_SAVE_ORDER = new SaveOrder(SaveOrder.OrderType.ORIGINAL, List.of(
new SaveOrder.SortCriterion(StandardField.AUTHOR),
new SaveOrder.SortCriterion(StandardField.YEAR),
new SaveOrder.SortCriterion(StandardField.TITLE),
// Pro users generate their citation keys well. They can just delete the above three proposals and get a well-sorted library.
new SaveOrder.SortCriterion(InternalField.KEY_FIELD)
));

private final BooleanProperty protectDisableProperty = new SimpleBooleanProperty();
private final BooleanProperty libraryProtectedProperty = new SimpleBooleanProperty();

Expand All @@ -41,24 +51,23 @@ public class SavingPropertiesViewModel implements PropertiesTabViewModel {

private final BibDatabaseContext databaseContext;
private final MetaData initialMetaData;
private final SaveOrder exportSaveOrder;
private final SaveOrder saveOrder;
private final PreferencesService preferencesService;

public SavingPropertiesViewModel(BibDatabaseContext databaseContext, PreferencesService preferencesService) {
this.databaseContext = databaseContext;
this.preferencesService = preferencesService;
this.initialMetaData = databaseContext.getMetaData();
this.exportSaveOrder = initialMetaData.getSaveOrderConfig()
.orElseGet(() -> preferencesService.getExportPreferences().getExportSaveOrder());
this.saveOrder = initialMetaData.getSaveOrder().orElse(UI_DEFAULT_SAVE_ORDER);
}

@Override
public void setValues() {
libraryProtectedProperty.setValue(initialMetaData.isProtected());

// SaveOrderConfigPanel
// SaveOrderConfigPanel, included via <?import ...> in FXML

switch (exportSaveOrder.getOrderType()) {
switch (saveOrder.getOrderType()) {
case SPECIFIED -> saveInSpecifiedOrderProperty.setValue(true);
case ORIGINAL -> saveInOriginalProperty.setValue(true);
case TABLE -> saveInTableOrderProperty.setValue(true);
Expand All @@ -74,11 +83,11 @@ public void setValues() {

sortableFieldsProperty.addAll(FieldFactory.getStandardFieldsWithCitationKey());
sortCriteriaProperty.clear();
sortCriteriaProperty.addAll(exportSaveOrder.getSortCriteria().stream()
.map(SortCriterionViewModel::new)
.toList());
sortCriteriaProperty.addAll(saveOrder.getSortCriteria().stream()
.map(SortCriterionViewModel::new)
.toList());

// FieldFormatterCleanupsPanel
// FieldFormatterCleanupsPanel, included via <?import ...> in FXML

Optional<FieldFormatterCleanups> saveActions = initialMetaData.getSaveActions();
saveActions.ifPresentOrElse(value -> {
Expand Down Expand Up @@ -120,11 +129,11 @@ public void storeSettings() {
SaveOrder.OrderType.fromBooleans(saveInSpecifiedOrderProperty.getValue(), saveInOriginalProperty.getValue()),
sortCriteriaProperty.stream().map(SortCriterionViewModel::getCriterion).toList());

if (!newSaveOrder.equals(exportSaveOrder)) {
if (!newSaveOrder.equals(saveOrder)) {
if (newSaveOrder.equals(SaveOrder.getDefaultSaveOrder())) {
newMetaData.clearSaveOrderConfig();
newMetaData.clearSaveOrder();
} else {
newMetaData.setSaveOrderConfig(newSaveOrder);
newMetaData.setSaveOrder(newSaveOrder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.preferences.PreferencesService;

import com.google.common.eventbus.Subscribe;
Expand Down Expand Up @@ -225,7 +226,7 @@ void performBackup(Path backupPath) {
// code similar to org.jabref.gui.exporter.SaveDatabaseAction.saveDatabase
SaveConfiguration saveConfiguration = new SaveConfiguration()
.withMakeBackup(false)
.withMetadataSaveOrder(true)
.withSaveOrder(bibDatabaseContext.getMetaData().getSaveOrder().orElse(SaveOrder.getDefaultSaveOrder()))
.withReformatOnSave(preferences.getLibraryPreferences().shouldAlwaysReformatOnSave());

Charset encoding = bibDatabaseContext.getMetaData().getEncoding().orElse(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public EnumSet<Difference> getDifferences(PreferencesService preferences) {
if (!Objects.equals(originalMetaData.getEncoding(), newMetaData.getEncoding())) {
changes.add(Difference.ENCODING);
}
if (!Objects.equals(originalMetaData.getSaveOrderConfig(), newMetaData.getSaveOrderConfig())) {
if (!Objects.equals(originalMetaData.getSaveOrder(), newMetaData.getSaveOrder())) {
changes.add(Difference.SAVE_SORT_ORDER);
}
if (!Objects.equals(
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/jabref/logic/crawler/StudyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.model.study.FetchResult;
import org.jabref.model.study.QueryResult;
import org.jabref.model.study.Study;
Expand Down Expand Up @@ -60,6 +62,11 @@ public class StudyRepository {
private static final String WORK_BRANCH = "work";
private static final String SEARCH_BRANCH = "search";

private static final SaveOrder SAVE_ORDER = new SaveOrder(SaveOrder.OrderType.SPECIFIED,
List.of(new SaveOrder.SortCriterion(StandardField.AUTHOR, false),
new SaveOrder.SortCriterion(StandardField.YEAR, true),
new SaveOrder.SortCriterion(StandardField.TITLE, false)));

private final Path repositoryPath;
private final Path studyDefinitionFile;
private final SlrGitHandler gitHandler;
Expand Down Expand Up @@ -426,7 +433,7 @@ private void generateCiteKeys(BibDatabaseContext existingEntries, BibDatabase ta
private void writeResultToFile(Path pathToFile, BibDatabase entries) throws SaveException {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(pathToFile, StandardCharsets.UTF_8)) {
SaveConfiguration saveConfiguration = new SaveConfiguration()
.withMetadataSaveOrder(true)
.withSaveOrder(SAVE_ORDER)
.withReformatOnSave(preferencesService.getLibraryPreferences().shouldAlwaysReformatOnSave());
BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE);
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(
Expand Down
Loading
Loading