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

Save sort order column of main table #4327

Merged
merged 13 commits into from
Sep 13, 2018
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 @@ -1022,7 +1022,7 @@ public void addParserResult(ParserResult pr, boolean focusPanel) {
*/
@Deprecated
public void output(final String s) {
statusLine.show(s, 3000);
DefaultTaskExecutor.runInJavaFXThread(() -> statusLine.show(s, 3000));
}

private void initActions() {
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/jabref/gui/maintable/ColumnPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;

import javafx.scene.control.TableColumn.SortType;

import org.jabref.model.entry.BibtexSingleField;
import org.jabref.model.entry.specialfields.SpecialField;
Expand All @@ -16,8 +19,9 @@ public class ColumnPreferences {
private final List<SpecialField> specialFieldColumns;
private final List<String> extraFileColumns;
private final Map<String, Double> columnWidths;
private final List<String> columnSortOrder;

public ColumnPreferences(boolean showFileColumn, boolean showUrlColumn, boolean preferDoiOverUrl, boolean showEprintColumn, List<String> normalColumns, List<SpecialField> specialFieldColumns, List<String> extraFileColumns, Map<String, Double> columnWidths) {
public ColumnPreferences(boolean showFileColumn, boolean showUrlColumn, boolean preferDoiOverUrl, boolean showEprintColumn, List<String> normalColumns, List<SpecialField> specialFieldColumns, List<String> extraFileColumns, Map<String, Double> columnWidths, List<String> columnSortOrder) {
this.showFileColumn = showFileColumn;
this.showUrlColumn = showUrlColumn;
this.preferDoiOverUrl = preferDoiOverUrl;
Expand All @@ -26,6 +30,7 @@ public ColumnPreferences(boolean showFileColumn, boolean showUrlColumn, boolean
this.specialFieldColumns = specialFieldColumns;
this.extraFileColumns = extraFileColumns;
this.columnWidths = columnWidths;
this.columnSortOrder = columnSortOrder;
}

public boolean showFileColumn() {
Expand Down Expand Up @@ -59,4 +64,14 @@ public List<String> getNormalColumns() {
public double getPrefColumnWidth(String columnName) {
return columnWidths.getOrDefault(columnName, BibtexSingleField.DEFAULT_FIELD_LENGTH);
}

public Optional<SortType> getSortTypeForColumn(String columnName) {

if (columnName.equals(columnSortOrder.get(0))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parsing should happen in the JabRefPreferences class so that the constructor here admits a map: column > sort order

return Optional.of(SortType.valueOf(columnSortOrder.get(1)));
}
return Optional.empty();

}

}
8 changes: 7 additions & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public class MainTable extends TableView<BibEntryTableViewModel> {
private final NewDroppedFileHandler fileHandler;
private final CustomLocalDragboard localDragboard = GUIGlobals.localDragboard;


public MainTable(MainTableDataModel model, JabRefFrame frame,
BasePanel panel, BibDatabaseContext database,
MainTablePreferences preferences, ExternalFileTypes externalFileTypes, KeyBindingRepository keyBindingRepository) {
Expand All @@ -83,6 +82,7 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
);

this.getColumns().addAll(new MainTableColumnFactory(database, preferences.getColumnPreferences(), externalFileTypes, panel.getUndoManager(), frame.getDialogService()).createColumns());

new ViewModelTableRowFactory<BibEntryTableViewModel>()
.withOnMouseClickedEvent((entry, event) -> {
if (event.getClickCount() == 2) {
Expand Down Expand Up @@ -114,6 +114,12 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,

this.pane.getStylesheets().add(MainTable.class.getResource("MainTable.css").toExternalForm());

//Set sort order column from preferences, currently only single column suported
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is only one column supported? You already transverse all columns here and set the right sort order, or do I miss something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I learned from the SO answer, with shift it's possible to add multiple columns to the sort order.
However, currently only one column is stored, e.g. if you have both author DESCENDING and timestamp ASCENDING, it will only store the first one. Or do we have an elegant solution to store Key-Value Pairs in the preferences? e.g. a list of columns with sort type.

Copy link
Member

@tobiasdiez tobiasdiez Sep 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standard way is to have two lists, one for the keys and one for the values. In the progress, it would be nice if you would add the corresponding wrapper methods (get/putMap) in JabRefPreferences for reuse in the future.

this.getColumns().forEach(col -> preferences.getColumnPreferences().getSortTypeForColumn(col.getText()).ifPresent(sortType -> {
this.getSortOrder().add(col);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this call this.getSortOrder().add(col); really necessary? I thought the TableView manages the sort order list on its own.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I found it out while testing. The sort oder defines the column which are considered to be sorting.
The sortType property only has an influence if the column is the sortOrder

col.setSortType(sortType);
}));

// Store visual state
new PersistenceVisualStateTable(this, Globals.prefs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public MainTableColumnFactory(BibDatabaseContext database, ColumnPreferences pre
new ValueTableCellFactory<BibEntryTableViewModel, List<AbstractGroup>>()
.withGraphic(this::createGroupColorRegion)
.install(column);
column.setSortable(true);
return column;
}

Expand Down Expand Up @@ -156,6 +157,7 @@ private Node createGroupColorRegion(BibEntryTableViewModel entry, List<AbstractG
new ValueTableCellFactory<BibEntryTableViewModel, String>()
.withText(text -> text)
.install(column);
column.setSortable(true);
column.setPrefWidth(preferences.getPrefColumnWidth(columnName));
columns.add(column);
}
Expand Down Expand Up @@ -199,6 +201,7 @@ private TableColumn<BibEntryTableViewModel, Optional<SpecialFieldValueViewModel>
column.setComparator(new RankingFieldComparator());
}

column.setSortable(true);
return column;
}

Expand Down Expand Up @@ -374,7 +377,6 @@ private TableColumn<BibEntryTableViewModel, List<LinkedFile>> createExtraFileCol
new ValueTableCellFactory<BibEntryTableViewModel, List<LinkedFile>>()
.withGraphic(linkedFiles -> createFileIcon(linkedFiles.stream().filter(linkedFile -> linkedFile.getFileType().equalsIgnoreCase(externalFileTypeName)).collect(Collectors.toList())))
.install(column);

return column;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/maintable/NormalTableColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ObservableValue<String> getColumnValue(BibEntryTableViewModel entry) {
return null;
}

ObjectBinding[] dependencies = bibtexFields.stream().map(entry::getField).toArray(ObjectBinding[]::new);
ObjectBinding<String>[] dependencies = bibtexFields.stream().map(entry::getField).toArray(ObjectBinding[]::new);
return Bindings.createStringBinding(() -> computeText(entry), dependencies);
}

Expand All @@ -99,7 +99,7 @@ private String computeText(BibEntryTableViewModel entry) {
result = toUnicode.format(MainTableNameFormatter.formatName(result));
}

if (result != null && !bibtexFields.contains(BibEntry.KEY_FIELD)) {
if ((result != null) && !bibtexFields.contains(BibEntry.KEY_FIELD)) {
result = toUnicode.format(result).trim();
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public PersistenceVisualStateTable(final MainTable mainTable, JabRefPreferences
this.preferences = preferences;

mainTable.getColumns().addListener(this::onColumnsChanged);
mainTable.getColumns().forEach(col -> col.sortTypeProperty().addListener(observable -> preferences.setMainTableColumnSortOrder(col.getText(), col.getSortType().name())));
}

private void onColumnsChanged(ListChangeListener.Change<? extends TableColumn<BibEntryTableViewModel, ?>> change) {
Expand All @@ -33,6 +34,7 @@ private void onColumnsChanged(ListChangeListener.Change<? extends TableColumn<Bi
if (changed) {
updateColumnPreferences();
}

}

/**
Expand All @@ -41,13 +43,16 @@ private void onColumnsChanged(ListChangeListener.Change<? extends TableColumn<Bi
private void updateColumnPreferences() {
List<String> columnNames = new ArrayList<>();
List<String> columnsWidths = new ArrayList<>();
List<String> columnSortOrders = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it's not used...


for (TableColumn<BibEntryTableViewModel, ?> column : mainTable.getColumns()) {
if (column instanceof NormalTableColumn) {
NormalTableColumn normalColumn = (NormalTableColumn) column;

columnNames.add(normalColumn.getColumnName());
columnsWidths.add(String.valueOf(normalColumn.getWidth()));
columnSortOrders.add(normalColumn.getSortType().name());

}
}

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public class JabRefPreferences implements PreferencesService {
public static final String NEWLINE = "newline";
public static final String COLUMN_WIDTHS = "columnWidths";
public static final String COLUMN_NAMES = "columnNames";
public static final String SORT_COLUMN = "columnSortOrders";

public static final String SIDE_PANE_COMPONENT_PREFERRED_POSITIONS = "sidePaneComponentPreferredPositions";
public static final String SIDE_PANE_COMPONENT_NAMES = "sidePaneComponentNames";
public static final String XMP_PRIVACY_FILTERS = "xmpPrivacyFilters";
Expand Down Expand Up @@ -536,6 +538,8 @@ private JabRefPreferences() {

defaults.put(COLUMN_NAMES, "entrytype;author/editor;title;year;journal/booktitle;bibtexkey");
defaults.put(COLUMN_WIDTHS, "75;300;470;60;130;100");
defaults.put(SORT_COLUMN, "title;ASCENDING");

defaults.put(XMP_PRIVACY_FILTERS, "pdf;timestamp;keywords;owner;note;review");
defaults.put(USE_XMP_PRIVACY_FILTER, Boolean.FALSE);
defaults.put(WORKING_DIRECTORY, USER_HOME);
Expand Down Expand Up @@ -1879,7 +1883,9 @@ public ColumnPreferences getColumnPreferences() {
getStringList(COLUMN_NAMES),
createSpecialFieldColumns(),
createExtraFileColumns(),
createColumnWidths());
createColumnWidths(),
getMainTableColumnSortOrder());

}

public MainTablePreferences getMainTablePreferences() {
Expand Down Expand Up @@ -1955,4 +1961,13 @@ public void setIdBasedFetcherForEntryGenerator(String fetcherName) {
public String getIdBasedFetcherForEntryGenerator() {
return get(ID_ENTRY_GENERATOR);
}

public void setMainTableColumnSortOrder(String column, String sortType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accept SortOrder and not a string as second argument.

putStringList(SORT_COLUMN, Arrays.asList(column, sortType));
}

public List<String> getMainTableColumnSortOrder() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used?

return getStringList(SORT_COLUMN);
}

}