Skip to content

Commit

Permalink
Migrate StateManager
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed May 23, 2024
1 parent 7bae500 commit 6d34bae
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 29 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/jabref/gui/DefaultInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -75,6 +76,8 @@ public void start(Stage stage) {

WebViewStore.init();

JabRefGUI.stateManager = new StateManager();

JabRefGUI.themeManager = new ThemeManager(
preferencesService.getWorkspacePreferences(),
fileUpdateMonitor,
Expand All @@ -93,7 +96,7 @@ public void start(Stage stage) {
dialogService,
fileUpdateMonitor,
preferencesService,
Globals.stateManager,
stateManager,
countingUndoManager,
Injector.instantiateModelOrService(BibEntryTypesManager.class),
Globals.TASK_EXECUTOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -35,6 +37,8 @@ public class FieldFormatterCleanupsPanel extends VBox {
@FXML private ComboBox<Field> addableFields;
@FXML private ComboBox<Formatter> addableFormatters;

@Inject private StateManager stateManager;

private FieldFormatterCleanupsPanelViewModel viewModel;

public FieldFormatterCleanupsPanel() {
Expand All @@ -45,7 +49,7 @@ public FieldFormatterCleanupsPanel() {

@FXML
private void initialize() {
this.viewModel = new FieldFormatterCleanupsPanelViewModel();
this.viewModel = new FieldFormatterCleanupsPanelViewModel(stateManager);

setupTable();
setupCombos();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,11 +31,14 @@ public class FieldFormatterCleanupsPanelViewModel {
private final ListProperty<Formatter> availableFormattersProperty = new SimpleListProperty<>(new SortedList<>(FXCollections.observableArrayList(Formatters.getAll()), Comparator.comparing(Formatter::getName)));
private final ObjectProperty<Formatter> 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 {
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,13 +35,6 @@ public class DefaultTaskExecutor implements TaskExecutor {
private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(2);
private final WeakHashMap<DelayTaskThrottler, Void> throttlers = new WeakHashMap<>();

private final StateManager stateManager;

public DefaultTaskExecutor(StateManager stateManager) {
super();
this.stateManager = stateManager;
}

/**
*
*/
Expand Down Expand Up @@ -106,7 +100,12 @@ public static void runInJavaFXThread(Runnable runnable) {
public <V> Future<V> execute(BackgroundTask<V> task) {
Task<V> 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);
}
Expand All @@ -127,7 +126,10 @@ public <V> Future<?> schedule(BackgroundTask<V> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jabref/gui/ClipBoardManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand All @@ -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);

Expand Down

0 comments on commit 6d34bae

Please sign in to comment.