Skip to content

Commit

Permalink
Fix column sort order gets overwritten
Browse files Browse the repository at this point in the history
Fixes #7524
  • Loading branch information
Siedlerchr committed Mar 26, 2021
1 parent badffe9 commit 4631038
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
18 changes: 16 additions & 2 deletions src/main/java/org/jabref/gui/maintable/ColumnPreferences.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.jabref.gui.maintable;

import java.util.ArrayList;
import java.util.List;

public class ColumnPreferences {

public static final double DEFAULT_COLUMN_WIDTH = 100;
public static final double ICON_COLUMN_WIDTH = 16 + 12; // add some additional space to improve appearance

private final List<MainTableColumnModel> columns;
private final List<MainTableColumnModel> columnSortOrder;
private List<MainTableColumnModel> columns;
private List<MainTableColumnModel> columnSortOrder = new ArrayList<>();

public ColumnPreferences(List<MainTableColumnModel> columns, List<MainTableColumnModel> columnSortOrder) {
this.columns = columns;
Expand All @@ -22,4 +23,17 @@ public List<MainTableColumnModel> getColumns() {
public List<MainTableColumnModel> getColumnSortOrder() {
return columnSortOrder;
}

public void setColumns(List<MainTableColumnModel> columns) {
this.columns = columns;
}

public void setColumnSorOrder(List<MainTableColumnModel> columnSortOrder) {
this.columnSortOrder = columnSortOrder;
}

@Override
public String toString() {
return "ColumnPreferences [columns=" + columns + ", columnSortOrder=" + columnSortOrder + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,11 @@ public static MainTableColumnModel parse(String rawColumnName) {

return new MainTableColumnModel(type, qualifier);
}

@Override
public String toString() {
return "MainTableColumnModel [typeProperty=" + typeProperty + ", qualifierProperty=" + qualifierProperty + ", widthProperty=" + widthProperty + ", sortTypeProperty=" + sortTypeProperty + "]";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,35 @@ public PersistenceVisualStateTable(final MainTable mainTable, PreferencesService
this.preferences = preferences;

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

// 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()));
mainTable.getColumns().forEach(col ->
((MainTableColumn<?>) col).getModel().sortTypeProperty().addListener(obs -> updateColumnPreferences()));
mainTable.getColumns().forEach(col -> ((MainTableColumn<?>) col).getModel().widthProperty().addListener(obs -> updateColumnPreferences()));
mainTable.getColumns().forEach(col -> ((MainTableColumn<?>) col).getModel().sortTypeProperty().addListener(obs -> updateColumnPreferences()));
}

private void updateColSortOrder() {
ColumnPreferences prefs = preferences.getColumnPreferences();

var sortOrder = mainTable.getSortOrder().stream()
.map(column -> ((MainTableColumn<?>) column).getModel())
.collect(Collectors.toList());

prefs.setColumnSorOrder(sortOrder);
preferences.storeColumnPreferences(prefs);
}

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

ColumnPreferences prefs = preferences.getColumnPreferences();
prefs.setColumns(mainTable.getColumns().stream()
.map(column -> ((MainTableColumn<?>) column).getModel())
.collect(Collectors.toList())
));
.collect(Collectors.toList()));
preferences.storeColumnPreferences(prefs);

}
}
9 changes: 5 additions & 4 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1934,10 +1934,11 @@ public void storeColumnPreferences(ColumnPreferences columnPreferences) {
columnPreferences.getColumns().forEach(column -> columnSortTypesInOrder.add(column.sortTypeProperty().getValue().toString()));
putStringList(COLUMN_SORT_TYPES, columnSortTypesInOrder);

putStringList(COLUMN_SORT_ORDER, columnPreferences
.getColumnSortOrder().stream()
.map(MainTableColumnModel::getName)
.collect(Collectors.toList()));
var columnSortOrder = columnPreferences
.getColumnSortOrder().stream()
.map(MainTableColumnModel::getName)
.collect(Collectors.toList());
putStringList(COLUMN_SORT_ORDER, columnSortOrder);

// Update cache
mainTableColumns = columnPreferences.getColumns();
Expand Down

0 comments on commit 4631038

Please sign in to comment.