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

Use tags editor for auto completion preferences #10990

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javafx.collections.ObservableSet;

import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;

public class AutoCompletePreferences {

Expand Down Expand Up @@ -80,8 +79,4 @@ public void setNameFormat(NameFormat nameFormat) {
public ObservableSet<Field> getCompleteFields() {
return completeFields;
}

public String getCompleteNamesAsString() {
return FieldFactory.serializeFieldsList(completeFields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import com.dlsc.gemsfx.TagsField?>
<fx:root spacing="10.0" type="VBox"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="org.jabref.gui.preferences.autocompletion.AutoCompletionTab">
Expand All @@ -22,11 +22,11 @@
<VBox spacing="10.0">
<HBox spacing="4.0">
<Label text="%Affected fields" disable="${!enableAutoComplete.selected}"/>
<TextField fx:id="autoCompleteFields" HBox.hgrow="ALWAYS" disable="${!enableAutoComplete.selected}">
<TagsField fx:id="autoCompleteFields" HBox.hgrow="ALWAYS" disable="${!enableAutoComplete.selected}">
<HBox.margin>
<Insets top="-4.0"/>
</HBox.margin>
</TextField>
</TagsField>
</HBox>
<Label text="%Name format" disable="${!enableAutoComplete.selected}"/>
<RadioButton fx:id="autoCompleteFirstLast" text="%Autocomplete names in 'Firstname Lastname' format only"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package org.jabref.gui.preferences.autocompletion;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;

import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.preferences.AbstractPreferenceTabView;
import org.jabref.gui.preferences.PreferencesTab;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.field.Field;

import com.airhacks.afterburner.views.ViewLoader;
import com.dlsc.gemsfx.TagsField;

public class AutoCompletionTab extends AbstractPreferenceTabView<AutoCompletionTabViewModel> implements PreferencesTab {

@FXML private CheckBox enableAutoComplete;
@FXML private TextField autoCompleteFields;
@FXML private TagsField<Field> autoCompleteFields;
@FXML private RadioButton autoCompleteFirstLast;
@FXML private RadioButton autoCompleteLastFirst;
@FXML private RadioButton autoCompleteBoth;
Expand All @@ -35,14 +41,33 @@ public String getTabName() {

public void initialize() {
viewModel = new AutoCompletionTabViewModel(preferencesService.getAutoCompletePreferences());

setupTagsFiled();
enableAutoComplete.selectedProperty().bindBidirectional(viewModel.enableAutoCompleteProperty());
autoCompleteFields.textProperty().bindBidirectional(viewModel.autoCompleteFieldsProperty());
autoCompleteFirstLast.selectedProperty().bindBidirectional(viewModel.autoCompleteFirstLastProperty());
autoCompleteLastFirst.selectedProperty().bindBidirectional(viewModel.autoCompleteLastFirstProperty());
autoCompleteBoth.selectedProperty().bindBidirectional(viewModel.autoCompleteBothProperty());
firstNameModeAbbreviated.selectedProperty().bindBidirectional(viewModel.firstNameModeAbbreviatedProperty());
firstNameModeFull.selectedProperty().bindBidirectional(viewModel.firstNameModeFullProperty());
firstNameModeBoth.selectedProperty().bindBidirectional(viewModel.firstNameModeBothProperty());
}

private void setupTagsFiled() {
autoCompleteFields.setCellFactory(new ViewModelListCellFactory<Field>().withText(Field::getDisplayName));
autoCompleteFields.setSuggestionProvider(request -> viewModel.getSuggestions(request.getUserText()));
autoCompleteFields.tagsProperty().bindBidirectional(viewModel.autoCompleteFieldsProperty());
autoCompleteFields.setConverter(viewModel.getFieldStringConverter());
autoCompleteFields.setTagViewFactory(this::createTag);
autoCompleteFields.setShowSearchIcon(false);
autoCompleteFields.getEditor().getStyleClass().clear();
autoCompleteFields.getEditor().getStyleClass().add("tags-field-editor");
}

private Node createTag(Field field) {
Label tagLabel = new Label();
tagLabel.setText(field.getDisplayName());
tagLabel.setGraphic(IconTheme.JabRefIcons.REMOVE_TAGS.getGraphicNode());
tagLabel.getGraphic().setOnMouseClicked(event -> autoCompleteFields.removeTags(field));
tagLabel.setContentDisplay(ContentDisplay.RIGHT);
return tagLabel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.util.StringConverter;

import org.jabref.gui.autocompleter.AutoCompleteFirstNameMode;
import org.jabref.gui.autocompleter.AutoCompletePreferences;
import org.jabref.gui.preferences.PreferenceTabViewModel;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;

public class AutoCompletionTabViewModel implements PreferenceTabViewModel {

private final BooleanProperty enableAutoCompleteProperty = new SimpleBooleanProperty();
private final StringProperty autoCompleteFieldsProperty = new SimpleStringProperty();
private final ListProperty<Field> autoCompleteFieldsProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
private final BooleanProperty autoCompleteFirstLastProperty = new SimpleBooleanProperty();
private final BooleanProperty autoCompleteLastFirstProperty = new SimpleBooleanProperty();
private final BooleanProperty autoCompleteBothProperty = new SimpleBooleanProperty();
Expand All @@ -36,8 +40,7 @@ public AutoCompletionTabViewModel(AutoCompletePreferences autoCompletePreference
@Override
public void setValues() {
enableAutoCompleteProperty.setValue(autoCompletePreferences.shouldAutoComplete());
autoCompleteFieldsProperty.setValue(autoCompletePreferences.getCompleteNamesAsString());

autoCompleteFieldsProperty.setValue(FXCollections.observableArrayList(autoCompletePreferences.getCompleteFields()));
if (autoCompletePreferences.getNameFormat() == AutoCompletePreferences.NameFormat.FIRST_LAST) {
autoCompleteFirstLastProperty.setValue(true);
} else if (autoCompletePreferences.getNameFormat() == AutoCompletePreferences.NameFormat.LAST_FIRST) {
Expand Down Expand Up @@ -82,7 +85,7 @@ public void storeSettings() {
}

autoCompletePreferences.getCompleteFields().clear();
autoCompletePreferences.getCompleteFields().addAll(FieldFactory.parseFieldList(autoCompleteFieldsProperty.getValue()));
autoCompletePreferences.getCompleteFields().addAll(autoCompleteFieldsProperty.getValue());
}

@Override
Expand All @@ -94,7 +97,7 @@ public BooleanProperty enableAutoCompleteProperty() {
return enableAutoCompleteProperty;
}

public StringProperty autoCompleteFieldsProperty() {
public ListProperty<Field> autoCompleteFieldsProperty() {
return autoCompleteFieldsProperty;
}

Expand All @@ -121,4 +124,24 @@ public BooleanProperty firstNameModeFullProperty() {
public BooleanProperty firstNameModeBothProperty() {
return firstNameModeBothProperty;
}

public StringConverter<Field> getFieldStringConverter() {
return new StringConverter<>() {
@Override
public String toString(Field field) {
return field.getDisplayName();
}

@Override
public Field fromString(String string) {
return FieldFactory.parseField(string);
}
};
}

public List<Field> getSuggestions(String request) {
return FieldFactory.getStandardFields().stream()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use getAllFieldsWithOutInternal

  1. it contains more than bibtex and biblatex fields (the additional fields for other entry types, e.g. Software or IEE)
  2. they are already sorted

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.filter(field -> field.getDisplayName().toLowerCase().contains(request.toLowerCase()))
.collect(Collectors.toList());
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/model/entry/field/FieldFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public static Set<Field> getCommonFields() {
return publicAndInternalFields;
}

/**
* Returns a List with all standard fields
*/
public static List<Field> getStandardFields() {
return new ArrayList<>(EnumSet.allOf(StandardField.class));
}

/**
* Returns a sorted Set of Fields (by {@link Field#getDisplayName} with all fields without internal ones
*/
Expand Down
Loading