Skip to content

Commit c0153bd

Browse files
calixtusSiedlerchrtobiasdiez
authored
Extraction of Globals.prefs.put and .get (#7121)
Co-authored-by: Siedlerchr <[email protected]> Co-authored-by: Tobias Diez <[email protected]>
1 parent f356f9e commit c0153bd

File tree

144 files changed

+2137
-1586
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+2137
-1586
lines changed

docs/getting-into-the-code/code-howtos.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ Optional<Path> file = FileHelper.expandFilename(database, fileText, preferences.
211211

212212
## How to work with Preferences
213213

214-
`model` and `logic` must not know JabRefPreferences. See `ProxyPreferences` for encapsulated preferences and [https://github.com/JabRef/jabref/pull/658](https://github.com/JabRef/jabref/pull/658) for a detailed discussion.
214+
`model` and `logic` must not know `JabRefPreferences`. See `ProxyPreferences` for encapsulated preferences and [https://github.com/JabRef/jabref/pull/658](https://github.com/JabRef/jabref/pull/658) for a detailed discussion.
215215

216216
See [https://github.com/JabRef/jabref/blob/master/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java](https://github.com/JabRef/jabref/blob/master/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java) \(via [https://github.com/JabRef/jabref/pull/3092](https://github.com/JabRef/jabref/pull/3092)\) for the current way how to deal with preferences.
217217

@@ -291,9 +291,12 @@ Or even better, try to mock the preferences and insert them via dependency injec
291291
@Test
292292
public void getTypeReturnsBibLatexArticleInBibLatexMode() {
293293
// Mock preferences
294-
JabrefPreferences mockedPrefs = mock(JabrefPreferences.class);
294+
PreferencesService mockedPrefs = mock(PreferencesService.class);
295+
GeneralPreferences mockedGeneralPrefs = mock(GeneralPReferences.class);
295296
// Switch to BibLatex mode
296-
when(mockedPrefs.getBoolean("BiblatexMode")).thenReturn(true);
297+
when(mockedPrefs.getGeneralPrefs()).thenReturn(mockedGeneralPrefs);
298+
when(mockedGeneralPrefs.getDefaultBibDatabaseMode())
299+
.thenReturn(BibDatabaseMode.BIBLATEX);
297300

298301
// Now test
299302
EntryTypes biblatexentrytypes = new EntryTypes(mockedPrefs);

src/main/java/org/jabref/cli/ArgumentProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
456456
private void importPreferences() {
457457
try {
458458
Globals.prefs.importPreferences(cli.getPreferencesImport());
459-
Globals.entryTypesManager.addCustomOrModifiedTypes(Globals.prefs.loadBibEntryTypes(BibDatabaseMode.BIBTEX),
460-
Globals.prefs.loadBibEntryTypes(BibDatabaseMode.BIBLATEX));
459+
Globals.entryTypesManager.addCustomOrModifiedTypes(Globals.prefs.getBibEntryTypes(BibDatabaseMode.BIBTEX),
460+
Globals.prefs.getBibEntryTypes(BibDatabaseMode.BIBLATEX));
461461
List<TemplateExporter> customExporters = Globals.prefs.getCustomExportFormats(Globals.journalAbbreviationRepository);
462462
LayoutFormatterPreferences layoutPreferences =
463463
Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository);

src/main/java/org/jabref/gui/ClipBoardManager.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.jabref.model.entry.BibEntry;
3434
import org.jabref.model.entry.identifier.DOI;
3535
import org.jabref.model.util.OptionalUtil;
36+
import org.jabref.preferences.PreferencesService;
3637

3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
@@ -47,15 +48,18 @@ public class ClipBoardManager {
4748
private static Clipboard clipboard;
4849
private static java.awt.datatransfer.Clipboard primary;
4950
private static ImportFormatReader importFormatReader;
51+
private final PreferencesService preferencesService;
5052

51-
public ClipBoardManager() {
52-
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER);
53+
public ClipBoardManager(PreferencesService preferencesService) {
54+
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER, preferencesService);
5355
}
5456

55-
public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, ImportFormatReader importFormatReader) {
57+
public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, ImportFormatReader importFormatReader, PreferencesService preferencesService) {
5658
ClipBoardManager.clipboard = clipboard;
5759
ClipBoardManager.primary = primary;
5860
ClipBoardManager.importFormatReader = importFormatReader;
61+
62+
this.preferencesService = preferencesService;
5963
}
6064

6165
/**
@@ -154,7 +158,7 @@ public void setContent(String string) {
154158

155159
public void setContent(List<BibEntry> entries) throws IOException {
156160
final ClipboardContent content = new ClipboardContent();
157-
BibEntryWriter writer = new BibEntryWriter(new FieldWriter(Globals.prefs.getFieldWriterPreferences()), Globals.entryTypesManager);
161+
BibEntryWriter writer = new BibEntryWriter(new FieldWriter(preferencesService.getFieldWriterPreferences()), Globals.entryTypesManager);
158162
String serializedEntries = writer.serializeAll(entries, BibDatabaseMode.BIBTEX);
159163
content.put(DragAndDropDataFormats.ENTRIES, serializedEntries);
160164
content.putString(serializedEntries);
@@ -172,7 +176,7 @@ public List<BibEntry> extractData() {
172176
}
173177

174178
private List<BibEntry> handleBibTeXData(String entries) {
175-
BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor());
179+
BibtexParser parser = new BibtexParser(preferencesService.getImportFormatPreferences(), Globals.getFileUpdateMonitor());
176180
try {
177181
return parser.parseEntries(new ByteArrayInputStream(entries.getBytes(StandardCharsets.UTF_8)));
178182
} catch (ParseException ex) {
@@ -206,7 +210,7 @@ private List<BibEntry> tryImportFormats(String data) {
206210
private List<BibEntry> fetchByDOI(DOI doi) {
207211
LOGGER.info("Found DOI in clipboard");
208212
try {
209-
Optional<BibEntry> entry = new DoiFetcher(Globals.prefs.getImportFormatPreferences()).performSearchById(doi.getDOI());
213+
Optional<BibEntry> entry = new DoiFetcher(preferencesService.getImportFormatPreferences()).performSearchById(doi.getDOI());
210214
return OptionalUtil.toList(entry);
211215
} catch (FetcherException ex) {
212216
LOGGER.error("Error while fetching", ex);

src/main/java/org/jabref/gui/EntryTypeView.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.jabref.model.entry.types.IEEETranEntryTypeDefinitions;
3535
import org.jabref.model.entry.types.StandardEntryType;
3636
import org.jabref.model.strings.StringUtil;
37-
import org.jabref.preferences.JabRefPreferences;
37+
import org.jabref.preferences.PreferencesService;
3838

3939
import com.airhacks.afterburner.views.ViewLoader;
4040
import com.tobiasdiez.easybind.EasyBind;
@@ -63,16 +63,16 @@ public class EntryTypeView extends BaseDialog<EntryType> {
6363

6464
private final LibraryTab libraryTab;
6565
private final DialogService dialogService;
66-
private final JabRefPreferences prefs;
66+
private final PreferencesService preferencesService;
6767

6868
private EntryType type;
6969
private EntryTypeViewModel viewModel;
7070
private final ControlsFxVisualizer visualizer = new ControlsFxVisualizer();
7171

72-
public EntryTypeView(LibraryTab libraryTab, DialogService dialogService, JabRefPreferences preferences) {
72+
public EntryTypeView(LibraryTab libraryTab, DialogService dialogService, PreferencesService preferences) {
7373
this.libraryTab = libraryTab;
7474
this.dialogService = dialogService;
75-
this.prefs = preferences;
75+
this.preferencesService = preferences;
7676

7777
this.setTitle(Localization.lang("Select entry type"));
7878
ViewLoader.view(this)
@@ -121,7 +121,7 @@ private void addEntriesToPane(FlowPane pane, Collection<? extends BibEntryType>
121121
@FXML
122122
public void initialize() {
123123
visualizer.setDecoration(new IconValidationDecorator());
124-
viewModel = new EntryTypeViewModel(prefs, libraryTab, dialogService, stateManager);
124+
viewModel = new EntryTypeViewModel(preferencesService, libraryTab, dialogService, stateManager);
125125

126126
idBasedFetchers.itemsProperty().bind(viewModel.fetcherItemsProperty());
127127
idTextField.textProperty().bindBidirectional(viewModel.idTextProperty());

src/main/java/org/jabref/gui/EntryTypeViewModel.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.jabref.logic.l10n.Localization;
2626
import org.jabref.model.entry.BibEntry;
2727
import org.jabref.model.strings.StringUtil;
28-
import org.jabref.preferences.JabRefPreferences;
28+
import org.jabref.preferences.PreferencesService;
2929

3030
import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
3131
import de.saxsys.mvvmfx.utils.validation.ValidationMessage;
@@ -38,7 +38,7 @@ public class EntryTypeViewModel {
3838

3939
private static final Logger LOGGER = LoggerFactory.getLogger(EntryTypeViewModel.class);
4040

41-
private final JabRefPreferences prefs;
41+
private final PreferencesService preferencesService;
4242
private final BooleanProperty searchingProperty = new SimpleBooleanProperty();
4343
private final BooleanProperty searchSuccesfulProperty = new SimpleBooleanProperty();
4444
private final ObjectProperty<IdBasedFetcher> selectedItemProperty = new SimpleObjectProperty<>();
@@ -51,9 +51,12 @@ public class EntryTypeViewModel {
5151
private final Validator idFieldValidator;
5252
private final StateManager stateManager;
5353

54-
public EntryTypeViewModel(JabRefPreferences preferences, LibraryTab libraryTab, DialogService dialogService, StateManager stateManager) {
54+
public EntryTypeViewModel(PreferencesService preferences,
55+
LibraryTab libraryTab,
56+
DialogService dialogService,
57+
StateManager stateManager) {
5558
this.libraryTab = libraryTab;
56-
this.prefs = preferences;
59+
this.preferencesService = preferences;
5760
this.dialogService = dialogService;
5861
this.stateManager = stateManager;
5962
fetchers.addAll(WebFetchers.getIdBasedFetchers(preferences.getImportFormatPreferences()));
@@ -86,12 +89,12 @@ public BooleanProperty getFocusAndSelectAllProperty() {
8689
}
8790

8891
public void storeSelectedFetcher() {
89-
prefs.setIdBasedFetcherForEntryGenerator(selectedItemProperty.getValue().getName());
92+
preferencesService.storeIdBasedFetcherForEntryGenerator(selectedItemProperty.getValue().getName());
9093
}
9194

9295
private IdBasedFetcher getLastSelectedFetcher() {
93-
return fetchers.stream().filter(fetcher -> fetcher.getName().equals(prefs.getIdBasedFetcherForEntryGenerator()))
94-
.findFirst().orElse(new DoiFetcher(prefs.getImportFormatPreferences()));
96+
return fetchers.stream().filter(fetcher -> fetcher.getName().equals(preferencesService.getIdBasedFetcherForEntryGenerator()))
97+
.findFirst().orElse(new DoiFetcher(preferencesService.getImportFormatPreferences()));
9598
}
9699

97100
public ListProperty<IdBasedFetcher> fetcherItemsProperty() {
@@ -170,7 +173,7 @@ public void runFetcherWorker() {
170173
}
171174
} else {
172175
// Regenerate CiteKey of imported BibEntry
173-
new CitationKeyGenerator(libraryTab.getBibDatabaseContext(), prefs.getCitationKeyPatternPreferences()).generateAndSetKey(entry);
176+
new CitationKeyGenerator(libraryTab.getBibDatabaseContext(), preferencesService.getCitationKeyPatternPreferences()).generateAndSetKey(entry);
174177
libraryTab.insertEntry(entry);
175178
}
176179
searchSuccesfulProperty.set(true);

src/main/java/org/jabref/gui/Globals.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class Globals {
7373
public static ExporterFactory exportFactory;
7474
public static CountingUndoManager undoManager = new CountingUndoManager();
7575
public static BibEntryTypesManager entryTypesManager = new BibEntryTypesManager();
76-
public static ClipBoardManager clipboardManager = new ClipBoardManager();
76+
public static ClipBoardManager clipboardManager = new ClipBoardManager(prefs);
7777

7878
// Key binding preferences
7979
private static KeyBindingRepository keyBindingRepository;
@@ -97,7 +97,7 @@ public static void startBackgroundTasks() {
9797
Globals.fileUpdateMonitor = new DefaultFileUpdateMonitor();
9898
JabRefExecutorService.INSTANCE.executeInterruptableTask(Globals.fileUpdateMonitor, "FileUpdateMonitor");
9999

100-
if (Globals.prefs.shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) {
100+
if (Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) {
101101
startTelemetryClient();
102102
}
103103
}
@@ -112,7 +112,7 @@ private static void stopTelemetryClient() {
112112
private static void startTelemetryClient() {
113113
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive();
114114
telemetryConfiguration.setInstrumentationKey(Globals.BUILD_INFO.azureInstrumentationKey);
115-
telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.shouldCollectTelemetry());
115+
telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry());
116116
telemetryClient = new TelemetryClient(telemetryConfiguration);
117117
telemetryClient.getContext().getProperties().put("JabRef version", Globals.BUILD_INFO.version.toString());
118118
telemetryClient.getContext().getProperties().put("Java version", StandardSystemProperty.JAVA_VERSION.value());

src/main/java/org/jabref/gui/JabRefDialogService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import org.jabref.gui.util.FileDialogConfiguration;
4141
import org.jabref.gui.util.ZipFileChooser;
4242
import org.jabref.logic.l10n.Localization;
43-
import org.jabref.preferences.JabRefPreferences;
43+
import org.jabref.preferences.PreferencesService;
4444

4545
import com.jfoenix.controls.JFXSnackbar;
4646
import com.jfoenix.controls.JFXSnackbar.SnackbarEvent;
@@ -67,12 +67,12 @@ public class JabRefDialogService implements DialogService {
6767

6868
private static final Duration TOAST_MESSAGE_DISPLAY_TIME = Duration.millis(3000);
6969
private static final Logger LOGGER = LoggerFactory.getLogger(JabRefDialogService.class);
70-
private static JabRefPreferences preferences;
70+
private static PreferencesService preferences;
7171

7272
private final Window mainWindow;
7373
private final JFXSnackbar statusLine;
7474

75-
public JabRefDialogService(Window mainWindow, Pane mainPane, JabRefPreferences preferences) {
75+
public JabRefDialogService(Window mainWindow, Pane mainPane, PreferencesService preferences) {
7676
this.mainWindow = mainWindow;
7777
this.statusLine = new JFXSnackbar(mainPane);
7878
JabRefDialogService.preferences = preferences;

0 commit comments

Comments
 (0)