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 @@ -1020,7 +1020,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
10 changes: 9 additions & 1 deletion src/main/java/org/jabref/gui/maintable/ColumnPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import java.util.Map;

import javafx.scene.control.TableColumn.SortType;

import org.jabref.model.entry.BibtexSingleField;
import org.jabref.model.entry.specialfields.SpecialField;

Expand All @@ -16,8 +18,9 @@ public class ColumnPreferences {
private final List<SpecialField> specialFieldColumns;
private final List<String> extraFileColumns;
private final Map<String, Double> columnWidths;
private final Map<String, SortType> columnSortType;

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, Map<String, SortType> columnSortType) {
this.showFileColumn = showFileColumn;
this.showUrlColumn = showUrlColumn;
this.preferDoiOverUrl = preferDoiOverUrl;
Expand All @@ -26,6 +29,7 @@ public ColumnPreferences(boolean showFileColumn, boolean showUrlColumn, boolean
this.specialFieldColumns = specialFieldColumns;
this.extraFileColumns = extraFileColumns;
this.columnWidths = columnWidths;
this.columnSortType = columnSortType;
}

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

public Map<String, SortType> getSortTypesForColumns() {
return columnSortType;
}
}
102 changes: 14 additions & 88 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -13,6 +14,8 @@
import javafx.collections.ListChangeListener;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.SortType;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
Expand Down Expand Up @@ -62,7 +65,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 @@ -73,14 +75,15 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
this.undoManager = panel.getUndoManager();

fileHandler = new NewDroppedFileHandler(frame.getDialogService(), database, externalFileTypes,
Globals.prefs.getFilePreferences(),
Globals.prefs.getFilePreferences(),
Globals.prefs.getImportFormatPreferences(),
Globals.prefs.getUpdateFieldPreferences(),
Globals.getFileUpdateMonitor()
Globals.getFileUpdateMonitor()

);

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 All @@ -94,13 +97,20 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
.setOnMouseDragEntered(this::handleOnDragEntered)
.install(this);

for (Entry<String, SortType> entries : preferences.getColumnPreferences().getSortTypesForColumns().entrySet()) {
Optional<TableColumn<BibEntryTableViewModel, ?>> column = this.getColumns().stream().filter(col -> entries.getKey().equals(col.getText())).findFirst();
column.ifPresent(col -> {
col.setSortType(entries.getValue());
this.getSortOrder().add(col);
});
}

if (preferences.resizeColumnsToFit()) {
this.setColumnResizePolicy(new SmartConstrainedResizePolicy());
}
this.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

this.setItems(model.getEntriesFilteredAndSorted());

// Enable sorting
model.getEntriesFilteredAndSorted().comparatorProperty().bind(this.comparatorProperty());

Expand All @@ -115,12 +125,6 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
// Store visual state
new PersistenceVisualStateTable(this, Globals.prefs);

// TODO: enable DnD
//setDragEnabled(true);
//TransferHandler xfer = new EntryTableTransferHandler(this, frame, panel);
//setTransferHandler(xfer);
//pane.setTransferHandler(xfer);

// TODO: Float marked entries
//model.updateMarkingState(Globals.prefs.getBoolean(JabRefPreferences.FLOAT_MARKED_ENTRIES));

Expand Down Expand Up @@ -348,84 +352,6 @@ public List<BibEntry> getSelectedEntries() {
.collect(Collectors.toList());
}

