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,21 +7,30 @@
<?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?>
<?import javafx.scene.layout.VBox?>
<DialogPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="600.0" prefWidth="950.0"
xmlns="http://javafx.com/javafx/8.0.121" fx:controller="org.jabref.gui.preferences.PreferencesFilterDialog">

<content>
<BorderPane>
<bottom>
<HBox>
<CheckBox fx:id="showOnlyDeviatingPreferenceOptions"
text="%Show only preferences deviating from their default value"/>
<HBox HBox.hgrow="ALWAYS"/>
<HBox alignment="BOTTOM_RIGHT">
<Label fx:id="count"/>
</HBox>
</bottom>
<top>
<HBox spacing="20">
<TextField fx:id="searchField" promptText="%Search"/>
<VBox alignment="CENTER">
<CheckBox fx:id="showOnlyDeviatingPreferenceOptions"
text="%Show only preferences deviating from their default value" alignment="bottom_right"/>
</VBox>
<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,16 +1,20 @@
package org.jabref.gui.preferences;

import java.util.Locale;
import java.util.Objects;

import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
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 +26,7 @@ public class PreferencesFilterDialog extends BaseDialog<Void> {

private final JabRefPreferencesFilter preferencesFilter;
private final ObservableList<JabRefPreferencesFilter.PreferenceOption> preferenceOptions;
private final FilteredList<JabRefPreferencesFilter.PreferenceOption> filteredOptions;

@FXML private TableView<JabRefPreferencesFilter.PreferenceOption> table;
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, JabRefPreferencesFilter.PreferenceType> columnType;
Expand All @@ -30,10 +35,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.filteredOptions = new FilteredList<>(this.preferenceOptions);

ViewLoader.view(this)
.load()
Expand All @@ -45,11 +52,20 @@ public PreferencesFilterDialog(JabRefPreferencesFilter preferencesFilter) {
@FXML
private void initialize() {
showOnlyDeviatingPreferenceOptions.setOnAction(event -> updateModel());
filteredOptions.predicateProperty().bind(Bindings.createObjectBinding(() -> {
String searchText = searchField.getText();
if ((searchText == null) || searchText.isEmpty()) {
return null;
}
String lowerCaseSearchText = searchText.toLowerCase(Locale.ROOT);
return (option) -> option.getKey().toLowerCase(Locale.ROOT).contains(lowerCaseSearchText);
}, searchField.textProperty()));
columnType.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getType()));
columnKey.setCellValueFactory(data -> new ReadOnlyStringWrapper(data.getValue().getKey()));
columnValue.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getValue()));
columnDefaultValue.setCellValueFactory(data -> new ReadOnlyObjectWrapper<>(data.getValue().getDefaultValue().orElse("")));
table.setItems(preferenceOptions);
table.setItems(filteredOptions);
count.textProperty().bind(Bindings.size(table.getItems()).asString("(%d)"));
updateModel();
}

Expand All @@ -59,6 +75,6 @@ private void updateModel() {
} else {
preferenceOptions.setAll(preferencesFilter.getPreferenceOptions());
}
count.setText(String.format("(%d)", preferenceOptions.size()));
}

}