Skip to content

Commit

Permalink
Migrate PreferencesService
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed May 23, 2024
1 parent da9b991 commit f174227
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 61 deletions.
8 changes: 5 additions & 3 deletions src/jmh/java/org/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Random;
import java.util.stream.Collectors;

import org.jabref.gui.Globals;
import org.jabref.logic.bibtex.FieldPreferences;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.exporter.BibWriter;
Expand All @@ -35,7 +34,9 @@
import org.jabref.model.metadata.MetaData;
import org.jabref.model.search.rules.SearchRules.SearchFlags;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
Expand All @@ -55,7 +56,7 @@ public class Benchmarks {

@Setup
public void init() throws Exception {
Globals.prefs = JabRefPreferences.getInstance();
Injector.setModelOrService(PreferencesService.class, JabRefPreferences.getInstance());

Random randomizer = new Random();
for (int i = 0; i < 1000; i++) {
Expand Down Expand Up @@ -92,7 +93,8 @@ private StringWriter getOutputWriter() throws IOException {

@Benchmark
public ParserResult parse() throws IOException {
BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
PreferencesService preferencesService = Injector.instantiateModelOrService(PreferencesService.class);
BibtexParser parser = new BibtexParser(preferencesService.getImportFormatPreferences());
return parser.parse(new StringReader(bibtexString));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public static void main(String[] args) {

// Initialize preferences
final JabRefPreferences preferences = JabRefPreferences.getInstance();
Injector.setModelOrService(PreferencesService.class, preferences);

// Early exit in case another instance is already running
if (!handleMultipleAppInstances(args, preferences.getRemotePreferences())) {
return;
}

Globals.prefs = preferences;
PreferencesMigrations.runMigrations(preferences, entryTypesManager);

// Initialize rest of preferences
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jabref.model.entry.BibtexString;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,16 +38,13 @@ public class ClipBoardManager {
private static Clipboard clipboard;
private static java.awt.datatransfer.Clipboard primary;

private final PreferencesService preferencesService;

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

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

/**
Expand Down Expand Up @@ -169,6 +167,7 @@ public void setContent(List<BibEntry> entries, BibEntryTypesManager entryTypesMa
}

private String serializeEntries(List<BibEntry> entries, BibEntryTypesManager entryTypesManager) throws IOException {
PreferencesService preferencesService = Injector.instantiateModelOrService(PreferencesService.class);
// BibEntry is not Java serializable. Thus, we need to do the serialization manually
// At reading of the clipboard in JabRef, we parse the plain string in all cases, so we don't need to flag we put BibEntries here
// Furthermore, storing a string also enables other applications to work with the data
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/jabref/gui/DefaultInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import com.airhacks.afterburner.injection.PresenterFactory;
Expand All @@ -29,8 +28,6 @@ private static Object createDependency(Class<?> clazz) {
return JabRefGUI.getDialogService();
} else if (clazz == TaskExecutor.class) {
return Globals.TASK_EXECUTOR;
} else if (clazz == PreferencesService.class) {
return Globals.prefs;
} else if (clazz == KeyBindingRepository.class) {
return Globals.getKeyPrefs();
} else if (clazz == JournalAbbreviationRepository.class) {
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;

/**
* @deprecated try to use {@link StateManager} and {@link org.jabref.preferences.PreferencesService}
*/
Expand All @@ -35,11 +37,6 @@ public class Globals {

public static final TaskExecutor TASK_EXECUTOR = new DefaultTaskExecutor(stateManager);

/**
* Each test case initializes this field if required
*/
public static PreferencesService prefs;

/**
* This field is initialized upon startup.
* <p>
Expand All @@ -65,14 +62,15 @@ private Globals() {
// Key binding preferences
public static synchronized KeyBindingRepository getKeyPrefs() {
if (keyBindingRepository == null) {
keyBindingRepository = prefs.getKeyBindingRepository();
PreferencesService preferences = Injector.instantiateModelOrService(PreferencesService.class);
keyBindingRepository = preferences.getKeyBindingRepository();
}
return keyBindingRepository;
}

public static synchronized ClipBoardManager getClipboardManager() {
if (clipBoardManager == null) {
clipBoardManager = new ClipBoardManager(prefs);
clipBoardManager = new ClipBoardManager();
}
return clipBoardManager;
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,37 +262,37 @@ private boolean isWindowPositionOutOfBounds() {
preferencesService.getGuiPreferences().getPositionY());
}

public static JabRefFrame getMainFrame() {
return mainFrame;
}

public static DialogService getDialogService() {
return dialogService;
}

public static ThemeManager getThemeManager() {
return themeManager;
}

// Background tasks
public static void startBackgroundTasks() {
public void startBackgroundTasks() {
// TODO Currently deactivated due to incompatibilities in XML
/* if (Globals.prefs.getTelemetryPreferences().shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) {
Telemetry.start(prefs.getTelemetryPreferences());
} */
RemotePreferences remotePreferences = Globals.prefs.getRemotePreferences();
RemotePreferences remotePreferences = preferencesService.getRemotePreferences();
if (remotePreferences.useRemoteServer()) {
Globals.REMOTE_LISTENER.openAndStart(
new CLIMessageHandler(
Globals.prefs,
preferencesService,
Globals.getFileUpdateMonitor(),
Injector.instantiateModelOrService(BibEntryTypesManager.class)),
remotePreferences.getPort());
}
}

public static void stopBackgroundTasks() {
public void stopBackgroundTasks() {
Telemetry.shutdown();
Unirest.shutDown();
}

public static JabRefFrame getMainFrame() {
return mainFrame;
}

public static DialogService getDialogService() {
return dialogService;
}

public static ThemeManager getThemeManager() {
return themeManager;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private void onDatabaseLoadingStarted() {

private void onDatabaseLoadingSucceed(ParserResult result) {
BibDatabaseContext context = result.getDatabaseContext();
OpenDatabaseAction.performPostOpenActions(result, dialogService);
OpenDatabaseAction.performPostOpenActions(result, dialogService, preferencesService);

setDatabaseContext(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void openDatabases(List<ParserResult> parserResults) {
// if we found new entry types that can be imported, or checking
// if the database contents should be modified due to new features
// in this version of JabRef.
parserResults.forEach(pr -> OpenDatabaseAction.performPostOpenActions(pr, dialogService));
parserResults.forEach(pr -> OpenDatabaseAction.performPostOpenActions(pr, dialogService, prefs));

LOGGER.debug("Finished adding panels");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import java.util.stream.Collectors;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.importer.ImportCustomEntryTypesDialog;
import org.jabref.logic.importer.ParserResult;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.LibraryPreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;

Expand All @@ -20,18 +21,19 @@
public class CheckForNewEntryTypesAction implements GUIPostOpenAction {

@Override
public boolean isActionNecessary(ParserResult parserResult) {
return !getListOfUnknownAndUnequalCustomizations(parserResult).isEmpty();
public boolean isActionNecessary(ParserResult parserResult, PreferencesService preferencesService) {
return !getListOfUnknownAndUnequalCustomizations(parserResult, preferencesService.getLibraryPreferences()).isEmpty();
}

@Override
public void performAction(ParserResult parserResult, DialogService dialogService) {
BibDatabaseMode mode = getBibDatabaseModeFromParserResult(parserResult);
dialogService.showCustomDialogAndWait(new ImportCustomEntryTypesDialog(mode, getListOfUnknownAndUnequalCustomizations(parserResult)));
public void performAction(ParserResult parserResult, DialogService dialogService, PreferencesService preferencesService) {
LibraryPreferences preferences = preferencesService.getLibraryPreferences();
BibDatabaseMode mode = getBibDatabaseModeFromParserResult(parserResult, preferences);
dialogService.showCustomDialogAndWait(new ImportCustomEntryTypesDialog(mode, getListOfUnknownAndUnequalCustomizations(parserResult, preferences)));
}

private List<BibEntryType> getListOfUnknownAndUnequalCustomizations(ParserResult parserResult) {
BibDatabaseMode mode = getBibDatabaseModeFromParserResult(parserResult);
private List<BibEntryType> getListOfUnknownAndUnequalCustomizations(ParserResult parserResult, LibraryPreferences preferences) {
BibDatabaseMode mode = getBibDatabaseModeFromParserResult(parserResult, preferences);
BibEntryTypesManager entryTypesManager = Injector.instantiateModelOrService(BibEntryTypesManager.class);

return parserResult.getEntryTypes()
Expand All @@ -40,7 +42,7 @@ private List<BibEntryType> getListOfUnknownAndUnequalCustomizations(ParserResult
.collect(Collectors.toList());
}

private BibDatabaseMode getBibDatabaseModeFromParserResult(ParserResult parserResult) {
return parserResult.getMetaData().getMode().orElse(Globals.prefs.getLibraryPreferences().getDefaultBibDatabaseMode());
private BibDatabaseMode getBibDatabaseModeFromParserResult(ParserResult parserResult, LibraryPreferences preferences) {
return parserResult.getMetaData().getMode().orElse(preferences.getDefaultBibDatabaseMode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jabref.gui.DialogService;
import org.jabref.logic.importer.ParserResult;
import org.jabref.preferences.PreferencesService;

/**
* This interface defines potential actions that may need to be taken after
Expand All @@ -21,7 +22,7 @@ public interface GUIPostOpenAction {
* @param pr The result of the BIB parse operation.
* @return true if the action should be called, false otherwise.
*/
boolean isActionNecessary(ParserResult pr);
boolean isActionNecessary(ParserResult pr, PreferencesService preferencesService);

/**
* This method is called after the new database has been added to the GUI, if
Expand All @@ -33,5 +34,5 @@ public interface GUIPostOpenAction {
*
* @param pr The result of the BIB parse operation.
*/
void performAction(ParserResult pr, DialogService dialogService);
void performAction(ParserResult pr, DialogService dialogService, PreferencesService preferencesService);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import org.jabref.logic.importer.ParserResult;
import org.jabref.migrations.MergeReviewIntoCommentMigration;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.PreferencesService;

public class MergeReviewIntoCommentAction implements GUIPostOpenAction {

@Override
public boolean isActionNecessary(ParserResult parserResult) {
public boolean isActionNecessary(ParserResult parserResult, PreferencesService preferencesService) {
return MergeReviewIntoCommentMigration.needsMigration(parserResult);
}

@Override
public void performAction(ParserResult parserResult, DialogService dialogService) {
public void performAction(ParserResult parserResult, DialogService dialogService, PreferencesService preferencesService) {
MergeReviewIntoCommentMigration migration = new MergeReviewIntoCommentMigration();

migration.performMigration(parserResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public OpenDatabaseAction(LibraryTabContainer tabContainer,
*
* @param result The result of the BIB file parse operation.
*/
public static void performPostOpenActions(ParserResult result, DialogService dialogService) {
public static void performPostOpenActions(ParserResult result, DialogService dialogService, PreferencesService preferencesService) {
for (GUIPostOpenAction action : OpenDatabaseAction.POST_OPEN_ACTIONS) {
if (action.isActionNecessary(result)) {
action.performAction(result, dialogService);
if (action.isActionNecessary(result, preferencesService)) {
action.performAction(result, dialogService, preferencesService);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.preferences.PreferencesService;

public class CopyFieldValueCommand extends SimpleCommand {
private final String fieldValue;
private final ClipBoardManager clipBoardManager;

public CopyFieldValueCommand(PreferencesService preferencesService, final String fieldValue) {
public CopyFieldValueCommand(final String fieldValue) {
Objects.requireNonNull(fieldValue, "Field value cannot be null");
this.fieldValue = fieldValue;
this.clipBoardManager = new ClipBoardManager(preferencesService);
this.clipBoardManager = new ClipBoardManager();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private Button createCopyButton() {
FontIcon copyIcon = FontIcon.of(MaterialDesignC.CONTENT_COPY);
copyIcon.getStyleClass().add("action-icon");

Button copyButton = factory.createIconButton(() -> Localization.lang("Copy"), new CopyFieldValueCommand(preferencesService, getText()));
Button copyButton = factory.createIconButton(() -> Localization.lang("Copy"), new CopyFieldValueCommand(getText()));
copyButton.setGraphic(copyIcon);
copyButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
copyButton.setMaxHeight(Double.MAX_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.pdf.search.PdfSearchResults;
import org.jabref.model.pdf.search.SearchResult;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -55,7 +57,8 @@ public PdfSearchResults getFulltextResults(String query, BibEntry bibEntry) {
LOGGER.trace("Performing full query {}.", query);
PdfIndexer pdfIndexer;
try {
pdfIndexer = PdfIndexerManager.getIndexer(Globals.stateManager.getActiveDatabase().get(), Globals.prefs.getFilePreferences());
PreferencesService preferencesService = Injector.instantiateModelOrService(PreferencesService.class);
pdfIndexer = PdfIndexerManager.getIndexer(Globals.stateManager.getActiveDatabase().get(), preferencesService.getFilePreferences());
} catch (IOException e) {
LOGGER.error("Could not access full text index.", e);
return new PdfSearchResults();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -92,7 +93,7 @@ private BibDatabase initializeDatabaseFromPath(Path testFile) throws Exception {
Globals.stateManager.setActiveDatabase(context);
PreferencesService preferencesService = mock(PreferencesService.class);
when(preferencesService.getFilePreferences()).thenReturn(filePreferences);
Globals.prefs = preferencesService;
Injector.setModelOrService(PreferencesService.class, preferencesService);

pdfIndexer = PdfIndexerManager.getIndexer(context, filePreferences);
// Alternative - For debugging with Luke (part of the Apache Lucene distribution)
Expand Down

0 comments on commit f174227

Please sign in to comment.