Moved ResourceChangeListener to DebugTarget; Listener now deregisters
authorFabian Stemmler <stemmler@in.tum.de>
Sat, 5 Oct 2019 22:43:31 +0000 (00:43 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Sat, 5 Oct 2019 22:43:31 +0000 (00:43 +0200)
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineDebugTarget.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineLaunchConfigType.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/tables/mi/InstructionView.java

index ad0441b..ca74804 100644 (file)
@@ -1,10 +1,22 @@
 package net.mograsim.plugin.launch;
 
+import static org.eclipse.core.resources.IResourceDelta.CHANGED;
+
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IMarkerDelta;
+import org.eclipse.core.resources.IResourceChangeEvent;
+import org.eclipse.core.resources.IResourceChangeListener;
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.PlatformObject;
 import org.eclipse.core.runtime.Status;
@@ -22,12 +34,15 @@ import org.eclipse.debug.core.model.IMemoryBlockRetrievalExtension;
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.debug.core.model.IStepFilters;
 import org.eclipse.debug.core.model.IThread;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.ui.PlatformUI;
 
 import net.mograsim.logic.model.LogicExecuter;
 import net.mograsim.machine.Machine;
 import net.mograsim.machine.MachineDefinition;
+import net.mograsim.machine.mi.MicroInstructionMemoryParser;
+import net.mograsim.machine.standard.memory.MainMemoryParser;
 import net.mograsim.plugin.MograsimActivator;
-import net.mograsim.plugin.launch.MachineLaunchConfigType.MachineLaunchParams;
 
 public class MachineDebugTarget extends PlatformObject implements IDebugTarget, IMemoryBlockRetrievalExtension
 {
@@ -37,21 +52,28 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
        private final Machine machine;
        private final LogicExecuter exec;
        private final MachineThread thread;
+       private final IFile mpmFile;
+       private final Optional<IFile> memFile;
 
        private boolean running;
 
        private final List<Consumer<Double>> executionSpeedListeners;
 
-       private final MachineLaunchParams launchParams;
+       private final IResourceChangeListener resChangedListener;
 
-       public MachineDebugTarget(ILaunch launch, MachineLaunchParams launchParams, MachineDefinition machineDefinition)
+       public MachineDebugTarget(ILaunch launch, IFile mpmFile, Optional<IFile> memFile, MachineDefinition machineDefinition)
+                       throws CoreException
        {
                this.launch = launch;
                this.machine = machineDefinition.createNew();
                this.exec = new LogicExecuter(machine.getTimeline());
 
                this.executionSpeedListeners = new ArrayList<>();
-               this.launchParams = launchParams;
+               this.mpmFile = mpmFile;
+               this.memFile = memFile;
+
+               assignMicroInstructionMemory();
+               assignMainMemory();
 
                exec.startLiveExecution();
                running = true;
@@ -59,6 +81,9 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
                getLaunch().addDebugTarget(this);
                fireCreationEvent();
 
+               this.resChangedListener = this::resourceChanged;
+               ResourcesPlugin.getWorkspace().addResourceChangeListener(resChangedListener, IResourceChangeEvent.POST_CHANGE);
+
                // create after creating ourself
                this.thread = USE_PSEUDO_THREAD ? new MachineThread(this) : null;
        }
@@ -92,11 +117,6 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
                return launch;
        }
 
-       public MachineLaunchParams getLaunchParams()
-       {
-               return launchParams;
-       }
-
        public double getExecutionSpeed()
        {
                return exec.getSpeedFactor();
@@ -171,6 +191,7 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
                if (isTerminated())
                        return;
 
+               ResourcesPlugin.getWorkspace().removeResourceChangeListener(resChangedListener);
                exec.stopLiveExecution();
                running = false;
                fireTerminateEvent();
@@ -347,4 +368,75 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
                throw new DebugException(
                                new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED, message, null));
        }
