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

JournalAbbreviation search feature #7804

Merged
merged 22 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added a feature that allows the user to open all linked files of multiple selected entries by "Open file" option. [#6966](https://github.com/JabRef/jabref/issues/6966)
- We added a keybinding preset for new entries. [#7705](https://github.com/JabRef/jabref/issues/7705)
- We added a select all button for the library import function. [#7786](https://github.com/JabRef/jabref/issues/7786)
- We added a search feature for journal abbreviations [#7804](https://github.com/JabRef/jabref/pull/7804).

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.preferences.journals;

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

import javafx.beans.property.BooleanProperty;
Expand Down Expand Up @@ -94,4 +95,11 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(getName(), isPseudoAbbreviation());
}

public boolean containsCaseIndependent(String searchTerm) {
searchTerm = searchTerm.toLowerCase(Locale.ROOT);
return this.abbreviation.get().toLowerCase(Locale.ROOT).contains(searchTerm) ||
this.name.get().toLowerCase(Locale.ROOT).contains(searchTerm) ||
this.shortestUniqueAbbreviation.get().toLowerCase(Locale.ROOT).contains(searchTerm);
}
}
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
Expand Up @@ -2,6 +2,15 @@

import javax.inject.Inject;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
Expand All @@ -10,6 +19,8 @@
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.paint.Color;
import javafx.util.Duration;

import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.preferences.AbstractPreferenceTabView;
Expand All @@ -20,6 +31,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 @@ -33,16 +45,23 @@ public class JournalAbbreviationsTab extends AbstractPreferenceTabView<JournalAb
@FXML private TableColumn<AbbreviationViewModel, String> journalTableNameColumn;
@FXML private TableColumn<AbbreviationViewModel, String> journalTableAbbreviationColumn;
@FXML private TableColumn<AbbreviationViewModel, String> journalTableShortestUniqueAbbreviationColumn;
private FilteredList<AbbreviationViewModel> filteredAbbreviations;
@FXML private ComboBox<AbbreviationsFileViewModel> journalFilesBox;
@FXML private Button addAbbreviationButton;
@FXML private Button removeAbbreviationButton;
@FXML private Button openAbbreviationListButton;
@FXML private Button addAbbreviationListButton;
@FXML private Button removeAbbreviationListButton;

@FXML private CustomTextField searchBox;

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

private Timeline invalidateSearch;
private ObjectProperty<Color> flashingColor;
private StringProperty flashingColorStringProperty;

public JournalAbbreviationsTab() {
ViewLoader.view(this)
.root(this)
Expand All @@ -53,9 +72,15 @@ public JournalAbbreviationsTab() {
private void initialize() {
viewModel = new JournalAbbreviationsTabViewModel(preferencesService, dialogService, taskExecutor, abbreviationRepository);

filteredAbbreviations = new FilteredList<>(viewModel.abbreviationsProperty());

setButtonStyles();
setUpTable();
setBindings();
setAnimations();
btut marked this conversation as resolved.
Show resolved Hide resolved

searchBox.setPromptText(Localization.lang("Search") + "...");
searchBox.setLeft(IconTheme.JabRefIcons.SEARCH.getGraphicNode());
}

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

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

EasyBind.subscribe(journalAbbreviationsTable.getSelectionModel().selectedItemProperty(), newValue ->
viewModel.currentAbbreviationProperty().set(newValue));
Expand All @@ -98,6 +123,24 @@ private void setBindings() {

loadingLabel.visibleProperty().bind(viewModel.isLoadingProperty());
progressIndicator.visibleProperty().bind(viewModel.isLoadingProperty());

searchBox.textProperty().addListener((observable, previousText, searchTerm) -> {
filteredAbbreviations.setPredicate(abbreviation -> searchTerm.isEmpty() ? true : abbreviation.containsCaseIndependent(searchTerm));
});
}

private void setAnimations() {
flashingColor = new SimpleObjectProperty<>(Color.TRANSPARENT);
flashingColorStringProperty = createFlashingColorStringProperty(flashingColor);
searchBox.styleProperty().bind(
new SimpleStringProperty("-fx-background-color: ").concat(flashingColorStringProperty).concat(";")
btut marked this conversation as resolved.
Show resolved Hide resolved
);
invalidateSearch = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(flashingColor, Color.TRANSPARENT, Interpolator.LINEAR)),
new KeyFrame(Duration.seconds(0.25), new KeyValue(flashingColor, Color.RED, Interpolator.LINEAR)),
new KeyFrame(Duration.seconds(0.25), new KeyValue(searchBox.textProperty(), "", Interpolator.DISCRETE)),
new KeyFrame(Duration.seconds(0.5), new KeyValue(flashingColor, Color.TRANSPARENT, Interpolator.LINEAR))
);
}

@FXML
Expand All @@ -118,10 +161,31 @@ private void removeList() {
@FXML
private void addAbbreviation() {
viewModel.addAbbreviation();
if (!searchBox.getText().isEmpty()) {
invalidateSearch.play();
}
selectNewAbbreviation();
editAbbreviation();
}

private static StringProperty createFlashingColorStringProperty(final ObjectProperty<Color> flashingColor) {
final StringProperty flashingColorStringProperty = new SimpleStringProperty();
setColorStringFromColor(flashingColorStringProperty, flashingColor);
flashingColor.addListener((observable, oldValue, newValue) -> setColorStringFromColor(flashingColorStringProperty, flashingColor));
return flashingColorStringProperty;
}

private static void setColorStringFromColor(StringProperty colorStringProperty, ObjectProperty<Color> color) {
colorStringProperty.set(
btut marked this conversation as resolved.
Show resolved Hide resolved
"rgba(" +
((int) (color.get().getRed() * 255)) + "," +
((int) (color.get().getGreen() * 255)) + "," +
((int) (color.get().getBlue() * 255)) + "," +
color.get().getOpacity() +
")"
);
}

@FXML
private void editAbbreviation() {
journalAbbreviationsTable.edit(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jabref.gui.preferences.journals;

import java.util.stream.Stream;

import org.jabref.logic.journals.Abbreviation;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AbbreviationViewModelTest {

@ParameterizedTest
@MethodSource("provideContainsCaseIndependentContains")
void containsCaseIndependentContains(String searchTerm, AbbreviationViewModel abbreviation) {
assertTrue(abbreviation.containsCaseIndependent(searchTerm));
}

private static Stream<Arguments> provideContainsCaseIndependentContains() {
return Stream.of(
Arguments.of("name", new AbbreviationViewModel(new Abbreviation("Long Name", "abbr", "unique"))),
Arguments.of("bBr", new AbbreviationViewModel(new Abbreviation("Long Name", "abbr", "unique"))),
Arguments.of("Uniq", new AbbreviationViewModel(new Abbreviation("Long Name", "abbr", "unique"))),
Arguments.of("", new AbbreviationViewModel(new Abbreviation("Long Name", "abbr", "unique"))),
Arguments.of("", new AbbreviationViewModel(new Abbreviation("", "", "")))
);
}

@ParameterizedTest
@MethodSource("provideContainsCaseIndependentDoesNotContain")
void containsCaseIndependentDoesNotContain(String searchTerm, AbbreviationViewModel abbreviation) {
assertFalse(abbreviation.containsCaseIndependent(searchTerm));
}

private static Stream<Arguments> provideContainsCaseIndependentDoesNotContain() {
return Stream.of(
btut marked this conversation as resolved.
Show resolved Hide resolved
Arguments.of("Something else", new AbbreviationViewModel(new Abbreviation("Long Name", "abbr", "unique"))),
Arguments.of("Something", new AbbreviationViewModel(new Abbreviation("", "", "")))
);
}
}