-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Implemented TagsField for the Keywords field #10910
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ef9bf12
added selection box next to the keyword field
LoayGhreeb 9621c26
Merge branch 'main' into fix-for-issue-10560
LoayGhreeb 351a0a6
added selection box next to the keyword field
LoayGhreeb 1d44909
Update CHANGELOG.md
LoayGhreeb 306af87
Merge branch 'main' into fix-for-issue-10560
LoayGhreeb 4173beb
Rewrite keywords field: used tags field
LoayGhreeb 623cf3a
Update src/main/java/org/jabref/gui/fieldeditors/KeywordsEditorViewMo…
LoayGhreeb ba7487d
Fix Suggestion Provider [#8145]
LoayGhreeb 9022430
Add context menu for keywordsEditor
LoayGhreeb 21242e6
Fixed OpenRewrite test issue
LoayGhreeb 9681856
Add typed text as first suggestion
LoayGhreeb 8d18aa5
Revert "Fix Suggestion Provider"
LoayGhreeb 28df186
Remove unused imports
LoayGhreeb ad0bb94
Bind the keywords and tags field
LoayGhreeb 7348ad4
Update CHANGELOG.md
LoayGhreeb efb1a14
Remove duplicates from the suggestions list
LoayGhreeb f26ce6f
use Labels as a tag view
LoayGhreeb 53880bc
Add LOGGER.debug
LoayGhreeb d16f77c
remove chip-view from CSS file
LoayGhreeb c4d7d40
Update CHANGELOG.md
LoayGhreeb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/org/jabref/gui/fieldeditors/KeywordsEditor.fxml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
138 changes: 133 additions & 5 deletions
138
src/main/java/org/jabref/gui/fieldeditors/KeywordsEditor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,152 @@ | ||
| package org.jabref.gui.fieldeditors; | ||
|
|
||
| import java.util.Comparator; | ||
|
|
||
| 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.ContextMenu; | ||
| import javafx.scene.layout.HBox; | ||
|
|
||
| import org.jabref.gui.ClipBoardManager; | ||
| import org.jabref.gui.DialogService; | ||
| import org.jabref.gui.JabRefDialogService; | ||
| import org.jabref.gui.actions.ActionFactory; | ||
| import org.jabref.gui.actions.SimpleCommand; | ||
| import org.jabref.gui.actions.StandardActions; | ||
| import org.jabref.gui.autocompleter.SuggestionProvider; | ||
| import org.jabref.gui.keyboard.KeyBindingRepository; | ||
| import org.jabref.gui.util.ViewModelListCellFactory; | ||
| import org.jabref.logic.integrity.FieldCheckers; | ||
| import org.jabref.logic.l10n.Localization; | ||
| 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.ChipView; | ||
| import com.dlsc.gemsfx.TagsField; | ||
| import jakarta.inject.Inject; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class KeywordsEditor extends HBox implements FieldEditorFX { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(KeywordsEditor.class); | ||
|
|
||
| @FXML private TagsField<Keyword> keywordTagsField; | ||
|
|
||
| @Inject private PreferencesService preferencesService; | ||
| @Inject private DialogService dialogService; | ||
| @Inject private UndoManager undoManager; | ||
| @Inject private ClipBoardManager clipBoardManager; | ||
| @Inject private KeyBindingRepository keyBindingRepository; | ||
|
|
||
| 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 -> viewModel.getSuggestions(request.getUserText())); | ||
| 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.setShowSearchIcon(false); | ||
| keywordTagsField.getEditor().getStyleClass().clear(); | ||
| keywordTagsField.getEditor().getStyleClass().add("tags-field-editor"); | ||
|
|
||
| Bindings.bindContentBidirectional(keywordTagsField.getTags(), viewModel.keywordListProperty()); | ||
| } | ||
|
|
||
| private Node createTag(Keyword keyword) { | ||
| ChipView<String> chip = new ChipView<>(); | ||
LoayGhreeb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| chip.setText(keyword.get()); | ||
| chip.setOnClose(event -> keywordTagsField.removeTags(keyword)); | ||
|
|
||
| ContextMenu contextMenu = new ContextMenu(); | ||
| ActionFactory factory = new ActionFactory(keyBindingRepository); | ||
| contextMenu.getItems().addAll( | ||
| factory.createMenuItem(StandardActions.COPY, new KeywordsEditor.TagContextAction(StandardActions.COPY, keyword)), | ||
| factory.createMenuItem(StandardActions.CUT, new KeywordsEditor.TagContextAction(StandardActions.CUT, keyword)), | ||
| factory.createMenuItem(StandardActions.DELETE, new KeywordsEditor.TagContextAction(StandardActions.DELETE, keyword)) | ||
| ); | ||
| chip.setContextMenu(contextMenu); | ||
| return chip; | ||
| } | ||
|
|
||
| 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 | ||
| public double getWeight() { | ||
| return 2; | ||
| } | ||
|
|
||
| private class TagContextAction extends SimpleCommand { | ||
| private final StandardActions command; | ||
| private final Keyword keyword; | ||
|
|
||
| public TagContextAction(StandardActions command, Keyword keyword) { | ||
| this.command = command; | ||
| this.keyword = keyword; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| switch (command) { | ||
| case COPY -> { | ||
| clipBoardManager.setContent(keyword.get()); | ||
| dialogService.notify(Localization.lang("Copied '%0' to clipboard.", | ||
| JabRefDialogService.shortenDialogMessage(keyword.get()))); | ||
| } | ||
| case CUT -> { | ||
| clipBoardManager.setContent(keyword.get()); | ||
| dialogService.notify(Localization.lang("Copied '%0' to clipboard.", | ||
| JabRefDialogService.shortenDialogMessage(keyword.get()))); | ||
| keywordTagsField.removeTags(keyword); | ||
| } | ||
| case DELETE -> | ||
| keywordTagsField.removeTags(keyword); | ||
| default -> | ||
| LOGGER.info("Action {} not defined", command.getText()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
90 changes: 90 additions & 0 deletions
90
src/main/java/org/jabref/gui/fieldeditors/KeywordsEditorViewModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package org.jabref.gui.fieldeditors; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| 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.gui.util.BindingsHelper; | ||
| import org.jabref.logic.integrity.FieldCheckers; | ||
| import org.jabref.model.entry.Keyword; | ||
| import org.jabref.model.entry.KeywordList; | ||
| import org.jabref.model.entry.field.Field; | ||
| import org.jabref.preferences.PreferencesService; | ||
|
|
||
| public class KeywordsEditorViewModel extends AbstractEditorViewModel { | ||
|
|
||
| private final ListProperty<Keyword> keywordListProperty; | ||
| private final Character keywordSeparator; | ||
| private final SuggestionProvider<?> suggestionProvider; | ||
|
|
||
| public KeywordsEditorViewModel(Field field, | ||
| SuggestionProvider<?> suggestionProvider, | ||
| FieldCheckers fieldCheckers, | ||
| PreferencesService preferencesService, | ||
| UndoManager undoManager) { | ||
|
|
||
| super(field, suggestionProvider, fieldCheckers, undoManager); | ||
|
|
||
| keywordListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); | ||
| this.keywordSeparator = preferencesService.getBibEntryPreferences().getKeywordSeparator(); | ||
| this.suggestionProvider = suggestionProvider; | ||
|
|
||
| BindingsHelper.bindContentBidirectional( | ||
| keywordListProperty, | ||
| text, | ||
| this::serializeKeywords, | ||
| this::parseKeywords); | ||
| } | ||
|
|
||
| private String serializeKeywords(List<Keyword> keywords) { | ||
| return KeywordList.serialize(keywords, keywordSeparator); | ||
| } | ||
|
|
||
| private List<Keyword> parseKeywords(String newText) { | ||
| return KeywordList.parse(newText, keywordSeparator).stream().toList(); | ||
| } | ||
|
|
||
| public ListProperty<Keyword> keywordListProperty() { | ||
| return keywordListProperty; | ||
| } | ||
|
|
||
| public StringConverter<Keyword> getStringConverter() { | ||
| return new StringConverter<>() { | ||
| @Override | ||
| public String toString(Keyword keyword) { | ||
| if (keyword == null) { | ||
LoayGhreeb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return ""; | ||
| } | ||
| return keyword.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public Keyword fromString(String keywordString) { | ||
| return new Keyword(keywordString); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public List<Keyword> getSuggestions(String request) { | ||
| List<Keyword> suggestions = suggestionProvider.getPossibleSuggestions().stream() | ||
| .map(String.class::cast) | ||
| .filter(keyword -> keyword.toLowerCase().contains(request.toLowerCase())) | ||
| .map(Keyword::new) | ||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
koppor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Keyword requestedKeyword = new Keyword(request); | ||
| if (!suggestions.contains(requestedKeyword)) { | ||
| suggestions.addFirst(requestedKeyword); | ||
| } | ||
LoayGhreeb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return suggestions; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.