Skip to content

Commit

Permalink
ChipView for crossref and groups (#6228)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Diez <tobiasdiez@gmx.de>
  • Loading branch information
dextep and tobiasdiez authored Sep 1, 2020
1 parent cadadca commit 63270ad
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .idea/runConfigurations/JabRef_Main.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/main/java/org/jabref/gui/Base.css
Original file line number Diff line number Diff line change
Expand Up @@ -1174,10 +1174,16 @@ TextFlow * {
-fx-fill: -fx-text-background-color;
}

.chips-pane > .editor {
-fx-pref-height: 30px;
-fx-padding: 0px 0px 0px -8px;
-fx-margin: 0em;

.searchBar:invalid {
-fx-background-color: rgba(240, 128, 128, 0.5);
}

.searchBar:unsupported {
-fx-background-color: rgba(255, 159, 67, 0.5);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jabref.gui.autocompleter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -22,4 +24,14 @@ public ContentSelectorSuggestionProvider(SuggestionProvider<String> suggestionPr
public Stream<String> getSource() {
return Stream.concat(contentSelectorValues.stream(), suggestionProvider.getSource());
}

@Override
public Collection<String> getPossibleSuggestions() {
List<String> suggestions = new ArrayList<>();
if (suggestionProvider != null) {
suggestions.addAll(suggestionProvider.getPossibleSuggestions());
}
suggestions.addAll(contentSelectorValues);
return suggestions;
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/gui/autocompleter/SuggestionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public final Collection<T> provideSuggestions(ISuggestionRequest request) {

protected abstract Equivalence<T> getEquivalence();

public Collection<T> getPossibleSuggestions() {
Comparator<T> comparator = getComparator().reversed();
Equivalence<T> equivalence = getEquivalence();
return getSource().map(equivalence::wrap) // Need to do a bit of acrobatic as there is no distinctBy method
.distinct()
.map(Equivalence.Wrapper::get)
.sorted(comparator)
.collect(Collectors.toList());
}

/**
* Get the comparator to order the suggestions
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public SuggestionProvider<?> getForField(Field field) {
Set<FieldProperty> fieldProperties = field.getProperties();
if (fieldProperties.contains(FieldProperty.PERSON_NAMES)) {
return new PersonNameSuggestionProvider(field, database);
} else if (fieldProperties.contains(FieldProperty.SINGLE_ENTRY_LINK)) {
} else if (fieldProperties.contains(FieldProperty.SINGLE_ENTRY_LINK) || fieldProperties.contains(FieldProperty.MULTIPLE_ENTRY_LINK)) {
return new BibEntrySuggestionProvider(database);
} else if (fieldProperties.contains(FieldProperty.JOURNAL_NAME)
|| StandardField.PUBLISHER.equals(field)) {
} else if (fieldProperties.contains(FieldProperty.JOURNAL_NAME) || StandardField.PUBLISHER.equals(field)) {
return new JournalsSuggestionProvider(field, database, abbreviationRepository);
} else {
return new WordSuggestionProvider(field, database);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.FieldProperty;
Expand All @@ -27,6 +28,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("unchecked")
public class FieldEditors {

private static final Logger LOGGER = LoggerFactory.getLogger(FieldEditors.class);
Expand Down Expand Up @@ -83,9 +85,9 @@ public static FieldEditorFX getForField(final Field field,
return new OptionEditor<>(new TypeEditorViewModel(field, suggestionProvider, fieldCheckers));
}
} else if (fieldProperties.contains(FieldProperty.SINGLE_ENTRY_LINK)) {
return new LinkedEntriesEditor(field, databaseContext, suggestionProvider, fieldCheckers);
return new LinkedEntriesEditor(field, databaseContext, (SuggestionProvider<BibEntry>) suggestionProvider, fieldCheckers);
} else if (fieldProperties.contains(FieldProperty.MULTIPLE_ENTRY_LINK)) {
return new LinkedEntriesEditor(field, databaseContext, suggestionProvider, fieldCheckers);
return new LinkedEntriesEditor(field, databaseContext, (SuggestionProvider<BibEntry>) suggestionProvider, fieldCheckers);
} else if (fieldProperties.contains(FieldProperty.PERSON_NAMES)) {
return new PersonsEditor(field, suggestionProvider, preferences, fieldCheckers, isMultiLine);
} else if (StandardField.KEYWORDS.equals(field)) {
Expand All @@ -98,7 +100,6 @@ public static FieldEditorFX getForField(final Field field,
}
}

@SuppressWarnings("unchecked")
private static SuggestionProvider<?> getSuggestionProvider(Field field, SuggestionProviders suggestionProviders, MetaData metaData) {
SuggestionProvider<?> suggestionProvider = suggestionProviders.getForField(field);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.HBox?>
<?import org.jabref.gui.util.component.TagBar?>
<?import com.jfoenix.controls.JFXChipView?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="HBox" xmlns="http://javafx.com/javafx/8.0.112"
fx:controller="org.jabref.gui.fieldeditors.LinkedEntriesEditor">
<TagBar fx:id="linkedEntriesBar" maxWidth="Infinity" HBox.hgrow="ALWAYS"/>
<JFXChipView fx:id="chipView" HBox.hgrow="ALWAYS" maxWidth="Infinity"/>
</fx:root>
30 changes: 21 additions & 9 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedEntriesEditor.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
package org.jabref.gui.fieldeditors;

import java.util.stream.Collectors;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.layout.HBox;

import org.jabref.gui.autocompleter.AutoCompletionTextInputBinding;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.util.component.TagBar;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.ParsedEntryLink;
import org.jabref.model.entry.field.Field;

import com.airhacks.afterburner.views.ViewLoader;
import com.jfoenix.controls.JFXChip;
import com.jfoenix.controls.JFXChipView;
import com.jfoenix.controls.JFXDefaultChip;

public class LinkedEntriesEditor extends HBox implements FieldEditorFX {

@FXML
private final LinkedEntriesEditorViewModel viewModel;
@FXML
private TagBar<ParsedEntryLink> linkedEntriesBar;
private JFXChipView<ParsedEntryLink> chipView;

public LinkedEntriesEditor(Field field, BibDatabaseContext databaseContext, SuggestionProvider<?> suggestionProvider, FieldCheckers fieldCheckers) {
public LinkedEntriesEditor(Field field, BibDatabaseContext databaseContext, SuggestionProvider<BibEntry> suggestionProvider, FieldCheckers fieldCheckers) {
this.viewModel = new LinkedEntriesEditorViewModel(field, suggestionProvider, databaseContext, fieldCheckers);

ViewLoader.view(this)
.root(this)
.load();

linkedEntriesBar.setFieldProperties(field.getProperties());
linkedEntriesBar.setStringConverter(viewModel.getStringConverter());
linkedEntriesBar.setOnTagClicked((parsedEntryLink, mouseEvent) -> viewModel.jumpToEntry(parsedEntryLink));
chipView.setConverter(viewModel.getStringConverter());
var autoCompletionItemFactory = new ViewModelListCellFactory<ParsedEntryLink>()
.withText(ParsedEntryLink::getKey);
chipView.getAutoCompletePopup().setSuggestionsCellFactory(autoCompletionItemFactory);
chipView.getAutoCompletePopup().setCellLimit(5);
chipView.getSuggestions().addAll(suggestionProvider.getPossibleSuggestions().stream().map(ParsedEntryLink::new).collect(Collectors.toList()));

chipView.setChipFactory((view, item) -> {
JFXChip<ParsedEntryLink> chip = new JFXDefaultChip<>(view, item);
chip.setOnMouseClicked(event -> viewModel.jumpToEntry(item));
return chip;
});

AutoCompletionTextInputBinding.autoComplete(linkedEntriesBar.getInputTextField(), viewModel::complete, viewModel.getStringConverter());
Bindings.bindContentBidirectional(linkedEntriesBar.tagsProperty(), viewModel.linkedEntriesProperty());
Bindings.bindContentBidirectional(chipView.getChips(), viewModel.linkedEntriesProperty());
}

public LinkedEntriesEditorViewModel getViewModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.jabref.gui.fieldeditors;

import java.util.Collection;
import java.util.stream.Collectors;

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
Expand All @@ -12,13 +9,10 @@
import org.jabref.gui.util.BindingsHelper;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.EntryLinkList;
import org.jabref.model.entry.ParsedEntryLink;
import org.jabref.model.entry.field.Field;

import org.controlsfx.control.textfield.AutoCompletionBinding;

public class LinkedEntriesEditorViewModel extends AbstractEditorViewModel {

private final BibDatabaseContext databaseContext;
Expand All @@ -36,20 +30,12 @@ public LinkedEntriesEditorViewModel(Field field, SuggestionProvider<?> suggestio
newText -> EntryLinkList.parse(newText, databaseContext.getDatabase()));
}

@Override
@SuppressWarnings("unchecked")
public Collection<ParsedEntryLink> complete(AutoCompletionBinding.ISuggestionRequest request) {
// We have to cast the BibEntries from the BibEntrySuggestionProvider to ParsedEntryLink
Collection<BibEntry> bibEntries = (Collection<BibEntry>) super.complete(request);
return bibEntries.stream().map(ParsedEntryLink::new).collect(Collectors.toList());
}

public ListProperty<ParsedEntryLink> linkedEntriesProperty() {
return linkedEntries;
}

public StringConverter<ParsedEntryLink> getStringConverter() {
return new StringConverter<ParsedEntryLink>() {
return new StringConverter<>() {

@Override
public String toString(ParsedEntryLink linkedEntry) {
Expand All @@ -61,7 +47,7 @@ public String toString(ParsedEntryLink linkedEntry) {

@Override
public ParsedEntryLink fromString(String key) {
return databaseContext.getDatabase().getEntryByKey(key).map(ParsedEntryLink::new).orElse(null);
return new ParsedEntryLink(key, databaseContext.getDatabase());
}
};
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/jabref/model/entry/ParsedEntryLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class ParsedEntryLink {

private String key;
private Optional<BibEntry> linkedEntry;
private BibDatabase dataBase;
private BibDatabase database;

public ParsedEntryLink(String key, BibDatabase dataBase) {
public ParsedEntryLink(String key, BibDatabase database) {
this.key = key;
this.linkedEntry = dataBase.getEntryByKey(this.key);
this.dataBase = dataBase;
this.linkedEntry = database.getEntryByKey(this.key);
this.database = database;
}

public ParsedEntryLink(BibEntry bibEntry) {
Expand All @@ -32,7 +32,7 @@ public Optional<BibEntry> getLinkedEntry() {

public void setKey(String newKey) {
this.key = newKey;
this.linkedEntry = getDataBase().getEntryByKey(this.key);
this.linkedEntry = getDatabase().getEntryByKey(this.key);
}

@Override
Expand All @@ -52,7 +52,7 @@ public boolean equals(Object obj) {
return Objects.equals(key, other.key) && Objects.equals(linkedEntry, other.linkedEntry);
}

public BibDatabase getDataBase() {
return dataBase;
public BibDatabase getDatabase() {
return database;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void givenFieldValueAndDatabaseWhenParsingThenExpectKey() {

@Test
public void givenFieldValueAndDatabaseWhenParsingThenExpectDataBase() {
assertEquals(database, link.getDataBase());
assertEquals(database, link.getDatabase());
}

@Test
Expand Down

0 comments on commit 63270ad

Please sign in to comment.