Made simulation view / LogicUIPart to an Editor
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 24 Sep 2019 14:51:00 +0000 (16:51 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 24 Sep 2019 14:51:00 +0000 (16:51 +0200)
plugins/net.mograsim.plugin.core/META-INF/MANIFEST.MF
plugins/net.mograsim.plugin.core/plugin.xml
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MograsimActivator.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java [new file with mode: 0644]
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java [deleted file]

index 54f99cd..30d191e 100644 (file)
@@ -7,13 +7,15 @@ Export-Package: net.mograsim.plugin;uses:="org.eclipse.ui.themes,org.eclipse.swt
  net.mograsim.plugin.asm,
  net.mograsim.plugin.asm.editor,
  net.mograsim.plugin.asm.editor.rules,
+ net.mograsim.plugin.editors,
  net.mograsim.plugin.nature,
  net.mograsim.plugin.nature.properties,
  net.mograsim.plugin.tables,
  net.mograsim.plugin.tables.memory,
  net.mograsim.plugin.tables.mi,
  net.mograsim.plugin.util,
- net.mograsim.plugin.views
+ net.mograsim.plugin.wizards.newWizards,
+ org.eclipse.jface.snippets.viewers
 Require-Bundle: org.eclipse.core.runtime,
  org.eclipse.ui,
  org.eclipse.jface.text,
index 76a2161..965c260 100644 (file)
             name="%content-type.mpm.name"
             priority="high">
       </content-type>
+      <content-type
+            base-type="org.eclipse.core.runtime.text"
+            default-charset="UTF-8"
+            file-extensions="regs"
+            id="net.mograsim.plugin.regs"
+            name="Registers / Latches state"
+            priority="high">
+      </content-type>
    </extension>
    <extension
          point="org.eclipse.ui.menus">
                id="net.mograsim.plugin.tables.mi.InstructionView">
                <contentTypeBinding contentTypeId="net.mograsim.plugin.mpm"/>
       </editor>
+      <editor
+            class="net.mograsim.plugin.editors.SimulationViewEditor"
+            icon="icons/mograsim/blue-orange/icon_blue-orange_16.png"
+            id="net.mograsim.plugin.editors.simulationvieweditor"
+            name="Simulation View Editor">
+               <contentTypeBinding contentTypeId="net.mograsim.plugin.regs"/>
+      </editor>
    </extension>
    <extension
          point="org.eclipse.ui.genericeditor.presentationReconcilers">
             inject="true"
             name="%view.name">
       </view>
-      <view
-            allowMultiple="true"
-            category="net.mograsim.plugin"
-            class="net.mograsim.plugin.views.LogicUIPart"
-            icon="icons/mograsim/blue-orange/icon_blue-orange_16.png"
-            id="net.mograsim.plugin.core.simulationView"
-            inject="true"
-            name="%view.name.0"
-            restorable="true">
-      </view>
       <view
             category="net.mograsim.plugin"
                id="net.mograsim.plugin.tables.memory.MemoryView"
                        <selection class="org.eclipse.core.resources.IResource"/>
                </wizard>
        </extension>
-</plugin>
\ No newline at end of file
+</plugin>
index b3340da..9311ddd 100644 (file)
@@ -1,8 +1,10 @@
 package net.mograsim.plugin;
 
+import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
 
 import net.mograsim.machine.MachineRegistry;
+import net.mograsim.preferences.Preferences;
 
 public final class MograsimActivator extends AbstractUIPlugin
 {
@@ -14,6 +16,8 @@ public final class MograsimActivator extends AbstractUIPlugin
                        throw new IllegalStateException("MograsimActivator already created!");
                instance = this;
                MachineRegistry.initialize();
+               Preferences.setPreferences(new EclipsePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(),
+                               MograsimActivator.instance().getPreferenceStore()));
        }
 
        public static MograsimActivator instance()
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java
new file mode 100644 (file)
index 0000000..e6d05ea
--- /dev/null
@@ -0,0 +1,236 @@
+package net.mograsim.plugin.editors;
+
+import java.io.ByteArrayInputStream;
+import java.util.Optional;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.util.SafeRunnable;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.MouseTrackListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Slider;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorSite;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.part.EditorPart;
+
+import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
+import net.mograsim.logic.model.LogicExecuter;
+import net.mograsim.logic.model.LogicUICanvas;
+import net.mograsim.machine.Machine;
+import net.mograsim.plugin.nature.MachineContext;
+import net.mograsim.plugin.nature.ProjectMachineContext;
+
+//TODO what if we open multiple editors?
+//TODO actually save / load register and latch states
+public class SimulationViewEditor extends EditorPart
+{
+       private MachineContext context;
+
+       private LogicExecuter exec;
+
+       private Composite parent;
+       private LogicUICanvas canvas;
+       private Label noMachineLabel;
+
+       @Override
+       public void createPartControl(Composite parent)
+       {
+               this.parent = parent;
+               // initialize UI
+               parent.setLayout(new GridLayout());
+
+               noMachineLabel = new Label(parent, SWT.NONE);
+               noMachineLabel.setText("No machine present...");// TODO internationalize?
+               addSimulationControlWidgets(parent);
+               recreateContextDependentControls();
+       }
+
+       private void recreateContextDependentControls()
+       {
+               if (parent == null)
+                       // createPartControls has not been called yet
+                       return;
+
+               if (canvas != null)
+                       canvas.dispose();
+               if (exec != null)
+                       exec.stopLiveExecution();
+
+               Optional<Machine> machineOptional;
+               if (context != null && (machineOptional = context.getActiveMachine()).isPresent())
+               {
+                       noMachineLabel.setVisible(false);
+                       Machine machine = machineOptional.get();
+                       canvas = new LogicUICanvas(parent, SWT.NONE, machine.getModel());
+                       ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
+                       userInput.buttonDrag = 3;
+                       userInput.buttonZoom = 2;
+                       userInput.enableUserInput();
+
+                       GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
+                       canvas.setLayoutData(uiData);
+
+                       // initialize executer
+                       exec = new LogicExecuter(machine.getTimeline());
+                       exec.startLiveExecution();
+               } else
+                       noMachineLabel.setVisible(true);
+       }
+
+       private void addSimulationControlWidgets(Composite parent)
+       {
+               Composite c = new Composite(parent, SWT.NONE);
+               c.setLayout(new GridLayout(6, false));
+
+               Button pauseButton = new Button(c, SWT.TOGGLE);
+               pauseButton.setSelection(true);
+               setPauseText(pauseButton, false);
+
+               pauseButton.addListener(SWT.Selection, e ->
+               {
+                       setPauseText(pauseButton, false);
+                       if (pauseButton.getSelection())
+                       {
+                               exec.unpauseLiveExecution();
+                       } else
+                       {
+                               exec.pauseLiveExecution();
+                       }
+               });
+               pauseButton.addMouseTrackListener(new MouseTrackListener()
+               {
+                       @Override
+                       public void mouseHover(MouseEvent e)
+                       {
+                               // nothing
+                       }
+
+                       @Override
+                       public void mouseExit(MouseEvent e)
+                       {
+                               setPauseText(pauseButton, false);
+                       }
+
+                       @Override
+                       public void mouseEnter(MouseEvent e)
+                       {
+                               setPauseText(pauseButton, true);
+                       }
+               });
+
+               Label speedLabel = new Label(c, SWT.NONE);
+               speedLabel.setText("Simulation Speed: ");
+
+               Slider slider = new Slider(c, SWT.NONE);
+               slider.setMinimum(1);
+               slider.setMaximum(100 + slider.getThumb());
+               slider.setIncrement(1);
+
+               Label speedPercentageLabel = new Label(c, SWT.NONE);
+               speedPercentageLabel.setText("100%");
+
+               slider.addListener(SWT.Selection, e ->
+               {
+                       int selection = slider.getSelection();
+                       speedPercentageLabel.setText(selection + "%");
+
+                       exec.setSpeedPercentage(slider.getSelection());
+               });
+               slider.setSelection(100);
+
+               c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
+               c.pack();
+               c.setVisible(true);
+       }
+
+       private static void setPauseText(Button pauseButton, boolean hovered)
+       {
+               if (hovered)
+                       if (pauseButton.getSelection())
+                               pauseButton.setText("Pause?");
+                       else
+                               pauseButton.setText("Resume?");
+               else if (pauseButton.getSelection())
+                       pauseButton.setText("Running");
+               else
+                       pauseButton.setText("Paused");
+       }
+
+       @Override
+       public void init(IEditorSite site, IEditorInput input) throws PartInitException
+       {
+               if (input instanceof IFileEditorInput)
+               {
+                       IFileEditorInput fileInput = (IFileEditorInput) input;
+                       context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
+                       recreateContextDependentControls();
+
+                       setPartName(fileInput.getName());
+                       open(fileInput.getFile());
+               } else
+                       throw new IllegalArgumentException("SimulationViewEditor can only be used with Files");
+
+               setSite(site);
+               setInput(input);
+       }
+
+       @Override
+       public void doSave(IProgressMonitor monitor)
+       {
+               IEditorInput input = getEditorInput();
+               if (input instanceof IFileEditorInput)
+                       SafeRunnable.getRunner().run(() -> save(((IFileEditorInput) input).getFile(), monitor));
+               else
+                       throw new IllegalArgumentException("SimulationViewEditor can only be used with Files");
+       }
+
+       private void save(IFile file, IProgressMonitor monitor) throws CoreException
+       {
+               file.setContents(new ByteArrayInputStream("actual contents will go here".getBytes()), 0, monitor);
+       }
+
+       private void open(IFile file)
+       {
+               // do nothing yet
+       }
+
+       @Override
+       public void doSaveAs()
+       {
+               throw new UnsupportedOperationException();
+       }
+
+       @Override
+       public boolean isDirty()
+       {
+               return false;
+       }
+
+       @Override
+       public boolean isSaveAsAllowed()
+       {
+               return false;
+       }
+
+       @Override
+       public void setFocus()
+       {
+               canvas.setFocus();
+       }
+
+       @Override
+       public void dispose()
+       {
+               exec.stopLiveExecution();
+               super.dispose();
+       }
+}
\ No newline at end of file
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java
deleted file mode 100644 (file)
index cf883d2..0000000
+++ /dev/null
@@ -1,190 +0,0 @@
-package net.mograsim.plugin.views;
-
-import javax.inject.Inject;
-
-import org.eclipse.e4.ui.model.application.ui.basic.MPart;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.MouseEvent;
-import org.eclipse.swt.events.MouseTrackListener;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Slider;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.part.ViewPart;
-
-import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
-import net.mograsim.logic.model.LogicExecuter;
-import net.mograsim.logic.model.LogicUICanvas;
-import net.mograsim.machine.Machine;
-import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
-import net.mograsim.plugin.EclipsePreferences;
-import net.mograsim.plugin.MachineContext;
-import net.mograsim.plugin.MograsimActivator;
-import net.mograsim.plugin.nature.MachineContextSwtTools;
-import net.mograsim.plugin.nature.MachineContextSwtTools.MachineCombo;
-import net.mograsim.plugin.nature.MachineContextSwtTools.MograsimProjectCombo;
-import net.mograsim.plugin.tables.DisplaySettings;
-import net.mograsim.plugin.tables.mi.ActiveInstructionPreviewContentProvider;
-import net.mograsim.plugin.tables.mi.InstructionTable;
-import net.mograsim.preferences.Preferences;
-
-public class LogicUIPart extends ViewPart
-{
-       @Inject
-       private MPart part;
-
-       private LogicExecuter exec;
-       private LogicUICanvas ui;
-
-       @Override
-       public void dispose()
-       {
-               if (exec != null)
-                       exec.stopLiveExecution();
-               super.dispose();
-       }
-
-       @Override
-       public void createPartControl(Composite parent)
-       {
-               // set preferences
-               Preferences.setPreferences(new EclipsePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(),
-                               MograsimActivator.instance().getPreferenceStore()));
-
-               Machine m = MachineContext.getInstance().getMachine();
-
-               // initialize UI
-               GridLayout layout = new GridLayout(1, true);
-               parent.setLayout(layout);
-
-               addSimulationControlWidgets(parent);
-
-               ui = new LogicUICanvas(parent, SWT.NONE, m.getModel());
-               ui.addTransformListener((x, y, z) -> part.setDirty(z < 1));
-               ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
-               userInput.buttonDrag = 3;
-               userInput.buttonZoom = 2;
-               userInput.enableUserInput();
-
-               GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
-               ui.setLayoutData(uiData);
-
-               // initialize Instruction preview
-               InstructionTable instPreview = new InstructionTable(parent, new DisplaySettings());
-               instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer()));
-               AssignableMicroInstructionMemory mIMemory = m.getMicroInstructionMemory();
-               instPreview.bindMicroInstructionMemory(mIMemory);
-               mIMemory.registerCellModifiedListener(a -> instPreview.refresh());
-               mIMemory.registerMemoryReassignedListener(n -> instPreview.refresh());
-
-               GridData previewData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
-               instPreview.getTableViewer().getTable().setLayoutData(previewData);
-
-               // initialize executer
-               exec = new LogicExecuter(m.getTimeline());
-
-               // run it
-               exec.startLiveExecution();
-       }
-
-       private void addSimulationControlWidgets(Composite parent)
-       {
-               Composite c = new Composite(parent, SWT.NONE);
-               c.setLayout(new GridLayout(6, false));
-
-               MograsimProjectCombo projectCombo = MachineContextSwtTools.createMograsimProjectSelector(c, SWT.NONE);
-               MachineCombo machineCombo = MachineContextSwtTools.createMachineSelector(c, SWT.NONE);
-
-               Button pauseButton = new Button(c, SWT.TOGGLE);
-               pauseButton.setSelection(true);
-               setPauseText(pauseButton, false);
-
-               pauseButton.addListener(SWT.Selection, e ->
-               {
-                       setPauseText(pauseButton, false);
-                       if (pauseButton.getSelection())
-                       {
-                               exec.unpauseLiveExecution();
-                       } else
-                       {
-                               exec.pauseLiveExecution();
-                       }
-               });
-               pauseButton.addMouseTrackListener(new MouseTrackListener()
-               {
-                       @Override
-                       public void mouseHover(MouseEvent e)
-                       {
-                               // nothing
-                       }
-
-                       @Override
-                       public void mouseExit(MouseEvent e)
-                       {
-                               setPauseText(pauseButton, false);
-                       }
-
-                       @Override
-                       public void mouseEnter(MouseEvent e)
-                       {
-                               setPauseText(pauseButton, true);
-                       }
-               });
-
-               Label speedLabel = new Label(c, SWT.NONE);
-               speedLabel.setText("Simulation Speed: ");
-
-               Slider slider = new Slider(c, SWT.NONE);
-               slider.setMinimum(1);
-               slider.setMaximum(100 + slider.getThumb());
-               slider.setIncrement(1);
-
-               Label speedPercentageLabel = new Label(c, SWT.NONE);
-               speedPercentageLabel.setText("100%");
-
-               slider.addListener(SWT.Selection, e ->
-               {
-                       int selection = slider.getSelection();
-                       speedPercentageLabel.setText(selection + "%");
-
-                       exec.setSpeedPercentage(slider.getSelection());
-               });
-               slider.setSelection(100);
-
-               c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
-               c.pack();
-               c.setVisible(true);
-       }
-
-       private void setPauseText(Button pauseButton, boolean hovered)
-       {
-               if (hovered)
-               {
-                       if (pauseButton.getSelection())
-                       {
-                               pauseButton.setText("Pause?");
-                       } else
-                       {
-                               pauseButton.setText("Resume?");
-                       }
-               } else
-               {
-                       if (pauseButton.getSelection())
-                       {
-                               pauseButton.setText("Running");
-                       } else
-                       {
-                               pauseButton.setText("Paused");
-                       }
-               }
-       }
-
-       @Override
-       public void setFocus()
-       {
-               ui.setFocus();
-       }
-}
\ No newline at end of file