From 6d34baee811ff4c19f3175af52955e07a8bc0271 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 23 May 2024 11:44:20 +0200 Subject: [PATCH] Migrate StateManager --- .../java/org/jabref/gui/DefaultInjector.java | 2 -- src/main/java/org/jabref/gui/Globals.java | 7 +------ src/main/java/org/jabref/gui/JabRefGUI.java | 5 ++++- .../FieldFormatterCleanupsPanel.java | 6 +++++- .../FieldFormatterCleanupsPanelViewModel.java | 9 ++++++--- .../jabref/gui/util/DefaultTaskExecutor.java | 20 ++++++++++--------- .../search/rules/FullTextSearchRule.java | 5 +++-- .../org/jabref/gui/ClipBoardManagerTest.java | 2 +- .../DatabaseSearcherWithBibFilesTest.java | 13 ++++++++---- 9 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/jabref/gui/DefaultInjector.java b/src/main/java/org/jabref/gui/DefaultInjector.java index 6c5535a6239..cf93ad54b38 100644 --- a/src/main/java/org/jabref/gui/DefaultInjector.java +++ b/src/main/java/org/jabref/gui/DefaultInjector.java @@ -29,8 +29,6 @@ private static Object createDependency(Class clazz) { return Globals.getKeyPrefs(); } else if (clazz == JournalAbbreviationRepository.class) { return Globals.journalAbbreviationRepository; - } else if (clazz == StateManager.class) { - return Globals.stateManager; } else if (clazz == FileUpdateMonitor.class) { return Globals.getFileUpdateMonitor(); } else if (clazz == ProtectedTermsLoader.class) { diff --git a/src/main/java/org/jabref/gui/Globals.java b/src/main/java/org/jabref/gui/Globals.java index 41d7283ba2e..e3c0f4ef225 100644 --- a/src/main/java/org/jabref/gui/Globals.java +++ b/src/main/java/org/jabref/gui/Globals.java @@ -30,12 +30,7 @@ public class Globals { public static final RemoteListenerServerManager REMOTE_LISTENER = new RemoteListenerServerManager(); - /** - * Manager for the state of the GUI. - */ - public static StateManager stateManager = new StateManager(); - - public static final TaskExecutor TASK_EXECUTOR = new DefaultTaskExecutor(stateManager); + public static final TaskExecutor TASK_EXECUTOR = new DefaultTaskExecutor(); /** * This field is initialized upon startup. diff --git a/src/main/java/org/jabref/gui/JabRefGUI.java b/src/main/java/org/jabref/gui/JabRefGUI.java index cd4980c4322..d18a36a4089 100644 --- a/src/main/java/org/jabref/gui/JabRefGUI.java +++ b/src/main/java/org/jabref/gui/JabRefGUI.java @@ -52,6 +52,7 @@ public class JabRefGUI extends Application { private static JabRefFrame mainFrame; private static DialogService dialogService; private static ThemeManager themeManager; + private static StateManager stateManager; private static CountingUndoManager countingUndoManager; @@ -75,6 +76,8 @@ public void start(Stage stage) { WebViewStore.init(); + JabRefGUI.stateManager = new StateManager(); + JabRefGUI.themeManager = new ThemeManager( preferencesService.getWorkspacePreferences(), fileUpdateMonitor, @@ -93,7 +96,7 @@ public void start(Stage stage) { dialogService, fileUpdateMonitor, preferencesService, - Globals.stateManager, + stateManager, countingUndoManager, Injector.instantiateModelOrService(BibEntryTypesManager.class), Globals.TASK_EXECUTOR); diff --git a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java index f9994545561..287b2918aac 100644 --- a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanel.java @@ -13,6 +13,7 @@ import javafx.scene.input.KeyCode; import javafx.scene.layout.VBox; +import org.jabref.gui.StateManager; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.util.BindingsHelper; import org.jabref.gui.util.FieldsUtil; @@ -24,6 +25,7 @@ import org.jabref.model.entry.field.Field; import com.airhacks.afterburner.views.ViewLoader; +import jakarta.inject.Inject; public class FieldFormatterCleanupsPanel extends VBox { @@ -35,6 +37,8 @@ public class FieldFormatterCleanupsPanel extends VBox { @FXML private ComboBox addableFields; @FXML private ComboBox addableFormatters; + @Inject private StateManager stateManager; + private FieldFormatterCleanupsPanelViewModel viewModel; public FieldFormatterCleanupsPanel() { @@ -45,7 +49,7 @@ public FieldFormatterCleanupsPanel() { @FXML private void initialize() { - this.viewModel = new FieldFormatterCleanupsPanelViewModel(); + this.viewModel = new FieldFormatterCleanupsPanelViewModel(stateManager); setupTable(); setupCombos(); diff --git a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java index 7f77458301b..d7a926291e1 100644 --- a/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java +++ b/src/main/java/org/jabref/gui/commonfxcontrols/FieldFormatterCleanupsPanelViewModel.java @@ -12,7 +12,7 @@ import javafx.collections.transformation.SortedList; import javafx.scene.control.SelectionModel; -import org.jabref.gui.Globals; +import org.jabref.gui.StateManager; import org.jabref.gui.util.NoSelectionModel; import org.jabref.logic.cleanup.FieldFormatterCleanup; import org.jabref.logic.cleanup.FieldFormatterCleanups; @@ -31,11 +31,14 @@ public class FieldFormatterCleanupsPanelViewModel { private final ListProperty availableFormattersProperty = new SimpleListProperty<>(new SortedList<>(FXCollections.observableArrayList(Formatters.getAll()), Comparator.comparing(Formatter::getName))); private final ObjectProperty selectedFormatterProperty = new SimpleObjectProperty<>(); - public FieldFormatterCleanupsPanelViewModel() { + private final StateManager stateManager; + + public FieldFormatterCleanupsPanelViewModel(StateManager stateManager) { + this.stateManager = stateManager; } public void resetToRecommended() { - Globals.stateManager.getActiveDatabase().ifPresent(databaseContext -> { + stateManager.getActiveDatabase().ifPresent(databaseContext -> { if (databaseContext.isBiblatexMode()) { cleanupsListProperty.setAll(FieldFormatterCleanups.RECOMMEND_BIBLATEX_ACTIONS); } else { diff --git a/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java b/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java index 6490ad85946..dd836d6420a 100644 --- a/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java +++ b/src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java @@ -19,6 +19,7 @@ import org.jabref.gui.StateManager; import org.jabref.logic.util.DelayTaskThrottler; +import com.airhacks.afterburner.injection.Injector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,13 +35,6 @@ public class DefaultTaskExecutor implements TaskExecutor { private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2); private final WeakHashMap throttlers = new WeakHashMap<>(); - private final StateManager stateManager; - - public DefaultTaskExecutor(StateManager stateManager) { - super(); - this.stateManager = stateManager; - } - /** * */ @@ -106,7 +100,12 @@ public static void runInJavaFXThread(Runnable runnable) { public Future execute(BackgroundTask task) { Task javafxTask = getJavaFXTask(task); if (task.showToUser()) { - stateManager.addBackgroundTask(task, javafxTask); + StateManager stateManager = Injector.instantiateModelOrService(StateManager.class); + if (stateManager != null) { + stateManager.addBackgroundTask(task, javafxTask); + } else { + LOGGER.info("Background task visible without GUI"); + } } return execute(javafxTask); } @@ -127,7 +126,10 @@ public Future schedule(BackgroundTask task, long delay, TimeUnit unit) */ @Override public void shutdown() { - stateManager.getBackgroundTasks().stream().filter(task -> !task.isDone()).forEach(Task::cancel); + StateManager stateManager = Injector.instantiateModelOrService(StateManager.class); + if (stateManager != null) { + stateManager.getBackgroundTasks().stream().filter(task -> !task.isDone()).forEach(Task::cancel); + } executor.shutdownNow(); scheduledExecutor.shutdownNow(); throttlers.forEach((throttler, aVoid) -> throttler.shutdown()); diff --git a/src/main/java/org/jabref/model/search/rules/FullTextSearchRule.java b/src/main/java/org/jabref/model/search/rules/FullTextSearchRule.java index fe216bdc557..b015681ed3a 100644 --- a/src/main/java/org/jabref/model/search/rules/FullTextSearchRule.java +++ b/src/main/java/org/jabref/model/search/rules/FullTextSearchRule.java @@ -6,7 +6,7 @@ import java.util.List; import org.jabref.architecture.AllowedToUseLogic; -import org.jabref.gui.Globals; +import org.jabref.gui.StateManager; import org.jabref.logic.pdf.search.PdfIndexer; import org.jabref.logic.pdf.search.PdfIndexerManager; import org.jabref.logic.pdf.search.PdfSearcher; @@ -57,8 +57,9 @@ public PdfSearchResults getFulltextResults(String query, BibEntry bibEntry) { LOGGER.trace("Performing full query {}.", query); PdfIndexer pdfIndexer; try { + StateManager stateManager = Injector.instantiateModelOrService(StateManager.class); PreferencesService preferencesService = Injector.instantiateModelOrService(PreferencesService.class); - pdfIndexer = PdfIndexerManager.getIndexer(Globals.stateManager.getActiveDatabase().get(), preferencesService.getFilePreferences()); + pdfIndexer = PdfIndexerManager.getIndexer(stateManager.getActiveDatabase().get(), preferencesService.getFilePreferences()); } catch (IOException e) { LOGGER.error("Could not access full text index.", e); return new PdfSearchResults(); diff --git a/src/test/java/org/jabref/gui/ClipBoardManagerTest.java b/src/test/java/org/jabref/gui/ClipBoardManagerTest.java index 087e6573b9d..0c32b0d8651 100644 --- a/src/test/java/org/jabref/gui/ClipBoardManagerTest.java +++ b/src/test/java/org/jabref/gui/ClipBoardManagerTest.java @@ -56,7 +56,7 @@ void setUp() { entryTypesManager = new BibEntryTypesManager(); // initialize a clipBoardManager - clipBoardManager = new ClipBoardManager(clipboard, clipboardPrimary, preferencesService); + clipBoardManager = new ClipBoardManager(clipboard, clipboardPrimary); } @DisplayName("Check that the ClipBoardManager can set a bibentry as its content from the clipboard") diff --git a/src/test/java/org/jabref/logic/search/DatabaseSearcherWithBibFilesTest.java b/src/test/java/org/jabref/logic/search/DatabaseSearcherWithBibFilesTest.java index 6a3064dcf01..0a2b3f1f746 100644 --- a/src/test/java/org/jabref/logic/search/DatabaseSearcherWithBibFilesTest.java +++ b/src/test/java/org/jabref/logic/search/DatabaseSearcherWithBibFilesTest.java @@ -7,7 +7,7 @@ import java.util.Objects; import java.util.stream.Stream; -import org.jabref.gui.Globals; +import org.jabref.gui.StateManager; import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.importer.fileformat.BibtexImporter; @@ -69,11 +69,12 @@ public class DatabaseSearcherWithBibFilesTest { .withCitationKey("minimal-note-mixed-case") .withFiles(Collections.singletonList(new LinkedFile("", "minimal-note-mixed-case.pdf", StandardFileType.PDF.getName()))); - FilePreferences filePreferences = mock(FilePreferences.class); @TempDir private Path indexDir; private PdfIndexer pdfIndexer; + private StateManager stateManager; + private PreferencesService preferencesService; private BibDatabase initializeDatabaseFromPath(String testFile) throws Exception { return initializeDatabaseFromPath(Path.of(Objects.requireNonNull(DatabaseSearcherWithBibFilesTest.class.getResource(testFile)).toURI())); @@ -90,8 +91,12 @@ private BibDatabase initializeDatabaseFromPath(Path testFile) throws Exception { when(context.getEntries()).thenReturn(database.getEntries()); // Required because of {@Link org.jabref.model.search.rules.FullTextSearchRule.FullTextSearchRule} - Globals.stateManager.setActiveDatabase(context); - PreferencesService preferencesService = mock(PreferencesService.class); + stateManager = new StateManager(); + stateManager.setActiveDatabase(context); + Injector.setModelOrService(StateManager.class, stateManager); + + preferencesService = mock(PreferencesService.class); + FilePreferences filePreferences = mock(FilePreferences.class); when(preferencesService.getFilePreferences()).thenReturn(filePreferences); Injector.setModelOrService(PreferencesService.class, preferencesService);