Skip to content

Commit

Permalink
Merge branch 'customizeEntrydlg' of github.com:JabRef/jabref into cus…
Browse files Browse the repository at this point in the history
…tomizeEntrydlg

* 'customizeEntrydlg' of github.com:JabRef/jabref:
  increase width
  move reloading custom entry types from pref to apply
  add missing l10n
  fix l10n
  add changelog fix typo
  refactor and pass entrytypes manager as ctor param move RadioButtonCell to util use common fields instead of all
  TableView is now properly updated when radio button is toggled
  Add commit handler TODO: commit event is not propagated somehow
  • Loading branch information
Siedlerchr committed Feb 3, 2020
2 parents e497e03 + a522f21 commit 66eee3b
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 474 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ We refer to [GitHub issues](/~https://github.com/JabRef/jabref/issues) by using `#

- We cleaned up the group add/edit dialog. [#5826](/~https://github.com/JabRef/jabref/pull/5826)
- We reintroduced the index column. [#5844](/~https://github.com/JabRef/jabref/pull/5844)
- We reimplemented and improved the dialog "Customize entry types" [#4719](/~https://github.com/JabRef/jabref/issues/4719)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@
requires org.antlr.antlr4.runtime;
requires flowless;
requires org.apache.tika.core;
requires javafx.base;
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ private MenuBar createMenu() {
new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.MANAGE_CONTENT_SELECTORS, new ManageContentSelectorAction(this, stateManager)),
factory.createMenuItem(StandardActions.CUSTOMIZE_ENTRY_TYPES, new CustomizeEntryAction(this))
factory.createMenuItem(StandardActions.CUSTOMIZE_ENTRY_TYPES, new CustomizeEntryAction(stateManager, Globals.entryTypesManager))
);

help.getItems().addAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import javafx.collections.ObservableList;
import javafx.util.StringConverter;

import org.jabref.Globals;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.BibField;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
Expand Down Expand Up @@ -49,61 +50,61 @@ public Field fromString(String string) {
}
};

private ListProperty<BibEntryType> entryTypesProperty;
private ListProperty<Field> fieldsProperty;
private ObjectProperty<BibEntryType> selectedEntryTypesProperty = new SimpleObjectProperty<>();
private ListProperty<FieldViewModel> fieldsForTypeProperty;
private ObjectProperty<Field> selectedFieldToAddProperty = new SimpleObjectProperty<>();
private StringProperty entryTypeToAddProperty = new SimpleStringProperty("");
private ObservableList<BibEntryType> entryTypes;
private ObservableList<FieldViewModel> fieldsForType = FXCollections.observableArrayList(extractor -> new Observable[] {extractor.fieldNameProperty(), extractor.fieldTypeProperty()});
private ObjectProperty<Field> newFieldToAddProperty = new SimpleObjectProperty<>();
private ListProperty<BibEntryType> entryTypes;
private ListProperty<Field> fields;
private ObjectProperty<BibEntryType> selectedEntryTypes = new SimpleObjectProperty<>();
private ListProperty<FieldViewModel> fieldsForType;
private ObjectProperty<Field> selectedFieldToAdd = new SimpleObjectProperty<>();
private StringProperty entryTypeToAdd = new SimpleStringProperty("");
private ObservableList<BibEntryType> allEntryTypes;
private ObservableList<FieldViewModel> allFieldsForType = FXCollections.observableArrayList(extractor -> new Observable[] {extractor.fieldName(), extractor.fieldType()});
private ObjectProperty<Field> newFieldToAdd = new SimpleObjectProperty<>();
private BibDatabaseMode mode;
private Map<BibEntryType, List<FieldViewModel>> typesWithFields = new HashMap<>();
private List<BibEntryType> typesToRemove = new ArrayList<>();

private PreferencesService preferencesService;
private BibEntryTypesManager entryTypesManager;

public CustomEntryTypeDialogViewModel(BibDatabaseMode mode, PreferencesService preferencesService) {
public CustomEntryTypeDialogViewModel(BibDatabaseMode mode, PreferencesService preferencesService, BibEntryTypesManager entryTypesManager) {
this.mode = mode;
this.preferencesService = preferencesService;
this.entryTypesManager = entryTypesManager;

Collection<BibEntryType> allTypes = Globals.entryTypesManager.getAllTypes(mode);
allTypes.addAll(Globals.entryTypesManager.getAllCustomTypes(mode));
Collection<BibEntryType> allTypes = entryTypesManager.getAllTypes(mode);
allTypes.addAll(entryTypesManager.getAllCustomTypes(mode));

entryTypes = FXCollections.observableArrayList(allTypes);
entryTypesProperty = new SimpleListProperty<>(entryTypes);
allEntryTypes = FXCollections.observableArrayList(allTypes);
entryTypes = new SimpleListProperty<>(allEntryTypes);

fieldsProperty = new SimpleListProperty<>(FXCollections.observableArrayList(FieldFactory.getAllFields()));
fields = new SimpleListProperty<>(FXCollections.observableArrayList(FieldFactory.getCommonFields()));

for (BibEntryType entryType : allTypes) {
List<FieldViewModel> fields = entryType.getAllFields().stream().map(bibField -> new FieldViewModel(bibField.getField(), entryType.isRequired(bibField.getField()), bibField.getPriority(), entryType)).collect(Collectors.toList());
typesWithFields.put(entryType, fields);
}

this.fieldsForTypeProperty = new SimpleListProperty<>(fieldsForType);
this.fieldsForType = new SimpleListProperty<>(allFieldsForType);

EasyBind.subscribe(selectedEntryTypesProperty, type -> {
EasyBind.subscribe(selectedEntryTypes, type -> {
if (type != null) {
List<FieldViewModel> typesForField = typesWithFields.get(type);
fieldsForType.setAll(typesForField);
allFieldsForType.setAll(typesWithFields.get(type));
}
});

}

public ListProperty<BibEntryType> entryTypesProperty() {
return this.entryTypesProperty;
public ListProperty<BibEntryType> entryTypes() {
return this.entryTypes;
}

public ListProperty<Field> fieldsProperty() {
return this.fieldsProperty;
public ListProperty<Field> fields() {
return this.fields;
}

public enum FieldType {

REQUIRED("Required"),
OPTIONAL("Optional");
REQUIRED(Localization.lang("Required")),
OPTIONAL(Localization.lang("Optional"));

private String name;

Expand All @@ -122,68 +123,72 @@ public String toString() {
}

public void addNewField() {
Field field = newFieldToAddProperty.getValue();
FieldViewModel model = new FieldViewModel(field, true, FieldPriority.IMPORTANT, selectedEntryTypesProperty.getValue());
typesWithFields.computeIfAbsent(selectedEntryTypesProperty.getValue(), key -> new ArrayList<>()).add(model);
fieldsForType.add(model);
Field field = newFieldToAdd.getValue();
FieldViewModel model = new FieldViewModel(field, true, FieldPriority.IMPORTANT, selectedEntryTypes.getValue());
typesWithFields.computeIfAbsent(selectedEntryTypes.getValue(), key -> new ArrayList<>()).add(model);
allFieldsForType.add(model);
newFieldToAddProperty().setValue(null);
}

public void addNewCustomEntryType() {
EntryType newentryType = new UnknownEntryType(entryTypeToAddProperty.getValue());
EntryType newentryType = new UnknownEntryType(entryTypeToAdd.getValue());
BibEntryType type = new BibEntryType(newentryType, new ArrayList<>(), Collections.emptyList());
this.entryTypes.add(type);

this.allEntryTypes.add(type);
this.entryTypeToAdd.setValue("");
this.typesWithFields.put(type, new ArrayList<>());
}

public ObjectProperty<BibEntryType> selectedEntryTypeProperty() {
return this.selectedEntryTypesProperty;
return this.selectedEntryTypes;
}

public ListProperty<FieldViewModel> fieldsforTypesProperty() {
return this.fieldsForTypeProperty;
return this.fieldsForType;
}

public ObjectProperty<Field> selectedFieldToAddProperty() {
return this.selectedFieldToAddProperty;
return this.selectedFieldToAdd;
}

public StringProperty entryTypeToAddProperty() {
return this.entryTypeToAddProperty;
return this.entryTypeToAdd;
}

public ObjectProperty<Field> newFieldToAddProperty() {
return this.newFieldToAddProperty;
return this.newFieldToAdd;
}

public void removeEntryType(BibEntryType focusedItem) {
typesToRemove.add(focusedItem);
typesWithFields.remove(focusedItem);
entryTypes.remove(focusedItem);
allEntryTypes.remove(focusedItem);
}

public void removeField(FieldViewModel focusedItem) {
typesWithFields.computeIfAbsent(selectedEntryTypesProperty.getValue(), key -> new ArrayList<>()).remove(focusedItem);
fieldsForType.remove(focusedItem);
typesWithFields.computeIfAbsent(selectedEntryTypes.getValue(), key -> new ArrayList<>()).remove(focusedItem);
allFieldsForType.remove(focusedItem);
}

public void apply() {

for (var entry : typesWithFields.entrySet()) {
BibEntryType type = entry.getKey();
List<FieldViewModel> allFields = entry.getValue();
for (var typeWithField : typesWithFields.entrySet()) {
BibEntryType type = typeWithField.getKey();
List<FieldViewModel> allFields = typeWithField.getValue();

List<OrFields> requiredFields = allFields.stream().filter(field -> field.getFieldType() == FieldType.REQUIRED).map(FieldViewModel::getField).map(OrFields::new).collect(Collectors.toList());
List<BibField> otherFields = allFields.stream().filter(field -> field.getFieldType() == FieldType.OPTIONAL).map(bibField -> new BibField(bibField.getField(), bibField.getFieldPriority())).collect(Collectors.toList());

BibEntryType newType = new BibEntryType(type.getType(), otherFields, requiredFields);
Globals.entryTypesManager.addCustomOrModifiedType(newType, mode);
entryTypesManager.addCustomOrModifiedType(newType, mode);
}

for (var type : typesToRemove) {
Globals.entryTypesManager.removeCustomEntryType(type, mode);
entryTypesManager.removeCustomOrModifiedEntryType(type, mode);
}
preferencesService.saveCustomEntryTypes();
//Reload types from preferences to make sure any modifications are present when reopening the dialog
entryTypesManager.addCustomOrModifiedTypes(preferencesService.loadBibEntryTypes(BibDatabaseMode.BIBTEX),
preferencesService.loadBibEntryTypes(BibDatabaseMode.BIBLATEX));
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package org.jabref.gui.customentrytypes;

import org.jabref.gui.JabRefFrame;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;

import static org.jabref.gui.actions.ActionHelper.needsDatabase;

public class CustomizeEntryAction extends SimpleCommand {

private final JabRefFrame frame;
private final StateManager stateManager;
private final BibEntryTypesManager entryTypesManager;

public CustomizeEntryAction(JabRefFrame frame) {
this.frame = frame;
public CustomizeEntryAction(StateManager stateManager, BibEntryTypesManager entryTypesManager) {
this.stateManager = stateManager;
this.executable.bind(needsDatabase(this.stateManager));
this.entryTypesManager = entryTypesManager;
}

@Override
public void execute() {
CustomizeEntryTypeDialogView dialog = new CustomizeEntryTypeDialogView(this.frame.getCurrentBasePanel().getBibDatabaseContext());
BibDatabaseContext database = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null"));
CustomizeEntryTypeDialogView dialog = new CustomizeEntryTypeDialogView(database, entryTypesManager);
dialog.showAndWait();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<?import org.jabref.gui.icon.JabRefIconView?>
<?import javafx.scene.control.Tooltip?>

<DialogPane prefHeight="596.0" prefWidth="600.0"
<DialogPane prefHeight="596.0" prefWidth="680"
xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.jabref.gui.customentrytypes.CustomizeEntryTypeDialogView">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@
import org.jabref.gui.customentrytypes.CustomEntryTypeDialogViewModel.FieldType;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.RadioButtonCell;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.Field;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.views.ViewLoader;

public class CustomizeEntryTypeDialogView extends BaseDialog<Void> {

private final BibDatabaseMode mode;
private final BibEntryTypesManager entryTypesManager;

@FXML private TableView<BibEntryType> entryTypes;
@FXML private TableColumn<BibEntryType, String> entryTypColumn;
@FXML private TableColumn<BibEntryType, String> entryTypeActionsColumn;
Expand All @@ -42,10 +47,10 @@ public class CustomizeEntryTypeDialogView extends BaseDialog<Void> {
@Inject private PreferencesService preferencesService;

private CustomEntryTypeDialogViewModel viewModel;
private BibDatabaseMode mode;

public CustomizeEntryTypeDialogView(BibDatabaseContext bibDatabaseContext) {
public CustomizeEntryTypeDialogView(BibDatabaseContext bibDatabaseContext, BibEntryTypesManager entryTypesManager) {
this.mode = bibDatabaseContext.getMode();
this.entryTypesManager = entryTypesManager;

ViewLoader.view(this)
.load()
Expand All @@ -61,15 +66,15 @@ public CustomizeEntryTypeDialogView(BibDatabaseContext bibDatabaseContext) {

@FXML
private void initialize() {
viewModel = new CustomEntryTypeDialogViewModel(mode, preferencesService);

viewModel = new CustomEntryTypeDialogViewModel(mode, preferencesService, entryTypesManager);
setupTable();
}

private void setupTable() {

fields.setEditable(true); //Table View must be editable, otherwise the change of the Radiobuttons does not propagate the commit event
entryTypColumn.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getType().getDisplayName()));
entryTypes.itemsProperty().bind(viewModel.entryTypesProperty());
entryTypes.itemsProperty().bind(viewModel.entryTypes());
entryTypes.getSelectionModel().selectFirst();

entryTypeActionsColumn.setSortable(false);
Expand All @@ -78,27 +83,33 @@ private void setupTable() {
new ValueTableCellFactory<BibEntryType, String>()
.withGraphic(item -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode())
.withTooltip(name -> Localization.lang("Remove entry type") + " " + name)
.withOnMouseClickedEvent(item -> evt -> viewModel.removeEntryType(entryTypes.getFocusModel().getFocusedItem()))
.withOnMouseClickedEvent(item -> evt -> viewModel.removeEntryType(entryTypes.getSelectionModel().getSelectedItem()))
.install(entryTypeActionsColumn);

fieldTypeColumn.setCellFactory(cellData -> new RadioButtonCell<>(EnumSet.allOf(FieldType.class)));
fieldTypeColumn.setCellValueFactory(item -> item.getValue().fieldTypeProperty());
fieldNameColumn.setCellValueFactory(item -> item.getValue().fieldNameProperty());
fieldTypeColumn.setCellValueFactory(item -> item.getValue().fieldType());

fieldTypeColumn.setEditable(true);
fieldTypeColumn.setOnEditCommit(event -> {
event.getTableView().getItems().get(event.getTablePosition().getRow()).setFieldType(event.getNewValue());
});

fieldNameColumn.setCellValueFactory(item -> item.getValue().fieldName());

viewModel.selectedEntryTypeProperty().bind(entryTypes.getSelectionModel().selectedItemProperty());
viewModel.entryTypeToAddProperty().bind(addNewEntryType.textProperty());

addNewField.setItems(viewModel.fieldsProperty());
addNewField.setItems(viewModel.fields());
addNewField.setConverter(viewModel.FIELD_STRING_CONVERTER);

fieldTypeActionColumn.setSortable(false);
fieldTypeActionColumn.setReorderable(false);
fieldTypeActionColumn.setCellValueFactory(cellData -> cellData.getValue().fieldNameProperty());
fieldTypeActionColumn.setCellValueFactory(cellData -> cellData.getValue().fieldName());

new ValueTableCellFactory<FieldViewModel, String>()
.withGraphic(item -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode())
.withTooltip(name -> Localization.lang("Remove field from entry type") + " " + name)
.withOnMouseClickedEvent(item -> evt -> viewModel.removeField(fields.getFocusModel().getFocusedItem()))
.withOnMouseClickedEvent(item -> evt -> viewModel.removeField(fields.getSelectionModel().getSelectedItem()))
.install(fieldTypeActionColumn);

viewModel.newFieldToAddProperty().bindBidirectional(addNewField.valueProperty());
Expand Down
Loading

0 comments on commit 66eee3b

Please sign in to comment.