Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / InstructionView.java
index 5867656..b14e42d 100644 (file)
@@ -5,6 +5,7 @@ import java.io.IOException;
 import java.util.Arrays;
 import java.util.Optional;
 
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
 import org.eclipse.jface.viewers.EditingSupport;
 import org.eclipse.jface.viewers.TableViewerColumn;
@@ -15,9 +16,14 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableColumn;
-import org.eclipse.ui.part.ViewPart;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorSite;
+import org.eclipse.ui.IPathEditorInput;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.part.EditorPart;
 
 import net.mograsim.machine.Machine;
+import net.mograsim.machine.MemoryObserver;
 import net.mograsim.machine.mi.MicroInstructionDefinition;
 import net.mograsim.machine.mi.MicroInstructionMemory;
 import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
@@ -33,7 +39,7 @@ import net.mograsim.plugin.tables.RadixSelector;
 import net.mograsim.plugin.util.DropDownMenu;
 import net.mograsim.plugin.util.DropDownMenu.DropDownEntry;
 
-public class InstructionView extends ViewPart implements ContextObserver
+public class InstructionView extends EditorPart implements ContextObserver, MemoryObserver
 {
        private String saveLoc = null;
        private LazyTableViewer viewer;
@@ -43,6 +49,7 @@ public class InstructionView extends ViewPart implements ContextObserver
        private DisplaySettings displaySettings;
        private InstructionTableContentProvider provider;
        private int highlighted = 0;
+       private boolean dirty = false;
 
        @SuppressWarnings("unused")
        @Override
@@ -122,6 +129,7 @@ public class InstructionView extends ViewPart implements ContextObserver
                this.memory = memory;
                viewer.setInput(memory);
                this.miDef = memory.getDefinition().getMicroInstructionDefinition();
+               this.memory.registerObserver(this);
                createColumns();
        }
 
@@ -250,17 +258,15 @@ public class InstructionView extends ViewPart implements ContextObserver
                if (memory == null)
                {
                        System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
+                       return;
                }
-               if (saveLoc != null)
+               try
                {
-                       try
-                       {
-                               MicroInstructionMemoryParser.write(memory, file);
-                       }
-                       catch (IOException e)
-                       {
-                               e.printStackTrace();
-                       }
+                       MicroInstructionMemoryParser.write(memory, file);
+               }
+               catch (IOException e)
+               {
+                       e.printStackTrace();
                }
        }
 
@@ -279,4 +285,54 @@ public class InstructionView extends ViewPart implements ContextObserver
                        bindMicroInstructionMemory(actualMachine.getMicroInstructionMemory());
                }
        }
+
+       @Override
+       public void doSave(IProgressMonitor progressMonitor)
+       {
+               IEditorInput input = getEditorInput();
+               if (input instanceof IPathEditorInput)
+               {
+                       IPathEditorInput pathInput = (IPathEditorInput) input;
+                       save(pathInput.getPath().toOSString());
+                       dirty = false;
+                       firePropertyChange(PROP_DIRTY);
+               }
+       }
+
+       @Override
+       public void doSaveAs()
+       {
+               // not allowed
+       }
+
+       @Override
+       public void init(IEditorSite site, IEditorInput input) throws PartInitException
+       {
+               setSite(site);
+               setInput(input);
+               if (input instanceof IPathEditorInput)
+               {
+                       IPathEditorInput pathInput = (IPathEditorInput) input;
+                       open(pathInput.getPath().toOSString());
+               }
+       }
+
+       @Override
+       public boolean isDirty()
+       {
+               return dirty;
+       }
+
+       @Override
+       public boolean isSaveAsAllowed()
+       {
+               return false;
+       }
+
+       @Override
+       public void update(long address)
+       {
+               dirty = true;
+               firePropertyChange(PROP_DIRTY);
+       }
 }