Skip to content

Commit

Permalink
Fix column sort order gets overwritten (#7573)
Browse files Browse the repository at this point in the history
Co-authored-by: Carl Christian Snethlage <cc.snethlage@gmail.com>
  • Loading branch information
Siedlerchr and calixtus authored Mar 29, 2021
1 parent 55c4467 commit 2948e6d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed an issue where the table column sort order was not properly stored and resulted in unsorted eports [#7524](https://github.com/JabRef/jabref/issues/7524)
- We fixed an issue where the value of the field `school` or `institution` would be printed twice in the HTML Export [forum#2634](https://discourse.jabref.org/t/problem-with-exporting-techreport-phdthesis-mastersthesis-to-html/2634)
- We fixed an issue preventing to connect to a shared database. [#7570](https://github.com/JabRef/jabref/pull/7570)
- We fixed an issue preventing files from being dragged & dropped into an empty library. [#6851](https://github.com/JabRef/jabref/issues/6851)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public enum Type {

public static final EnumSet<Type> ICON_COLUMNS = EnumSet.of(EXTRAFILE, FILES, GROUPS, LINKED_IDENTIFIER);

private String name;
private String displayName;
private final String name;
private final String displayName;

Type(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,37 @@ public PersistenceVisualStateTable(final MainTable mainTable, PreferencesService
this.mainTable = mainTable;
this.preferences = preferences;

mainTable.getColumns().addListener((InvalidationListener) obs -> updateColumnPreferences());
mainTable.getSortOrder().addListener((InvalidationListener) obs -> updateColumnPreferences());
mainTable.getColumns().addListener((InvalidationListener) obs -> updateColumns());
mainTable.getSortOrder().addListener((InvalidationListener) obs -> updateSortOrder());

// As we store the ColumnModels of the MainTable, we need to add the listener to the ColumnModel properties,
// since the value is bound to the model after the listener to the column itself is called.
mainTable.getColumns().forEach(col ->
((MainTableColumn<?>) col).getModel().widthProperty().addListener(obs -> updateColumnPreferences()));
((MainTableColumn<?>) col).getModel().widthProperty().addListener(obs -> updateColumns()));
mainTable.getColumns().forEach(col ->
((MainTableColumn<?>) col).getModel().sortTypeProperty().addListener(obs -> updateColumnPreferences()));
((MainTableColumn<?>) col).getModel().sortTypeProperty().addListener(obs -> updateColumns()));
}

/**
* Store shown columns, their width and their sortType in preferences.
* Stores shown columns, their width and their sortType in preferences.
*/
private void updateColumnPreferences() {
private void updateColumns() {
preferences.storeColumnPreferences(new ColumnPreferences(
mainTable.getColumns().stream()
.map(column -> ((MainTableColumn<?>) column).getModel())
.collect(Collectors.toList()),
preferences.getColumnPreferences().getColumnSortOrder()));
}

/**
* Stores the SortOrder of the the Table in the preferences. Cannot be combined with updateColumns, because JavaFX
* would provide just an empty list for the sort order on other changes.
*/
private void updateSortOrder() {
preferences.storeColumnPreferences(new ColumnPreferences(
preferences.getColumnPreferences().getColumns(),
mainTable.getSortOrder().stream()
.map(column -> ((MainTableColumn<?>) column).getModel())
.collect(Collectors.toList())
));
.collect(Collectors.toList())));
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/model/metadata/SaveOrderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public static class SortCriterion {

public boolean descending;

/**
*
* @param field The field
* @param descending Must be a boolean value as string, e.g. "true", "false"
*/
public SortCriterion(Field field, String descending) {
this.field = field;
this.descending = Boolean.parseBoolean(descending);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1152,9 +1152,12 @@ private SaveOrderConfig loadTableSaveOrder() {
updateMainTableColumns();
List<MainTableColumnModel> sortOrder = createMainTableColumnSortOrder();

sortOrder.forEach(column -> config.getSortCriteria().add(new SaveOrderConfig.SortCriterion(
FieldFactory.parseField(column.getQualifier()),
column.getSortType().toString())));
for (var column : sortOrder) {
boolean descending = (column.getSortType() == SortType.DESCENDING);
config.getSortCriteria().add(new SaveOrderConfig.SortCriterion(
FieldFactory.parseField(column.getQualifier()),
descending));
}

return config;
}
Expand Down

0 comments on commit 2948e6d

Please sign in to comment.