From 59afdc935cc4455d8ade9264b81b3cf7a8223eda Mon Sep 17 00:00:00 2001 From: Christian Femers Date: Thu, 19 Sep 2019 18:11:15 +0200 Subject: [PATCH] Added a listener system to project specific machine context --- .../plugin/nature/MachineContext.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/MachineContext.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/MachineContext.java index b434ec97..b1708482 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/MachineContext.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/MachineContext.java @@ -1,8 +1,10 @@ package net.mograsim.plugin.nature; import java.io.IOException; +import java.util.HashSet; import java.util.Objects; import java.util.Optional; +import java.util.Set; import org.eclipse.core.resources.IProject; import org.eclipse.jface.util.PropertyChangeEvent; @@ -20,6 +22,8 @@ public class MachineContext Optional machineDefinition; Optional activeMachine; + private final Set observers = new HashSet<>(); + public MachineContext(IProject owner) { this.owner = Objects.requireNonNull(owner); @@ -87,6 +91,7 @@ public class MachineContext public final void setActiveMachine(Machine machine) { activeMachine = Optional.ofNullable(machine); + notifyObservers(); } public final Optional getMachineId() @@ -117,4 +122,26 @@ public class MachineContext updateDefinition(); } } + + public void registerObserver(ActiveMachineListener ob) + { + observers.add(ob); + ob.setMachine(activeMachine); + } + + public void deregisterObserver(ActiveMachineListener ob) + { + observers.remove(ob); + } + + private void notifyObservers() + { + observers.forEach(ob -> ob.setMachine(activeMachine)); + } + + @FunctionalInterface + public static interface ActiveMachineListener + { + void setMachine(Optional machine); + } } \ No newline at end of file -- 2.17.1