Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extraction of Globals.prefs.put and .get #7121

Merged
merged 31 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
87c0da9
Extracted Globals.prefs from SidePaneManager
calixtus Nov 21, 2020
c0566c4
Merge remote-tracking branch 'upstream/master' into refactor-globals-…
calixtus Nov 21, 2020
fd83cfe
Fixed failing tests
calixtus Nov 21, 2020
064e773
Extracted Globals.prefs from ClipBoardManager
calixtus Nov 21, 2020
edc04c6
Introduced GuiPreferences
calixtus Nov 21, 2020
1051c2c
Extracted prefs.get and put from ImportCommand and ExportCommand
calixtus Nov 22, 2020
eafc356
Extracted prefs.get and put from ExternalFileTypes
calixtus Nov 22, 2020
0f9301b
Extracted more prefs.get and put
calixtus Nov 22, 2020
ddfb805
Extracted more prefs.get and put from MergeEntries
calixtus Nov 22, 2020
bf36959
Extracted Globals.prefs.get and .put out of push package
calixtus Nov 29, 2020
0d03690
Merge remote-tracking branch 'upstream/master' into refactor-prefs-calls
Siedlerchr Dec 5, 2020
0d19091
Extrract calls to JabRefPreferences.getInstance()
Siedlerchr Dec 5, 2020
fb7e499
Extracted MrDlibPreferences, Fixed CrawlerTest, Removed some unnecess…
calixtus Dec 5, 2020
9f57e38
Fixed tests
calixtus Dec 7, 2020
9ee60c7
Merge remote-tracking branch 'upstream/master' into refactor-prefs-calls
calixtus Dec 7, 2020
85d0a6a
Fixed more tests
calixtus Dec 7, 2020
f2a62fd
Fixed NPEs in tests and checkstyle
calixtus Dec 7, 2020
426532b
l10n
calixtus Dec 7, 2020
ac6d4b5
Fixed NPE in CrawlerTest
calixtus Dec 7, 2020
9e85f9c
Cleanups
calixtus Dec 7, 2020
de26c7d
Merge remote-tracking branch 'upstream/master' into refactor-prefs-calls
calixtus Dec 7, 2020
eeb679b
Extracted TelemetryPreferences
calixtus Dec 7, 2020
43ced4c
Extracted calls to JabRefPreferences out of CustomImportList
calixtus Dec 8, 2020
7b56a94
Made storing of ProtectedTermsPreferences consistent
calixtus Dec 9, 2020
5a11a64
Extracted JabRefPreferences out ouf EntryEditor, reduced unnecessary …
calixtus Dec 9, 2020
2d15f61
Finished extracting unnecessary calls to JabRefPreferences
calixtus Dec 9, 2020
95882e5
Merge remote-tracking branch 'upstream/master' into refactor-prefs-calls
calixtus Dec 9, 2020
14d509d
l10n
calixtus Dec 9, 2020
465b180
Merge branch 'master' into refactor-prefs-calls
tobiasdiez Dec 13, 2020
abe7f4f
Fixed merge error
calixtus Dec 14, 2020
f1b16ce
Fix checkstyle
Siedlerchr Dec 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guees Tobi or Olli will complain about the empty space ;)

Suggested change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No here it's fine...just don't add empty lines at the beginning or end of a method

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