import net.mograsim.machine.Machine;
import net.mograsim.machine.MachineDefinition;
import net.mograsim.machine.MachineRegistry;
+import net.mograsim.plugin.nature.ProjectContextEvent.ProjectContextEventType;
public class MachineContext
{
{
machineDefinition = machineId.map(MachineRegistry::getMachine);
machineDefinition.ifPresent(md -> setActiveMachine(md.createNew()));
+ ProjectMachineContext.notifyListeners(new ProjectContextEvent(this, ProjectContextEventType.MACHINE_DEFINITION_CHANGE));
}
private void preferenceListener(PropertyChangeEvent changeEvent)
--- /dev/null
+package net.mograsim.plugin.nature;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResourceChangeEvent;
+
+public class ProjectContextEvent
+{
+ private final MachineContext machineContext;
+ private final ProjectContextEventType eventType;
+
+ public ProjectContextEvent(MachineContext machineContext, ProjectContextEventType eventType)
+ {
+ this.machineContext = machineContext;
+ this.eventType = eventType;
+ }
+
+ public final MachineContext getMachineContext()
+ {
+ return machineContext;
+ }
+
+ public final ProjectContextEventType getEventType()
+ {
+ return eventType;
+ }
+
+ public final IProject getProject()
+ {
+ return machineContext.getProject();
+ }
+
+ public enum ProjectContextEventType
+ {
+ NEW, MACHINE_DEFINITION_CHANGE, OTHER_CHANGE, REFRESH, CLOSE, DELETE;
+
+ static ProjectContextEventType ofResourceChangeEvent(int id)
+ {
+ switch (id)
+ {
+ case IResourceChangeEvent.POST_CHANGE:
+ return OTHER_CHANGE;
+ case IResourceChangeEvent.PRE_CLOSE:
+ return CLOSE;
+ case IResourceChangeEvent.PRE_DELETE:
+ return DELETE;
+ case IResourceChangeEvent.PRE_REFRESH:
+ return REFRESH;
+ default:
+ return null;
+ }
+ }
+ }
+}
\ No newline at end of file
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
+import java.util.Set;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.ProjectScope;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import net.mograsim.plugin.nature.ProjectContextEvent.ProjectContextEventType;
+
public class ProjectMachineContext
{
private static Map<IProject, MachineContext> projectMachineContexts = Collections.synchronizedMap(new HashMap<>());
+ private static final Set<ProjectContextListener> listeners = Collections.synchronizedSet(new HashSet<>());
public static final String MOGRASIM_PROJECT_PREFS_NODE = "net.mograsim";
public static final String MACHINE_PROPERTY = "net.mograsim.projectMachineId";
validateMograsimNatureProject(project);
mc = new MachineContext(project);
projectMachineContexts.put(project, mc);
+ notifyListeners(new ProjectContextEvent(mc, ProjectContextEventType.NEW));
return mc;
}
return getMachineContextOf(project);
}
+ public static Map<IProject, MachineContext> getAllProjectMachineContexts()
+ {
+ return Collections.unmodifiableMap(projectMachineContexts);
+ }
+
static ScopedPreferenceStore getProjectPrefs(IProject mograsimProject)
{
return new ScopedPreferenceStore(new ProjectScope(mograsimProject), MOGRASIM_PROJECT_PREFS_NODE);
return Optional.of(preferenceStore.getString(MACHINE_PROPERTY));
return Optional.empty();
}
+
+ static void notifyListeners(ProjectContextEvent projectContextEvent)
+ {
+ listeners.forEach(l -> l.onProjectContextChange(projectContextEvent));
+ }
+
+ public static void addProjectContextListener(ProjectContextListener listener)
+ {
+ listeners.add(listener);
+ }
+
+ public static void removeProjectContextListener(ProjectContextListener listener)
+ {
+ listeners.remove(listener);
+ }
+
+ static
+ {
+ ResourcesPlugin.getWorkspace().addResourceChangeListener(ProjectMachineContext::resourceChanged);
+ }
+
+ private static void resourceChanged(IResourceChangeEvent event)
+ {
+ ProjectContextEventType eventType = ProjectContextEventType.ofResourceChangeEvent(event.getType());
+ if (eventType == null)
+ return;
+ IProject p = event.getResource().getProject();
+ if (p == null)
+ return;
+ MachineContext mc = projectMachineContexts.get(p);
+ if (mc == null)
+ return;
+ notifyListeners(new ProjectContextEvent(mc, eventType));
+ }
}