+
+       private void resourceChanged(IResourceChangeEvent event)
+       {
+               IResourceDelta mpmDelta;
+               if (event.getType() == IResourceChangeEvent.POST_CHANGE && (mpmDelta = event.getDelta().findMember(mpmFile.getFullPath())) != null
+                               && (mpmDelta.getKind() & CHANGED) == CHANGED && mpmFile.exists())
+               {
+                       AtomicBoolean doHotReplace = new AtomicBoolean();
+                       PlatformUI.getWorkbench().getDisplay().syncExec(() ->
+                       {
+                               if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace MPM?",
+                                               String.format("The MPM %s has been modified on the file system. Replace simulated MPM with modified contents?",
+                                                               mpmFile.getName())))
+                                       doHotReplace.set(true);
+                       });
+                       if (doHotReplace.get())
+                       {
+                               try
+                               {
+                                       assignMicroInstructionMemory();
+                               }
+                               catch (CoreException e)
+                               {
+                                       PlatformUI.getWorkbench().getDisplay()
+                                                       .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+                                                                       "Failed Hot Replace!",
+                                                                       "An error occurred trying to read the modified MPM from the file system: " + e.getMessage()));
+                               }
+                       }
+               }
+       }
+
+       private void assignMicroInstructionMemory() throws CoreException
+       {
+               try (InputStream mpmStream = mpmFile.getContents())
+               {
+                       machine.getMicroInstructionMemory().bind(
+                                       MicroInstructionMemoryParser.parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream));
+               }
+               catch (IOException e)
+               {
+                       throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
+               }
+       }
+
+       private void assignMainMemory() throws CoreException
+       {
+               if (memFile.isPresent())
+               {
+                       try (InputStream initialRAMStream = memFile.get().getContents())
+                       {
+                               machine.getMainMemory()
+                                               .bind(MainMemoryParser.parseMemory(machine.getDefinition().getMainMemoryDefinition(), initialRAMStream));
+                       }
+                       catch (IOException e)
+                       {
+                               throw new CoreException(
+                                               new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
+                       }
+               }
+       }
+
+       public IFile getMPMFile()
+       {
+               return mpmFile;
+       }
+
+       public Optional<IFile> getMEMFile()
+       {
+               return memFile;
+       }
 }
\ No newline at end of file
index ab36508..9e94454 100644 (file)
@@ -1,17 +1,11 @@
 package net.mograsim.plugin.launch;
 
-import static org.eclipse.core.resources.IResourceDelta.CHANGED;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResourceChangeEvent;
-import org.eclipse.core.resources.IResourceChangeListener;
-import org.eclipse.core.resources.IResourceDelta;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -20,15 +14,11 @@ import org.eclipse.core.runtime.Status;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.statushandlers.StatusManager;
 
 import net.mograsim.machine.Machine;
 import net.mograsim.machine.MachineDefinition;
-import net.mograsim.machine.MainMemory;
 import net.mograsim.machine.MainMemoryDefinition;
-import net.mograsim.machine.mi.MicroInstructionMemory;
 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
 import net.mograsim.machine.standard.memory.MainMemoryParser;
@@ -43,23 +33,9 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate
        public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
        public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
 
-       private final IResourceChangeListener resChangedListener;
        private IFile mpmFile;
        private Machine machine;
 
-       public MachineLaunchConfigType()
-       {
-               this.resChangedListener = this::resourceChanged;
-               ResourcesPlugin.getWorkspace().addResourceChangeListener(resChangedListener,
-                               // IResourceChangeEvent.POST_BUILD |
-                               IResourceChangeEvent.POST_CHANGE |
-                               // IResourceChangeEvent.PRE_BUILD |
-                               // IResourceChangeEvent.PRE_CLOSE |
-                               // IResourceChangeEvent.PRE_DELETE |
-                               // IResourceChangeEvent.PRE_REFRESH |
-                                               0);
-       }
-
        @Override
        public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
        {
@@ -134,135 +110,20 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate
 
                MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
                MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
-               MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
 
-               String mpmName;
-               mpmFile = project.getFile(mpmName = configuration.getAttribute(MPM_FILE_ATTR, ""));
+               mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
 
                String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
-               MainMemory mem;
+               Optional<IFile> memFile = Optional.empty();
                if (!"".equals(initialRAMFileName))
                {
-                       IFile initialRAMFile = project.getFile(initialRAMFileName);
-                       try (InputStream initialRAMStream = initialRAMFile.getContents())
-                       {
-                               mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
-                       }
-                       catch (IOException e)
-                       {
-                               throw new CoreException(
-                                               new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
-                       }
-               } else
-                       mem = null;
-
-               MachineLaunchParams params = new MachineLaunchParams(projName, mpmName, initialRAMFileName);
-               MachineDebugTarget debugTarget = new MachineDebugTarget(launch, params, machineDefinition);
+                       memFile = Optional.of(project.getFile(initialRAMFileName));
+               }
+               MachineDebugTarget debugTarget = new MachineDebugTarget(launch, mpmFile, memFile, machineDefinition);
                debugTarget.suspend();
                debugTarget.setExecutionSpeed(1);
                machine = debugTarget.getMachine();
-               assignMicroInstructionMemory();
-               if (mem != null)
-                       machine.getMainMemory().bind(mem);
                machine.reset();
        }
 
