diff --git a/src/main/java/org/jabref/gui/preferences/TableColumnsItemModel.java b/src/main/java/org/jabref/gui/preferences/TableColumnsItemModel.java index 7ef5656ea5b..dc686d820b4 100644 --- a/src/main/java/org/jabref/gui/preferences/TableColumnsItemModel.java +++ b/src/main/java/org/jabref/gui/preferences/TableColumnsItemModel.java @@ -14,24 +14,24 @@ public class TableColumnsItemModel { private final SimpleObjectProperty field; private final SimpleDoubleProperty length; - private final ReadOnlyBooleanProperty editablePropery; + private final ReadOnlyBooleanProperty editableProperty; public TableColumnsItemModel() { this.field = new SimpleObjectProperty<>(new UnknownField(Localization.lang("new Column"))); this.length = new SimpleDoubleProperty(ColumnPreferences.DEFAULT_FIELD_LENGTH); - this.editablePropery = new SimpleBooleanProperty(true); + this.editableProperty = new SimpleBooleanProperty(true); } public TableColumnsItemModel(Field field) { this.field = new SimpleObjectProperty<>(field); this.length = new SimpleDoubleProperty(ColumnPreferences.DEFAULT_FIELD_LENGTH); - this.editablePropery = new SimpleBooleanProperty(this.field.get() instanceof UnknownField); + this.editableProperty = new SimpleBooleanProperty(this.field.get() instanceof UnknownField); } public TableColumnsItemModel(Field field, double length) { this.field = new SimpleObjectProperty<>(field); this.length = new SimpleDoubleProperty(length); - this.editablePropery = new SimpleBooleanProperty(this.field.get() instanceof UnknownField); + this.editableProperty = new SimpleBooleanProperty(this.field.get() instanceof UnknownField); } public Field getField() { @@ -47,13 +47,21 @@ public String getName() { } public void setName(String name) { - if (editablePropery.get()) { + if (editableProperty.get()) { field.setValue(new UnknownField(name)); } } - public ReadOnlyBooleanProperty editablePropery() { - return editablePropery; + public double getLength() { + return length.get(); + } + + public void setLength(double length) { + this.length.set(length); + } + + public ReadOnlyBooleanProperty editableProperty() { + return editableProperty; } } diff --git a/src/main/java/org/jabref/gui/preferences/TableColumnsTabView.java b/src/main/java/org/jabref/gui/preferences/TableColumnsTabView.java index 99da6a8982b..bcce9d33eb7 100644 --- a/src/main/java/org/jabref/gui/preferences/TableColumnsTabView.java +++ b/src/main/java/org/jabref/gui/preferences/TableColumnsTabView.java @@ -3,13 +3,11 @@ import javax.inject.Inject; import javafx.fxml.FXML; -import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.RadioButton; import javafx.scene.control.cell.CheckBoxListCell; import javafx.scene.input.KeyEvent; -import javafx.scene.layout.VBox; import org.jabref.Globals; import org.jabref.gui.DialogService; @@ -29,7 +27,7 @@ import com.airhacks.afterburner.views.ViewLoader; import org.controlsfx.control.CheckListView; -public class TableColumnsTabView extends VBox implements PrefsTab { +public class TableColumnsTabView extends AbstractPreferenceTabView implements PreferencesTab { @FXML private CheckListView columnsList; @FXML private Button sortUp; @@ -59,8 +57,6 @@ public class TableColumnsTabView extends VBox implements PrefsTab { private long lastKeyPressTime; private String listSearchTerm; - private TableColumnsTabViewModel viewModel; - public TableColumnsTabView(JabRefPreferences preferences, JabRefFrame frame) { this.preferences = preferences; this.frame = frame; @@ -68,13 +64,18 @@ public TableColumnsTabView(JabRefPreferences preferences, JabRefFrame frame) { ViewLoader.view(this) .root(this) .load(); + } + @Override + public String getTabName() { + return Localization.lang("Entry table columns"); } public void initialize() { - viewModel = new TableColumnsTabViewModel(dialogService, preferences, frame); + TableColumnsTabViewModel tableColumnsTabViewModel = new TableColumnsTabViewModel(dialogService, preferences, frame); + this.viewModel = tableColumnsTabViewModel; - columnsList.itemsProperty().bindBidirectional(viewModel.columnsListProperty()); + columnsList.itemsProperty().bindBidirectional(tableColumnsTabViewModel.columnsListProperty()); columnsList.setOnKeyTyped(event -> jumpToSearchKey(columnsList, event)); columnsList.setCellFactory(checkBoxListView -> new CheckBoxListCell(columnsList::getItemBooleanProperty) { @Override @@ -100,17 +101,17 @@ public void updateItem(TableColumnsItemModel item, boolean empty) { } }); - viewModel.checkedColumnsModelProperty().setValue(columnsList.getCheckModel()); + tableColumnsTabViewModel.checkedColumnsModelProperty().setValue(columnsList.getCheckModel()); - showFileColumn.selectedProperty().bindBidirectional(viewModel.fileFieldProperty()); - showUrlColumn.selectedProperty().bindBidirectional(viewModel.urlFieldEnabledProperty()); - urlFirst.selectedProperty().bindBidirectional(viewModel.preferUrlProperty()); - doiFirst.selectedProperty().bindBidirectional(viewModel.preferDoiProperty()); - showEprintColumn.selectedProperty().bindBidirectional(viewModel.eprintFieldProperty()); - enableSpecialFields.selectedProperty().bindBidirectional(viewModel.specialFieldsEnabledProperty()); - syncKeywords.selectedProperty().bindBidirectional(viewModel.specialFieldsSyncKeyWordsProperty()); - serializeSpecial.selectedProperty().bindBidirectional(viewModel.specialFieldsSerializeProperty()); - enableExtraColumns.selectedProperty().bindBidirectional(viewModel.extraFieldsEnabledProperty()); + showFileColumn.selectedProperty().bindBidirectional(tableColumnsTabViewModel.showFileColumnProperty()); + showUrlColumn.selectedProperty().bindBidirectional(tableColumnsTabViewModel.showUrlColumnProperty()); + urlFirst.selectedProperty().bindBidirectional(tableColumnsTabViewModel.preferUrlProperty()); + doiFirst.selectedProperty().bindBidirectional(tableColumnsTabViewModel.preferDoiProperty()); + showEprintColumn.selectedProperty().bindBidirectional(tableColumnsTabViewModel.showEPrintColumnProperty()); + enableSpecialFields.selectedProperty().bindBidirectional(tableColumnsTabViewModel.specialFieldsEnabledProperty()); + syncKeywords.selectedProperty().bindBidirectional(tableColumnsTabViewModel.specialFieldsSyncKeyWordsProperty()); + serializeSpecial.selectedProperty().bindBidirectional(tableColumnsTabViewModel.specialFieldsSerializeProperty()); + enableExtraColumns.selectedProperty().bindBidirectional(tableColumnsTabViewModel.showExtraFileColumnsProperty()); ActionFactory actionFactory = new ActionFactory(Globals.getKeyPrefs()); actionFactory.configureIconButton(PreferencesActions.COLUMN_SORT_UP, new SimpleCommand() { @@ -134,9 +135,6 @@ public void updateItem(TableColumnsItemModel item, boolean empty) { public void execute() { String ab = "a" + "b"; } }, updateToTable); actionFactory.configureIconButton(StandardActions.HELP_SPECIAL_FIELDS, new HelpAction(HelpFile.SPECIAL_FIELDS), enableSpecialFieldsHelp); - - viewModel.setValues(); - viewModel.setChecks(); } private void jumpToSearchKey(CheckListView list, KeyEvent keypressed) { @@ -155,23 +153,4 @@ private void jumpToSearchKey(CheckListView list, KeyEvent list.getItems().stream().filter(item -> item.getName().toLowerCase().startsWith(listSearchTerm)) .findFirst().ifPresent(list::scrollTo); } - - @Override - public Node getBuilder() { - return this; - } - - @Override - public void setValues() { viewModel.setValues(); } - - @Override - public void storeSettings() { viewModel.storeSettings(); } - - @Override - public boolean validateSettings() { return viewModel.validateSettings(); } - - @Override - public String getTabName() { - return Localization.lang("Entry table columns"); - } } diff --git a/src/main/java/org/jabref/gui/preferences/TableColumnsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/TableColumnsTabViewModel.java index 92e6310f2a7..1bd7bab3b24 100644 --- a/src/main/java/org/jabref/gui/preferences/TableColumnsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/TableColumnsTabViewModel.java @@ -1,10 +1,13 @@ package org.jabref.gui.preferences; +import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.TreeMap; import java.util.stream.Collectors; import javafx.beans.property.ListProperty; @@ -20,6 +23,7 @@ import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.maintable.ColumnPreferences; import org.jabref.gui.util.NoCheckModel; +import org.jabref.logic.l10n.Localization; import org.jabref.model.entry.field.Field; import org.jabref.model.entry.field.FieldFactory; import org.jabref.model.entry.field.FieldProperty; @@ -35,25 +39,28 @@ public class TableColumnsTabViewModel implements PreferenceTabViewModel { private final ListProperty columnsListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); private final ObjectProperty> checkedColumnsModelProperty = new SimpleObjectProperty<>(new NoCheckModel<>()); - // ToDo: Convert to CheckListView-CheckModel private final SimpleBooleanProperty specialFieldsEnabledProperty = new SimpleBooleanProperty(); private final SimpleBooleanProperty specialFieldsSyncKeyWordsProperty = new SimpleBooleanProperty(); private final SimpleBooleanProperty specialFieldsSerializeProperty = new SimpleBooleanProperty(); - private final SimpleBooleanProperty fileFieldProperty = new SimpleBooleanProperty(); - private final SimpleBooleanProperty urlFieldEnabledProperty = new SimpleBooleanProperty(); + private final SimpleBooleanProperty showFileColumnProperty = new SimpleBooleanProperty(); + private final SimpleBooleanProperty showUrlColumnProperty = new SimpleBooleanProperty(); private final SimpleBooleanProperty preferUrlProperty = new SimpleBooleanProperty(); private final SimpleBooleanProperty preferDoiProperty = new SimpleBooleanProperty(); - private final SimpleBooleanProperty ePrintFieldProperty = new SimpleBooleanProperty(); + private final SimpleBooleanProperty showEPrintColumnProperty = new SimpleBooleanProperty(); - private final SimpleBooleanProperty extraFileFieldsEnabledProperty = new SimpleBooleanProperty(); + private final SimpleBooleanProperty showExtraFileColumnsProperty = new SimpleBooleanProperty(); + + private List restartWarnings = new ArrayList<>(); private final DialogService dialogService; + private final JabRefPreferences preferences; private final ColumnPreferences columnPreferences; private final JabRefFrame frame; public TableColumnsTabViewModel(DialogService dialogService, JabRefPreferences preferences, JabRefFrame frame) { this.dialogService = dialogService; + this.preferences = preferences; this.columnPreferences = preferences.getColumnPreferences(); this.frame = frame; @@ -63,16 +70,14 @@ public TableColumnsTabViewModel(DialogService dialogService, JabRefPreferences p } else { removeSpecialColumns(); } - // ToDo: RestartMessage }); - extraFileFieldsEnabledProperty.addListener((observable, oldValue, newValue) -> { + showExtraFileColumnsProperty.addListener((observable, oldValue, newValue) -> { if (newValue) { insertExtraFileColumns(); } else { removeExtraFileColumns(); } - // ToDo: RestartMessage }); } @@ -83,7 +88,11 @@ public void setValues() { columnsListProperty.getValue().clear(); columnPreferences.getNormalColumns().forEach( - item -> columnsListProperty.getValue().add(new TableColumnsItemModel(FieldFactory.parseField(item)))); + item -> columnsListProperty.getValue().add( + new TableColumnsItemModel( + FieldFactory.parseField(item), + columnPreferences.getPrefColumnWidth(item) + ))); // Internal Fields fields.add(new TableColumnsItemModel(InternalField.OWNER)); @@ -103,50 +112,70 @@ public void setValues() { } // HardCoded Fields - fileFieldProperty.setValue(columnPreferences.showFileColumn()); - urlFieldEnabledProperty.setValue(columnPreferences.showUrlColumn()); + showFileColumnProperty.setValue(columnPreferences.showFileColumn()); + showUrlColumnProperty.setValue(columnPreferences.showUrlColumn()); preferUrlProperty.setValue(!columnPreferences.preferDoiOverUrl()); preferDoiProperty.setValue(columnPreferences.preferDoiOverUrl()); - ePrintFieldProperty.setValue(columnPreferences.showEprintColumn()); - extraFileFieldsEnabledProperty.setValue(!columnPreferences.getExtraFileColumns().isEmpty()); - if (extraFileFieldsEnabledProperty.getValue()) { + showEPrintColumnProperty.setValue(columnPreferences.showEprintColumn()); + showExtraFileColumnsProperty.setValue(!columnPreferences.getExtraFileColumns().isEmpty()); + if (showExtraFileColumnsProperty.getValue()) { insertExtraFileColumns(); } insertColumns(EnumSet.allOf(StandardField.class).stream().map(TableColumnsItemModel::new).collect(Collectors.toSet())); + + // Checks + List checks = new ArrayList<>(columnPreferences.getNormalColumns()); + if (specialFieldsEnabledProperty.getValue()) { + List specialFields = new ArrayList<>(columnPreferences.getSpecialFieldColumns()); + checks.add(specialFields.contains(SpecialField.QUALITY) ? SpecialField.QUALITY.getName() : null); + checks.add(specialFields.contains(SpecialField.PRIORITY) ? SpecialField.READ_STATUS.getName() : null); + checks.add(specialFields.contains(SpecialField.RELEVANCE) ? SpecialField.RELEVANCE.getName() : null); + checks.add(specialFields.contains(SpecialField.PRINTED) ? SpecialField.PRINTED.getName() : null); + checks.add(specialFields.contains(SpecialField.READ_STATUS) ? SpecialField.READ_STATUS.getName() : null); + } + setChecks(checks); } private void insertSpecialColumns() { + List backupChecks = getChecks(); Set fields = new HashSet<>(); EnumSet.allOf(SpecialField.class) .forEach(specialField -> fields.add(new TableColumnsItemModel(specialField))); insertColumns(fields); + setChecks(backupChecks); } private void removeSpecialColumns() { + List backupChecks = getChecks(); List columns = columnsListProperty.getValue().stream() .filter(column -> (column.getField() instanceof SpecialField)) .collect(Collectors.toList()); columnsListProperty.getValue().removeAll(columns); + setChecks(backupChecks); } private void insertExtraFileColumns() { + List backupChecks = getChecks(); Set fileTypes = ExternalFileTypes.getInstance().getExternalFileTypeSelection(); Set fileColumns = new HashSet<>(); fileTypes.stream().map(ExternalFileType::getName) .forEach(fileName -> fileColumns.add(new TableColumnsItemModel(new ExtraFileField(fileName)))); insertColumns(fileColumns); + setChecks(backupChecks); } private void removeExtraFileColumns() { + List backupChecks = getChecks(); List columns = columnsListProperty.getValue().stream() .filter(column -> (column.getField() instanceof ExtraFileField)) .collect(Collectors.toList()); columnsListProperty.getValue().removeAll(columns); + setChecks(backupChecks); } private void insertColumns(Set fields) { @@ -155,60 +184,84 @@ private void insertColumns(Set fields) { .forEach(columnsListProperty.getValue()::add); } - public void setChecks() { - /* columnPreferences.getNormalColumns().stream().map(FieldFactory::parseField).forEach( - field -> columnsListProperty.stream().filter(item -> item.getField().equals(field)) - .forEach(item -> checkedColumnsModelProperty.getValue().check(item)) - ); */ + public List getChecks() { + return checkedColumnsModelProperty.getValue().getCheckedItems().stream() + .map(item -> item.getField().getName()) + .collect(Collectors.toList()); + } - List fieldNames = columnPreferences.getNormalColumns(); - for (TableColumnsItemModel item : columnsListProperty.getValue()) { - if (fieldNames.contains(item.getField().getName())) { - checkedColumnsModelProperty.getValue().check(item); - } - } - /* columnsListProperty.stream().filter(item -> fieldNames.contains(item.getField().getName())) - .forEach(item -> checkedColumnsModelProperty.getValue().check(item)); */ - - /* specialFieldRankingProperty.setValue(columnPreferences.getSpecialFieldColumns().contains(SpecialField.RANKING)); - specialFieldQualityProperty.setValue(columnPreferences.getSpecialFieldColumns().contains(SpecialField.QUALITY)); - specialFieldPriorityProperty.setValue(columnPreferences.getSpecialFieldColumns().contains(SpecialField.PRIORITY)); - specialFieldRelevanceProperty.setValue(columnPreferences.getSpecialFieldColumns().contains(SpecialField.RELEVANCE)); - specialFieldPrintedProperty.setValue(columnPreferences.getSpecialFieldColumns().contains(SpecialField.PRINTED)); - specialFieldReadStatusProperty.setValue(columnPreferences.getSpecialFieldColumns().contains(SpecialField.READ_STATUS)); */ - - /* if (specialField == SpecialField.RANKING && specialFieldRankingProperty.getValue()) { - checkedColumnsModelProperty.getValue().check(column); - } else if (specialField == SpecialField.QUALITY && specialFieldQualityProperty.getValue()) { - checkedColumnsModelProperty.getValue().check(column); - } else if (specialField == SpecialField.PRIORITY && specialFieldPriorityProperty.getValue()) { - checkedColumnsModelProperty.getValue().check(column); - } else if (specialField == SpecialField.RELEVANCE && specialFieldRelevanceProperty.getValue()) { - checkedColumnsModelProperty.getValue().check(column); - } else if (specialField == SpecialField.PRINTED && specialFieldPrintedProperty.getValue()) { - checkedColumnsModelProperty.getValue().check(column); - } else if (specialField == SpecialField.READ_STATUS && specialFieldReadStatusProperty.getValue()) { - checkedColumnsModelProperty.getValue().check(column); - } */ + public void setChecks(List fieldNames) { + columnsListProperty.stream().filter(item -> fieldNames.contains(item.getField().getName())) + .forEach(item -> checkedColumnsModelProperty.getValue().check(item)); } @Override public void storeSettings() { + List normalColumns = getChecks(); + List specialFields = new ArrayList<>(); + List deleteNames = new ArrayList<>(); + List extraFileNames = new ArrayList<>(); + Map columnWidths = new TreeMap<>(); + + normalColumns.forEach(fieldName -> { + SpecialField.fromName(fieldName).ifPresent(field -> { + specialFields.add(field); + deleteNames.add(fieldName); + }); + if (columnPreferences.getExtraFileColumns().contains(fieldName)) { + extraFileNames.add(fieldName); + deleteNames.add(fieldName); + } + }); + normalColumns.removeAll(deleteNames); + + normalColumns.forEach(field -> columnWidths.put(field,columnPreferences.getPrefColumnWidth(field))); + + ColumnPreferences newColumnPreferences = new ColumnPreferences( + showFileColumnProperty.getValue(), + showUrlColumnProperty.getValue(), + preferDoiProperty.getValue(), + showEPrintColumnProperty.getValue(), + normalColumns, + specialFields, + specialFieldsSyncKeyWordsProperty.getValue(), + specialFieldsSerializeProperty.getValue(), + extraFileNames, + columnWidths, + columnPreferences.getSortTypesForColumns() + ); + + if (!(columnPreferences.getSpecialFieldColumns().equals(newColumnPreferences.getSpecialFieldColumns()))) { + restartWarnings.add(Localization.lang("Special Field Column added/removed")); + } + + if (columnPreferences.getAutoSyncSpecialFieldsToKeyWords() != newColumnPreferences.getAutoSyncSpecialFieldsToKeyWords()) { + restartWarnings.add(Localization.lang("Synchronize SpecialFields to keywords")); + } + + if (columnPreferences.getSerializeSpecialFields() != newColumnPreferences.getSerializeSpecialFields()) { + restartWarnings.add(Localization.lang("Serialize special fields")); + } + + preferences.storeColumnPreferences(newColumnPreferences); } @Override - public boolean validateSettings() { - return true; + public boolean validateSettings() { return true; } // should contain at least one column + + @Override + public List getRestartWarnings() { + return restartWarnings; } public ListProperty columnsListProperty() { return this.columnsListProperty; } public ObjectProperty> checkedColumnsModelProperty() { return this.checkedColumnsModelProperty; } - public SimpleBooleanProperty fileFieldProperty() { return this.fileFieldProperty; } + public SimpleBooleanProperty showFileColumnProperty() { return this.showFileColumnProperty; } - public SimpleBooleanProperty urlFieldEnabledProperty() { return this.urlFieldEnabledProperty; } + public SimpleBooleanProperty showUrlColumnProperty() { return this.showUrlColumnProperty; } public SimpleBooleanProperty preferUrlProperty() { return this.preferUrlProperty; } @@ -220,9 +273,9 @@ public boolean validateSettings() { public SimpleBooleanProperty specialFieldsSerializeProperty() { return this.specialFieldsSerializeProperty; } - public SimpleBooleanProperty eprintFieldProperty() { return this.ePrintFieldProperty; } + public SimpleBooleanProperty showEPrintColumnProperty() { return this.showEPrintColumnProperty; } - public SimpleBooleanProperty extraFieldsEnabledProperty() { return this.extraFileFieldsEnabledProperty; } + public SimpleBooleanProperty showExtraFileColumnsProperty() { return this.showExtraFileColumnsProperty; } class ExtraFileField implements Field {