X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.machine%2Fsrc%2Fnet%2Fmograsim%2Fmachine%2FMachineRegistry.java;h=d3124123874ee91b95d2e5fb0bebd10d4f452259;hb=535fdb21372b0d55ada7331177e0dd72997920d2;hp=52e5d60dc682a0c18b924f021f6fbb90e7b3c3c8;hpb=e4320ff6606b0e8bc5184b8b1ae1e7cae95a5726;p=Mograsim.git
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..d3124123 100644
--- a/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineRegistry.java
+++ b/plugins/net.mograsim.machine/src/net/mograsim/machine/MachineRegistry.java
@@ -1,9 +1,12 @@
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;
@@ -12,29 +15,35 @@ import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IRegistryEventListener;
import org.eclipse.core.runtime.Platform;
+/**
+ * The MachineRegsitry is a static context registry for all {@link MachineDefinition}s known at any point during runtime.
+ *
+ * It is updated automatically as mograsim machine definition extension points ({@link #MACHINE_EXT_ID}) are added or removed.
+ *
+ * @author Christian Femers
+ *
+ */
public class MachineRegistry
{
- private static final String MACHINE_EXT_ID = "net.mograsim.machine.machine_definition";
- private static final Map installedMachines = new HashMap<>();
+ public static final String MACHINE_EXT_ID = "net.mograsim.machine.machine_definition";
+
+ private static final Map installedMachines = Collections.synchronizedMap(new HashMap<>());
+ private static final Set listeners = Collections.synchronizedSet(new HashSet<>());
private static void reload()
{
installedMachines.clear();
IExtensionRegistry registry = Platform.getExtensionRegistry();
- System.out.println(Arrays.toString(registry.getExtensionPoints("net.mograsim.machine")));
IConfigurationElement[] config = registry.getConfigurationElementsFor(MACHINE_EXT_ID);
try
{
for (IConfigurationElement e : config)
{
- System.out.println(e.getNamespaceIdentifier());
- System.out.println(Arrays.toString(e.getAttributeNames()));
final Object o = e.createExecutableExtension("class");
final String id = e.getAttribute("unique_id");
if (o instanceof MachineDefinition)
{
- System.out.println("Found " + id);
MachineDefinition md = (MachineDefinition) o;
if (Objects.equals(id, md.getId()))
installedMachines.put(id, md);
@@ -48,8 +57,10 @@ public class MachineRegistry
}
catch (CoreException ex)
{
- System.out.println(ex.getMessage());
+ System.err.println("An error occurred reloading the machines:");
+ ex.printStackTrace();
}
+ notifyListeners();
}
public static void initialize()
@@ -93,4 +104,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);
+ }
}