Skip to content

Commit

Permalink
Extraction of Globals.prefs.put and .get (#7121)
Browse files Browse the repository at this point in the history
Co-authored-by: Siedlerchr <[email protected]>
Co-authored-by: Tobias Diez <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2020
1 parent f356f9e commit c0153bd
Show file tree
Hide file tree
Showing 144 changed files with 2,137 additions and 1,586 deletions.
9 changes: 6 additions & 3 deletions docs/getting-into-the-code/code-howtos.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Optional<Path> file = FileHelper.expandFilename(database, fileText, preferences.

## How to work with Preferences

`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.
`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.

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.

Expand Down Expand Up @@ -291,9 +291,12 @@ Or even better, try to mock the preferences and insert them via dependency injec
@Test
public void getTypeReturnsBibLatexArticleInBibLatexMode() {
// Mock preferences
JabrefPreferences mockedPrefs = mock(JabrefPreferences.class);
PreferencesService mockedPrefs = mock(PreferencesService.class);
GeneralPreferences mockedGeneralPrefs = mock(GeneralPReferences.class);
// Switch to BibLatex mode
when(mockedPrefs.getBoolean("BiblatexMode")).thenReturn(true);
when(mockedPrefs.getGeneralPrefs()).thenReturn(mockedGeneralPrefs);
when(mockedGeneralPrefs.getDefaultBibDatabaseMode())
.thenReturn(BibDatabaseMode.BIBLATEX);

// Now test
EntryTypes biblatexentrytypes = new EntryTypes(mockedPrefs);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
private void importPreferences() {
try {
Globals.prefs.importPreferences(cli.getPreferencesImport());
Globals.entryTypesManager.addCustomOrModifiedTypes(Globals.prefs.loadBibEntryTypes(BibDatabaseMode.BIBTEX),
Globals.prefs.loadBibEntryTypes(BibDatabaseMode.BIBLATEX));
Globals.entryTypesManager.addCustomOrModifiedTypes(Globals.prefs.getBibEntryTypes(BibDatabaseMode.BIBTEX),
Globals.prefs.getBibEntryTypes(BibDatabaseMode.BIBLATEX));
List<TemplateExporter> customExporters = Globals.prefs.getCustomExportFormats(Globals.journalAbbreviationRepository);
LayoutFormatterPreferences layoutPreferences =
Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationRepository);
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -47,15 +48,18 @@ public class ClipBoardManager {
private static Clipboard clipboard;
private static java.awt.datatransfer.Clipboard primary;
private static ImportFormatReader importFormatReader;
private final PreferencesService preferencesService;

public ClipBoardManager() {
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER);
public ClipBoardManager(PreferencesService preferencesService) {
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), Globals.IMPORT_FORMAT_READER, preferencesService);
}

public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, ImportFormatReader importFormatReader) {
public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, ImportFormatReader importFormatReader, PreferencesService preferencesService) {
ClipBoardManager.clipboard = clipboard;
ClipBoardManager.primary = primary;
ClipBoardManager.importFormatReader = importFormatReader;

this.preferencesService = preferencesService;
}

/**
Expand Down Expand Up @@ -154,7 +158,7 @@ public void setContent(String string) {

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

private List<BibEntry> handleBibTeXData(String entries) {
BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor());
BibtexParser parser = new BibtexParser(preferencesService.getImportFormatPreferences(), Globals.getFileUpdateMonitor());
try {
return parser.parseEntries(new ByteArrayInputStream(entries.getBytes(StandardCharsets.UTF_8)));
} catch (ParseException ex) {
Expand Down Expand Up @@ -206,7 +210,7 @@ private List<BibEntry> tryImportFormats(String data) {
private List<BibEntry> fetchByDOI(DOI doi) {
LOGGER.info("Found DOI in clipboard");
try {
Optional<BibEntry> entry = new DoiFetcher(Globals.prefs.getImportFormatPreferences()).performSearchById(doi.getDOI());
Optional<BibEntry> entry = new DoiFetcher(preferencesService.getImportFormatPreferences()).performSearchById(doi.getDOI());
return OptionalUtil.toList(entry);
} catch (FetcherException ex) {
LOGGER.error("Error while fetching", ex);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jabref/gui/EntryTypeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.jabref.model.entry.types.IEEETranEntryTypeDefinitions;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

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

private final LibraryTab libraryTab;
private final DialogService dialogService;
private final JabRefPreferences prefs;
private final PreferencesService preferencesService;

private EntryType type;
private EntryTypeViewModel viewModel;
private final ControlsFxVisualizer visualizer = new ControlsFxVisualizer();

public EntryTypeView(LibraryTab libraryTab, DialogService dialogService, JabRefPreferences preferences) {
public EntryTypeView(LibraryTab libraryTab, DialogService dialogService, PreferencesService preferences) {
this.libraryTab = libraryTab;
this.dialogService = dialogService;
this.prefs = preferences;
this.preferencesService = preferences;

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

idBasedFetchers.itemsProperty().bind(viewModel.fetcherItemsProperty());
idTextField.textProperty().bindBidirectional(viewModel.idTextProperty());
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/jabref/gui/EntryTypeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

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

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

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

public EntryTypeViewModel(JabRefPreferences preferences, LibraryTab libraryTab, DialogService dialogService, StateManager stateManager) {
public EntryTypeViewModel(PreferencesService preferences,
LibraryTab libraryTab,
DialogService dialogService,
StateManager stateManager) {
this.libraryTab = libraryTab;
this.prefs = preferences;
this.preferencesService = preferences;
this.dialogService = dialogService;
this.stateManager = stateManager;
fetchers.addAll(WebFetchers.getIdBasedFetchers(preferences.getImportFormatPreferences()));
Expand Down Expand Up @@ -86,12 +89,12 @@ public BooleanProperty getFocusAndSelectAllProperty() {
}

public void storeSelectedFetcher() {
prefs.setIdBasedFetcherForEntryGenerator(selectedItemProperty.getValue().getName());
preferencesService.storeIdBasedFetcherForEntryGenerator(selectedItemProperty.getValue().getName());
}

private IdBasedFetcher getLastSelectedFetcher() {
return fetchers.stream().filter(fetcher -> fetcher.getName().equals(prefs.getIdBasedFetcherForEntryGenerator()))
.findFirst().orElse(new DoiFetcher(prefs.getImportFormatPreferences()));
return fetchers.stream().filter(fetcher -> fetcher.getName().equals(preferencesService.getIdBasedFetcherForEntryGenerator()))
.findFirst().orElse(new DoiFetcher(preferencesService.getImportFormatPreferences()));
}

public ListProperty<IdBasedFetcher> fetcherItemsProperty() {
Expand Down Expand Up @@ -170,7 +173,7 @@ public void runFetcherWorker() {
}
} else {
// Regenerate CiteKey of imported BibEntry
new CitationKeyGenerator(libraryTab.getBibDatabaseContext(), prefs.getCitationKeyPatternPreferences()).generateAndSetKey(entry);
new CitationKeyGenerator(libraryTab.getBibDatabaseContext(), preferencesService.getCitationKeyPatternPreferences()).generateAndSetKey(entry);
libraryTab.insertEntry(entry);
}
searchSuccesfulProperty.set(true);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class Globals {
public static ExporterFactory exportFactory;
public static CountingUndoManager undoManager = new CountingUndoManager();
public static BibEntryTypesManager entryTypesManager = new BibEntryTypesManager();
public static ClipBoardManager clipboardManager = new ClipBoardManager();
public static ClipBoardManager clipboardManager = new ClipBoardManager(prefs);

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

if (Globals.prefs.shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) {
if (Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) {
startTelemetryClient();
}
}
Expand All @@ -112,7 +112,7 @@ private static void stopTelemetryClient() {
private static void startTelemetryClient() {
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive();
telemetryConfiguration.setInstrumentationKey(Globals.BUILD_INFO.azureInstrumentationKey);
telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.shouldCollectTelemetry());
telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry());
telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.getContext().getProperties().put("JabRef version", Globals.BUILD_INFO.version.toString());
telemetryClient.getContext().getProperties().put("Java version", StandardSystemProperty.JAVA_VERSION.value());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/JabRefDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.gui.util.ZipFileChooser;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

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

private static final Duration TOAST_MESSAGE_DISPLAY_TIME = Duration.millis(3000);
private static final Logger LOGGER = LoggerFactory.getLogger(JabRefDialogService.class);
private static JabRefPreferences preferences;
private static PreferencesService preferences;

private final Window mainWindow;
private final JFXSnackbar statusLine;

public JabRefDialogService(Window mainWindow, Pane mainPane, JabRefPreferences preferences) {
public JabRefDialogService(Window mainWindow, Pane mainPane, PreferencesService preferences) {
this.mainWindow = mainWindow;
this.statusLine = new JFXSnackbar(mainPane);
JabRefDialogService.preferences = preferences;
Expand Down
Loading

0 comments on commit c0153bd

Please sign in to comment.