Skip to content

Commit

Permalink
Initial draf of abbreviation search feature
Browse files Browse the repository at this point in the history
Implemented a string filter for the journal abbreviations tab.
Not working. Performing a search currently cleares a list of filtered
abbreviations and fills it back up with abbreviations that contain the
search string. Right now the clearing not only cleares the filtered list
but also the list of all abbreviations and I don't unterstand why.

Issue JabRef#7751
  • Loading branch information
btut committed Jun 7, 2021
1 parent fa8cc82 commit 90cbc2f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(getName(), isPseudoAbbreviation());
}

public boolean contains(String s) {
if (this.abbreviation.get().toLowerCase().contains(s)) {
return true;
}
if (this.name.get().toLowerCase().contains(s)) {
return true;
}
if (this.shortestUniqueAbbreviation.get().toLowerCase().contains(s)) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import org.controlsfx.control.textfield.CustomTextField?>
<?import javafx.geometry.Insets?>
<fx:root spacing="10.0" type="VBox"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="org.jabref.gui.preferences.journals.JournalAbbreviationsTab">
Expand Down Expand Up @@ -64,4 +66,9 @@
<ProgressIndicator fx:id="progressIndicator" maxHeight="30.0" opacity="0.4"/>
</placeholder>
</TableView>
<CustomTextField fx:id="searchBox" promptText="%Search" VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0"/>
</VBox.margin>
</CustomTextField>
</fx:root>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jabref.gui.preferences.journals;

import java.util.Locale;

import javax.inject.Inject;

import javafx.fxml.FXML;
Expand All @@ -20,6 +22,7 @@

import com.airhacks.afterburner.views.ViewLoader;
import com.tobiasdiez.easybind.EasyBind;
import org.controlsfx.control.textfield.CustomTextField;

/**
* This class controls the user interface of the journal abbreviations dialog. The UI elements and their layout are
Expand All @@ -40,6 +43,8 @@ public class JournalAbbreviationsTab extends AbstractPreferenceTabView<JournalAb
@FXML private Button addAbbreviationListButton;
@FXML private Button removeAbbreviationListButton;

@FXML private CustomTextField searchBox;

@Inject private TaskExecutor taskExecutor;
@Inject private JournalAbbreviationRepository abbreviationRepository;

Expand All @@ -56,6 +61,14 @@ private void initialize() {
setButtonStyles();
setUpTable();
setBindings();

searchBox.textProperty().addListener((observable, previousText, newText) -> {
viewModel.filterAbbreviations(newText.toLowerCase(Locale.ROOT));
journalAbbreviationsTable.getSelectionModel().clearSelection();
journalAbbreviationsTable.getSelectionModel().selectFirst();
});
searchBox.setPromptText(Localization.lang("Search") + "...");
searchBox.setLeft(IconTheme.JabRefIcons.SEARCH.getGraphicNode());
}

private void setButtonStyles() {
Expand All @@ -78,7 +91,7 @@ private void setUpTable() {
}

private void setBindings() {
journalAbbreviationsTable.itemsProperty().bindBidirectional(viewModel.abbreviationsProperty());
journalAbbreviationsTable.itemsProperty().bindBidirectional(viewModel.filteredAbbreviationsProperty());

EasyBind.subscribe(journalAbbreviationsTable.getSelectionModel().selectedItemProperty(), newValue ->
viewModel.currentAbbreviationProperty().set(newValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class JournalAbbreviationsTabViewModel implements PreferenceTabViewModel
private final Logger logger = LoggerFactory.getLogger(JournalAbbreviationsTabViewModel.class);
private final SimpleListProperty<AbbreviationsFileViewModel> journalFiles = new SimpleListProperty<>(FXCollections.observableArrayList());
private final SimpleListProperty<AbbreviationViewModel> abbreviations = new SimpleListProperty<>(FXCollections.observableArrayList());
private final SimpleListProperty<AbbreviationViewModel> filteredAbbreviations = new SimpleListProperty<>(FXCollections.observableArrayList());
private final SimpleIntegerProperty abbreviationsCount = new SimpleIntegerProperty();
private final SimpleObjectProperty<AbbreviationsFileViewModel> currentFile = new SimpleObjectProperty<>();
private final SimpleObjectProperty<AbbreviationViewModel> currentAbbreviation = new SimpleObjectProperty<>();
Expand Down Expand Up @@ -101,6 +102,8 @@ public JournalAbbreviationsTabViewModel(PreferencesService preferences,
}
}
});
filteredAbbreviations.setAll(abbreviations);
filteredAbbreviations.bindBidirectional(abbreviations);
}

@Override
Expand Down Expand Up @@ -373,6 +376,10 @@ public SimpleListProperty<AbbreviationViewModel> abbreviationsProperty() {
return abbreviations;
}

public SimpleListProperty<AbbreviationViewModel> filteredAbbreviationsProperty() {
return filteredAbbreviations;
}

public SimpleIntegerProperty abbreviationsCountProperty() {
return abbreviationsCount;
}
Expand All @@ -396,4 +403,24 @@ public SimpleBooleanProperty isAbbreviationEditableAndRemovable() {
public SimpleBooleanProperty isFileRemovableProperty() {
return isFileRemovable;
}

public void filterAbbreviations(String text) {
if (text.isEmpty()) {
clearSearch();
return;
}

this.filteredAbbreviations.unbindBidirectional(this.abbreviations);
this.filteredAbbreviations.clear();
for (AbbreviationViewModel abbreviation : abbreviations) {
if (abbreviation.contains(text)) {
filteredAbbreviations.add(abbreviation);
}
}
}

private void clearSearch() {
filteredAbbreviations.setAll(this.abbreviations);
filteredAbbreviations.bindBidirectional(this.abbreviations);
}
}

0 comments on commit 90cbc2f

Please sign in to comment.