From 456c05676f545c4c29c3ab58dcc929103a3c9549 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Tue, 10 Mar 2020 17:36:24 +0100 Subject: [PATCH 01/10] Reordered packages, removed CustomJFXPanel.java --- .../gui/cleanup/CleanupPresetPanel.java | 1 + .../BibtexKeyPatternPanel.fxml} | 2 +- .../BibtexKeyPatternPanel.java} | 34 +++++++++---------- .../BibtexKeyPatternPanelItemModel.java} | 6 ++-- .../BibtexKeyPatternPanelViewModel.java} | 22 ++++++------ .../FieldFormatterCleanupsPanel.java | 2 +- .../SaveOrderConfigPanel.fxml} | 2 +- .../SaveOrderConfigPanel.java} | 10 +++--- .../SaveOrderConfigPanelViewModel.java} | 6 ++-- .../jabref/gui/customjfx/CustomJFXPanel.java | 21 ------------ .../LibraryPropertiesDialogView.java | 16 ++++----- .../preferences/BibtexKeyPatternTabView.java | 6 ++-- .../BibtexKeyPatternTabViewModel.java | 14 ++++---- .../gui/preferences/ExportSortingTabView.java | 6 ++-- 14 files changed, 64 insertions(+), 84 deletions(-) rename src/main/java/org/jabref/gui/{bibtexkeypattern/BibtexKeyPatternTable.fxml => commonfxcontrols/BibtexKeyPatternPanel.fxml} (90%) rename src/main/java/org/jabref/gui/{bibtexkeypattern/BibtexKeyPatternTableView.java => commonfxcontrols/BibtexKeyPatternPanel.java} (79%) rename src/main/java/org/jabref/gui/{bibtexkeypattern/BibtexKeyPatternTableItemModel.java => commonfxcontrols/BibtexKeyPatternPanelItemModel.java} (88%) rename src/main/java/org/jabref/gui/{bibtexkeypattern/BibtexKeyPatternTableViewModel.java => commonfxcontrols/BibtexKeyPatternPanelViewModel.java} (83%) rename src/main/java/org/jabref/gui/{cleanup => commonfxcontrols}/FieldFormatterCleanupsPanel.java (99%) rename src/main/java/org/jabref/gui/{SaveOrderConfigDisplay.fxml => commonfxcontrols/SaveOrderConfigPanel.fxml} (97%) rename src/main/java/org/jabref/gui/{SaveOrderConfigDisplayView.java => commonfxcontrols/SaveOrderConfigPanel.java} (93%) rename src/main/java/org/jabref/gui/{SaveOrderConfigDisplayViewModel.java => commonfxcontrols/SaveOrderConfigPanelViewModel.java} (97%) delete mode 100644 src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java diff --git a/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java b/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java index 563fb7ac9e4..6e29f84563c 100644 --- a/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java +++ b/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java @@ -11,6 +11,7 @@ import javafx.scene.control.Label; import javafx.scene.layout.VBox; +import org.jabref.gui.commonfxcontrols.FieldFormatterCleanupsPanel; import org.jabref.logic.cleanup.CleanupPreset; import org.jabref.logic.l10n.Localization; import org.jabref.model.database.BibDatabaseContext; diff --git a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTable.fxml b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanel.fxml similarity index 90% rename from src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTable.fxml rename to src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanel.fxml index 5b2924eb764..0031e6a1fd4 100644 --- a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTable.fxml +++ b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanel.fxml @@ -5,7 +5,7 @@ + fx:controller="org.jabref.gui.commonfxcontrols.BibtexKeyPatternPanel"> diff --git a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableView.java b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanel.java similarity index 79% rename from src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableView.java rename to src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanel.java index 124e8b8c48c..a6f96bb25b5 100644 --- a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableView.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanel.java @@ -1,4 +1,4 @@ -package org.jabref.gui.bibtexkeypattern; +package org.jabref.gui.commonfxcontrols; import java.util.Collection; @@ -21,21 +21,21 @@ import com.airhacks.afterburner.views.ViewLoader; -public class BibtexKeyPatternTableView extends TableView { +public class BibtexKeyPatternPanel extends TableView { - @FXML public TableColumn entryTypeColumn; - @FXML public TableColumn patternColumn; - @FXML public TableColumn actionsColumn; + @FXML public TableColumn entryTypeColumn; + @FXML public TableColumn patternColumn; + @FXML public TableColumn actionsColumn; - private BibtexKeyPatternTableViewModel viewModel; + private BibtexKeyPatternPanelViewModel viewModel; private long lastKeyPressTime; private String tableSearchTerm; - public BibtexKeyPatternTableView(JabRefPreferences preferences, Collection entryTypeList, AbstractBibtexKeyPattern keyPattern) { + public BibtexKeyPatternPanel(JabRefPreferences preferences, Collection entryTypeList, AbstractBibtexKeyPattern keyPattern) { super(); - viewModel = new BibtexKeyPatternTableViewModel(preferences, entryTypeList, keyPattern); + viewModel = new BibtexKeyPatternPanelViewModel(preferences, entryTypeList, keyPattern); ViewLoader.view(this) .root(this) @@ -49,11 +49,11 @@ private void initialize() { entryTypeColumn.setSortable(true); entryTypeColumn.setReorderable(false); entryTypeColumn.setCellValueFactory(cellData -> cellData.getValue().entryType()); - new ValueTableCellFactory() + new ValueTableCellFactory() .withText(EntryType::getDisplayName) .install(entryTypeColumn); this.setOnSort(event -> - viewModel.patternListProperty().sort(BibtexKeyPatternTableViewModel.defaultOnTopComparator)); + viewModel.patternListProperty().sort(BibtexKeyPatternPanelViewModel.defaultOnTopComparator)); patternColumn.setSortable(true); patternColumn.setReorderable(false); @@ -61,13 +61,13 @@ private void initialize() { patternColumn.setCellFactory(TextFieldTableCell.forTableColumn()); patternColumn.setEditable(true); patternColumn.setOnEditCommit( - (TableColumn.CellEditEvent event) -> + (TableColumn.CellEditEvent event) -> event.getRowValue().setPattern(event.getNewValue())); actionsColumn.setSortable(false); actionsColumn.setReorderable(false); actionsColumn.setCellValueFactory(cellData -> cellData.getValue().entryType()); - new ValueTableCellFactory() + new ValueTableCellFactory() .withGraphic(entryType -> IconTheme.JabRefIcons.REFRESH.getGraphicNode()) .withTooltip(entryType -> String.format(Localization.lang("Reset %s to default value"), entryType.getDisplayName())) @@ -84,9 +84,9 @@ private void initialize() { public void resetAll() { viewModel.resetAll(); } - public ListProperty patternListProperty() { return viewModel.patternListProperty(); } + public ListProperty patternListProperty() { return viewModel.patternListProperty(); } - public ObjectProperty defaultKeyPatternProperty() { return viewModel.defaultKeyPatternProperty(); } + public ObjectProperty defaultKeyPatternProperty() { return viewModel.defaultKeyPatternProperty(); } private void jumpToSearchKey(KeyEvent keypressed) { if (keypressed.getCharacter() == null) { @@ -105,15 +105,15 @@ private void jumpToSearchKey(KeyEvent keypressed) { .findFirst().ifPresent(this::scrollTo); } - private static class HighlightTableRow extends TableRow { + private static class HighlightTableRow extends TableRow { @Override - public void updateItem(BibtexKeyPatternTableItemModel item, boolean empty) { + public void updateItem(BibtexKeyPatternPanelItemModel item, boolean empty) { super.updateItem(item, empty); if (item == null || item.getEntryType() == null) { setStyle(""); } else if (isSelected()) { setStyle("-fx-background-color: -fx-selection-bar"); - } else if (item.getEntryType().getName().equals(BibtexKeyPatternTableViewModel.ENTRY_TYPE_DEFAULT_NAME)) { + } else if (item.getEntryType().getName().equals(BibtexKeyPatternPanelViewModel.ENTRY_TYPE_DEFAULT_NAME)) { setStyle("-fx-background-color: -fx-default-button"); } else { setStyle(""); diff --git a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableItemModel.java b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanelItemModel.java similarity index 88% rename from src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableItemModel.java rename to src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanelItemModel.java index 7431346de46..f65180bd6a1 100644 --- a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableItemModel.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanelItemModel.java @@ -1,4 +1,4 @@ -package org.jabref.gui.bibtexkeypattern; +package org.jabref.gui.commonfxcontrols; import java.util.Objects; @@ -9,11 +9,11 @@ import org.jabref.model.entry.types.EntryType; -public class BibtexKeyPatternTableItemModel { +public class BibtexKeyPatternPanelItemModel { private final ObjectProperty entryType = new SimpleObjectProperty<>(); private final StringProperty pattern = new SimpleStringProperty(""); - public BibtexKeyPatternTableItemModel(EntryType entryType, String pattern) { + public BibtexKeyPatternPanelItemModel(EntryType entryType, String pattern) { Objects.requireNonNull(entryType); Objects.requireNonNull(pattern); this.entryType.setValue(entryType); diff --git a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableViewModel.java b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanelViewModel.java similarity index 83% rename from src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableViewModel.java rename to src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanelViewModel.java index 971347b4b3f..ccd765635bf 100644 --- a/src/main/java/org/jabref/gui/bibtexkeypattern/BibtexKeyPatternTableViewModel.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/BibtexKeyPatternPanelViewModel.java @@ -1,4 +1,4 @@ -package org.jabref.gui.bibtexkeypattern; +package org.jabref.gui.commonfxcontrols; import java.util.Collection; import java.util.Comparator; @@ -15,11 +15,11 @@ import org.jabref.model.entry.types.EntryType; import org.jabref.preferences.JabRefPreferences; -public class BibtexKeyPatternTableViewModel { +public class BibtexKeyPatternPanelViewModel { public static final String ENTRY_TYPE_DEFAULT_NAME = "default"; - public static Comparator defaultOnTopComparator = (o1, o2) -> { + public static Comparator defaultOnTopComparator = (o1, o2) -> { String itemOneName = o1.getEntryType().getName(); String itemTwoName = o2.getEntryType().getName(); @@ -34,13 +34,13 @@ public class BibtexKeyPatternTableViewModel { return 0; }; - private final ListProperty patternListProperty = new SimpleListProperty<>(); - private final ObjectProperty defaultItemProperty = new SimpleObjectProperty<>(); + private final ListProperty patternListProperty = new SimpleListProperty<>(); + private final ObjectProperty defaultItemProperty = new SimpleObjectProperty<>(); private final AbstractBibtexKeyPattern initialKeyPattern; private final Collection bibEntryTypeList; private final JabRefPreferences preferences; - public BibtexKeyPatternTableViewModel(JabRefPreferences preferences, Collection entryTypeList, AbstractBibtexKeyPattern initialKeyPattern) { + public BibtexKeyPatternPanelViewModel(JabRefPreferences preferences, Collection entryTypeList, AbstractBibtexKeyPattern initialKeyPattern) { this.preferences = preferences; this.bibEntryTypeList = entryTypeList; this.initialKeyPattern = initialKeyPattern; @@ -54,7 +54,7 @@ public void setValues() { defaultPattern = initialKeyPattern.getDefaultValue().get(0); } - defaultItemProperty.setValue(new BibtexKeyPatternTableItemModel(new DefaultEntryType(), defaultPattern)); + defaultItemProperty.setValue(new BibtexKeyPatternPanelItemModel(new DefaultEntryType(), defaultPattern)); patternListProperty.setValue(FXCollections.observableArrayList()); patternListProperty.add(defaultItemProperty.getValue()); @@ -67,11 +67,11 @@ public void setValues() { } else { pattern = initialKeyPattern.getPatterns().get(entryType).get(0); } - patternListProperty.add(new BibtexKeyPatternTableItemModel(entryType, pattern)); + patternListProperty.add(new BibtexKeyPatternPanelItemModel(entryType, pattern)); }); } - public void setItemToDefaultPattern(BibtexKeyPatternTableItemModel item) { + public void setItemToDefaultPattern(BibtexKeyPatternPanelItemModel item) { item.setPattern((String) preferences.defaults.get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN)); } @@ -80,9 +80,9 @@ public void resetAll() { defaultItemProperty.getValue().setPattern((String) preferences.defaults.get(JabRefPreferences.DEFAULT_BIBTEX_KEY_PATTERN)); } - public ListProperty patternListProperty() { return patternListProperty; } + public ListProperty patternListProperty() { return patternListProperty; } - public ObjectProperty defaultKeyPatternProperty() { return defaultItemProperty; } + public ObjectProperty defaultKeyPatternProperty() { return defaultItemProperty; } public static class DefaultEntryType implements EntryType { @Override diff --git a/src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java similarity index 99% rename from src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java rename to src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java index 70cc748e2b7..239bcb289ee 100644 --- a/src/main/java/org/jabref/gui/cleanup/FieldFormatterCleanupsPanel.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java @@ -1,4 +1,4 @@ -package org.jabref.gui.cleanup; +package org.jabref.gui.commonfxcontrols; import java.util.List; import java.util.Objects; diff --git a/src/main/java/org/jabref/gui/SaveOrderConfigDisplay.fxml b/src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanel.fxml similarity index 97% rename from src/main/java/org/jabref/gui/SaveOrderConfigDisplay.fxml rename to src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanel.fxml index 17e458f2e81..e0d26cf19d3 100644 --- a/src/main/java/org/jabref/gui/SaveOrderConfigDisplay.fxml +++ b/src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanel.fxml @@ -12,7 +12,7 @@ + fx:controller="org.jabref.gui.commonfxcontrols.SaveOrderConfigPanel"> diff --git a/src/main/java/org/jabref/gui/SaveOrderConfigDisplayView.java b/src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanel.java similarity index 93% rename from src/main/java/org/jabref/gui/SaveOrderConfigDisplayView.java rename to src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanel.java index 15679bc9ac2..0224e9442ab 100644 --- a/src/main/java/org/jabref/gui/SaveOrderConfigDisplayView.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanel.java @@ -1,4 +1,4 @@ -package org.jabref.gui; +package org.jabref.gui.commonfxcontrols; import javax.inject.Inject; @@ -18,7 +18,7 @@ import com.airhacks.afterburner.views.ViewLoader; -public class SaveOrderConfigDisplayView extends VBox { +public class SaveOrderConfigPanel extends VBox { @FXML private ToggleGroup saveOrderToggleGroup; @FXML private RadioButton exportInSpecifiedOrder; @@ -32,9 +32,9 @@ public class SaveOrderConfigDisplayView extends VBox { @FXML private CheckBox saveTerDesc; @Inject private PreferencesService preferencesService; - private SaveOrderConfigDisplayViewModel viewModel; + private SaveOrderConfigPanelViewModel viewModel; - public SaveOrderConfigDisplayView() { + public SaveOrderConfigPanel() { ViewLoader.view(this) .root(this) .load(); @@ -43,7 +43,7 @@ public SaveOrderConfigDisplayView() { @FXML private void initialize() { - viewModel = new SaveOrderConfigDisplayViewModel(preferencesService); + viewModel = new SaveOrderConfigPanelViewModel(preferencesService); exportInOriginalOrder.selectedProperty().bindBidirectional(viewModel.saveInOriginalProperty()); exportInTableOrder.selectedProperty().bindBidirectional(viewModel.saveInTableOrderProperty()); diff --git a/src/main/java/org/jabref/gui/SaveOrderConfigDisplayViewModel.java b/src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanelViewModel.java similarity index 97% rename from src/main/java/org/jabref/gui/SaveOrderConfigDisplayViewModel.java rename to src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanelViewModel.java index 89ef04f1c25..878f2a7ec64 100644 --- a/src/main/java/org/jabref/gui/SaveOrderConfigDisplayViewModel.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/SaveOrderConfigPanelViewModel.java @@ -1,4 +1,4 @@ -package org.jabref.gui; +package org.jabref.gui.commonfxcontrols; import java.util.Objects; import java.util.Set; @@ -17,7 +17,7 @@ import org.jabref.model.metadata.SaveOrderConfig.SortCriterion; import org.jabref.preferences.PreferencesService; -public class SaveOrderConfigDisplayViewModel { +public class SaveOrderConfigPanelViewModel { private final ListProperty priSortFieldsProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); private final ListProperty secSortFieldsProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); @@ -37,7 +37,7 @@ public class SaveOrderConfigDisplayViewModel { private final PreferencesService prefs; - public SaveOrderConfigDisplayViewModel(PreferencesService prefs) { + public SaveOrderConfigPanelViewModel(PreferencesService prefs) { this.prefs = prefs; Set fieldNames = FieldFactory.getCommonFields(); diff --git a/src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java b/src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java deleted file mode 100644 index e21f8ce805e..00000000000 --- a/src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.jabref.gui.customjfx; - -import javafx.embed.swing.JFXPanel; -import javafx.scene.Scene; - -import org.jabref.Globals; -import org.jabref.gui.util.DefaultTaskExecutor; - -/** - * TODO: Remove as soon as possible - */ -public class CustomJFXPanel { - - public static JFXPanel wrap(Scene scene) { - JFXPanel container = new JFXPanel(); - Globals.getThemeLoader().installCss(scene, Globals.prefs); - DefaultTaskExecutor.runInJavaFXThread(() -> container.setScene(scene)); - return container; - } - -} diff --git a/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialogView.java b/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialogView.java index ea4066568b0..c05fd46db06 100644 --- a/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialogView.java +++ b/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialogView.java @@ -17,8 +17,8 @@ import org.jabref.gui.BasePanel; import org.jabref.gui.DialogService; -import org.jabref.gui.SaveOrderConfigDisplayView; -import org.jabref.gui.cleanup.FieldFormatterCleanupsPanel; +import org.jabref.gui.commonfxcontrols.FieldFormatterCleanupsPanel; +import org.jabref.gui.commonfxcontrols.SaveOrderConfigPanel; import org.jabref.gui.util.BaseDialog; import org.jabref.logic.l10n.Localization; import org.jabref.model.database.BibDatabaseMode; @@ -43,7 +43,7 @@ public class LibraryPropertiesDialogView extends BaseDialog { private LibraryPropertiesDialogViewModel viewModel; private final DialogService dialogService; private FieldFormatterCleanupsPanel fieldFormatterCleanupsPanel; - private SaveOrderConfigDisplayView saveOrderConfigDisplayView; + private SaveOrderConfigPanel saveOrderConfigPanel; private SaveOrderConfig oldSaveOrderConfig; public LibraryPropertiesDialogView(BasePanel panel, DialogService dialogService) { @@ -80,16 +80,16 @@ private void initialize() { databaseMode.itemsProperty().bind(viewModel.databaseModesProperty()); databaseMode.valueProperty().bindBidirectional(viewModel.selectedDatabaseModeProperty()); - saveOrderConfigDisplayView = new SaveOrderConfigDisplayView(); + saveOrderConfigPanel = new SaveOrderConfigPanel(); Optional storedSaveOrderConfig = panel.getBibDatabaseContext().getMetaData().getSaveOrderConfig(); oldSaveOrderConfig = storedSaveOrderConfig.orElseGet(preferencesService::loadExportSaveOrder); - saveOrderConfigDisplayView.changeExportDescriptionToSave(); + saveOrderConfigPanel.changeExportDescriptionToSave(); fieldFormatterCleanupsPanel = new FieldFormatterCleanupsPanel(Localization.lang("Enable save actions")); Label saveActions = new Label(Localization.lang("Save actions")); saveActions.getStyleClass().add("sectionHeader"); - contentVbox.getChildren().addAll(saveOrderConfigDisplayView, saveActions, fieldFormatterCleanupsPanel); + contentVbox.getChildren().addAll(saveOrderConfigPanel, saveActions, fieldFormatterCleanupsPanel); protect.selectedProperty().bindBidirectional(viewModel.libraryProtectedProperty()); @@ -98,7 +98,7 @@ private void initialize() { private void setValues() { fieldFormatterCleanupsPanel.setValues(panel.getBibDatabaseContext().getMetaData()); - saveOrderConfigDisplayView.setValues(oldSaveOrderConfig); + saveOrderConfigPanel.setValues(oldSaveOrderConfig); } @FXML @@ -154,7 +154,7 @@ private void storeSettings() { metaData.markAsNotProtected(); } - SaveOrderConfig newSaveOrderConfig = saveOrderConfigDisplayView.getSaveOrderConfig(); + SaveOrderConfig newSaveOrderConfig = saveOrderConfigPanel.getSaveOrderConfig(); boolean saveOrderConfigChanged = !newSaveOrderConfig.equals(oldSaveOrderConfig); diff --git a/src/main/java/org/jabref/gui/preferences/BibtexKeyPatternTabView.java b/src/main/java/org/jabref/gui/preferences/BibtexKeyPatternTabView.java index a8821ca4c96..139c9bed6e7 100644 --- a/src/main/java/org/jabref/gui/preferences/BibtexKeyPatternTabView.java +++ b/src/main/java/org/jabref/gui/preferences/BibtexKeyPatternTabView.java @@ -10,7 +10,7 @@ import org.jabref.Globals; import org.jabref.gui.actions.ActionFactory; import org.jabref.gui.actions.StandardActions; -import org.jabref.gui.bibtexkeypattern.BibtexKeyPatternTableView; +import org.jabref.gui.commonfxcontrols.BibtexKeyPatternPanel; import org.jabref.gui.help.HelpAction; import org.jabref.logic.help.HelpFile; import org.jabref.logic.l10n.Localization; @@ -31,12 +31,12 @@ public class BibtexKeyPatternTabView extends AbstractPreferenceTabView patternListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); - private ObjectProperty defaultKeyPatternProperty = new SimpleObjectProperty<>( - new BibtexKeyPatternTableItemModel(new BibtexKeyPatternTableViewModel.DefaultEntryType(), "")); + private ListProperty patternListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); + private ObjectProperty defaultKeyPatternProperty = new SimpleObjectProperty<>( + new BibtexKeyPatternPanelItemModel(new BibtexKeyPatternPanelViewModel.DefaultEntryType(), "")); private final DialogService dialogService; private final JabRefPreferences preferences; @@ -133,7 +133,7 @@ public void storeSettings() { public StringProperty keyPatternReplacementProperty() { return keyPatternReplacementProperty; } - public ListProperty patternListProperty() { return patternListProperty; } + public ListProperty patternListProperty() { return patternListProperty; } - public ObjectProperty defaultKeyPatternProperty() { return defaultKeyPatternProperty; } + public ObjectProperty defaultKeyPatternProperty() { return defaultKeyPatternProperty; } } diff --git a/src/main/java/org/jabref/gui/preferences/ExportSortingTabView.java b/src/main/java/org/jabref/gui/preferences/ExportSortingTabView.java index a7fdbb2eec0..9cf3ef98950 100644 --- a/src/main/java/org/jabref/gui/preferences/ExportSortingTabView.java +++ b/src/main/java/org/jabref/gui/preferences/ExportSortingTabView.java @@ -5,13 +5,13 @@ import javafx.scene.control.Label; -import org.jabref.gui.SaveOrderConfigDisplayView; +import org.jabref.gui.commonfxcontrols.SaveOrderConfigPanel; import org.jabref.logic.l10n.Localization; import org.jabref.preferences.JabRefPreferences; public class ExportSortingTabView extends AbstractPreferenceTabView implements PreferencesTab { - private final SaveOrderConfigDisplayView exportOrderPanel; + private final SaveOrderConfigPanel exportOrderPanel; public ExportSortingTabView(JabRefPreferences preferences) { this.preferences = preferences; @@ -19,7 +19,7 @@ public ExportSortingTabView(JabRefPreferences preferences) { Label title = new Label(Localization.lang("Export sorting")); title.getStyleClass().add("titleHeader"); - exportOrderPanel = new SaveOrderConfigDisplayView(); + exportOrderPanel = new SaveOrderConfigPanel(); exportOrderPanel.setValues(preferences.loadExportSaveOrder()); this.setWidth(650.0); From 7eced7f90e25a8de43684d946b2bb78bf82b5e00 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Wed, 11 Mar 2020 01:17:41 +0100 Subject: [PATCH 02/10] Refactored FieldFormatterCleanupsPanel to mvvm-pattern --- .../gui/cleanup/CleanupPresetPanel.java | 6 +- .../FieldFormatterCleanupsPanel.fxml | 77 +++++ .../FieldFormatterCleanupsPanel.java | 309 ++++++------------ .../FieldFormatterCleanupsPanelViewModel.java | 78 +++++ .../OldFieldFormatterCleanupsPanel.java | 252 ++++++++++++++ .../LibraryPropertiesDialog.fxml | 5 + .../LibraryPropertiesDialogView.java | 44 ++- 7 files changed, 542 insertions(+), 229 deletions(-) create mode 100644 src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.fxml create mode 100644 src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java create mode 100644 src/main/java/org/jabref/gui/commonfxcontrols/OldFieldFormatterCleanupsPanel.java diff --git a/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java b/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java index 6e29f84563c..7e332691f54 100644 --- a/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java +++ b/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java @@ -11,7 +11,7 @@ import javafx.scene.control.Label; import javafx.scene.layout.VBox; -import org.jabref.gui.commonfxcontrols.FieldFormatterCleanupsPanel; +import org.jabref.gui.commonfxcontrols.OldFieldFormatterCleanupsPanel; import org.jabref.logic.cleanup.CleanupPreset; import org.jabref.logic.l10n.Localization; import org.jabref.model.database.BibDatabaseContext; @@ -35,7 +35,7 @@ public class CleanupPresetPanel extends VBox { @FXML private CheckBox cleanUpBiblatex; @FXML private CheckBox cleanUpBibtex; @FXML private VBox formatterContainer; - private FieldFormatterCleanupsPanel cleanUpFormatters; + private OldFieldFormatterCleanupsPanel cleanUpFormatters; public CleanupPresetPanel(BibDatabaseContext databaseContext, CleanupPreset cleanupPreset, FilePreferences filePreferences) { this.databaseContext = Objects.requireNonNull(databaseContext); @@ -64,7 +64,7 @@ private void init(CleanupPreset cleanupPreset, FilePreferences filePreferences) cleanUpUpgradeExternalLinks.setText(Localization.lang("Upgrade external PDF/PS links to use the '%0' field.", StandardField.FILE.getDisplayName())); - cleanUpFormatters = new FieldFormatterCleanupsPanel(Localization.lang("Run field formatter:")); + cleanUpFormatters = new OldFieldFormatterCleanupsPanel(Localization.lang("Run field formatter:")); formatterContainer.getChildren().setAll(cleanUpFormatters); String currentPattern = Localization.lang("Filename format pattern") diff --git a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.fxml b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.fxml new file mode 100644 index 00000000000..25c0ace5af6 --- /dev/null +++ b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.fxml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java index 239bcb289ee..5f6e1e064b1 100644 --- a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java @@ -1,252 +1,135 @@ package org.jabref.gui.commonfxcontrols; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.TreeSet; -import java.util.stream.Collectors; - -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import javafx.scene.control.Button; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.ListProperty; +import javafx.beans.property.ReadOnlyObjectWrapper; +import javafx.fxml.FXML; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; -import javafx.scene.control.Label; -import javafx.scene.control.ListView; import javafx.scene.control.SelectionMode; -import javafx.scene.layout.ColumnConstraints; -import javafx.scene.layout.GridPane; -import javafx.scene.layout.RowConstraints; - -import org.jabref.Globals; -import org.jabref.JabRefGUI; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.VBox; + +import org.jabref.gui.icon.IconTheme; +import org.jabref.gui.util.FieldsUtil; +import org.jabref.gui.util.ValueTableCellFactory; import org.jabref.gui.util.ViewModelListCellFactory; -import org.jabref.logic.cleanup.Cleanups; -import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter; import org.jabref.logic.l10n.Localization; import org.jabref.model.cleanup.FieldFormatterCleanup; -import org.jabref.model.cleanup.FieldFormatterCleanups; import org.jabref.model.cleanup.Formatter; -import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.field.Field; -import org.jabref.model.entry.field.FieldFactory; -import org.jabref.model.entry.field.InternalField; -import org.jabref.model.metadata.MetaData; - -import org.fxmisc.easybind.EasyBind; - -public class FieldFormatterCleanupsPanel extends GridPane { - - private static final String DESCRIPTION = Localization.lang("Description") + ": "; - private final CheckBox cleanupEnabled; - private final List availableFormatters; - private FieldFormatterCleanups fieldFormatterCleanups; - private ListView actionsList; - private ComboBox formattersCombobox; - private ComboBox selectFieldCombobox; - private Button addButton; - private Label descriptionAreaText; - private Button removeSelectedButton; - private Button removeAllButton; - private Button recommendButton; - private ObservableList actions; - - public FieldFormatterCleanupsPanel(String description) { - cleanupEnabled = new CheckBox(description); - availableFormatters = Cleanups.getBuiltInFormatters(); - availableFormatters.add(new ProtectTermsFormatter(Globals.protectedTermsLoader)); - } - public void setValues(MetaData metaData) { - Objects.requireNonNull(metaData); - Optional saveActions = metaData.getSaveActions(); - setValues(saveActions.orElse(Cleanups.DEFAULT_SAVE_ACTIONS)); - } +import com.airhacks.afterburner.views.ViewLoader; - public void setValues(FieldFormatterCleanups formatterCleanups) { - fieldFormatterCleanups = formatterCleanups; +public class FieldFormatterCleanupsPanel extends VBox { - // first clear existing content - this.getChildren().clear(); + @FXML private CheckBox cleanupsEnabled; + @FXML private TableView cleanupsList; + @FXML private TableColumn fieldColumn; + @FXML private TableColumn formatterColumn; + @FXML private TableColumn actionsColumn; + @FXML private ComboBox addableFields; + @FXML private ComboBox addableFormatters; - List configuredActions = fieldFormatterCleanups.getConfiguredActions(); - actions = FXCollections.observableArrayList(configuredActions); - buildLayout(); + private FieldFormatterCleanupsPanelViewModel viewModel; + + public FieldFormatterCleanupsPanel() { + ViewLoader.view(this) + .root(this) + .load(); } - private void buildLayout() { - setHgap(10.0); - setVgap(4.0); - - ColumnConstraints first = new ColumnConstraints(); - first.setPrefWidth(25); - ColumnConstraints second = new ColumnConstraints(); - second.setPrefWidth(175); - ColumnConstraints third = new ColumnConstraints(); - third.setPrefWidth(200); - ColumnConstraints fourth = new ColumnConstraints(); - fourth.setPrefWidth(200); - getColumnConstraints().addAll(first, second, third, fourth); - RowConstraints firstR = new RowConstraints(); - firstR.setPrefHeight(25); - RowConstraints secondR = new RowConstraints(); - secondR.setPrefHeight(100); - RowConstraints thirdR = new RowConstraints(); - thirdR.setPrefHeight(50); - RowConstraints fourthR = new RowConstraints(); - fourthR.setPrefHeight(50); - RowConstraints fifthR = new RowConstraints(); - fifthR.setPrefHeight(50); - getRowConstraints().addAll(firstR, secondR, thirdR, fourthR, fifthR); - add(cleanupEnabled, 0, 0, 4, 1); - - actionsList = new ListView<>(actions); - actionsList.setMinHeight(100.0); - actionsList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); - - new ViewModelListCellFactory() - .withText(action -> action.getField().getDisplayName() + ": " + action.getFormatter().getName()) - .withStringTooltip(action -> action.getFormatter().getDescription()) - .install(actionsList); - add(actionsList, 1, 1, 3, 1); - - removeAllButton = new Button(Localization.lang("Remove all")); - removeAllButton.setOnAction(e -> actions.clear()); - - BibDatabaseContext databaseContext = JabRefGUI.getMainFrame().getCurrentBasePanel().getBibDatabaseContext(); - - recommendButton = new Button(Localization.lang("Reset to recommended")); - boolean isBiblatex = databaseContext.isBiblatexMode(); - - recommendButton.setOnAction(e -> { - if (isBiblatex) { - actions.setAll(Cleanups.RECOMMEND_BIBLATEX_ACTIONS.getConfiguredActions()); - } else { - actions.setAll(Cleanups.RECOMMEND_BIBTEX_ACTIONS.getConfiguredActions()); - } - }); + @FXML + private void initialize() { + this.viewModel = new FieldFormatterCleanupsPanelViewModel(); - removeSelectedButton = new Button(Localization.lang("Remove selected")); - removeSelectedButton.setOnAction(e -> actions.remove(actionsList.getSelectionModel().getSelectedItem())); - descriptionAreaText = new Label(DESCRIPTION); - descriptionAreaText.setWrapText(true); + setupTable(); + setupCombos(); + setupBindings(); + } - add(recommendButton, 1, 2, 1, 1); - add(removeSelectedButton, 2, 2, 1, 1); - add(removeAllButton, 3, 2, 1, 1); - add(getSelectorPanel(), 1, 3, 3, 1); - add(descriptionAreaText, 1, 4, 3, 1); + private void setupTable() { + cleanupsList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); - updateDescription(); + // ToDo: To be editable the list needs a view model wrapper for FieldFormatterCleanup - // make sure the layout is set according to the checkbox - cleanupEnabled.selectedProperty().addListener(new EnablementStatusListener<>(fieldFormatterCleanups.isEnabled())); - cleanupEnabled.setSelected(fieldFormatterCleanups.isEnabled()); - } + fieldColumn.setCellValueFactory(cellData -> new ReadOnlyObjectWrapper<>(cellData.getValue().getField())); + new ValueTableCellFactory() + .withText(Field::getDisplayName) + .install(fieldColumn); + + formatterColumn.setCellValueFactory(cellData -> new ReadOnlyObjectWrapper<>(cellData.getValue().getFormatter())); + new ValueTableCellFactory() + .withText(Formatter::getName) + .install(formatterColumn); + + actionsColumn.setCellValueFactory(cellData -> new ReadOnlyObjectWrapper<>(cellData.getValue().getField())); + new ValueTableCellFactory() + .withGraphic(field -> IconTheme.JabRefIcons.DELETE_ENTRY.getGraphicNode()) + .withTooltip(field -> Localization.lang("Remove formatter for %0", field.getDisplayName())) + .withOnMouseClickedEvent(item -> event -> viewModel.removeCleanup(cleanupsList.getSelectionModel().getSelectedItem())) + .install(actionsColumn); - private void updateDescription() { - FieldFormatterCleanup formatterCleanup = getFieldFormatterCleanup(); - if (formatterCleanup.getFormatter() != null) { - descriptionAreaText.setText(DESCRIPTION + formatterCleanup.getFormatter().getDescription()); - } else { - Formatter selectedFormatter = formattersCombobox.getValue(); - if (selectedFormatter != null) { - descriptionAreaText.setText(DESCRIPTION + selectedFormatter.getDescription()); - } else { - descriptionAreaText.setText(DESCRIPTION); - } - } + viewModel.selectedCleanupProperty().setValue(cleanupsList.getSelectionModel()); + + cleanupsList.setOnKeyPressed(event -> { + if (event.getCode() == KeyCode.DELETE) { + viewModel.removeCleanup(cleanupsList.getSelectionModel().getSelectedItem()); + } + }); } - /** - * This panel contains the two comboboxes and the Add button - */ - private GridPane getSelectorPanel() { - GridPane builder = new GridPane(); - builder.setHgap(10.0); - Set fields = FieldFactory.getCommonFields(); - fields.add(InternalField.KEY_FIELD); - Set fieldsString = fields.stream().map(Field::getDisplayName).sorted().collect(Collectors.toCollection(TreeSet::new)); - selectFieldCombobox = new ComboBox<>(FXCollections.observableArrayList(fieldsString)); - selectFieldCombobox.setEditable(true); - builder.add(selectFieldCombobox, 1, 1); - - formattersCombobox = new ComboBox<>(FXCollections.observableArrayList(availableFormatters)); + private void setupCombos() { + new ViewModelListCellFactory() + .withText(Field::getDisplayName) + .install(addableFields); + addableFields.setConverter(FieldsUtil.fieldStringConverter); + addableFields.setOnKeyPressed(event -> { + if (event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER) { + addableFormatters.requestFocus(); + event.consume(); + } + }); + new ViewModelListCellFactory() .withText(Formatter::getName) .withStringTooltip(Formatter::getDescription) - .install(formattersCombobox); - EasyBind.subscribe(formattersCombobox.valueProperty(), e -> updateDescription()); - formattersCombobox.getSelectionModel().selectFirst(); - builder.add(formattersCombobox, 3, 1); - - addButton = new Button(Localization.lang("Add")); - addButton.setOnAction(e -> { - FieldFormatterCleanup newAction = getFieldFormatterCleanup(); - - if (!actions.contains(newAction)) { - actions.add(newAction); - } + .install(addableFormatters); + addableFormatters.setOnKeyPressed(event -> { + if (event.getCode() == KeyCode.ENTER) { + viewModel.addCleanup(); + event.consume(); + } }); - builder.add(addButton, 5, 1); - - return builder; } - public void storeSettings(MetaData metaData) { - Objects.requireNonNull(metaData); - - FieldFormatterCleanups formatterCleanups = getFormatterCleanups(); - - // if all actions have been removed, remove the save actions from the MetaData - if (formatterCleanups.getConfiguredActions().isEmpty()) { - metaData.clearSaveActions(); - return; - } - - metaData.setSaveActions(formatterCleanups); + private void setupBindings() { + cleanupsEnabled.selectedProperty().bindBidirectional(cleanupsDisableProperty()); + cleanupsList.itemsProperty().bind(viewModel.cleanupsListProperty()); + addableFields.itemsProperty().bind(viewModel.availableFieldsProperty()); + addableFields.valueProperty().bindBidirectional(viewModel.selectedFieldProperty()); + addableFormatters.itemsProperty().bind(viewModel.availableFormattersProperty()); + addableFormatters.valueProperty().bindBidirectional(viewModel.selectedFormatterProperty()); } - public FieldFormatterCleanups getFormatterCleanups() { - return new FieldFormatterCleanups(cleanupEnabled.isSelected(), actions); + @FXML + private void resetToRecommended() { + viewModel.resetToRecommended(); } - public boolean hasChanged() { - return !fieldFormatterCleanups.equals(getFormatterCleanups()); + @FXML + private void clearAll() { + viewModel.clearAll(); } - public boolean isDefaultSaveActions() { - return Cleanups.DEFAULT_SAVE_ACTIONS.equals(getFormatterCleanups()); + @FXML + private void addCleanup() { + viewModel.addCleanup(); } - private FieldFormatterCleanup getFieldFormatterCleanup() { - Formatter selectedFormatter = formattersCombobox.getValue(); - Field field = FieldFactory.parseField(selectFieldCombobox.getSelectionModel().getSelectedItem()); - return new FieldFormatterCleanup(field, selectedFormatter); - } + public BooleanProperty cleanupsDisableProperty() { return viewModel.cleanupsDisableProperty(); } - class EnablementStatusListener implements ChangeListener { - - public EnablementStatusListener(boolean initialStatus) { - setStatus(initialStatus); - } - - private void setStatus(boolean status) { - actionsList.setDisable(!status); - selectFieldCombobox.setDisable(!status); - formattersCombobox.setDisable(!status); - addButton.setDisable(!status); - removeSelectedButton.setDisable(!status); - removeAllButton.setDisable(!status); - recommendButton.setDisable(!status); - } - - @Override - public void changed(ObservableValue observable, T oldValue, T newValue) { - setStatus(cleanupEnabled.isSelected()); - } - } + public ListProperty cleanupsProperty() { return viewModel.cleanupsListProperty(); } } diff --git a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java new file mode 100644 index 00000000000..86705892e2e --- /dev/null +++ b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java @@ -0,0 +1,78 @@ +package org.jabref.gui.commonfxcontrols; + +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.ListProperty; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.collections.FXCollections; +import javafx.scene.control.SelectionModel; + +import org.jabref.Globals; +import org.jabref.gui.util.NoSelectionModel; +import org.jabref.logic.cleanup.Cleanups; +import org.jabref.model.cleanup.FieldFormatterCleanup; +import org.jabref.model.cleanup.Formatter; +import org.jabref.model.entry.field.Field; +import org.jabref.model.entry.field.FieldFactory; + +public class FieldFormatterCleanupsPanelViewModel { + + private final BooleanProperty cleanupsDisableProperty = new SimpleBooleanProperty(); + private final ListProperty cleanupsListProperty = new SimpleListProperty<>(FXCollections.observableArrayList()); + private final ObjectProperty> selectedCleanupProperty = new SimpleObjectProperty<>(new NoSelectionModel<>()); + private final ListProperty availableFieldsProperty = new SimpleListProperty<>(FXCollections.observableArrayList(FieldFactory.getCommonFields())); + private final ObjectProperty selectedFieldProperty = new SimpleObjectProperty<>(); + private final ListProperty availableFormattersProperty = new SimpleListProperty<>(FXCollections.observableArrayList(Cleanups.getBuiltInFormatters())); + private final ObjectProperty selectedFormatterProperty = new SimpleObjectProperty<>(); + + public FieldFormatterCleanupsPanelViewModel() { + } + + public void resetToRecommended() { + Globals.stateManager.getActiveDatabase().ifPresent(databaseContext -> { + if (databaseContext.isBiblatexMode()) { + cleanupsListProperty.setAll(Cleanups.RECOMMEND_BIBLATEX_ACTIONS.getConfiguredActions()); + } else { + cleanupsListProperty.setAll(Cleanups.RECOMMEND_BIBTEX_ACTIONS.getConfiguredActions()); + } + }); + } + + public void clearAll() { + cleanupsListProperty.clear(); + } + + public void addCleanup() { + if (selectedFieldProperty.getValue() == null || selectedFormatterProperty.getValue() == null) { + return; + } + + FieldFormatterCleanup cleanup = new FieldFormatterCleanup( + selectedFieldProperty.getValue(), + selectedFormatterProperty.getValue()); + + if (cleanupsListProperty.stream().noneMatch(item -> item.equals(cleanup))) { + cleanupsListProperty.add(cleanup); + } + } + + public void removeCleanup(FieldFormatterCleanup cleanup) { + cleanupsListProperty.remove(cleanup); + } + + public BooleanProperty cleanupsDisableProperty() { return cleanupsDisableProperty; } + + public ListProperty cleanupsListProperty() { return cleanupsListProperty; } + + public ObjectProperty> selectedCleanupProperty() { return selectedCleanupProperty; } + + public ListProperty availableFieldsProperty() { return availableFieldsProperty; } + + public ObjectProperty selectedFieldProperty() { return selectedFieldProperty; } + + public ListProperty availableFormattersProperty() { return availableFormattersProperty; } + + public ObjectProperty selectedFormatterProperty() { return selectedFormatterProperty; } +} diff --git a/src/main/java/org/jabref/gui/commonfxcontrols/OldFieldFormatterCleanupsPanel.java b/src/main/java/org/jabref/gui/commonfxcontrols/OldFieldFormatterCleanupsPanel.java new file mode 100644 index 00000000000..abfb835d716 --- /dev/null +++ b/src/main/java/org/jabref/gui/commonfxcontrols/OldFieldFormatterCleanupsPanel.java @@ -0,0 +1,252 @@ +package org.jabref.gui.commonfxcontrols; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Collectors; + +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; +import javafx.scene.control.ComboBox; +import javafx.scene.control.Label; +import javafx.scene.control.ListView; +import javafx.scene.control.SelectionMode; +import javafx.scene.layout.ColumnConstraints; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.RowConstraints; + +import org.jabref.Globals; +import org.jabref.JabRefGUI; +import org.jabref.gui.util.ViewModelListCellFactory; +import org.jabref.logic.cleanup.Cleanups; +import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter; +import org.jabref.logic.l10n.Localization; +import org.jabref.model.cleanup.FieldFormatterCleanup; +import org.jabref.model.cleanup.FieldFormatterCleanups; +import org.jabref.model.cleanup.Formatter; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.entry.field.Field; +import org.jabref.model.entry.field.FieldFactory; +import org.jabref.model.entry.field.InternalField; +import org.jabref.model.metadata.MetaData; + +import org.fxmisc.easybind.EasyBind; + +public class OldFieldFormatterCleanupsPanel extends GridPane { + + private static final String DESCRIPTION = Localization.lang("Description") + ": "; + private final CheckBox cleanupEnabled; + private final List availableFormatters; + private FieldFormatterCleanups fieldFormatterCleanups; + private ListView actionsList; + private ComboBox formattersCombobox; + private ComboBox selectFieldCombobox; + private Button addButton; + private Label descriptionAreaText; + private Button removeSelectedButton; + private Button removeAllButton; + private Button recommendButton; + private ObservableList actions; + + public OldFieldFormatterCleanupsPanel(String description) { + cleanupEnabled = new CheckBox(description); + availableFormatters = Cleanups.getBuiltInFormatters(); + availableFormatters.add(new ProtectTermsFormatter(Globals.protectedTermsLoader)); + } + + public void setValues(MetaData metaData) { + Objects.requireNonNull(metaData); + Optional saveActions = metaData.getSaveActions(); + setValues(saveActions.orElse(Cleanups.DEFAULT_SAVE_ACTIONS)); + } + + public void setValues(FieldFormatterCleanups formatterCleanups) { + fieldFormatterCleanups = formatterCleanups; + + // first clear existing content + this.getChildren().clear(); + + List configuredActions = fieldFormatterCleanups.getConfiguredActions(); + actions = FXCollections.observableArrayList(configuredActions); + buildLayout(); + } + + private void buildLayout() { + setHgap(10.0); + setVgap(4.0); + + ColumnConstraints first = new ColumnConstraints(); + first.setPrefWidth(25); + ColumnConstraints second = new ColumnConstraints(); + second.setPrefWidth(175); + ColumnConstraints third = new ColumnConstraints(); + third.setPrefWidth(200); + ColumnConstraints fourth = new ColumnConstraints(); + fourth.setPrefWidth(200); + getColumnConstraints().addAll(first, second, third, fourth); + RowConstraints firstR = new RowConstraints(); + firstR.setPrefHeight(25); + RowConstraints secondR = new RowConstraints(); + secondR.setPrefHeight(100); + RowConstraints thirdR = new RowConstraints(); + thirdR.setPrefHeight(50); + RowConstraints fourthR = new RowConstraints(); + fourthR.setPrefHeight(50); + RowConstraints fifthR = new RowConstraints(); + fifthR.setPrefHeight(50); + getRowConstraints().addAll(firstR, secondR, thirdR, fourthR, fifthR); + add(cleanupEnabled, 0, 0, 4, 1); + + actionsList = new ListView<>(actions); + actionsList.setMinHeight(100.0); + actionsList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); + + new ViewModelListCellFactory() + .withText(action -> action.getField().getDisplayName() + ": " + action.getFormatter().getName()) + .withStringTooltip(action -> action.getFormatter().getDescription()) + .install(actionsList); + add(actionsList, 1, 1, 3, 1); + + removeAllButton = new Button(Localization.lang("Remove all")); + removeAllButton.setOnAction(e -> actions.clear()); + + BibDatabaseContext databaseContext = JabRefGUI.getMainFrame().getCurrentBasePanel().getBibDatabaseContext(); + + recommendButton = new Button(Localization.lang("Reset to recommended")); + boolean isBiblatex = databaseContext.isBiblatexMode(); + + recommendButton.setOnAction(e -> { + if (isBiblatex) { + actions.setAll(Cleanups.RECOMMEND_BIBLATEX_ACTIONS.getConfiguredActions()); + } else { + actions.setAll(Cleanups.RECOMMEND_BIBTEX_ACTIONS.getConfiguredActions()); + } + }); + + removeSelectedButton = new Button(Localization.lang("Remove selected")); + removeSelectedButton.setOnAction(e -> actions.remove(actionsList.getSelectionModel().getSelectedItem())); + descriptionAreaText = new Label(DESCRIPTION); + descriptionAreaText.setWrapText(true); + + add(recommendButton, 1, 2, 1, 1); + add(removeSelectedButton, 2, 2, 1, 1); + add(removeAllButton, 3, 2, 1, 1); + add(getSelectorPanel(), 1, 3, 3, 1); + add(descriptionAreaText, 1, 4, 3, 1); + + updateDescription(); + + // make sure the layout is set according to the checkbox + cleanupEnabled.selectedProperty().addListener(new EnablementStatusListener<>(fieldFormatterCleanups.isEnabled())); + cleanupEnabled.setSelected(fieldFormatterCleanups.isEnabled()); + } + + private void updateDescription() { + FieldFormatterCleanup formatterCleanup = getFieldFormatterCleanup(); + if (formatterCleanup.getFormatter() != null) { + descriptionAreaText.setText(DESCRIPTION + formatterCleanup.getFormatter().getDescription()); + } else { + Formatter selectedFormatter = formattersCombobox.getValue(); + if (selectedFormatter != null) { + descriptionAreaText.setText(DESCRIPTION + selectedFormatter.getDescription()); + } else { + descriptionAreaText.setText(DESCRIPTION); + } + } + } + + /** + * This panel contains the two comboboxes and the Add button + */ + private GridPane getSelectorPanel() { + GridPane builder = new GridPane(); + builder.setHgap(10.0); + Set fields = FieldFactory.getCommonFields(); + fields.add(InternalField.KEY_FIELD); + Set fieldsString = fields.stream().map(Field::getDisplayName).sorted().collect(Collectors.toCollection(TreeSet::new)); + selectFieldCombobox = new ComboBox<>(FXCollections.observableArrayList(fieldsString)); + selectFieldCombobox.setEditable(true); + builder.add(selectFieldCombobox, 1, 1); + + formattersCombobox = new ComboBox<>(FXCollections.observableArrayList(availableFormatters)); + new ViewModelListCellFactory() + .withText(Formatter::getName) + .withStringTooltip(Formatter::getDescription) + .install(formattersCombobox); + EasyBind.subscribe(formattersCombobox.valueProperty(), e -> updateDescription()); + formattersCombobox.getSelectionModel().selectFirst(); + builder.add(formattersCombobox, 3, 1); + + addButton = new Button(Localization.lang("Add")); + addButton.setOnAction(e -> { + FieldFormatterCleanup newAction = getFieldFormatterCleanup(); + + if (!actions.contains(newAction)) { + actions.add(newAction); + } + }); + builder.add(addButton, 5, 1); + + return builder; + } + + public void storeSettings(MetaData metaData) { + Objects.requireNonNull(metaData); + + FieldFormatterCleanups formatterCleanups = getFormatterCleanups(); + + // if all actions have been removed, remove the save actions from the MetaData + if (formatterCleanups.getConfiguredActions().isEmpty()) { + metaData.clearSaveActions(); + return; + } + + metaData.setSaveActions(formatterCleanups); + } + + public FieldFormatterCleanups getFormatterCleanups() { + return new FieldFormatterCleanups(cleanupEnabled.isSelected(), actions); + } + + public boolean hasChanged() { + return !fieldFormatterCleanups.equals(getFormatterCleanups()); + } + + public boolean isDefaultSaveActions() { + return Cleanups.DEFAULT_SAVE_ACTIONS.equals(getFormatterCleanups()); + } + + private FieldFormatterCleanup getFieldFormatterCleanup() { + Formatter selectedFormatter = formattersCombobox.getValue(); + Field field = FieldFactory.parseField(selectFieldCombobox.getSelectionModel().getSelectedItem()); + return new FieldFormatterCleanup(field, selectedFormatter); + } + + class EnablementStatusListener implements ChangeListener { + + public EnablementStatusListener(boolean initialStatus) { + setStatus(initialStatus); + } + + private void setStatus(boolean status) { + actionsList.setDisable(!status); + selectFieldCombobox.setDisable(!status); + formattersCombobox.setDisable(!status); + addButton.setDisable(!status); + removeSelectedButton.setDisable(!status); + removeAllButton.setDisable(!status); + recommendButton.setDisable(!status); + } + + @Override + public void changed(ObservableValue observable, T oldValue, T newValue) { + setStatus(cleanupEnabled.isSelected()); + } + } +} diff --git a/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml b/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml index 50faf887c78..9b2420c4c6d 100644 --- a/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml +++ b/src/main/java/org/jabref/gui/libraryproperties/LibraryPropertiesDialog.fxml @@ -16,6 +16,8 @@ + + +