Skip to content

Commit

Permalink
JournalAbbreviation search feature (#7804)
Browse files Browse the repository at this point in the history
  • Loading branch information
btut authored Jun 15, 2021
1 parent 0c9217e commit d80e164
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ 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 auto-key-generation progress to the background task list. [#7267](https://github.com/JabRef/jabref/issues/7267)
- We added a search feature for journal abbreviations. [#7804](https://github.com/JabRef/jabref/pull/7804)
- We added auto-key-generation progress to the background task list. [#7267](https://github.com/JabRef/jabref/issues/72)

### 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,16 @@

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.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
Expand All @@ -10,16 +20,20 @@
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;
import org.jabref.gui.preferences.PreferencesTab;
import org.jabref.gui.util.ColorUtil;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;

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 +47,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 +74,15 @@ public JournalAbbreviationsTab() {
private void initialize() {
viewModel = new JournalAbbreviationsTabViewModel(preferencesService, dialogService, taskExecutor, abbreviationRepository);

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

setButtonStyles();
setUpTable();
setBindings();
setAnimations();

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

private void setButtonStyles() {
Expand All @@ -78,7 +105,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 +125,27 @@ 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-control-inner-background: ").concat(flashingColorStringProperty).concat(";")
);
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.25), (ActionEvent event) -> {
addAbbreviationActions();
}),
new KeyFrame(Duration.seconds(0.5), new KeyValue(flashingColor, Color.TRANSPARENT, Interpolator.LINEAR))
);
}

@FXML
Expand All @@ -117,11 +165,30 @@ private void removeList() {

@FXML
private void addAbbreviation() {
if (!searchBox.getText().isEmpty()) {
invalidateSearch.play();
} else {
addAbbreviationActions();
}
}

private void addAbbreviationActions() {
viewModel.addAbbreviation();
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(ColorUtil.toRGBACode(color.get()));
}

@FXML
private void editAbbreviation() {
journalAbbreviationsTable.edit(
Expand All @@ -138,7 +205,7 @@ private void selectNewAbbreviation() {
int lastRow = viewModel.abbreviationsCountProperty().get() - 1;
journalAbbreviationsTable.scrollTo(lastRow);
journalAbbreviationsTable.getSelectionModel().select(lastRow);
journalAbbreviationsTable.getFocusModel().focus(lastRow);
journalAbbreviationsTable.getFocusModel().focus(lastRow, journalTableNameColumn);
}

@Override
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/jabref/gui/util/ColorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public static String toRGBCode(Color color) {
(int) (color.getBlue() * 255));
}

public static String toRGBACode(Color color) {
return String.format("rgba(%d,%d,%d,%f)",
(int) (color.getRed() * 255),
(int) (color.getGreen() * 255),
(int) (color.getBlue() * 255),
color.getOpacity());
}

public static String toHex(Color validFieldBackgroundColor) {
return String.format("#%02x%02x%02x", (int) validFieldBackgroundColor.getRed(), (int) validFieldBackgroundColor.getGreen(), (int) validFieldBackgroundColor.getBlue());
}
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(
Arguments.of("Something else", new AbbreviationViewModel(new Abbreviation("Long Name", "abbr", "unique"))),
Arguments.of("Something", new AbbreviationViewModel(new Abbreviation("", "", "")))
);
}
}
37 changes: 31 additions & 6 deletions src/test/java/org/jabref/gui/util/ColorUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
package org.jabref.gui.util;

import java.util.stream.Stream;

import javafx.scene.paint.Color;

import org.junit.jupiter.api.Test;
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.assertEquals;

public class ColorUtilTest {

private static final Color C1 = Color.color(0.2, 0.4, 1);
private static final Color C2 = Color.rgb(255, 255, 255);
private static final Color C3 = Color.color(0, 0, 0, 0);
private static final Color C4 = Color.color(1, 1, 1, 1);
private static final Color C5 = Color.color(0.6, 0.8, 0.5, 0.3);

private ColorUtil colorUtil = new ColorUtil();
private final Color c1 = Color.color(0.2, 0.4, 1);
private final Color c2 = Color.rgb(255, 255, 255);

@Test
public void toRGBCodeTest() {
assertEquals("#3366FF", ColorUtil.toRGBCode(c1));
assertEquals("#FFFFFF", ColorUtil.toRGBCode(c2));
assertEquals("#3366FF", ColorUtil.toRGBCode(C1));
assertEquals("#FFFFFF", ColorUtil.toRGBCode(C2));
}

@ParameterizedTest
@MethodSource("provideToRGBACodeTest")
public void toRGBACodeTest(Color color, String expected) {
assertEquals(expected, ColorUtil.toRGBACode(color));
}

private static Stream<Arguments> provideToRGBACodeTest() {
return Stream.of(
Arguments.of(C1, String.format("rgba(51,102,255,%f)", 1.0)),
Arguments.of(C2, String.format("rgba(255,255,255,%f)", 1.0)),
Arguments.of(C3, String.format("rgba(0,0,0,%f)", 0.0)),
Arguments.of(C4, String.format("rgba(255,255,255,%f)", 1.0)),
Arguments.of(C5, String.format("rgba(153,204,127,%f)", 0.3))
);
}

@Test
public void toHexTest() {
assertEquals("#000001", ColorUtil.toHex(c1));
assertEquals("#010101", ColorUtil.toHex(c2));
assertEquals("#000001", ColorUtil.toHex(C1));
assertEquals("#010101", ColorUtil.toHex(C2));
}
}

0 comments on commit d80e164

Please sign in to comment.