Merge branch 'machines-are-launch-configs' into development
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / nature / MachineContext.java
index 5531681..c84e622 100644 (file)
@@ -1,6 +1,13 @@
 package net.mograsim.plugin.nature;
 
-import static net.mograsim.plugin.nature.MachineContextStatus.*;
+import static net.mograsim.plugin.nature.MachineContextStatus.ACTIVE;
+import static net.mograsim.plugin.nature.MachineContextStatus.ACTIVE_CHANGED;
+import static net.mograsim.plugin.nature.MachineContextStatus.BROKEN;
+import static net.mograsim.plugin.nature.MachineContextStatus.CLOSED;
+import static net.mograsim.plugin.nature.MachineContextStatus.DEAD;
+import static net.mograsim.plugin.nature.MachineContextStatus.INTACT;
+import static net.mograsim.plugin.nature.MachineContextStatus.READY;
+import static net.mograsim.plugin.nature.MachineContextStatus.UNKOWN;
 
 import java.io.IOException;
 import java.util.HashSet;
@@ -63,7 +70,7 @@ public class MachineContext
         */
        public final boolean isCurrentyValid()
        {
-               return status == READY || status == ACTIVE;
+               return status == READY || isActive();
        }
 
        /**
@@ -115,9 +122,10 @@ public class MachineContext
         */
        public final void setActiveMachine(Machine machine)
        {
+               Optional<Machine> oldMachine = activeMachine;
                activeMachine = Optional.ofNullable(machine);
                updateStatus();
-               notifyActiveMachineListeners();
+               notifyActiveMachineListeners(oldMachine, activeMachine);
        }
 
        public final Optional<String> getMachineId()
@@ -147,8 +155,6 @@ public class MachineContext
                if (status == ACTIVE)
                        return true;
                machineDefinition.ifPresent(md -> setActiveMachine(md.createNew()));
-               if (activeMachine.isPresent())
-                       System.out.format("Created new machine %s for project %s%n", activeMachine.get().getDefinition().getId(), owner.getName());
                updateStatus();
                return isActive();
        }
@@ -172,7 +178,6 @@ public class MachineContext
                if (oldStatus == newStatus)
                        return;
                status = newStatus;
-               System.out.format("Project %s context status: %s -> %s%n", owner.getName(), oldStatus, newStatus);
                doPostStatusChangedAction();
                notifyMachineContextStatusListeners(oldStatus);
        }
@@ -204,9 +209,9 @@ public class MachineContext
        {
                if ((status == DEAD || status == CLOSED) && activeMachine.isPresent())
                {
-                       System.out.format("Removed machine %s for project %s%n", activeMachine.get().getDefinition().getId(), owner.getName());
+                       Optional<Machine> oldMachine = activeMachine;
                        activeMachine = Optional.empty();
-                       notifyActiveMachineListeners();
+                       notifyActiveMachineListeners(oldMachine, activeMachine);
                }
        }
 
@@ -244,15 +249,15 @@ public class MachineContext
                }
        }
 
-       private void notifyActiveMachineListeners()
+       private void notifyActiveMachineListeners(Optional<Machine> oldMachine, Optional<Machine> newMachine)
        {
-               machineListeners.forEach(ob -> ob.setMachine(activeMachine));
+               machineListeners.forEach(ob -> ob.setMachine(oldMachine, newMachine));
        }
 
        public void addActiveMachineListener(ActiveMachineListener ob)
        {
                machineListeners.add(ob);
-               ob.setMachine(activeMachine);
+               ob.setMachine(Optional.empty(), activeMachine);
        }
 
        public void removeActiveMachineListener(ActiveMachineListener ob)
@@ -279,7 +284,7 @@ public class MachineContext
        @FunctionalInterface
        public static interface ActiveMachineListener
        {
-               void setMachine(Optional<Machine> machine);
+               void setMachine(Optional<Machine> oldMachine, Optional<Machine> newMachine);
        }
 
        @FunctionalInterface