-       private void assignMicroInstructionMemory() throws CoreException
-       {
-               try (InputStream mpmStream = mpmFile.getContents())
-               {
-                       MicroInstructionMemory mpm = MicroInstructionMemoryParser
-                                       .parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream);
-                       machine.getMicroInstructionMemory().bind(mpm);
-               }
-               catch (IOException e)
-               {
-                       throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
-               }
-       }
-
-       private void resourceChanged(IResourceChangeEvent event)
-       {
-               // TODO remove Sysout
-               int type = event.getType();
-               String typeStr;
-               switch (type)
-               {
-               case IResourceChangeEvent.POST_BUILD:
-                       typeStr = "POST_BUILD";
-                       break;
-               case IResourceChangeEvent.POST_CHANGE:
-                       typeStr = "POST_CHANGE";
-                       IResourceDelta mpmDelta;
-                       if ((mpmDelta = event.getDelta().findMember(mpmFile.getFullPath())) != null && (mpmDelta.getKind() & CHANGED) == CHANGED
-                                       && mpmFile.exists())
-                       {
-                               AtomicBoolean doHotReplace = new AtomicBoolean();
-                               PlatformUI.getWorkbench().getDisplay().syncExec(() ->
-                               {
-                                       if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace MPM?",
-                                                       String.format("The MPM %s has been modified on the file system. Replace simulated MPM with modified contents?",
-                                                                       mpmFile.getName())))
-                                               doHotReplace.set(true);
-                               });
-                               if (doHotReplace.get())
-                               {
-                                       try
-                                       {
-                                               assignMicroInstructionMemory();
-                                       }
-                                       catch (CoreException e)
-                                       {
-                                               PlatformUI.getWorkbench().getDisplay()
-                                                               .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
-                                                                               "Failed Hot Replace!",
-                                                                               "An error occurred trying to read the modified MPM from the file system: " + e.getMessage()));
-                                       }
-                               }
-                       }
-                       break;
-               case IResourceChangeEvent.PRE_BUILD:
-                       typeStr = "PRE_BUILD";
-                       break;
-               case IResourceChangeEvent.PRE_CLOSE:
-                       typeStr = "PRE_CLOSE";
-                       break;
-               case IResourceChangeEvent.PRE_DELETE:
-                       typeStr = "PRE_DELETE";
-                       break;
-               case IResourceChangeEvent.PRE_REFRESH:
-                       typeStr = "PRE_REFRESH";
-                       break;
-               default:
-                       typeStr = "<unknown: " + type + ">";
-               }
-               System.out.println(typeStr + ": " + event);
-       }
-
-       public static class MachineLaunchParams
-       {
-               public final String projectPath, mpmPath, ramPath;
-
-               MachineLaunchParams(String projectPath, String mpmPath, String ramPath)
-               {
-                       this.projectPath = projectPath;
-                       this.mpmPath = mpmPath;
-                       this.ramPath = ramPath;
-               }
-
-               public String getProjectPath()
-               {
-                       return projectPath;
-               }
-
-               public String getMpmPath()
-               {
-                       return mpmPath;
-               }
-
-               public String getRamPath()
-               {
-                       return ramPath;
-               }
-       }
 }
\ No newline at end of file
index 801082d..1da1cbf 100644 (file)
@@ -5,8 +5,6 @@ import java.io.InputStream;
 import java.util.Optional;
 
 import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.debug.ui.DebugUITools;
@@ -30,7 +28,6 @@ import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
 import net.mograsim.plugin.launch.MachineDebugContextListener;
 import net.mograsim.plugin.launch.MachineDebugTarget;
-import net.mograsim.plugin.launch.MachineLaunchConfigType.MachineLaunchParams;
 import net.mograsim.plugin.nature.MachineContext;
 import net.mograsim.plugin.nature.ProjectMachineContext;
 import net.mograsim.plugin.tables.DisplaySettings;
@@ -68,10 +65,7 @@ public class InstructionView extends EditorPart
 
                        newTarget.ifPresent(target ->
                        {
-                               MachineLaunchParams params = target.getLaunchParams();
-                               IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(params.getProjectPath());
-
-                               if (file.equals(project.getFile(params.getMpmPath())))
+                               if (file.equals(target.getMPMFile()))
                                {
                                        Machine m = target.getMachine();
                                        target.getMachine().addActiveMicroInstructionChangedListener(instChangeListener);