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

Do not resize main table columns in search dialog window #8253

Merged
merged 19 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jabref.gui.maintable;

import javafx.beans.InvalidationListener;
import javafx.scene.control.TableView;

import org.jabref.gui.maintable.columns.MainTableColumn;
import org.jabref.preferences.PreferencesService;

/**
* Keep track of changes made to the columns (reordering, resorting, resizing).
*/
public abstract class AbstractPersistenceVisualStateTable {

protected final TableView<BibEntryTableViewModel> mainTable;
protected final PreferencesService preferences;

public AbstractPersistenceVisualStateTable(final TableView<BibEntryTableViewModel> mainTable, PreferencesService preferences) {
this.mainTable = mainTable;
this.preferences = preferences;

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

/**
* Stores shown columns, their width and their sortType in preferences.
* override in subclass
*/
protected abstract void updateColumns();

/**
* Stores the SortOrder of 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.
* override in subclass
*/
protected abstract void updateSortOrder();
}
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.maintable.columns.LibraryColumn;
import org.jabref.gui.maintable.columns.MainTableColumn;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.CustomLocalDragboard;
Expand Down Expand Up @@ -101,11 +102,14 @@ public MainTable(MainTableDataModel model,
new MainTableColumnFactory(
database,
preferencesService,
preferencesService.getColumnPreferences(),
externalFileTypes,
libraryTab.getUndoManager(),
dialogService,
stateManager).createColumns());

this.getColumns().removeIf(col -> col instanceof LibraryColumn);

new ViewModelTableRowFactory<BibEntryTableViewModel>()
.withOnMouseClickedEvent((entry, event) -> {
if (event.getClickCount() == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.maintable.columns.FieldColumn;
import org.jabref.gui.maintable.columns.FileColumn;
import org.jabref.gui.maintable.columns.LibraryColumn;
import org.jabref.gui.maintable.columns.LinkedIdentifierColumn;
import org.jabref.gui.maintable.columns.MainTableColumn;
import org.jabref.gui.maintable.columns.SpecialFieldColumn;
Expand Down Expand Up @@ -62,13 +63,14 @@ public class MainTableColumnFactory {

public MainTableColumnFactory(BibDatabaseContext database,
PreferencesService preferencesService,
ColumnPreferences abstractColumnPrefs,
ExternalFileTypes externalFileTypes,
UndoManager undoManager,
DialogService dialogService,
StateManager stateManager) {
this.database = Objects.requireNonNull(database);
this.preferencesService = Objects.requireNonNull(preferencesService);
this.columnPreferences = preferencesService.getColumnPreferences();
this.columnPreferences = abstractColumnPrefs;
this.externalFileTypes = Objects.requireNonNull(externalFileTypes);
this.dialogService = dialogService;
this.cellFactory = new CellFactory(externalFileTypes, preferencesService, undoManager);
Expand All @@ -94,6 +96,9 @@ public MainTableColumnFactory(BibDatabaseContext database,
case LINKED_IDENTIFIER:
columns.add(createIdentifierColumn(column));
break;
case LIBRARY_NAME:
columns.add(createLibraryColumn(column));
break;
case EXTRAFILE:
if (!column.getQualifier().isBlank()) {
columns.add(createExtraFileColumn(column));
Expand Down Expand Up @@ -244,4 +249,11 @@ private TableColumn<BibEntryTableViewModel, List<LinkedFile>> createExtraFileCol
preferencesService,
columnModel.getQualifier());
}

/**
* Create library column containing the Filename of the library's bib file
*/
private TableColumn<BibEntryTableViewModel, String> createLibraryColumn(MainTableColumnModel columnModel) {
return new LibraryColumn(columnModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public enum Type {
GROUPS("groups", Localization.lang("Groups")),
LINKED_IDENTIFIER("linked_id", Localization.lang("Linked identifiers")),
NORMALFIELD("field"),
SPECIALFIELD("special", Localization.lang("Special"));
SPECIALFIELD("special", Localization.lang("Special")),
LIBRARY_NAME("library", Localization.lang("Library"));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,28 @@

import java.util.stream.Collectors;

import javafx.beans.InvalidationListener;
import javafx.scene.control.TableView;

import org.jabref.gui.maintable.columns.MainTableColumn;
import org.jabref.preferences.PreferencesService;

/**
* Keep track of changes made to the columns (reordering, resorting, resizing).
*/
public class PersistenceVisualStateTable {
public class PersistenceVisualStateTable extends AbstractPersistenceVisualStateTable {

private final MainTable mainTable;
private final PreferencesService preferences;

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

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 -> updateColumns()));
mainTable.getColumns().forEach(col ->
((MainTableColumn<?>) col).getModel().sortTypeProperty().addListener(obs -> updateColumns()));
public PersistenceVisualStateTable(final TableView<BibEntryTableViewModel> mainTable, PreferencesService preferences) {
super(mainTable, preferences);
}

/**
* Stores shown columns, their width and their sortType in preferences.
*/
private void updateColumns() {
preferences.storeColumnPreferences(new ColumnPreferences(
@Override
protected void updateColumns() {
preferences.storeMainTableColumnPreferences(new ColumnPreferences(
mainTable.getColumns().stream()
.filter(col -> col instanceof MainTableColumn<?>)
.map(column -> ((MainTableColumn<?>) column).getModel())
.collect(Collectors.toList()),
preferences.getColumnPreferences().getColumnSortOrder()));
Expand All @@ -45,10 +33,12 @@ private void updateColumns() {
* 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(
@Override
protected void updateSortOrder() {
preferences.storeMainTableColumnPreferences(new ColumnPreferences(
preferences.getColumnPreferences().getColumns(),
mainTable.getSortOrder().stream()
.filter(col -> col instanceof MainTableColumn<?>)
.map(column -> ((MainTableColumn<?>) column).getModel())
.collect(Collectors.toList())));
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/jabref/gui/maintable/columns/LibraryColumn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jabref.gui.maintable.columns;

import org.jabref.gui.maintable.BibEntryTableViewModel;
import org.jabref.gui.maintable.MainTableColumnModel;
import org.jabref.gui.maintable.MainTableColumnModel.Type;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;

public class LibraryColumn extends MainTableColumn<String> {

public LibraryColumn(MainTableColumnModel model) {
super(model);

setText(Localization.lang("Library"));
new ValueTableCellFactory<BibEntryTableViewModel, String>().withText(FileUtil::getBaseName)
.install(this);
setCellValueFactory(param -> param.getValue().bibDatabaseContextProperty());
}

public LibraryColumn() {
this(new MainTableColumnModel(Type.LIBRARY_NAME));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class LinkedIdentifierColumn extends MainTableColumn<Map<Field, String>>
private final CellFactory cellFactory;
private final DialogService dialogService;
private final PreferencesService preferences;
private final StateManager stateManager;

public LinkedIdentifierColumn(MainTableColumnModel model,
CellFactory cellFactory,
Expand All @@ -48,7 +47,6 @@ public LinkedIdentifierColumn(MainTableColumnModel model,
this.cellFactory = cellFactory;
this.dialogService = dialogService;
this.preferences = preferences;
this.stateManager = stateManager;

Node headerGraphic = IconTheme.JabRefIcons.WWW.getGraphicNode();
Tooltip.install(headerGraphic, new Tooltip(Localization.lang("Linked identifiers")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.jabref.gui.maintable.ColumnPreferences;
import org.jabref.gui.maintable.MainTableColumnModel;
import org.jabref.gui.maintable.MainTableNameFormatPreferences;
import org.jabref.gui.maintable.MainTableNameFormatPreferences.AbbreviationStyle;
import org.jabref.gui.maintable.MainTableNameFormatPreferences.DisplayStyle;
import org.jabref.gui.maintable.MainTablePreferences;
import org.jabref.gui.preferences.PreferenceTabViewModel;
import org.jabref.gui.specialfields.SpecialFieldsPreferences;
Expand All @@ -34,9 +36,6 @@
import de.saxsys.mvvmfx.utils.validation.ValidationStatus;
import de.saxsys.mvvmfx.utils.validation.Validator;

import static org.jabref.gui.maintable.MainTableNameFormatPreferences.AbbreviationStyle;
import static org.jabref.gui.maintable.MainTableNameFormatPreferences.DisplayStyle;

public class TableTabViewModel implements PreferenceTabViewModel {

static StringConverter<MainTableColumnModel> columnNameStringConverter = new StringConverter<>() {
Expand Down Expand Up @@ -210,7 +209,7 @@ public void removeColumn(MainTableColumnModel column) {
public void moveColumnUp() {
MainTableColumnModel selectedColumn = selectedColumnModelProperty.getValue().getSelectedItem();
int row = columnsListProperty.getValue().indexOf(selectedColumn);
if (selectedColumn == null || row < 1) {
if ((selectedColumn == null) || (row < 1)) {
return;
}

Expand All @@ -222,7 +221,7 @@ public void moveColumnUp() {
public void moveColumnDown() {
MainTableColumnModel selectedColumn = selectedColumnModelProperty.getValue().getSelectedItem();
int row = columnsListProperty.getValue().indexOf(selectedColumn);
if (selectedColumn == null || row > columnsListProperty.getValue().size() - 2) {
if ((selectedColumn == null) || (row > (columnsListProperty.getValue().size() - 2))) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import javafx.fxml.FXML;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.ToggleButton;
import javafx.stage.Modality;
import javafx.stage.Stage;
Expand All @@ -14,13 +13,9 @@
import org.jabref.gui.StateManager;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.maintable.BibEntryTableViewModel;
import org.jabref.gui.maintable.columns.SpecialFieldColumn;
import org.jabref.gui.preview.PreviewViewer;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.views.ViewLoader;
Expand Down Expand Up @@ -58,14 +53,9 @@ private void initialize() {
previewViewer.setTheme(preferencesService.getTheme());
previewViewer.setLayout(preferencesService.getPreviewPreferences().getCurrentPreviewStyle());

TableColumn<BibEntryTableViewModel, String> fieldColumn = new TableColumn<>(Localization.lang("Library"));
new ValueTableCellFactory<BibEntryTableViewModel, String>().withText(FileUtil::getBaseName)
.install(fieldColumn);
fieldColumn.setCellValueFactory(param -> param.getValue().bibDatabaseContextProperty());

SearchResultsTableDataModel model = new SearchResultsTableDataModel(viewModel.getSearchDatabaseContext(), preferencesService, stateManager);
SearchResultsTable resultsTable = new SearchResultsTable(model, viewModel.getSearchDatabaseContext(), preferencesService, undoManager, dialogService, stateManager, externalFileTypes);
resultsTable.getColumns().add(0, fieldColumn);

resultsTable.getColumns().removeIf(col -> col instanceof SpecialFieldColumn);
resultsTable.getSelectionModel().selectFirst();
resultsTable.getSelectionModel().selectedItemProperty().addListener((obs, old, newValue) -> {
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/org/jabref/gui/search/SearchResultsTable.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.jabref.gui.search;

import java.util.List;

import javax.swing.undo.UndoManager;

import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import org.jabref.gui.DialogService;
Expand All @@ -13,6 +16,7 @@
import org.jabref.gui.maintable.MainTableColumnFactory;
import org.jabref.gui.maintable.MainTablePreferences;
import org.jabref.gui.maintable.SmartConstrainedResizePolicy;
import org.jabref.gui.maintable.columns.LibraryColumn;
import org.jabref.gui.maintable.columns.MainTableColumn;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.preferences.PreferencesService;
Expand All @@ -30,16 +34,22 @@ public SearchResultsTable(SearchResultsTableDataModel model,

MainTablePreferences mainTablePreferences = preferencesService.getMainTablePreferences();

this.getColumns().addAll(new MainTableColumnFactory(
database,
preferencesService,
externalFileTypes,
undoManager,
dialogService,
stateManager).createColumns());
List<TableColumn<BibEntryTableViewModel, ?>> allCols = new MainTableColumnFactory(
database,
preferencesService,
preferencesService.getSearchDialogColumnPreferences(),
externalFileTypes,
undoManager,
dialogService,
stateManager).createColumns();

if (!allCols.stream().anyMatch(col -> col instanceof LibraryColumn)) {
allCols.add(0, new LibraryColumn());
}
this.getColumns().addAll(allCols);

this.getSortOrder().clear();
mainTablePreferences.getColumnPreferences().getColumnSortOrder().forEach(columnModel ->
preferencesService.getSearchDialogColumnPreferences().getColumnSortOrder().forEach(columnModel ->
this.getColumns().stream()
.map(column -> (MainTableColumn<?>) column)
.filter(column -> column.getModel().equals(columnModel))
Expand All @@ -56,6 +66,10 @@ public SearchResultsTable(SearchResultsTableDataModel model,
model.getEntriesFilteredAndSorted().comparatorProperty().bind(this.comparatorProperty());

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

// Store visual state
new SearchResultsTablePersistenceVisualState(this, preferencesService);

database.getDatabase().registerListener(this);
}
}
Expand Down
Loading