Added listener and event structure to ProjectMachineContexts
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / nature / ProjectMachineContext.java
index 0e6185c..cf727af 100644 (file)
@@ -2,20 +2,27 @@ package net.mograsim.plugin.nature;
 
 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";
@@ -28,6 +35,7 @@ public class ProjectMachineContext
                validateMograsimNatureProject(project);
                mc = new MachineContext(project);
                projectMachineContexts.put(project, mc);
+               notifyListeners(new ProjectContextEvent(mc, ProjectContextEventType.NEW));
                return mc;
        }
 
@@ -38,6 +46,11 @@ public class ProjectMachineContext
                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);
@@ -88,4 +101,38 @@ public class ProjectMachineContext
                        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));
+       }
 }