From 62aafe6ae25b9c992473314a6e479fa6a704b70e Mon Sep 17 00:00:00 2001 From: Arthur Poiret Date: Thu, 30 Apr 2026 07:42:14 +0200 Subject: [PATCH] refactor: remove task factories listeners to use application event bus --- .../core/components/BaseTaskFactory.java | 8 ------ .../components/ExploreTaskFactory.java | 20 +++++---------- .../controllers/ExploreController.java | 7 +++++- .../events/SourceSyncEvent.java} | 12 ++++----- .../plugin/components/PluginTaskFactory.java | 19 ++++---------- .../plugin/controllers/PluginsController.java | 8 ++++-- .../plugin/events/PluginScanEvent.java | 25 +++++++++++++++++++ .../components/ProjectTaskFactory.java | 20 +++++---------- .../controllers/ProjectsController.java | 15 +++++------ .../project/events/ProjectSyncEvent.java | 25 +++++++++++++++++++ 10 files changed, 92 insertions(+), 67 deletions(-) rename owlplug-client/src/main/java/com/owlplug/{core/tasks/SimpleEventListener.java => explore/events/SourceSyncEvent.java} (80%) create mode 100644 owlplug-client/src/main/java/com/owlplug/plugin/events/PluginScanEvent.java create mode 100644 owlplug-client/src/main/java/com/owlplug/project/events/ProjectSyncEvent.java diff --git a/owlplug-client/src/main/java/com/owlplug/core/components/BaseTaskFactory.java b/owlplug-client/src/main/java/com/owlplug/core/components/BaseTaskFactory.java index 3cdafe2f..64906269 100644 --- a/owlplug-client/src/main/java/com/owlplug/core/components/BaseTaskFactory.java +++ b/owlplug-client/src/main/java/com/owlplug/core/components/BaseTaskFactory.java @@ -19,9 +19,7 @@ package com.owlplug.core.components; import com.owlplug.core.tasks.AbstractTask; -import com.owlplug.core.tasks.SimpleEventListener; import com.owlplug.core.tasks.TaskExecutionContext; -import java.util.List; import org.springframework.beans.factory.annotation.Autowired; public class BaseTaskFactory { @@ -36,11 +34,5 @@ public TaskExecutionContext create(AbstractTask task) { protected TaskExecutionContext buildContext(AbstractTask task) { return new TaskExecutionContext(task, taskRunner); } - - protected void notifyListeners(List listeners) { - for (SimpleEventListener listener : listeners) { - listener.onAction(); - } - } } diff --git a/owlplug-client/src/main/java/com/owlplug/explore/components/ExploreTaskFactory.java b/owlplug-client/src/main/java/com/owlplug/explore/components/ExploreTaskFactory.java index 8c2c798b..03c37ae5 100644 --- a/owlplug-client/src/main/java/com/owlplug/explore/components/ExploreTaskFactory.java +++ b/owlplug-client/src/main/java/com/owlplug/explore/components/ExploreTaskFactory.java @@ -20,9 +20,9 @@ import com.owlplug.core.components.ApplicationDefaults; import com.owlplug.core.components.BaseTaskFactory; -import com.owlplug.core.tasks.SimpleEventListener; import com.owlplug.core.tasks.TaskExecutionContext; import com.owlplug.core.utils.FileUtils; +import com.owlplug.explore.events.SourceSyncEvent; import com.owlplug.explore.model.PackageBundle; import com.owlplug.explore.repositories.RemotePackageRepository; import com.owlplug.explore.repositories.RemoteSourceRepository; @@ -30,8 +30,8 @@ import com.owlplug.explore.tasks.SourceSyncTask; import com.owlplug.plugin.components.PluginTaskFactory; import java.io.File; -import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @Component @@ -45,20 +45,20 @@ public class ExploreTaskFactory extends BaseTaskFactory { private RemoteSourceRepository remoteSourceRepository; @Autowired private RemotePackageRepository remotePackageRepository; - - private ArrayList syncSourcesListeners = new ArrayList<>(); + @Autowired + private ApplicationEventPublisher publisher; /** * Creates a {@link SourceSyncTask} and binds listeners to the success callback. - * + * * @return */ public TaskExecutionContext createSourceSyncTask() { SourceSyncTask task = new SourceSyncTask(remoteSourceRepository, remotePackageRepository); task.setOnSucceeded(e -> { - notifyListeners(syncSourcesListeners); + publisher.publishEvent(new SourceSyncEvent()); }); return create(task); } @@ -78,12 +78,4 @@ public TaskExecutionContext createBundleInstallTask(PackageBundle bundle, File t - public void addSyncSourcesListener(SimpleEventListener eventListener) { - syncSourcesListeners.add(eventListener); - } - - public void removeSyncSourcesListener(SimpleEventListener eventListener) { - syncSourcesListeners.remove(eventListener); - } - } diff --git a/owlplug-client/src/main/java/com/owlplug/explore/controllers/ExploreController.java b/owlplug-client/src/main/java/com/owlplug/explore/controllers/ExploreController.java index fd9f110d..a33d1c05 100644 --- a/owlplug-client/src/main/java/com/owlplug/explore/controllers/ExploreController.java +++ b/owlplug-client/src/main/java/com/owlplug/explore/controllers/ExploreController.java @@ -30,6 +30,7 @@ import com.owlplug.explore.components.ExploreTaskFactory; import com.owlplug.explore.controllers.dialogs.InstallStepDialogController; import com.owlplug.explore.events.RemoteSourceUpdatedEvent; +import com.owlplug.explore.events.SourceSyncEvent; import com.owlplug.explore.model.PackageBundle; import com.owlplug.explore.model.RemotePackage; import com.owlplug.explore.model.search.ExploreFilterCriteria; @@ -229,7 +230,6 @@ public void initialize() { }); lazyLoadBar.setVisible(false); - exploreTaskFactory.addSyncSourcesListener(() -> performPackageSearch()); performPackageSearch(); masonryPane.setHSpacing(5); @@ -401,6 +401,11 @@ public void requestLayout() { scrollPane.requestLayout(); } + @EventListener + private void handle(SourceSyncEvent event) { + FX.run(this::performPackageSearch); + } + @EventListener private void handle(RemoteSourceUpdatedEvent event) { FX.run(this::performPackageSearch); diff --git a/owlplug-client/src/main/java/com/owlplug/core/tasks/SimpleEventListener.java b/owlplug-client/src/main/java/com/owlplug/explore/events/SourceSyncEvent.java similarity index 80% rename from owlplug-client/src/main/java/com/owlplug/core/tasks/SimpleEventListener.java rename to owlplug-client/src/main/java/com/owlplug/explore/events/SourceSyncEvent.java index 0d21f90e..9f091a0e 100644 --- a/owlplug-client/src/main/java/com/owlplug/core/tasks/SimpleEventListener.java +++ b/owlplug-client/src/main/java/com/owlplug/explore/events/SourceSyncEvent.java @@ -15,13 +15,11 @@ * You should have received a copy of the GNU General Public License * along with OwlPlug. If not, see . */ - -package com.owlplug.core.tasks; -import java.util.EventListener; - -@FunctionalInterface -public interface SimpleEventListener extends EventListener { - void onAction(); +package com.owlplug.explore.events; +/** + * Remote source sync task has completed successfully. + */ +public record SourceSyncEvent() { } diff --git a/owlplug-client/src/main/java/com/owlplug/plugin/components/PluginTaskFactory.java b/owlplug-client/src/main/java/com/owlplug/plugin/components/PluginTaskFactory.java index 1e783663..9c321a18 100644 --- a/owlplug-client/src/main/java/com/owlplug/plugin/components/PluginTaskFactory.java +++ b/owlplug-client/src/main/java/com/owlplug/plugin/components/PluginTaskFactory.java @@ -21,9 +21,9 @@ import com.owlplug.core.components.ApplicationDefaults; import com.owlplug.core.components.ApplicationPreferences; import com.owlplug.core.components.BaseTaskFactory; -import com.owlplug.core.tasks.SimpleEventListener; import com.owlplug.core.tasks.TaskExecutionContext; import com.owlplug.core.utils.FileUtils; +import com.owlplug.plugin.events.PluginScanEvent; import com.owlplug.plugin.model.Plugin; import com.owlplug.plugin.repositories.FileStatRepository; import com.owlplug.plugin.repositories.PluginFootprintRepository; @@ -36,9 +36,9 @@ import com.owlplug.plugin.tasks.PluginScanTask; import com.owlplug.plugin.tasks.discovery.PluginScanTaskParameters; import com.owlplug.project.components.ProjectTaskFactory; -import java.util.ArrayList; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -64,9 +64,8 @@ public class PluginTaskFactory extends BaseTaskFactory { private ProjectTaskFactory projectTaskFactory; @Autowired private FileStatRepository fileStatRepository; - - - private ArrayList scanPluginsListeners = new ArrayList<>(); + @Autowired + private ApplicationEventPublisher publisher; /** * Creates a {@link PluginScanTask} and binds listeners to the success callback. @@ -122,7 +121,7 @@ public TaskExecutionContext createPluginScanTask(String directoryScope, boolean nativeHostService); scanTask.setOnSucceeded(scanEvent -> { - notifyListeners(scanPluginsListeners); + publisher.publishEvent(new PluginScanEvent()); TaskExecutionContext lookupTask = projectTaskFactory.createLookupTask(); if (prefs.getBoolean(ApplicationDefaults.SYNC_FILE_STAT_KEY, true) @@ -164,12 +163,4 @@ public TaskExecutionContext createPluginRemoveTask(Plugin plugin) { return create(task); } - public void addScanPluginsListener(SimpleEventListener eventListener) { - scanPluginsListeners.add(eventListener); - } - - public void removeScanPluginsListener(SimpleEventListener eventListener) { - scanPluginsListeners.remove(eventListener); - } - } diff --git a/owlplug-client/src/main/java/com/owlplug/plugin/controllers/PluginsController.java b/owlplug-client/src/main/java/com/owlplug/plugin/controllers/PluginsController.java index 7a6e327d..d0dbbca5 100644 --- a/owlplug-client/src/main/java/com/owlplug/plugin/controllers/PluginsController.java +++ b/owlplug-client/src/main/java/com/owlplug/plugin/controllers/PluginsController.java @@ -25,6 +25,7 @@ import com.owlplug.plugin.controllers.dialogs.ExportDialogController; import com.owlplug.plugin.controllers.dialogs.NewLinkController; import com.owlplug.plugin.events.PluginRefreshEvent; +import com.owlplug.plugin.events.PluginScanEvent; import com.owlplug.plugin.events.PluginUpdateEvent; import com.owlplug.plugin.model.Plugin; import com.owlplug.plugin.repositories.PluginRepository; @@ -194,8 +195,6 @@ public void initialize() { pluginService.scanPlugins(false); }); - taskFactory.addScanPluginsListener(this::displayPlugins); - exportButton.setOnAction(e -> { this.getTelemetryService().event("/Plugins/Export"); exportDialogController.show(); @@ -234,6 +233,11 @@ public void toggleInfoPaneDisplay() { pluginInfoPane.setVisible(!pluginInfoPane.isVisible()); } + @EventListener + private void handle(PluginScanEvent event) { + FX.run(this::displayPlugins); + } + @EventListener private void handle(PluginRefreshEvent event) { FX.run(this::refresh); diff --git a/owlplug-client/src/main/java/com/owlplug/plugin/events/PluginScanEvent.java b/owlplug-client/src/main/java/com/owlplug/plugin/events/PluginScanEvent.java new file mode 100644 index 00000000..a0657c79 --- /dev/null +++ b/owlplug-client/src/main/java/com/owlplug/plugin/events/PluginScanEvent.java @@ -0,0 +1,25 @@ +/* OwlPlug + * Copyright (C) 2021 Arthur + * + * This file is part of OwlPlug. + * + * OwlPlug is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 + * as published by the Free Software Foundation. + * + * OwlPlug is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with OwlPlug. If not, see . + */ + +package com.owlplug.plugin.events; + +/** + * Plugin scan task has completed successfully. + */ +public record PluginScanEvent() { +} diff --git a/owlplug-client/src/main/java/com/owlplug/project/components/ProjectTaskFactory.java b/owlplug-client/src/main/java/com/owlplug/project/components/ProjectTaskFactory.java index d0358db9..fd6bcae8 100644 --- a/owlplug-client/src/main/java/com/owlplug/project/components/ProjectTaskFactory.java +++ b/owlplug-client/src/main/java/com/owlplug/project/components/ProjectTaskFactory.java @@ -22,16 +22,16 @@ import com.owlplug.core.components.ApplicationDefaults; import com.owlplug.core.components.ApplicationPreferences; import com.owlplug.core.components.BaseTaskFactory; -import com.owlplug.core.tasks.SimpleEventListener; import com.owlplug.core.tasks.TaskExecutionContext; +import com.owlplug.project.events.ProjectSyncEvent; import com.owlplug.project.repositories.DawPluginRepository; import com.owlplug.project.repositories.DawProjectRepository; import com.owlplug.project.services.PluginLookupService; import com.owlplug.project.tasks.PluginLookupTask; import com.owlplug.project.tasks.ProjectSyncTask; -import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @Component @@ -46,8 +46,8 @@ public class ProjectTaskFactory extends BaseTaskFactory { private DawProjectRepository projectRepository; @Autowired private DawPluginRepository dawPluginRepository; - - private ArrayList syncProjectsListeners = new ArrayList<>(); + @Autowired + private ApplicationEventPublisher publisher; public TaskExecutionContext createSyncTask() { @@ -56,7 +56,7 @@ public TaskExecutionContext createSyncTask() { ProjectSyncTask task = new ProjectSyncTask(projectRepository, directories); task.setOnSucceeded(e -> { createLookupTask().scheduleNow(); - notifyListeners(syncProjectsListeners); + publisher.publishEvent(new ProjectSyncEvent()); }); return create(task); } @@ -65,16 +65,8 @@ public TaskExecutionContext createLookupTask() { PluginLookupTask task = new PluginLookupTask(dawPluginRepository, lookupService); task.setOnSucceeded(e -> { - notifyListeners(syncProjectsListeners); + publisher.publishEvent(new ProjectSyncEvent()); }); return create(task); } - - public void addSyncProjectsListener(SimpleEventListener eventListener) { - syncProjectsListeners.add(eventListener); - } - - public void removeSyncProjectsListener(SimpleEventListener eventListener) { - syncProjectsListeners.remove(eventListener); - } } diff --git a/owlplug-client/src/main/java/com/owlplug/project/controllers/ProjectsController.java b/owlplug-client/src/main/java/com/owlplug/project/controllers/ProjectsController.java index dfc73d71..e5f5455c 100644 --- a/owlplug-client/src/main/java/com/owlplug/project/controllers/ProjectsController.java +++ b/owlplug-client/src/main/java/com/owlplug/project/controllers/ProjectsController.java @@ -22,7 +22,8 @@ import com.owlplug.core.controllers.BaseController; import com.owlplug.core.controllers.dialogs.ListDirectoryDialogController; import com.owlplug.core.ui.FilterableTreeItem; -import com.owlplug.project.components.ProjectTaskFactory; +import com.owlplug.core.utils.FX; +import com.owlplug.project.events.ProjectSyncEvent; import com.owlplug.project.model.DawProject; import com.owlplug.project.services.ProjectService; import com.owlplug.project.ui.ProjectTreeCell; @@ -37,6 +38,7 @@ import javafx.util.Callback; import jfxtras.styles.jmetro.JMetroStyleClass; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.event.EventListener; import org.springframework.stereotype.Controller; @Controller @@ -45,8 +47,6 @@ public class ProjectsController extends BaseController { @Autowired private ProjectService projectService; @Autowired - private ProjectTaskFactory projectTaskFactory; - @Autowired private ListDirectoryDialogController listDirectoryDialogController; @Autowired private ProjectInfoController projectInfoController; @@ -71,10 +71,6 @@ public void initialize() { projectService.syncProjects(); }); - projectTaskFactory.addSyncProjectsListener(() -> { - refresh(); - }); - projectTreeViewTabPane.getStyleClass().add(JMetroStyleClass.UNDERLINE_TAB_PANE); projectTreeView.setCellFactory((Callback, TreeCell>) @@ -125,4 +121,9 @@ public void refresh() { projectTreeNode.setExpanded(true); } + + @EventListener + private void handle(ProjectSyncEvent event) { + FX.run(this::refresh); + } } diff --git a/owlplug-client/src/main/java/com/owlplug/project/events/ProjectSyncEvent.java b/owlplug-client/src/main/java/com/owlplug/project/events/ProjectSyncEvent.java new file mode 100644 index 00000000..3348ce9e --- /dev/null +++ b/owlplug-client/src/main/java/com/owlplug/project/events/ProjectSyncEvent.java @@ -0,0 +1,25 @@ +/* OwlPlug + * Copyright (C) 2021 Arthur + * + * This file is part of OwlPlug. + * + * OwlPlug is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 + * as published by the Free Software Foundation. + * + * OwlPlug is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with OwlPlug. If not, see . + */ + +package com.owlplug.project.events; + +/** + * Project sync or plugin lookup task has completed successfully. + */ +public record ProjectSyncEvent() { +}