Added listener and event structure to ProjectMachineContexts
authorChristian Femers <femers@in.tum.de>
Tue, 24 Sep 2019 03:18:06 +0000 (05:18 +0200)
committerChristian Femers <femers@in.tum.de>
Tue, 24 Sep 2019 03:18:06 +0000 (05:18 +0200)
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/MachineContext.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextEvent.java [new file with mode: 0644]
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextListener.java [new file with mode: 0644]
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectMachineContext.java

index c65a95e..2e02e31 100644 (file)
@@ -13,6 +13,7 @@ import org.eclipse.ui.preferences.ScopedPreferenceStore;
 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
 {
@@ -113,6 +114,7 @@ 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)
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextEvent.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextEvent.java
new file mode 100644 (file)
index 0000000..059b3ea
--- /dev/null
@@ -0,0 +1,53 @@
+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
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextListener.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextListener.java
new file mode 100644 (file)
index 0000000..f7366bc
--- /dev/null
@@ -0,0 +1,7 @@
+package net.mograsim.plugin.nature;
+
+@FunctionalInterface
+public interface ProjectContextListener
+{
+       void onProjectContextChange(ProjectContextEvent projectContextEvent);
+}
\ No newline at end of file
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));
+       }
 }