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

Feature: implement search filter in show preferences #4759

Merged
merged 8 commits into from
Mar 15, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<DialogPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="600.0" prefWidth="950.0"
Expand All @@ -22,6 +23,12 @@
<Label fx:id="count"/>
</HBox>
</bottom>
<top>
<HBox>
<TextField fx:id="searchField" promptText="Search"/>
<HBox HBox.hgrow="ALWAYS"/>
</HBox>
</top>
<center>
<TableView fx:id="table">
<columns>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jabref.gui.preferences;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
Expand All @@ -11,6 +13,7 @@
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

import org.jabref.gui.util.BaseDialog;
import org.jabref.logic.l10n.Localization;
Expand All @@ -22,6 +25,7 @@ public class PreferencesFilterDialog extends BaseDialog<Void> {

private final JabRefPreferencesFilter preferencesFilter;
private final ObservableList<JabRefPreferencesFilter.PreferenceOption> preferenceOptions;
private final List<JabRefPreferencesFilter.PreferenceOption> auxillaryPreferenceOptions;
Copy link
Member

Choose a reason for hiding this comment

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

If you use the FilteredList, then the code gets somewhat cleaner (as you don't need to manage two lists on your own). Have a look here:

entriesFiltered = new FilteredList<>(entriesViewModel);
entriesFiltered.predicateProperty().bind(
Bindings.createObjectBinding(() -> this::isMatched,
Globals.stateManager.activeGroupProperty(), Globals.stateManager.activeSearchQueryProperty())
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestions! I have made the necessary changes.


@FXML private TableView<JabRefPreferencesFilter.PreferenceOption> table;
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, JabRefPreferencesFilter.PreferenceType> columnType;
Expand All @@ -30,10 +34,12 @@ public class PreferencesFilterDialog extends BaseDialog<Void> {
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, Object> columnDefaultValue;
@FXML private CheckBox showOnlyDeviatingPreferenceOptions;
@FXML private Label count;
@FXML private TextField searchField;

public PreferencesFilterDialog(JabRefPreferencesFilter preferencesFilter) {
this.preferencesFilter = Objects.requireNonNull(preferencesFilter);
this.preferenceOptions = FXCollections.observableArrayList();
this.auxillaryPreferenceOptions = preferencesFilter.getPreferenceOptions();

ViewLoader.view(this)
.load()
Expand All @@ -45,6 +51,7 @@ public PreferencesFilterDialog(JabRefPreferencesFilter preferencesFilter) {
@FXML
private void initialize() {
showOnlyDeviatingPreferenceOptions.setOnAction(event -> updateModel());
searchField.textProperty().addListener((observable, previousText, newText) -> filterPreferences(newText.toLowerCase()));
columnType.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getType()));
columnKey.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().getKey()));
columnValue.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getValue()));
Expand All @@ -54,11 +61,30 @@ private void initialize() {
}

private void updateModel() {
auxillaryPreferenceOptions.clear();
if (showOnlyDeviatingPreferenceOptions.isSelected()) {
preferenceOptions.setAll(preferencesFilter.getDeviatingPreferences());
auxillaryPreferenceOptions.addAll(preferencesFilter.getDeviatingPreferences());
} else {
preferenceOptions.setAll(preferencesFilter.getPreferenceOptions());
auxillaryPreferenceOptions.addAll(preferencesFilter.getPreferenceOptions());
}
preferenceOptions.setAll(auxillaryPreferenceOptions);
count.setText(String.format("(%d)", preferenceOptions.size()));
String searchText = searchField.getText();
if (!searchText.isEmpty()) {
filterPreferences(searchText);
}
}

private void filterPreferences(String searchText) {
if (searchText.isEmpty()) {
updateModel();
return;
}

List<JabRefPreferencesFilter.PreferenceOption> filteredOptions = auxillaryPreferenceOptions.stream()
.filter(p -> p.getKey().toLowerCase().contains(searchText))
Copy link
Member

Choose a reason for hiding this comment

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

Better use LOCALE.ROOT as second parameter for the toLowerCase function to avoid unexpected problems:
https://garygregory.wordpress.com/2015/11/03/java-lowercase-conversion-turkey/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood, I have made the necessary changes.

.collect(Collectors.toList());
preferenceOptions.setAll(filteredOptions);
}

}