From: Christian Femers Date: Tue, 24 Sep 2019 03:18:06 +0000 (+0200) Subject: Added listener and event structure to ProjectMachineContexts X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=15020b7263b6edd100d4b0a5de317e9eaff05c27;p=Mograsim.git Added listener and event structure to ProjectMachineContexts --- 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 index c65a95e1..2e02e31d 100644 --- 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 @@ -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 index 00000000..059b3eae --- /dev/null +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextEvent.java @@ -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 index 00000000..f7366bc3 --- /dev/null +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectContextListener.java @@ -0,0 +1,7 @@ +package net.mograsim.plugin.nature; + +@FunctionalInterface +public interface ProjectContextListener +{ + void onProjectContextChange(ProjectContextEvent projectContextEvent); +} \ No newline at end of file diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectMachineContext.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectMachineContext.java index 0e6185c4..cf727af2 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectMachineContext.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/nature/ProjectMachineContext.java @@ -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 projectMachineContexts = Collections.synchronizedMap(new HashMap<>()); + private static final Set 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 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)); + } }