From: Christian Femers Date: Tue, 24 Sep 2019 03:13:46 +0000 (+0200) Subject: Allow to listen for changes in the MachineRegistry X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=34c9da2d7580f2312da458db619dcf36e6325cef;p=Mograsim.git Allow to listen for changes in the MachineRegistry --- diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineRegistry.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineRegistry.java index 52e5d60d..ca52eaea 100644 --- a/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineRegistry.java +++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineRegistry.java @@ -3,7 +3,11 @@ package net.mograsim.machine; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Objects; +import java.util.Set; + import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtension; @@ -14,9 +18,11 @@ import org.eclipse.core.runtime.Platform; public class MachineRegistry { + private static final String MACHINE_EXT_ID = "net.mograsim.machine.machine_definition"; - private static final Map installedMachines = new HashMap<>(); + private static final Map installedMachines = Collections.synchronizedMap(new HashMap<>()); + private static final Set listeners = Collections.synchronizedSet(new HashSet<>()); private static void reload() { @@ -50,6 +56,7 @@ public class MachineRegistry { System.out.println(ex.getMessage()); } + notifyListeners(); } public static void initialize() @@ -93,4 +100,26 @@ public class MachineRegistry { return installedMachines.get(id); } + + private static void notifyListeners() + { + Map unmodMachines = getInstalledMachines(); + listeners.forEach(l -> l.onReload(unmodMachines)); + } + + public static void addMachineRegistryListener(MachineRegistryListener listener) + { + listeners.add(listener); + } + + public static void removeMachineRegistryListener(MachineRegistryListener listener) + { + listeners.remove(listener); + } + + @FunctionalInterface + public interface MachineRegistryListener + { + void onReload(Map installedMachines); + } }