Skip to content

Commit

Permalink
Feature: implement search filter in show preferences (#4759)
Browse files Browse the repository at this point in the history
* Feature: implement search filter in show preferences

Add a search box in show preferences window to allow users to filter
preference options.

Resolves #1019

* Add Locale.ROOT when converting to lower case

* Fixed indentation for fxml

* Added Locale.ROOT when converting to lower case

* Using FilteredList to filter options on search

* set the count label text, which was accidently removed

* Moved checkbox to the top

* Bind count label to table size property
  • Loading branch information
CaptainDaVinci authored and tobiasdiez committed Mar 15, 2019
1 parent 7ed15bc commit cd979c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
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()));
}

}

0 comments on commit cd979c5

Please sign in to comment.