From 4f9c8dee9a0fc97efd3aaad5492cb1ff578d44b0 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 27 Jul 2017 14:06:04 -0700 Subject: [PATCH] Add a PluginListener type for use in the PluginManager. --- .../nwapw/abacus/plugin/PluginListener.java | 20 +++++++++++++++++++ .../nwapw/abacus/plugin/PluginManager.java | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/org/nwapw/abacus/plugin/PluginListener.java diff --git a/src/org/nwapw/abacus/plugin/PluginListener.java b/src/org/nwapw/abacus/plugin/PluginListener.java new file mode 100644 index 0000000..bfe00ee --- /dev/null +++ b/src/org/nwapw/abacus/plugin/PluginListener.java @@ -0,0 +1,20 @@ +package org.nwapw.abacus.plugin; + +/** + * A listener that responds to changes in the PluginManager. + */ +public interface PluginListener { + + /** + * Called when the PluginManager loads plugins. + * @param manager the manager that fired the event. + */ + public void onLoad(PluginManager manager); + + /** + * Called when the PluginManager unloads all its plugins. + * @param manager the manager that fired the event. + */ + public void onUnload(PluginManager manager); + +} diff --git a/src/org/nwapw/abacus/plugin/PluginManager.java b/src/org/nwapw/abacus/plugin/PluginManager.java index 212bc7e..f654332 100644 --- a/src/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/org/nwapw/abacus/plugin/PluginManager.java @@ -48,6 +48,7 @@ public class PluginManager { cachedOperators = new HashMap<>(); allFunctions = new HashSet<>(); allOperators = new HashSet<>(); + listeners = new HashSet<>(); } /** @@ -129,6 +130,7 @@ public class PluginManager { allFunctions.addAll(plugin.providedFunctions()); allOperators.addAll(plugin.providedOperators()); } + listeners.forEach(e -> e.onLoad(this)); } /** @@ -138,6 +140,7 @@ public class PluginManager { for(Plugin plugin : plugins) plugin.disable(); allFunctions.clear(); allOperators.clear(); + listeners.forEach(e -> e.onUnload(this)); } /** @@ -163,4 +166,21 @@ public class PluginManager { public HashSet getAllOperators() { return allOperators; } + + /** + * Adds a plugin change listener to this plugin manager. + * @param listener the listener to add. + */ + public void addListener(PluginListener listener){ + listeners.add(listener); + } + + /** + * Remove the plugin change listener from this plugin manager. + * @param listener the listener to remove. + */ + public void removeListener(PluginListener listener){ + listeners.remove(listener); + } + }