/**
* This method sets up what Comparators are used for the various table columns.
* The ComparatorChooser enables and disables such Comparators as the user clicks
* columns, but this is where the Comparators are defined. Also, the ComparatorChooser
* is initialized with the sort order defined in Preferences.
*/
private void setupComparatorChooser() {
// TODO: Proper sorting

/*
// Set initial sort columns:

// Default sort order:
String[] sortFields = new String[] {
Globals.prefs.get(JabRefPreferences.TABLE_PRIMARY_SORT_FIELD),
Globals.prefs.get(JabRefPreferences.TABLE_SECONDARY_SORT_FIELD),
Globals.prefs.get(JabRefPreferences.TABLE_TERTIARY_SORT_FIELD)
};
boolean[] sortDirections = new boolean[] {
Globals.prefs.getBoolean(JabRefPreferences.TABLE_PRIMARY_SORT_DESCENDING),
Globals.prefs.getBoolean(JabRefPreferences.TABLE_SECONDARY_SORT_DESCENDING),
Globals.prefs.getBoolean(JabRefPreferences.TABLE_TERTIARY_SORT_DESCENDING)
}; // descending

model.getSortedForUserDefinedTableColumnSorting().getReadWriteLock().writeLock().lock();
try {
for (int i = 0; i < sortFields.length; i++) {
int index = -1;

// TODO where is this prefix set?
// if (!sortFields[i].startsWith(MainTableFormat.ICON_COLUMN_PREFIX))
if (sortFields[i].startsWith("iconcol:")) {
for (int j = 0; j < tableFormat.getColumnCount(); j++) {
if (sortFields[i].equals(tableFormat.getColumnName(j))) {
index = j;
break;
}
}
} else {
index = tableFormat.getColumnIndex(sortFields[i]);
}
if (index >= 0) {
comparatorChooser.appendComparator(index, 0, sortDirections[i]);
}
}
} finally {
model.getSortedForUserDefinedTableColumnSorting().getReadWriteLock().writeLock().unlock();
}

// Add action listener so we can remember the sort order:
comparatorChooser.addSortActionListener(e -> {
// Get the information about the current sort order:
List<String> fields = getCurrentSortFields();
List<Boolean> order = getCurrentSortOrder();
// Update preferences:
int count = Math.min(fields.size(), order.size());
if (count >= 1) {
Globals.prefs.put(JabRefPreferences.TABLE_PRIMARY_SORT_FIELD, fields.get(0));
Globals.prefs.putBoolean(JabRefPreferences.TABLE_PRIMARY_SORT_DESCENDING, order.get(0));
}
if (count >= 2) {
Globals.prefs.put(JabRefPreferences.TABLE_SECONDARY_SORT_FIELD, fields.get(1));
Globals.prefs.putBoolean(JabRefPreferences.TABLE_SECONDARY_SORT_DESCENDING, order.get(1));
} else {
Globals.prefs.put(JabRefPreferences.TABLE_SECONDARY_SORT_FIELD, "");
Globals.prefs.putBoolean(JabRefPreferences.TABLE_SECONDARY_SORT_DESCENDING, false);
}
if (count >= 3) {
Globals.prefs.put(JabRefPreferences.TABLE_TERTIARY_SORT_FIELD, fields.get(2));
Globals.prefs.putBoolean(JabRefPreferences.TABLE_TERTIARY_SORT_DESCENDING, order.get(2));
} else {
Globals.prefs.put(JabRefPreferences.TABLE_TERTIARY_SORT_FIELD, "");
Globals.prefs.putBoolean(JabRefPreferences.TABLE_TERTIARY_SORT_DESCENDING, false);
}
});
*/
}

private Optional<BibEntryTableViewModel> findEntry(BibEntry entry) {
return model.getEntriesFilteredAndSorted()
.stream()
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
@@ -1,10 +1,13 @@
package org.jabref.gui.maintable;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javafx.collections.ListChangeListener;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.SortType;

import org.jabref.preferences.JabRefPreferences;

Expand All @@ -16,12 +19,15 @@ public class PersistenceVisualStateTable {

private final MainTable mainTable;
private final JabRefPreferences preferences;
private final Map<String, SortType> columnsSortOrder = new LinkedHashMap<>();

public PersistenceVisualStateTable(final MainTable mainTable, JabRefPreferences preferences) {
this.mainTable = mainTable;
this.preferences = preferences;

mainTable.getColumns().addListener(this::onColumnsChanged);
mainTable.getColumns().forEach(col -> col.sortTypeProperty().addListener(obs -> updateColumnSortType(col.getText(), col.getSortType())));

}

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

}

private void updateColumnSortType(String text, SortType sortType) {
columnsSortOrder.put(text, sortType);
preferences.setMainTableColumnSortType(columnsSortOrder);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;


/**
* Preference tab for file sorting options.
*/
Expand All @@ -26,7 +25,7 @@ class ExportSortingPrefsTab extends Pane implements PrefsTab {
private final RadioButton exportInTableOrder;
private final RadioButton exportInSpecifiedOrder;
private final SaveOrderConfigDisplay exportOrderPanel;
private final GridPane builder = new GridPane();
private final GridPane builder = new GridPane();

public ExportSortingPrefsTab(JabRefPreferences prefs) {
this.prefs = prefs;
Expand All @@ -38,9 +37,9 @@ public ExportSortingPrefsTab(JabRefPreferences prefs) {

exportOrderPanel = new SaveOrderConfigDisplay();

EventHandler<ActionEvent> listener = (event) -> {
boolean selected = event.getSource() == exportInSpecifiedOrder;
exportOrderPanel.setEnabled(selected);
EventHandler<ActionEvent> listener = (event) -> {
boolean selected = event.getSource() == exportInSpecifiedOrder;
exportOrderPanel.setEnabled(selected);
};

exportInOriginalOrder.setOnAction(listener);
Expand All @@ -52,9 +51,9 @@ public ExportSortingPrefsTab(JabRefPreferences prefs) {
// create GUI
builder.add(exportSortOrder, 1, 1);
builder.add(new Separator(), 2, 1);
builder.add(exportInOriginalOrder, 1, 2);
builder.add(exportInOriginalOrder, 1, 2);
builder.add(new Line(), 2, 3);
builder.add(exportInTableOrder, 1, 4);
builder.add(exportInTableOrder, 1, 4);
builder.add(new Line(), 2, 5);
builder.add(exportInSpecifiedOrder, 1, 6);
builder.add(new Line(), 2, 7);
Expand All @@ -64,6 +63,7 @@ public ExportSortingPrefsTab(JabRefPreferences prefs) {

}

@Override
public Node getBuilder() {
return builder;
}
Expand Down
Loading