From b8e25b0c89c4f7a81b8a640e87d3a39652222d18 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Fri, 6 Nov 2020 13:58:50 +0100 Subject: [PATCH] Refactored for maintenance in specialField, extracted Globals --- src/main/java/org/jabref/gui/JabRefFrame.java | 14 +++--- .../org/jabref/gui/maintable/CellFactory.java | 15 ++++--- .../org/jabref/gui/maintable/MainTable.java | 1 + .../gui/maintable/MainTableColumnFactory.java | 4 +- .../jabref/gui/maintable/RightClickMenu.java | 17 ++++--- .../maintable/columns/SpecialFieldColumn.java | 9 ++-- .../gui/specialfields/SpecialFieldAction.java | 16 +++++-- .../SpecialFieldMenuItemFactory.java | 37 ++++++++++++---- .../specialfields/SpecialFieldViewModel.java | 27 ++++++++---- .../java/org/jabref/gui/util/FieldsUtil.java | 2 +- .../model/entry/field/SpecialFieldValue.java | 44 +++++++------------ 11 files changed, 110 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 4855a16e3ca..963df97bb57 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -740,18 +740,18 @@ private MenuBar createMenu() { new SeparatorMenuItem(), // ToDo: SpecialField needs the active BasePanel to mark it as changed. // Refactor BasePanel, should mark the BibDatabaseContext or the UndoManager as dirty instead! - SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, this, dialogService, stateManager), - SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, this, dialogService, stateManager), - SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, this, dialogService, stateManager), - SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, this, dialogService, stateManager), - SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, this, dialogService, stateManager), - SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, this, dialogService, stateManager) + SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, this, dialogService, prefs, undoManager, stateManager), + SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, this, dialogService, prefs, undoManager, stateManager), + SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, this, dialogService, prefs, undoManager, stateManager), + SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, this, dialogService, prefs, undoManager, stateManager), + SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, this, dialogService, prefs, undoManager, stateManager), + SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, this, dialogService, prefs, undoManager, stateManager) ); } // @formatter:off library.getItems().addAll( - factory.createMenuItem(StandardActions.NEW_ENTRY, new NewEntryAction(this, dialogService, Globals.prefs, stateManager)), + factory.createMenuItem(StandardActions.NEW_ENTRY, new NewEntryAction(this, dialogService, prefs, stateManager)), factory.createMenuItem(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new ExtractBibtexAction(stateManager)), factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, this, stateManager)), diff --git a/src/main/java/org/jabref/gui/maintable/CellFactory.java b/src/main/java/org/jabref/gui/maintable/CellFactory.java index 29b164f657e..9f0940831d4 100644 --- a/src/main/java/org/jabref/gui/maintable/CellFactory.java +++ b/src/main/java/org/jabref/gui/maintable/CellFactory.java @@ -16,12 +16,13 @@ import org.jabref.model.entry.field.SpecialField; import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.field.UnknownField; +import org.jabref.preferences.PreferencesService; public class CellFactory { private final Map TABLE_ICONS = new HashMap<>(); - public CellFactory(ExternalFileTypes externalFileTypes, UndoManager undoManager) { + public CellFactory(ExternalFileTypes externalFileTypes, PreferencesService preferencesService, UndoManager undoManager) { JabRefIcon icon; icon = IconTheme.JabRefIcons.PDF_FILE; // icon.setToo(Localization.lang("Open") + " PDF"); @@ -61,36 +62,36 @@ public CellFactory(ExternalFileTypes externalFileTypes, UndoManager undoManager) TABLE_ICONS.put(fileType.getField(), icon); } - SpecialFieldViewModel relevanceViewModel = new SpecialFieldViewModel(SpecialField.RELEVANCE, undoManager); + SpecialFieldViewModel relevanceViewModel = new SpecialFieldViewModel(SpecialField.RELEVANCE, preferencesService, undoManager); icon = relevanceViewModel.getIcon(); // icon.setToolTipText(relevanceViewModel.getLocalization()); TABLE_ICONS.put(SpecialField.RELEVANCE, icon); - SpecialFieldViewModel qualityViewModel = new SpecialFieldViewModel(SpecialField.QUALITY, undoManager); + SpecialFieldViewModel qualityViewModel = new SpecialFieldViewModel(SpecialField.QUALITY, preferencesService, undoManager); icon = qualityViewModel.getIcon(); // icon.setToolTipText(qualityViewModel.getLocalization()); TABLE_ICONS.put(SpecialField.QUALITY, icon); // Ranking item in the menu uses one star - SpecialFieldViewModel rankViewModel = new SpecialFieldViewModel(SpecialField.RANKING, undoManager); + SpecialFieldViewModel rankViewModel = new SpecialFieldViewModel(SpecialField.RANKING, preferencesService, undoManager); icon = rankViewModel.getIcon(); // icon.setToolTipText(rankViewModel.getLocalization()); TABLE_ICONS.put(SpecialField.RANKING, icon); // Priority icon used for the menu - SpecialFieldViewModel priorityViewModel = new SpecialFieldViewModel(SpecialField.PRIORITY, undoManager); + SpecialFieldViewModel priorityViewModel = new SpecialFieldViewModel(SpecialField.PRIORITY, preferencesService, undoManager); icon = priorityViewModel.getIcon(); // icon.setToolTipText(priorityViewModel.getLocalization()); TABLE_ICONS.put(SpecialField.PRIORITY, icon); // Read icon used for menu - SpecialFieldViewModel readViewModel = new SpecialFieldViewModel(SpecialField.READ_STATUS, undoManager); + SpecialFieldViewModel readViewModel = new SpecialFieldViewModel(SpecialField.READ_STATUS, preferencesService, undoManager); icon = readViewModel.getIcon(); // icon.setToolTipText(readViewModel.getLocalization()); TABLE_ICONS.put(SpecialField.READ_STATUS, icon); // Print icon used for menu - SpecialFieldViewModel printedViewModel = new SpecialFieldViewModel(SpecialField.PRINTED, undoManager); + SpecialFieldViewModel printedViewModel = new SpecialFieldViewModel(SpecialField.PRINTED, preferencesService, undoManager); icon = printedViewModel.getIcon(); // icon.setToolTipText(printedViewModel.getLocalization()); TABLE_ICONS.put(SpecialField.PRINTED, icon); diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index cf5c8cd2b2a..d0e42e0221c 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -114,6 +114,7 @@ public MainTable(MainTableDataModel model, dialogService, stateManager, preferencesService, + undoManager, Globals.clipboardManager)) .setOnDragDetected(this::handleOnDragDetected) .setOnDragDropped(this::handleOnDragDropped) diff --git a/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java b/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java index 167c12989f1..8ff3dd7efc9 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java +++ b/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java @@ -68,7 +68,7 @@ public MainTableColumnFactory(BibDatabaseContext database, this.columnPreferences = preferencesService.getColumnPreferences(); this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.dialogService = dialogService; - this.cellFactory = new CellFactory(externalFileTypes, undoManager); + this.cellFactory = new CellFactory(externalFileTypes, preferencesService, undoManager); this.undoManager = undoManager; } @@ -214,7 +214,7 @@ private TableColumn> createIdentifier * Creates a column that displays a {@link SpecialField} */ private TableColumn> createSpecialFieldColumn(MainTableColumnModel columnModel) { - return new SpecialFieldColumn(columnModel, undoManager); + return new SpecialFieldColumn(columnModel, preferencesService, undoManager); } /** diff --git a/src/main/java/org/jabref/gui/maintable/RightClickMenu.java b/src/main/java/org/jabref/gui/maintable/RightClickMenu.java index 6305baeaea7..a1bd6ff7fc9 100644 --- a/src/main/java/org/jabref/gui/maintable/RightClickMenu.java +++ b/src/main/java/org/jabref/gui/maintable/RightClickMenu.java @@ -1,5 +1,7 @@ package org.jabref.gui.maintable; +import javax.swing.undo.UndoManager; + import javafx.scene.control.ContextMenu; import javafx.scene.control.Menu; import javafx.scene.control.SeparatorMenuItem; @@ -37,6 +39,7 @@ public static ContextMenu create(BibEntryTableViewModel entry, DialogService dialogService, StateManager stateManager, PreferencesService preferencesService, + UndoManager undoManager, ClipBoardManager clipBoardManager) { ContextMenu contextMenu = new ContextMenu(); ActionFactory factory = new ActionFactory(keyBindingRepository); @@ -54,12 +57,12 @@ public static ContextMenu create(BibEntryTableViewModel entry, contextMenu.getItems().add(new SeparatorMenuItem()); if (preferencesService.getSpecialFieldsPreferences().isSpecialFieldsEnabled()) { - contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, libraryTab.frame(), dialogService, stateManager)); - contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, libraryTab.frame(), dialogService, stateManager)); - contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, libraryTab.frame(), dialogService, stateManager)); - contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, libraryTab.frame(), dialogService, stateManager)); - contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, libraryTab.frame(), dialogService, stateManager)); - contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, libraryTab.frame(), dialogService, stateManager)); + contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.RANKING, factory, libraryTab.frame(), dialogService, preferencesService, undoManager, stateManager)); + contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.RELEVANCE, factory, libraryTab.frame(), dialogService, preferencesService, undoManager, stateManager)); + contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.QUALITY, factory, libraryTab.frame(), dialogService, preferencesService, undoManager, stateManager)); + contextMenu.getItems().add(SpecialFieldMenuItemFactory.getSpecialFieldSingleItem(SpecialField.PRINTED, factory, libraryTab.frame(), dialogService, preferencesService, undoManager, stateManager)); + contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.PRIORITY, factory, libraryTab.frame(), dialogService, preferencesService, undoManager, stateManager)); + contextMenu.getItems().add(SpecialFieldMenuItemFactory.createSpecialFieldMenu(SpecialField.READ_STATUS, factory, libraryTab.frame(), dialogService, preferencesService, undoManager, stateManager)); } contextMenu.getItems().add(new SeparatorMenuItem()); @@ -94,7 +97,7 @@ private static Menu createCopySubMenu(LibraryTab libraryTab, copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, clipBoardManager, preferencesService))); // the submenu will behave dependent on what style is currently selected (citation/preview) - PreviewPreferences previewPreferences = Globals.prefs.getPreviewPreferences(); + PreviewPreferences previewPreferences = preferencesService.getPreviewPreferences(); PreviewLayout style = previewPreferences.getCurrentPreviewStyle(); if (style instanceof CitationStylePreviewLayout) { copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITATION_HTML, new CopyCitationAction(CitationStyleOutputFormat.HTML, dialogService, stateManager, clipBoardManager, previewPreferences))); diff --git a/src/main/java/org/jabref/gui/maintable/columns/SpecialFieldColumn.java b/src/main/java/org/jabref/gui/maintable/columns/SpecialFieldColumn.java index cb28ee91a3f..190f66374eb 100644 --- a/src/main/java/org/jabref/gui/maintable/columns/SpecialFieldColumn.java +++ b/src/main/java/org/jabref/gui/maintable/columns/SpecialFieldColumn.java @@ -26,6 +26,7 @@ import org.jabref.model.entry.field.FieldFactory; import org.jabref.model.entry.field.SpecialField; import org.jabref.model.entry.field.SpecialFieldValue; +import org.jabref.preferences.PreferencesService; import com.tobiasdiez.easybind.EasyBind; import org.controlsfx.control.Rating; @@ -35,14 +36,16 @@ */ public class SpecialFieldColumn extends MainTableColumn> { + private final PreferencesService preferencesService; private final UndoManager undoManager; - public SpecialFieldColumn(MainTableColumnModel model, UndoManager undoManager) { + public SpecialFieldColumn(MainTableColumnModel model, PreferencesService preferencesService, UndoManager undoManager) { super(model); + this.preferencesService = preferencesService; this.undoManager = undoManager; SpecialField specialField = (SpecialField) FieldFactory.parseField(model.getQualifier()); - SpecialFieldViewModel specialFieldViewModel = new SpecialFieldViewModel(specialField, undoManager); + SpecialFieldViewModel specialFieldViewModel = new SpecialFieldViewModel(specialField, preferencesService, undoManager); Node headerGraphic = specialFieldViewModel.getIcon().getGraphicNode(); Tooltip.install(headerGraphic, new Tooltip(specialFieldViewModel.getLocalization())); @@ -98,7 +101,7 @@ private Rating createSpecialRating(BibEntryTableViewModel entry, SpecialFieldVal Rating ranking = new Rating(); ranking.setRating(value.getValue().toRating()); EasyBind.subscribe(ranking.ratingProperty(), rating -> - new SpecialFieldViewModel(SpecialField.RANKING, undoManager) + new SpecialFieldViewModel(SpecialField.RANKING, preferencesService, undoManager) .setSpecialFieldValue(entry.getEntry(), SpecialFieldValue.getRating(rating.intValue()))); return ranking; diff --git a/src/main/java/org/jabref/gui/specialfields/SpecialFieldAction.java b/src/main/java/org/jabref/gui/specialfields/SpecialFieldAction.java index 4701b93a58d..17b04069192 100644 --- a/src/main/java/org/jabref/gui/specialfields/SpecialFieldAction.java +++ b/src/main/java/org/jabref/gui/specialfields/SpecialFieldAction.java @@ -3,8 +3,9 @@ import java.util.List; import java.util.Objects; +import javax.swing.undo.UndoManager; + import org.jabref.gui.DialogService; -import org.jabref.gui.Globals; import org.jabref.gui.JabRefFrame; import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; @@ -16,6 +17,7 @@ import org.jabref.model.FieldChange; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.SpecialField; +import org.jabref.preferences.PreferencesService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,6 +31,8 @@ public class SpecialFieldAction extends SimpleCommand { private final boolean nullFieldIfValueIsTheSame; private final String undoText; private final DialogService dialogService; + private final PreferencesService preferencesService; + private final UndoManager undoManager; private final StateManager stateManager; /** @@ -40,6 +44,8 @@ public SpecialFieldAction(JabRefFrame frame, boolean nullFieldIfValueIsTheSame, String undoText, DialogService dialogService, + PreferencesService preferencesService, + UndoManager undoManager, StateManager stateManager) { this.frame = frame; this.specialField = specialField; @@ -47,6 +53,8 @@ public SpecialFieldAction(JabRefFrame frame, this.nullFieldIfValueIsTheSame = nullFieldIfValueIsTheSame; this.undoText = undoText; this.dialogService = dialogService; + this.preferencesService = preferencesService; + this.undoManager = undoManager; this.stateManager = stateManager; this.executable.bind(ActionHelper.needsEntriesSelected(stateManager)); @@ -67,8 +75,8 @@ public void execute() { value, bibEntry, nullFieldIfValueIsTheSame, - Globals.prefs.getSpecialFieldsPreferences().isKeywordSyncEnabled(), - Globals.prefs.getKeywordDelimiter()); + preferencesService.getSpecialFieldsPreferences().isKeywordSyncEnabled(), + preferencesService.getKeywordDelimiter()); for (FieldChange change : changes) { ce.addEdit(new UndoableFieldChange(change)); @@ -97,7 +105,7 @@ public void execute() { private String getTextDone(SpecialField field, String... params) { Objects.requireNonNull(params); - SpecialFieldViewModel viewModel = new SpecialFieldViewModel(field, frame.getUndoManager()); + SpecialFieldViewModel viewModel = new SpecialFieldViewModel(field, preferencesService, undoManager); if (field.isSingleValueField() && (params.length == 1) && (params[0] != null)) { // Single value fields can be toggled only diff --git a/src/main/java/org/jabref/gui/specialfields/SpecialFieldMenuItemFactory.java b/src/main/java/org/jabref/gui/specialfields/SpecialFieldMenuItemFactory.java index a32fe926312..488d7ad637c 100644 --- a/src/main/java/org/jabref/gui/specialfields/SpecialFieldMenuItemFactory.java +++ b/src/main/java/org/jabref/gui/specialfields/SpecialFieldMenuItemFactory.java @@ -8,30 +8,51 @@ import javafx.scene.control.MenuItem; import org.jabref.gui.DialogService; -import org.jabref.gui.Globals; import org.jabref.gui.JabRefFrame; import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionFactory; import org.jabref.model.entry.field.SpecialField; import org.jabref.model.entry.field.SpecialFieldValue; +import org.jabref.preferences.PreferencesService; import de.saxsys.mvvmfx.utils.commands.Command; public class SpecialFieldMenuItemFactory { - public static MenuItem getSpecialFieldSingleItem(SpecialField field, ActionFactory factory, JabRefFrame frame, DialogService dialogService, StateManager stateManager) { + public static MenuItem getSpecialFieldSingleItem(SpecialField field, + ActionFactory factory, + JabRefFrame frame, + DialogService dialogService, + PreferencesService preferencesService, + UndoManager undoManager, + StateManager stateManager) { SpecialFieldValueViewModel specialField = new SpecialFieldValueViewModel(field.getValues().get(0)); + return factory.createMenuItem(specialField.getAction(), - new SpecialFieldViewModel(field, Globals.undoManager).getSpecialFieldAction(field.getValues().get(0), frame, dialogService, stateManager)); + new SpecialFieldViewModel(field, preferencesService, undoManager) + .getSpecialFieldAction(field.getValues().get(0), frame, dialogService, stateManager)); } - public static Menu createSpecialFieldMenu(SpecialField field, ActionFactory factory, JabRefFrame frame, DialogService dialogService, StateManager stateManager) { - return createSpecialFieldMenu(field, factory, Globals.undoManager, specialField -> - new SpecialFieldViewModel(field, Globals.undoManager).getSpecialFieldAction(specialField.getValue(), frame, dialogService, stateManager)); + public static Menu createSpecialFieldMenu(SpecialField field, + ActionFactory factory, + JabRefFrame frame, + DialogService dialogService, + PreferencesService preferencesService, + UndoManager undoManager, + StateManager stateManager) { + + return createSpecialFieldMenu(field, factory, preferencesService, undoManager, specialField -> + new SpecialFieldViewModel(field, preferencesService, undoManager) + .getSpecialFieldAction(specialField.getValue(), frame, dialogService, stateManager)); } - public static Menu createSpecialFieldMenu(SpecialField field, ActionFactory factory, UndoManager undoManager, Function commandFactory) { - SpecialFieldViewModel viewModel = new SpecialFieldViewModel(field, undoManager); + public static Menu createSpecialFieldMenu(SpecialField field, + ActionFactory factory, + PreferencesService preferencesService, + UndoManager undoManager, + Function commandFactory) { + SpecialFieldViewModel viewModel = new SpecialFieldViewModel(field, preferencesService, undoManager); Menu menu = factory.createMenu(viewModel.getAction()); + for (SpecialFieldValue Value : field.getValues()) { SpecialFieldValueViewModel valueViewModel = new SpecialFieldValueViewModel(Value); menu.getItems().add(factory.createMenuItem(valueViewModel.getAction(), commandFactory.apply(valueViewModel))); diff --git a/src/main/java/org/jabref/gui/specialfields/SpecialFieldViewModel.java b/src/main/java/org/jabref/gui/specialfields/SpecialFieldViewModel.java index 4a9f6cd4a45..a60f1621bff 100644 --- a/src/main/java/org/jabref/gui/specialfields/SpecialFieldViewModel.java +++ b/src/main/java/org/jabref/gui/specialfields/SpecialFieldViewModel.java @@ -7,7 +7,6 @@ import javax.swing.undo.UndoManager; import org.jabref.gui.DialogService; -import org.jabref.gui.Globals; import org.jabref.gui.JabRefFrame; import org.jabref.gui.StateManager; import org.jabref.gui.actions.Action; @@ -19,14 +18,17 @@ import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.SpecialField; import org.jabref.model.entry.field.SpecialFieldValue; +import org.jabref.preferences.PreferencesService; public class SpecialFieldViewModel { private final SpecialField field; - private UndoManager undoManager; + private final PreferencesService preferencesService; + private final UndoManager undoManager; - public SpecialFieldViewModel(SpecialField field, UndoManager undoManager) { + public SpecialFieldViewModel(SpecialField field, PreferencesService preferencesService, UndoManager undoManager) { this.field = Objects.requireNonNull(field); + this.preferencesService = Objects.requireNonNull(preferencesService); this.undoManager = Objects.requireNonNull(undoManager); } @@ -34,13 +36,20 @@ public SpecialField getField() { return field; } - public SpecialFieldAction getSpecialFieldAction(SpecialFieldValue value, JabRefFrame frame, DialogService dialogService, StateManager stateManager) { - return new SpecialFieldAction(frame, field, value.getFieldValue().orElse(null), - // if field contains only one value, it has to be nulled - // otherwise, another setting does not empty the field + public SpecialFieldAction getSpecialFieldAction(SpecialFieldValue value, + JabRefFrame frame, + DialogService dialogService, + StateManager stateManager) { + return new SpecialFieldAction( + frame, + field, + value.getFieldValue().orElse(null), + // if field contains only one value, it has to be nulled, as another setting does not empty the field field.getValues().size() == 1, getLocalization(), dialogService, + preferencesService, + undoManager, stateManager); } @@ -79,8 +88,8 @@ public void setSpecialFieldValue(BibEntry bibEntry, SpecialFieldValue value) { value.getFieldValue().orElse(null), bibEntry, getField().isSingleValueField(), - Globals.prefs.getSpecialFieldsPreferences().isKeywordSyncEnabled(), - Globals.prefs.getKeywordDelimiter()); + preferencesService.getSpecialFieldsPreferences().isKeywordSyncEnabled(), + preferencesService.getKeywordDelimiter()); for (FieldChange change : changes) { undoManager.addEdit(new UndoableFieldChange(change)); diff --git a/src/main/java/org/jabref/gui/util/FieldsUtil.java b/src/main/java/org/jabref/gui/util/FieldsUtil.java index 11e23e6ed0b..a6d6afe03f8 100644 --- a/src/main/java/org/jabref/gui/util/FieldsUtil.java +++ b/src/main/java/org/jabref/gui/util/FieldsUtil.java @@ -32,7 +32,7 @@ public Field fromString(String string) { public static String getNameWithType(Field field) { if (field instanceof SpecialField) { - return new SpecialFieldViewModel((SpecialField) field, Globals.undoManager).getLocalization() + return new SpecialFieldViewModel((SpecialField) field, Globals.prefs, Globals.undoManager).getLocalization() + " (" + Localization.lang("Special") + ")"; } else if (field instanceof IEEEField) { return field.getDisplayName() + " (" + Localization.lang("IEEE") + ")"; diff --git a/src/main/java/org/jabref/model/entry/field/SpecialFieldValue.java b/src/main/java/org/jabref/model/entry/field/SpecialFieldValue.java index 6a4014a4b91..d4c1f8df7d5 100644 --- a/src/main/java/org/jabref/model/entry/field/SpecialFieldValue.java +++ b/src/main/java/org/jabref/model/entry/field/SpecialFieldValue.java @@ -38,20 +38,14 @@ public enum SpecialFieldValue { } public static SpecialFieldValue getRating(int ranking) { - switch (ranking) { - case 1: - return RANK_1; - case 2: - return RANK_2; - case 3: - return RANK_3; - case 4: - return RANK_4; - case 5: - return RANK_5; - default: - throw new UnsupportedOperationException(ranking + "is not a valid ranking"); - } + return switch (ranking) { + case 1 -> RANK_1; + case 2 -> RANK_2; + case 3 -> RANK_3; + case 4 -> RANK_4; + case 5 -> RANK_5; + default -> throw new UnsupportedOperationException(ranking + "is not a valid ranking"); + }; } public Optional getKeyword() { @@ -63,19 +57,13 @@ public Optional getFieldValue() { } public int toRating() { - switch (this) { - case RANK_1: - return 1; - case RANK_2: - return 2; - case RANK_3: - return 3; - case RANK_4: - return 4; - case RANK_5: - return 5; - default: - throw new UnsupportedOperationException(this + "is not a valid ranking"); - } + return switch (this) { + case RANK_1 -> 1; + case RANK_2 -> 2; + case RANK_3 -> 3; + case RANK_4 -> 4; + case RANK_5 -> 5; + default -> throw new UnsupportedOperationException(this + "is not a valid ranking"); + }; } }