Skip to content

Commit

Permalink
Fix remove actions for entry types in the editor
Browse files Browse the repository at this point in the history
fixes JabRef#6906
* Created separate view models for standard and custom entry types, adding remove icon only for the custom ones
* Updated CHANGELOG.md
  • Loading branch information
dawidm committed Sep 22, 2020
1 parent 3406841 commit 971e246
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 73 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 remove icon was shown for custom entry types in th editor

### 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,16 @@ 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 StandardEntryTypeViewModel(entryType);
this.entryTypesWithFields.add(viewModel);
}
}

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

Expand Down Expand Up @@ -127,17 +131,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 +165,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 @@ -7,6 +7,7 @@
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
Expand Down Expand Up @@ -49,9 +50,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 +118,25 @@ 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 Down Expand Up @@ -210,7 +226,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 abstract class EntryTypeViewModel {

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

protected 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 + "]";
}

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

import org.jabref.model.entry.BibEntryType;

public class StandardEntryTypeViewModel extends EntryTypeViewModel {
public StandardEntryTypeViewModel(BibEntryType entryType) {
super(entryType);
}
}

0 comments on commit 971e246

Please sign in to comment.