-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 1 commit
74048b6
897c612
e377b73
d7e1f59
9d9d172
259626c
a7a7e79
882aa6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
||
@FXML private TableView<JabRefPreferencesFilter.PreferenceOption> table; | ||
@FXML private TableColumn<JabRefPreferencesFilter.PreferenceOption, JabRefPreferencesFilter.PreferenceType> columnType; | ||
|
@@ -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() | ||
|
@@ -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())); | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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:jabref/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java
Lines 29 to 33 in b39ed7b
There was a problem hiding this comment.
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.