Skip to content

Commit

Permalink
Fix remove actions for entry types in the editor (#6933)
Browse files Browse the repository at this point in the history
* Fix remove actions for entry types in the editor
fixes #6906
* Created separate view models for standard and custom entry types, adding remove icon only for the custom ones
* Updated CHANGELOG.md

* Update CHANGELOG.md

Co-authored-by: Christoph <cschwentker@gmail.com>

* Fix code style

* Remove unnecessary class StandardEntryTypeViewModel

Co-authored-by: Christoph <cschwentker@gmail.com>
  • Loading branch information
dawidm and Siedlerchr authored Sep 24, 2020
1 parent 129c36e commit 0480a89
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 74 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed the wrong behavior that font size changes are not reflected in dialogs. [#6039](https://github.com/JabRef/jabref/issues/6039)
- We fixed an issue where the sort order of the entry table was reset after a restart of JabRef. [#6898](https://github.com/JabRef/jabref/pull/6898)
- We fixed an issue where no longer a warning was displayed when inserting references into LibreOffice with an invalid "ReferenceParagraphFormat". [#6907](https://github.com/JabRef/jabref/pull/60907).
- We fixed an issue where a remove icon was shown for standard entry types in the custom entry types dialog [6906](https://github.com/JabRef/jabref/issues/6906)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public Field fromString(String string) {
};

private final ObservableList<Field> fieldsForAdding = FXCollections.observableArrayList(FieldFactory.getStandardFieldsWithCitationKey());
private final ObjectProperty<CustomEntryTypeViewModel> selectedEntryType = new SimpleObjectProperty<>();
private final ObjectProperty<EntryTypeViewModel> selectedEntryType = new SimpleObjectProperty<>();
private final ObjectProperty<Field> selectedFieldToAdd = new SimpleObjectProperty<>();
private final StringProperty entryTypeToAdd = new SimpleStringProperty("");
private final ObjectProperty<Field> newFieldToAdd = new SimpleObjectProperty<>();
private final BibDatabaseMode mode;
private final ObservableList<CustomEntryTypeViewModel> entryTypesWithFields = FXCollections.observableArrayList(extractor -> new Observable[] {extractor.entryType(), extractor.fields()});
private final ObservableList<EntryTypeViewModel> entryTypesWithFields = FXCollections.observableArrayList(extractor -> new Observable[] {extractor.entryType(), extractor.fields()});
private final List<BibEntryType> entryTypesToDelete = new ArrayList<>();

private final PreferencesService preferencesService;
Expand Down Expand Up @@ -86,12 +86,17 @@ public void addAllTypes() {
Collection<BibEntryType> allTypes = entryTypesManager.getAllTypes(mode);

for (BibEntryType entryType : allTypes) {
CustomEntryTypeViewModel viewModel = new CustomEntryTypeViewModel(entryType);
EntryTypeViewModel viewModel;
if (entryTypesManager.isCustomType(entryType.getType(), mode)) {
viewModel = new CustomEntryTypeViewModel(entryType);
} else {
viewModel = new EntryTypeViewModel(entryType);
}
this.entryTypesWithFields.add(viewModel);
}
}

public ObservableList<CustomEntryTypeViewModel> entryTypes() {
public ObservableList<EntryTypeViewModel> entryTypes() {
return this.entryTypesWithFields;
}

Expand Down Expand Up @@ -127,17 +132,17 @@ public void addNewField() {
newFieldToAddProperty().setValue(null);
}

public CustomEntryTypeViewModel addNewCustomEntryType() {
public EntryTypeViewModel addNewCustomEntryType() {
EntryType newentryType = new UnknownEntryType(entryTypeToAdd.getValue());
BibEntryType type = new BibEntryType(newentryType, new ArrayList<>(), Collections.emptyList());
CustomEntryTypeViewModel viewModel = new CustomEntryTypeViewModel(type);
EntryTypeViewModel viewModel = new CustomEntryTypeViewModel(type);
this.entryTypesWithFields.add(viewModel);
this.entryTypeToAdd.setValue("");

return viewModel;
}

public ObjectProperty<CustomEntryTypeViewModel> selectedEntryTypeProperty() {
public ObjectProperty<EntryTypeViewModel> selectedEntryTypeProperty() {
return this.selectedEntryType;
}

Expand All @@ -161,7 +166,7 @@ public ValidationStatus fieldValidationStatus() {
return fieldValidator.getValidationStatus();
}

public void removeEntryType(CustomEntryTypeViewModel focusedItem) {
public void removeEntryType(EntryTypeViewModel focusedItem) {
entryTypesWithFields.remove(focusedItem);
entryTypesToDelete.add(focusedItem.entryType().getValue());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,9 @@
package org.jabref.gui.customentrytypes;

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

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.model.entry.BibEntryType;

public class CustomEntryTypeViewModel {

private final ObjectProperty<BibEntryType> entryType = new SimpleObjectProperty<>();
private final ObservableList<FieldViewModel> fields;

public class CustomEntryTypeViewModel extends EntryTypeViewModel {
public CustomEntryTypeViewModel(BibEntryType entryType) {
this.entryType.set(entryType);

List<FieldViewModel> allFieldsForType = entryType.getAllBibFields().stream().map(bibField -> new FieldViewModel(bibField.getField(), entryType.isRequired(bibField.getField()), bibField.getPriority())).collect(Collectors.toList());
fields = FXCollections.observableArrayList((allFieldsForType));
}

@Override
public int hashCode() {
return Objects.hash(entryType, fields);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof CustomEntryTypeViewModel)) {
return false;
}
CustomEntryTypeViewModel other = (CustomEntryTypeViewModel) obj;
return Objects.equals(entryType, other.entryType) && Objects.equals(fields, other.fields);
}

public void addField(FieldViewModel field) {
this.fields.add(field);
}

public ObservableList<FieldViewModel> fields() {
return this.fields;
}

public ObjectProperty<BibEntryType> entryType() {
return this.entryType;
super(entryType);
}

public void removeField(FieldViewModel focusedItem) {
this.fields.remove(focusedItem);
}

@Override
public String toString() {
return "CustomEntryTypeViewModel [entryType=" + entryType + ", fields=" + fields + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public class CustomizeEntryTypeDialogView extends BaseDialog<Void> {
private final BibDatabaseMode mode;
private final BibEntryTypesManager entryTypesManager;

@FXML private TableView<CustomEntryTypeViewModel> entryTypes;
@FXML private TableColumn<CustomEntryTypeViewModel, String> entryTypColumn;
@FXML private TableColumn<CustomEntryTypeViewModel, String> entryTypeActionsColumn;
@FXML private TableView<EntryTypeViewModel> entryTypes;
@FXML private TableColumn<EntryTypeViewModel, String> entryTypColumn;
@FXML private TableColumn<EntryTypeViewModel, String> entryTypeActionsColumn;
@FXML private TextField addNewEntryType;
@FXML private TableView<FieldViewModel> fields;
@FXML private TableColumn<FieldViewModel, String> fieldNameColumn;
Expand Down Expand Up @@ -117,10 +117,29 @@ private void setupTable() {
entryTypeActionsColumn.setSortable(false);
entryTypeActionsColumn.setReorderable(false);
entryTypeActionsColumn.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().entryType().get().getType().getDisplayName()));
new ValueTableCellFactory<CustomEntryTypeViewModel, String>()
.withGraphic(item -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode())
.withTooltip(name -> Localization.lang("Remove entry type") + " " + name)
.withOnMouseClickedEvent(item -> evt -> viewModel.removeEntryType(entryTypes.getSelectionModel().getSelectedItem()))
new ValueTableCellFactory<EntryTypeViewModel, String>()
.withGraphic((type, name) -> {
if (type instanceof CustomEntryTypeViewModel) {
return IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode();
} else {
return null;
}
})
.withTooltip((type, name) -> {
if (type instanceof CustomEntryTypeViewModel) {
return (Localization.lang("Remove entry type") + " " + name);
} else {
return null;
}
})
.withOnMouseClickedEvent((type, name) -> {
if (type instanceof CustomEntryTypeViewModel) {
return evt -> viewModel.removeEntryType(entryTypes.getSelectionModel().getSelectedItem());
} else {
return evt -> {
};
}
})
.install(entryTypeActionsColumn);

fieldTypeColumn.setCellFactory(cellData -> new RadioButtonCell<>(EnumSet.allOf(FieldType.class)));
Expand All @@ -146,7 +165,9 @@ private void setupTable() {
new ValueTableCellFactory<FieldViewModel, String>()
.withGraphic(item -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode())
.withTooltip(name -> Localization.lang("Remove field %0 from currently selected entry type", name))
.withOnMouseClickedEvent(item -> evt -> viewModel.removeField(fields.getSelectionModel().getSelectedItem()))
.withOnMouseClickedEvent(item -> evt -> {
viewModel.removeField(fields.getSelectionModel().getSelectedItem());
})
.install(fieldTypeActionColumn);

viewModel.newFieldToAddProperty().bindBidirectional(addNewField.valueProperty());
Expand Down Expand Up @@ -210,7 +231,7 @@ private void handleOnDragDropped(TableRow<FieldViewModel> row, FieldViewModel or

@FXML
void addEntryType() {
CustomEntryTypeViewModel newlyAdded = viewModel.addNewCustomEntryType();
EntryTypeViewModel newlyAdded = viewModel.addNewCustomEntryType();
this.entryTypes.getSelectionModel().select(newlyAdded);
this.entryTypes.scrollTo(newlyAdded);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.jabref.gui.customentrytypes;

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

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.model.entry.BibEntryType;

public class EntryTypeViewModel {

private final ObjectProperty<BibEntryType> entryType = new SimpleObjectProperty<>();
private final ObservableList<FieldViewModel> fields;

public EntryTypeViewModel(BibEntryType entryType) {
this.entryType.set(entryType);

List<FieldViewModel> allFieldsForType = entryType.getAllBibFields().stream().map(bibField -> new FieldViewModel(bibField.getField(), entryType.isRequired(bibField.getField()), bibField.getPriority())).collect(Collectors.toList());
fields = FXCollections.observableArrayList((allFieldsForType));
}

@Override
public int hashCode() {
return Objects.hash(entryType, fields);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof EntryTypeViewModel)) {
return false;
}
EntryTypeViewModel other = (EntryTypeViewModel) obj;
return Objects.equals(entryType, other.entryType) && Objects.equals(fields, other.fields);
}

public void addField(FieldViewModel field) {
this.fields.add(field);
}

public ObservableList<FieldViewModel> fields() {
return this.fields;
}

public ObjectProperty<BibEntryType> entryType() {
return this.entryType;
}

public void removeField(FieldViewModel focusedItem) {
this.fields.remove(focusedItem);
}

@Override
public String toString() {
return "CustomEntryTypeViewModel [entryType=" + entryType + ", fields=" + fields + "]";
}

}

0 comments on commit 0480a89

Please sign in to comment.