X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Fnature%2FProjectMachineContext.java;h=ecce9e818f68226e1340e0f0bddd7bf02b8d8ed7;hb=b5d55c59d7069171bd928e4a945d9185ee4bc2b0;hp=86b9df6fc6c20351c23348f7eb3f5fa97d8be171;hpb=4c2b7a2100e55b5e4bf59666b9684d5a996bd0fb;p=Mograsim.git 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 86b9df6f..ecce9e81 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 @@ -17,8 +17,19 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.ui.preferences.ScopedPreferenceStore; +import net.mograsim.machine.MachineRegistry; import net.mograsim.plugin.nature.ProjectContextEvent.ProjectContextEventType; +/** + * This class is a register for {@link MachineContext} mapped by their {@link IProject} + *

+ * It can be used to obtain (and thereby create if necessary) {@link MachineContext}s for projects and {@link IAdaptable}s that are somewhat + * associated to Mograsim nature. The register is unique and static context of this class. Since it also depends on the installed machines, + * it listens to changes of the {@link MachineRegistry}. + * + * @author Christian Femers + * + */ public class ProjectMachineContext { private static Map projectMachineContexts = Collections.synchronizedMap(new HashMap<>()); @@ -27,6 +38,20 @@ public class ProjectMachineContext public static final String MOGRASIM_PROJECT_PREFS_NODE = "net.mograsim"; public static final String MACHINE_PROPERTY = "net.mograsim.projectMachineId"; + private ProjectMachineContext() + { + + } + + /** + * This method returns the associated machine context or created a new one if none is associated yet. + * + * @param project the project to get the {@link MachineContext} for (or create one, if possible). It must have Mograsim nature. + * + * @throws IllegalArgumentException if the project is not accessible or has no mograsim nature + * @throws NullPointerException if the project is null + * + */ public static MachineContext getMachineContextOf(IProject project) { MachineContext mc = projectMachineContexts.get(project); @@ -39,6 +64,17 @@ public class ProjectMachineContext return mc; } + /** + * This method returns the associated machine context or created a new one if none is associated yet. The given resource must be + * adaptable to {@link IProject}. + * + * @param mograsimProjectAdapable the {@link IProject}-{@link IAdaptable} to get the {@link MachineContext} for (or create one, if + * possible). Must be contained in a Mograsim nature project. + * + * @throws IllegalArgumentException if the project is not accessible or has no mograsim nature + * @throws NullPointerException if the {@link IAdaptable} is null or it cannot be adapted to {@link IProject} + * + */ public static MachineContext getMachineContextOf(IAdaptable mograsimProjectAdapable) { IProject project = Adapters.adapt(mograsimProjectAdapable, IProject.class, true); @@ -46,6 +82,9 @@ public class ProjectMachineContext return getMachineContextOf(project); } + /** + * Returns all {@link MachineContext} known, in the sense of all that got ever created during this runtime. + */ public static Map getAllProjectMachineContexts() { return Collections.unmodifiableMap(projectMachineContexts); @@ -62,6 +101,7 @@ public class ProjectMachineContext if (mograsimProjectAdapable instanceof IProject) { project = (IProject) mograsimProjectAdapable; + Objects.requireNonNull(project, "Project was null"); } else { project = Adapters.adapt(mograsimProjectAdapable, IProject.class, true); @@ -80,6 +120,9 @@ public class ProjectMachineContext return project; } + /** + * Tests for Mograsim nature. This method is null safe and will not throw any exception. + */ static boolean hasMograsimNature(IProject project) { if (project == null) @@ -120,20 +163,29 @@ public class ProjectMachineContext static { ResourcesPlugin.getWorkspace().addResourceChangeListener(ProjectMachineContext::resourceChanged); + MachineRegistry.addMachineRegistryListener(newMap -> updateAllStatus()); + } + + private static void updateAllStatus() + { + projectMachineContexts.forEach((p, mc) -> mc.updateStatus()); } private static void resourceChanged(IResourceChangeEvent event) { -// System.out.println(((ResourceChangeEvent) event).toDebugString()); - ProjectContextEventType eventType = ProjectContextEventType.ofResourceChangeEvent(event.getType()); - if (eventType == null) + // We try to do as many cheap tests first as possible, because this listener is not limited to plain project actions. + if (event.getResource() == null) return; - if (event.getResource() == null || event.getResource().getProject() == null) + IProject project = event.getResource().getProject(); + if (project == null) return; - MachineContext mc = projectMachineContexts.get(event.getResource().getProject()); + MachineContext mc = projectMachineContexts.get(project); if (mc == null) return; -// System.out.println(" " + eventType + " - " + mc.getProject()); + ProjectContextEventType eventType = ProjectContextEventType.ofResourceChangeEvent(event.getType()); +// if (eventType == ProjectContextEventType.OTHER_CHANGE && project.isOpen()) +// return; // we don't care about all small changes (TODO: research if this has any drawbacks) + eventType.getForcedStatus().ifPresent(mc::forceUpdateStatus); notifyListeners(new ProjectContextEvent(mc, eventType)); } }