X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Fnature%2FMachineContext.java;fp=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Fnature%2FMachineContext.java;h=b434ec9786505fa6bed9c30d92e937092dc1841d;hb=f919efc362c3de5c94d894dc8fc7fe22c03fc865;hp=0000000000000000000000000000000000000000;hpb=76c2b3eab6cec47490bb75713356152deb5d07ed;p=Mograsim.git 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 new file mode 100644 index 00000000..b434ec97 --- /dev/null +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/MachineContext.java @@ -0,0 +1,120 @@ +package net.mograsim.plugin.nature; + +import java.io.IOException; +import java.util.Objects; +import java.util.Optional; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.ui.preferences.ScopedPreferenceStore; + +import net.mograsim.machine.Machine; +import net.mograsim.machine.MachineDefinition; +import net.mograsim.machine.MachineRegistry; + +public class MachineContext +{ + IProject owner; + ScopedPreferenceStore prefs; + Optional machineId; + Optional machineDefinition; + Optional activeMachine; + + public MachineContext(IProject owner) + { + this.owner = Objects.requireNonNull(owner); + prefs = ProjectMachineContext.getProjectPrefs(owner); + prefs.addPropertyChangeListener(this::preferenceListener); + machineId = ProjectMachineContext.getMachineIdFrom(prefs); + updateDefinition(); + } + + public final IProject getProject() + { + return owner; + } + + public final ScopedPreferenceStore getPreferences() + { + return prefs; + } + + /** + * Returns true if the project configuration is valid in the current environment + */ + public final boolean isCurrentyValid() + { + return machineDefinition.isPresent(); + } + + /** + * Returns true if the persisted project configuration itself is intact + */ + public final boolean isIntact() + { + return machineId.isPresent(); + } + + /** + * Returns true if a machine is instantiated and (possibly) running + */ + public final boolean isActive() + { + return activeMachine.isPresent(); + } + + /** + * Sets the projects machineId. Will likely break things, if the {@link MachineContext} {@link #isActive()}. + */ + public final boolean setMachineId(String machineId) + { + prefs.setValue(ProjectMachineContext.MACHINE_PROPERTY, machineId); + try + { + prefs.save(); + } + catch (IOException e) + { + e.printStackTrace(); + return false; + } + return true; + } + + /** + * Sets the active machine in the {@link MachineContext}'s project scope. + */ + public final void setActiveMachine(Machine machine) + { + activeMachine = Optional.ofNullable(machine); + } + + public final Optional getMachineId() + { + return machineId; + } + + public final Optional getMachineDefinition() + { + return machineDefinition; + } + + public final Optional getActiveMachine() + { + return activeMachine; + } + + final void updateDefinition() + { + machineDefinition = machineId.map(MachineRegistry::getMachine); + } + + private void preferenceListener(PropertyChangeEvent changeEvent) + { + if (changeEvent.getProperty().equals(ProjectMachineContext.MACHINE_PROPERTY)) + { + machineId = Optional.ofNullable((String) changeEvent.getNewValue()); + updateDefinition(); + } + } +} \ No newline at end of file