+ if (activeMachine.isPresent())
+ System.out.format("Created new machine %s for project %s%n", activeMachine.get().getDefinition().getId(), owner.getName());
+ updateStatus();
+ return isActive();
+ }
+
+ /**
+ * This changes the internal status to a newly evaluated one and calls the {@link MachineContextStatusListener}s if this caused the
+ * status to change.
+ *
+ * @see #reevaluateStatus()
+ * @see #getStatus()
+ */
+ public final void updateStatus()
+ {
+ MachineContextStatus newStatus = reevaluateStatus();
+ forceUpdateStatus(newStatus);
+ }
+
+ final void forceUpdateStatus(MachineContextStatus newStatus)
+ {
+ MachineContextStatus oldStatus = status;
+ if (oldStatus == newStatus)
+ return;
+ status = newStatus;
+ System.out.format("Project %s context status: %s -> %s%n", owner.getName(), oldStatus, newStatus);
+ doPostStatusChangedAction();
+ notifyMachineContextStatusListeners(oldStatus);
+ }
+
+ /**
+ * This method reevaluates the status <b>but does not change/update it</b>.<br>
+ * To update the status of the {@link MachineContext}, use {@link #updateStatus()}.
+ *
+ * @return the raw status of the project at the time of the call.
+ */
+ public final MachineContextStatus reevaluateStatus()
+ {
+ if (!owner.exists())
+ return DEAD;
+ if (!owner.isOpen())
+ return CLOSED;
+ if (hasInvaildMograsimProject())
+ return BROKEN;
+ if (machineDefinition.isEmpty())
+ return INTACT;
+ if (activeMachine.isEmpty())
+ return READY;
+ if (!activeMachine.get().getDefinition().getId().equals(machineDefinition.get().getId()))
+ return ACTIVE_CHANGED;
+ return ACTIVE;
+ }
+
+ private void doPostStatusChangedAction()
+ {
+ if ((status == DEAD || status == CLOSED) && activeMachine.isPresent())
+ {
+ System.out.format("Removed machine %s for project %s%n", activeMachine.get().getDefinition().getId(), owner.getName());
+ activeMachine = Optional.empty();
+ notifyActiveMachineListeners();
+ }
+ }
+
+ private boolean hasInvaildMograsimProject()
+ {
+ try
+ {
+ if (!owner.isNatureEnabled(MograsimNature.NATURE_ID))
+ return true;
+ return machineId.isEmpty();
+ }
+ catch (CoreException e)
+ {
+ // cannot happen, because this method is called after the exceptional states were checked.
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ final void updateDefinition(Optional<String> newMachineDefinitionId)
+ {
+ if (newMachineDefinitionId.equals(machineId))
+ return;
+ machineId = newMachineDefinitionId;
+ machineDefinition = machineId.map(MachineRegistry::getMachine);
+ updateStatus();