sortCriteriaProperty() {
+ return sortCriteriaProperty;
+ }
}
diff --git a/src/main/java/org/jabref/model/metadata/SaveOrderConfig.java b/src/main/java/org/jabref/model/metadata/SaveOrderConfig.java
index a444e12a5c0..26720236083 100644
--- a/src/main/java/org/jabref/model/metadata/SaveOrderConfig.java
+++ b/src/main/java/org/jabref/model/metadata/SaveOrderConfig.java
@@ -1,13 +1,15 @@
package org.jabref.model.metadata;
import java.util.ArrayList;
-import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* Stores the save order config from MetaData
*
@@ -15,22 +17,46 @@
*/
public class SaveOrderConfig {
- private static final String ORIGINAL = "original";
- private static final String SPECIFIED = "specified";
- private final LinkedList sortCriteria = new LinkedList<>();
- private boolean saveInOriginalOrder;
- private boolean saveInSpecifiedOrder;
+ public enum OrderType {
+ SPECIFIED("specified"),
+ ORIGINAL("original"),
+ TABLE("table");
+
+ private final String name;
+
+ OrderType(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+
+ public static SaveOrderConfig.OrderType fromBooleans(boolean saveInSpecifiedOrder, boolean saveInTableOrder) {
+ SaveOrderConfig.OrderType orderType = SaveOrderConfig.OrderType.ORIGINAL;
+ if (saveInSpecifiedOrder) {
+ orderType = SaveOrderConfig.OrderType.SPECIFIED;
+ } else if (saveInTableOrder) {
+ orderType = SaveOrderConfig.OrderType.TABLE;
+ }
+
+ return orderType;
+ }
+ }
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SaveOrderConfig.class);
+
+ private final List sortCriteria = new ArrayList<>();
+ private OrderType orderType;
public SaveOrderConfig() {
- setSaveInOriginalOrder();
+ this.orderType = OrderType.ORIGINAL;
}
- public SaveOrderConfig(boolean saveInOriginalOrder, boolean saveInSpecifiedOrder, SortCriterion first, SortCriterion second, SortCriterion third) {
- this.saveInOriginalOrder = saveInOriginalOrder;
- this.saveInSpecifiedOrder = saveInSpecifiedOrder;
- sortCriteria.add(first);
- sortCriteria.add(second);
- sortCriteria.add(third);
+ public SaveOrderConfig(OrderType orderType, List sortCriteria) {
+ this.orderType = orderType;
+ this.sortCriteria.addAll(sortCriteria);
}
private SaveOrderConfig(List data) {
@@ -40,15 +66,21 @@ private SaveOrderConfig(List data) {
throw new IllegalArgumentException();
}
- String choice = data.get(0);
- if (ORIGINAL.equals(choice)) {
- setSaveInOriginalOrder();
- } else {
- setSaveInSpecifiedOrder();
+ try {
+ this.orderType = OrderType.valueOf(data.get(0));
+ } catch (IllegalArgumentException ex) {
+ if (data.size() > 1 && data.size() % 2 == 1) {
+ LOGGER.warn("Could not parse sort order: {} - trying to parse the sort criteria", data.get(0));
+ this.orderType = OrderType.SPECIFIED;
+ } else {
+ LOGGER.warn("Could not parse sort order: {}", data.get(0));
+ this.orderType = OrderType.ORIGINAL;
+ return;
+ }
}
for (int index = 1; index < data.size(); index = index + 2) {
- sortCriteria.addLast(new SortCriterion(FieldFactory.parseField(data.get(index)), data.get(index + 1)));
+ sortCriteria.add(new SortCriterion(FieldFactory.parseField(data.get(index)), data.get(index + 1)));
}
}
@@ -58,19 +90,15 @@ public static SaveOrderConfig parse(List orderedData) {
public static SaveOrderConfig getDefaultSaveOrder() {
SaveOrderConfig standard = new SaveOrderConfig();
- standard.setSaveInOriginalOrder();
+ standard.orderType = OrderType.ORIGINAL;
return standard;
}
- public boolean saveInOriginalOrder() {
- return saveInOriginalOrder;
+ public OrderType getOrderType() {
+ return orderType;
}
- public boolean saveInSpecifiedOrder() {
- return saveInSpecifiedOrder;
- }
-
- public LinkedList getSortCriteria() {
+ public List getSortCriteria() {
return sortCriteria;
}
@@ -79,46 +107,34 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
- if (o instanceof SaveOrderConfig) {
- SaveOrderConfig that = (SaveOrderConfig) o;
+ if (o instanceof SaveOrderConfig that) {
return Objects.equals(sortCriteria, that.sortCriteria) &&
- Objects.equals(saveInOriginalOrder, that.saveInOriginalOrder) &&
- Objects.equals(saveInSpecifiedOrder, that.saveInSpecifiedOrder);
+ Objects.equals(orderType, that.orderType);
}
return false;
}
@Override
public int hashCode() {
- return Objects.hash(saveInOriginalOrder, saveInSpecifiedOrder, sortCriteria);
+ return Objects.hash(orderType, sortCriteria);
}
@Override
public String toString() {
- return "SaveOrderConfig{" + "saveInOriginalOrder=" + saveInOriginalOrder
- + "saveInSpecifiedOrder =" + saveInSpecifiedOrder
- + ", sortCriteria=" + sortCriteria +
+ return "SaveOrderConfig{" + "orderType=" + orderType.toString() +
+ ", sortCriteria=" + sortCriteria +
'}';
}
- public void setSaveInOriginalOrder() {
- this.saveInOriginalOrder = true;
- }
-
- public void setSaveInSpecifiedOrder() {
- this.saveInOriginalOrder = false;
- this.saveInSpecifiedOrder = true;
- }
-
/**
* Outputs the current configuration to be consumed later by the constructor
*/
public List getAsStringList() {
List res = new ArrayList<>(7);
- if (saveInOriginalOrder) {
- res.add(ORIGINAL);
+ if (orderType == OrderType.ORIGINAL) {
+ res.add(OrderType.ORIGINAL.toString());
} else {
- res.add(SPECIFIED);
+ res.add(OrderType.SPECIFIED.toString());
}
for (SortCriterion sortCriterion : sortCriteria) {
diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java
index 1b3dd68b980..d6cb06ae8e7 100644
--- a/src/main/java/org/jabref/preferences/JabRefPreferences.java
+++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java
@@ -1141,43 +1141,6 @@ public void setCleanupPreset(CleanupPreset cleanupPreset) {
putStringList(CLEANUP_FORMATTERS, cleanupPreset.getFormatterCleanups().getAsStringList(OS.NEWLINE));
}
- @Override
- public void storeExportSaveOrder(SaveOrderConfig config) {
- putBoolean(EXPORT_PRIMARY_SORT_DESCENDING, config.getSortCriteria().get(0).descending);
- putBoolean(EXPORT_SECONDARY_SORT_DESCENDING, config.getSortCriteria().get(1).descending);
- putBoolean(EXPORT_TERTIARY_SORT_DESCENDING, config.getSortCriteria().get(2).descending);
- putBoolean(EXPORT_IN_ORIGINAL_ORDER, config.saveInOriginalOrder());
- putBoolean(EXPORT_IN_SPECIFIED_ORDER, config.saveInSpecifiedOrder());
-
- put(EXPORT_PRIMARY_SORT_FIELD, config.getSortCriteria().get(0).field.getName());
- put(EXPORT_SECONDARY_SORT_FIELD, config.getSortCriteria().get(1).field.getName());
- put(EXPORT_TERTIARY_SORT_FIELD, config.getSortCriteria().get(2).field.getName());
- }
-
- private SaveOrderConfig loadTableSaveOrder() {
- SaveOrderConfig config = new SaveOrderConfig();
-
- updateMainTableColumns();
- List sortOrder = createMainTableColumnSortOrder();
-
- for (var column : sortOrder) {
- boolean descending = (column.getSortType() == SortType.DESCENDING);
- config.getSortCriteria().add(new SaveOrderConfig.SortCriterion(
- FieldFactory.parseField(column.getQualifier()),
- descending));
- }
-
- return config;
- }
-
- @Override
- public SaveOrderConfig loadExportSaveOrder() {
- return new SaveOrderConfig(getBoolean(EXPORT_IN_ORIGINAL_ORDER), getBoolean(EXPORT_IN_SPECIFIED_ORDER),
- new SaveOrderConfig.SortCriterion(FieldFactory.parseField(get(EXPORT_PRIMARY_SORT_FIELD)), getBoolean(EXPORT_PRIMARY_SORT_DESCENDING)),
- new SaveOrderConfig.SortCriterion(FieldFactory.parseField(get(EXPORT_SECONDARY_SORT_FIELD)), getBoolean(EXPORT_SECONDARY_SORT_DESCENDING)),
- new SaveOrderConfig.SortCriterion(FieldFactory.parseField(get(EXPORT_TERTIARY_SORT_FIELD)), getBoolean(EXPORT_TERTIARY_SORT_DESCENDING)));
- }
-
public String getOrCreateUserId() {
Optional userId = getAsOptional(USER_ID);
if (userId.isPresent()) {
@@ -2050,13 +2013,56 @@ public ImportFormatPreferences getImportFormatPreferences() {
getSpecialFieldsPreferences().isKeywordSyncEnabled());
}
+ @Override
+ public SaveOrderConfig getExportSaveOrder() {
+ List sortCriteria = List.of(
+ new SaveOrderConfig.SortCriterion(FieldFactory.parseField(get(EXPORT_PRIMARY_SORT_FIELD)), getBoolean(EXPORT_PRIMARY_SORT_DESCENDING)),
+ new SaveOrderConfig.SortCriterion(FieldFactory.parseField(get(EXPORT_SECONDARY_SORT_FIELD)), getBoolean(EXPORT_SECONDARY_SORT_DESCENDING)),
+ new SaveOrderConfig.SortCriterion(FieldFactory.parseField(get(EXPORT_TERTIARY_SORT_FIELD)), getBoolean(EXPORT_TERTIARY_SORT_DESCENDING))
+ );
+
+ return new SaveOrderConfig(
+ SaveOrderConfig.OrderType.fromBooleans(getBoolean(EXPORT_IN_SPECIFIED_ORDER), getBoolean(EXPORT_IN_ORIGINAL_ORDER)),
+ sortCriteria
+ );
+ }
+
+ @Override
+ public void storeExportSaveOrder(SaveOrderConfig config) {
+ putBoolean(EXPORT_IN_ORIGINAL_ORDER, config.getOrderType() == SaveOrderConfig.OrderType.ORIGINAL);
+ putBoolean(EXPORT_IN_SPECIFIED_ORDER, config.getOrderType() == SaveOrderConfig.OrderType.SPECIFIED);
+
+ put(EXPORT_PRIMARY_SORT_FIELD, config.getSortCriteria().get(0).field.getName());
+ put(EXPORT_SECONDARY_SORT_FIELD, config.getSortCriteria().get(1).field.getName());
+ put(EXPORT_TERTIARY_SORT_FIELD, config.getSortCriteria().get(2).field.getName());
+ putBoolean(EXPORT_PRIMARY_SORT_DESCENDING, config.getSortCriteria().get(0).descending);
+ putBoolean(EXPORT_SECONDARY_SORT_DESCENDING, config.getSortCriteria().get(1).descending);
+ putBoolean(EXPORT_TERTIARY_SORT_DESCENDING, config.getSortCriteria().get(2).descending);
+ }
+
+ private SaveOrderConfig loadTableSaveOrder() {
+ SaveOrderConfig config = new SaveOrderConfig();
+
+ updateMainTableColumns();
+ List sortOrder = createMainTableColumnSortOrder();
+
+ for (var column : sortOrder) {
+ boolean descending = (column.getSortType() == SortType.DESCENDING);
+ config.getSortCriteria().add(new SaveOrderConfig.SortCriterion(
+ FieldFactory.parseField(column.getQualifier()),
+ descending));
+ }
+
+ return config;
+ }
+
@Override
public SavePreferences getSavePreferencesForExport() {
Boolean saveInOriginalOrder = this.getBoolean(EXPORT_IN_ORIGINAL_ORDER);
SaveOrderConfig saveOrder = null;
if (!saveInOriginalOrder) {
if (this.getBoolean(EXPORT_IN_SPECIFIED_ORDER)) {
- saveOrder = this.loadExportSaveOrder();
+ saveOrder = this.getExportSaveOrder();
} else {
saveOrder = this.loadTableSaveOrder();
}
diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java
index 70fa7bcfd07..bcbf0ab39c0 100644
--- a/src/main/java/org/jabref/preferences/PreferencesService.java
+++ b/src/main/java/org/jabref/preferences/PreferencesService.java
@@ -102,7 +102,7 @@ public interface PreferencesService {
String getUser();
- SaveOrderConfig loadExportSaveOrder();
+ SaveOrderConfig getExportSaveOrder();
void storeExportSaveOrder(SaveOrderConfig config);
diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties
index 2eb613f94de..7b60659b08c 100644
--- a/src/main/resources/l10n/JabRef_en.properties
+++ b/src/main/resources/l10n/JabRef_en.properties
@@ -638,7 +638,6 @@ Selected=Selected
Selected\ Layouts\ can\ not\ be\ empty=Selected Layouts can not be empty
Reset\ default\ preview\ style=Reset default preview style
Previous\ entry=Previous entry
-Primary\ sort\ criterion=Primary sort criterion
Problem\ with\ parsing\ entry=Problem with parsing entry
Processing\ %0=Processing %0
Pull\ changes\ from\ shared\ database=Pull changes from shared database
@@ -754,8 +753,6 @@ Searching\ for\ duplicates...=Searching for duplicates...
Searching\ for\ files=Searching for files
-Secondary\ sort\ criterion=Secondary sort criterion
-
Select\ all=Select all
Select\ new\ encoding=Select new encoding
@@ -816,8 +813,6 @@ Sublibrary\ from\ AUX\ to\ BibTeX=Sublibrary from AUX to BibTeX
Switches\ between\ full\ and\ abbreviated\ journal\ name\ if\ the\ journal\ name\ is\ known.=Switches between full and abbreviated journal name if the journal name is known.
-Tertiary\ sort\ criterion=Tertiary sort criterion
-
The\ chosen\ encoding\ '%0'\ could\ not\ encode\ the\ following\ characters\:=The chosen encoding '%0' could not encode the following characters:
diff --git a/src/test/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanelViewModelTest.java b/src/test/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanelViewModelTest.java
new file mode 100644
index 00000000000..da85a54cef9
--- /dev/null
+++ b/src/test/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanelViewModelTest.java
@@ -0,0 +1,67 @@
+package org.jabref.gui.commonfxcontrols;
+
+import java.util.List;
+
+import org.jabref.model.entry.field.StandardField;
+import org.jabref.model.metadata.SaveOrderConfig;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class SaveOrderConfigPanelViewModelTest {
+
+ SortCriterionViewModel sortCriterionKey = new SortCriterionViewModel(new SaveOrderConfig.SortCriterion(StandardField.KEY, false));
+ SortCriterionViewModel sortCriterionAuthor = new SortCriterionViewModel(new SaveOrderConfig.SortCriterion(StandardField.AUTHOR, false));
+ SortCriterionViewModel sortCriterionTitle = new SortCriterionViewModel(new SaveOrderConfig.SortCriterion(StandardField.TITLE, true));
+
+ SaveOrderConfigPanelViewModel viewModel;
+
+ @BeforeEach
+ void setUp() {
+ viewModel = new SaveOrderConfigPanelViewModel();
+ viewModel.sortCriteriaProperty().addAll(List.of(sortCriterionKey, sortCriterionAuthor, sortCriterionTitle));
+ }
+
+ @Test
+ void addCriterion() {
+ viewModel.addCriterion();
+ assertEquals(4, viewModel.sortCriteriaProperty().size());
+ }
+
+ @Test
+ void removeCriterion() {
+ SortCriterionViewModel unknownCriterion = new SortCriterionViewModel(new SaveOrderConfig.SortCriterion(StandardField.ABSTRACT, false));
+ viewModel.removeCriterion(unknownCriterion);
+ assertEquals(3, viewModel.sortCriteriaProperty().size());
+
+ viewModel.removeCriterion(sortCriterionAuthor);
+ assertEquals(2, viewModel.sortCriteriaProperty().size());
+ assertEquals(List.of(sortCriterionKey, sortCriterionTitle), viewModel.sortCriteriaProperty());
+ }
+
+ @Test
+ void moveCriterionUp() {
+ viewModel.moveCriterionUp(sortCriterionTitle);
+ assertEquals(List.of(sortCriterionKey, sortCriterionTitle, sortCriterionAuthor), viewModel.sortCriteriaProperty());
+
+ viewModel.moveCriterionUp(sortCriterionTitle);
+ assertEquals(List.of(sortCriterionTitle, sortCriterionKey, sortCriterionAuthor), viewModel.sortCriteriaProperty());
+
+ viewModel.moveCriterionUp(sortCriterionTitle);
+ assertEquals(List.of(sortCriterionTitle, sortCriterionKey, sortCriterionAuthor), viewModel.sortCriteriaProperty());
+ }
+
+ @Test
+ void moveCriterionDown() {
+ viewModel.moveCriterionDown(sortCriterionKey);
+ assertEquals(List.of(sortCriterionAuthor, sortCriterionKey, sortCriterionTitle), viewModel.sortCriteriaProperty());
+
+ viewModel.moveCriterionDown(sortCriterionKey);
+ assertEquals(List.of(sortCriterionAuthor, sortCriterionTitle, sortCriterionKey), viewModel.sortCriteriaProperty());
+
+ viewModel.moveCriterionDown(sortCriterionKey);
+ assertEquals(List.of(sortCriterionAuthor, sortCriterionTitle, sortCriterionKey), viewModel.sortCriteriaProperty());
+ }
+}
diff --git a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java
index 0854bb57467..87c241fa30b 100644
--- a/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java
+++ b/src/test/java/org/jabref/logic/exporter/BibtexDatabaseWriterTest.java
@@ -10,6 +10,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.List;
import org.jabref.logic.citationkeypattern.AbstractCitationKeyPattern;
import org.jabref.logic.citationkeypattern.DatabaseCitationKeyPattern;
@@ -496,9 +497,10 @@ void writeSaveActions() throws Exception {
@Test
void writeSaveOrderConfig() throws Exception {
- SaveOrderConfig saveOrderConfig = new SaveOrderConfig(false, true, new SaveOrderConfig.SortCriterion(StandardField.AUTHOR, false),
- new SaveOrderConfig.SortCriterion(StandardField.YEAR, true),
- new SaveOrderConfig.SortCriterion(StandardField.ABSTRACT, false));
+ SaveOrderConfig saveOrderConfig = new SaveOrderConfig(SaveOrderConfig.OrderType.SPECIFIED,
+ List.of(new SaveOrderConfig.SortCriterion(StandardField.AUTHOR, false),
+ new SaveOrderConfig.SortCriterion(StandardField.YEAR, true),
+ new SaveOrderConfig.SortCriterion(StandardField.ABSTRACT, false)));
metaData.setSaveOrderConfig(saveOrderConfig);
databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList());
@@ -557,10 +559,10 @@ void writeFileDirectories() throws Exception {
@Test
void writeEntriesSorted() throws Exception {
- SaveOrderConfig saveOrderConfig = new SaveOrderConfig(false, true,
- new SaveOrderConfig.SortCriterion(StandardField.AUTHOR, false),
- new SaveOrderConfig.SortCriterion(StandardField.YEAR, true),
- new SaveOrderConfig.SortCriterion(StandardField.ABSTRACT, false));
+ SaveOrderConfig saveOrderConfig = new SaveOrderConfig(SaveOrderConfig.OrderType.SPECIFIED,
+ List.of(new SaveOrderConfig.SortCriterion(StandardField.AUTHOR, false),
+ new SaveOrderConfig.SortCriterion(StandardField.YEAR, true),
+ new SaveOrderConfig.SortCriterion(StandardField.ABSTRACT, false)));
metaData.setSaveOrderConfig(saveOrderConfig);
BibEntry firstEntry = new BibEntry();
diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java
index 2879938f5ba..e0958825e03 100644
--- a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java
+++ b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java
@@ -1339,8 +1339,10 @@ void integrationTestSaveOrderConfig() throws IOException {
Optional saveOrderConfig = result.getMetaData().getSaveOrderConfig();
- assertEquals(new SaveOrderConfig(false, true, new SaveOrderConfig.SortCriterion(StandardField.AUTHOR, false),
- new SaveOrderConfig.SortCriterion(StandardField.YEAR, true), new SaveOrderConfig.SortCriterion(StandardField.ABSTRACT, false)),
+ assertEquals(new SaveOrderConfig(SaveOrderConfig.OrderType.SPECIFIED, List.of(
+ new SaveOrderConfig.SortCriterion(StandardField.AUTHOR, false),
+ new SaveOrderConfig.SortCriterion(StandardField.YEAR, true),
+ new SaveOrderConfig.SortCriterion(StandardField.ABSTRACT, false))),
saveOrderConfig.get());
}