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

Implemented TagsField for the Keywords field #10910

Merged
merged 20 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -23,6 +23,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- When pasting HTML into the abstract or a comment field, the hypertext is automatically converted to Markdown. [#10558](https://github.com/JabRef/jabref/issues/10558)
- We added the possibility to redownload files that had been present but are no longer in the specified location. [#10848](https://github.com/JabRef/jabref/issues/10848)
- We added the citation key pattern `[camelN]`. Equivalent to the first N words of the `[camel]` pattern.
- We added a selection box next to the keywords field, The list is filled with the contents of the content selectors, excluding existing keywords. [#10560](https://github.com/JabRef/jabref/issues/10560)
- We added ability to export in CFF (Citation File Format) [#10661](https://github.com/JabRef/jabref/issues/10661).

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static FieldEditorFX getForField(final Field field,
} else if (fieldProperties.contains(FieldProperty.PERSON_NAMES)) {
return new PersonsEditor(field, suggestionProvider, preferences, fieldCheckers, isMultiLine, undoManager);
} else if (StandardField.KEYWORDS == field) {
return new KeywordsEditor(field, suggestionProvider, fieldCheckers, preferences, undoManager);
return new KeywordsEditor(field, suggestionProvider, fieldCheckers);
} else if (field == InternalField.KEY_FIELD) {
return new CitationKeyEditor(field, suggestionProvider, fieldCheckers, databaseContext);
} else if (fieldProperties.contains(FieldProperty.MARKDOWN)) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/KeywordsEditor.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.HBox?>
<?import com.dlsc.gemsfx.TagsField?>
<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.KeywordsEditor" >
<TagsField fx:id="keywordTagsField" HBox.hgrow="ALWAYS"/>
</fx:root>
99 changes: 94 additions & 5 deletions src/main/java/org/jabref/gui/fieldeditors/KeywordsEditor.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,109 @@
package org.jabref.gui.fieldeditors;

import java.util.Comparator;
import java.util.stream.Collectors;

import javax.swing.undo.UndoManager;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;

import org.jabref.gui.DialogService;
import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Keyword;
import org.jabref.model.entry.field.Field;
import org.jabref.preferences.PreferencesService;

public class KeywordsEditor extends SimpleEditor implements FieldEditorFX {
import com.airhacks.afterburner.views.ViewLoader;
import com.dlsc.gemsfx.TagsField;
import jakarta.inject.Inject;

public class KeywordsEditor extends HBox implements FieldEditorFX {

@FXML private TagsField<Keyword> keywordTagsField;

@Inject private PreferencesService preferencesService;
@Inject private DialogService dialogService;
@Inject private UndoManager undoManager;

private final KeywordsEditorViewModel viewModel;

public KeywordsEditor(Field field,
SuggestionProvider<?> suggestionProvider,
FieldCheckers fieldCheckers,
PreferencesService preferences,
UndoManager undoManager) {
super(field, suggestionProvider, fieldCheckers, preferences, undoManager);
FieldCheckers fieldCheckers) {

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

this.viewModel = new KeywordsEditorViewModel(
field,
suggestionProvider,
fieldCheckers,
preferencesService,
undoManager);

keywordTagsField.setCellFactory(new ViewModelListCellFactory<Keyword>().withText(Keyword::toString));
keywordTagsField.setTagViewFactory(this::createTag);

keywordTagsField.setSuggestionProvider(request ->
suggestionProvider.getPossibleSuggestions().stream()
.filter(suggestion -> suggestion.toString()
.toLowerCase()
.contains(request.getUserText().toLowerCase()))
.map(suggestion -> new Keyword(suggestion.toString()))
.distinct()
.collect(Collectors.toList()));

keywordTagsField.setConverter(viewModel.getStringConverter());
keywordTagsField.setMatcher((keyword, searchText) -> keyword.get().toLowerCase().startsWith(searchText.toLowerCase()));
keywordTagsField.setComparator(Comparator.comparing(Keyword::get));

keywordTagsField.setNewItemProducer(searchText -> viewModel.getStringConverter().fromString(searchText));
LoayGhreeb marked this conversation as resolved.
Show resolved Hide resolved

keywordTagsField.setPlaceholder(null);
keywordTagsField.setShowSearchIcon(false);
keywordTagsField.getEditor().getStyleClass().clear();
keywordTagsField.getEditor().getStyleClass().add("tags-field-editor");

Bindings.bindContentBidirectional(keywordTagsField.getTags(), viewModel.keywordListProperty());
}

private Node createTag(Keyword keyword) {
Label tagLabel = new Label();
tagLabel.setText(keyword.get());
tagLabel.setGraphic(IconTheme.JabRefIcons.REMOVE_TAGS.getGraphicNode());
tagLabel.getGraphic().setOnMouseClicked(event -> keywordTagsField.removeTags(keyword));
tagLabel.setContentDisplay(ContentDisplay.RIGHT);
return tagLabel;
}

public KeywordsEditorViewModel getViewModel() {
return viewModel;
}

@Override
public void bindToEntry(BibEntry entry) {
viewModel.bindToEntry(entry);
}

@Override
public Parent getNode() {
return this;
}

@Override
public void requestFocus() {
keywordTagsField.requestFocus();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jabref.gui.fieldeditors;

import javax.swing.undo.UndoManager;

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.util.StringConverter;

import org.jabref.gui.autocompleter.SuggestionProvider;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.Keyword;
import org.jabref.model.entry.field.Field;
import org.jabref.preferences.PreferencesService;

public class KeywordsEditorViewModel extends AbstractEditorViewModel {

private final ListProperty<Keyword> keywordListProperty;

public KeywordsEditorViewModel(Field field,
SuggestionProvider<?> suggestionProvider,
FieldCheckers fieldCheckers,
PreferencesService preferencesService,
UndoManager undoManager) {

super(field, suggestionProvider, fieldCheckers, undoManager);

Character keywordSeparator = preferencesService.getBibEntryPreferences().getKeywordSeparator();
keywordListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
}

public ListProperty<Keyword> keywordListProperty() {
return keywordListProperty;
}

public StringConverter<Keyword> getStringConverter() {
return new StringConverter<>() {
@Override
public String toString(Keyword keyword) {
if (keyword != null) {
return keyword.get();
}
return "";
LoayGhreeb marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Keyword fromString(String keywordString) {
return new Keyword(keywordString);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public LinkedEntriesEditor(Field field, BibDatabaseContext databaseContext, Sugg
entryLinkField.setNewItemProducer(searchText -> viewModel.getStringConverter().fromString(searchText));
entryLinkField.setMatcher((entryLink, searchText) -> entryLink.getKey().toLowerCase().startsWith(searchText.toLowerCase()));
entryLinkField.setComparator(Comparator.comparing(ParsedEntryLink::getKey));

entryLinkField.setPlaceholder(null);
LoayGhreeb marked this conversation as resolved.
Show resolved Hide resolved
entryLinkField.setShowSearchIcon(false);
entryLinkField.getEditor().getStyleClass().clear();
entryLinkField.getEditor().getStyleClass().add("tags-field-editor");
Expand Down
Loading