1 package net.mograsim.plugin.editors;
3 import java.io.ByteArrayInputStream;
4 import java.util.Optional;
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.jface.util.SafeRunnable;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.MouseEvent;
12 import org.eclipse.swt.events.MouseTrackListener;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Slider;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorSite;
22 import org.eclipse.ui.IFileEditorInput;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.part.EditorPart;
26 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
27 import net.mograsim.logic.core.LogicObserver;
28 import net.mograsim.logic.core.components.CoreClock;
29 import net.mograsim.logic.model.LogicExecuter;
30 import net.mograsim.logic.model.LogicUICanvas;
31 import net.mograsim.machine.Machine;
32 import net.mograsim.plugin.nature.MachineContext;
33 import net.mograsim.plugin.nature.ProjectMachineContext;
35 //TODO what if we open multiple editors?
36 //TODO actually save / load register and latch states
37 public class SimulationViewEditor extends EditorPart
39 private MachineContext context;
41 private LogicExecuter exec;
42 private Machine machine;
44 private Composite parent;
45 private Button sbseButton;
46 private Button pauseButton;
47 private Slider simSpeedSlider;
48 private LogicUICanvas canvas;
49 private Label noMachineLabel;
52 public void createPartControl(Composite parent)
56 parent.setLayout(new GridLayout());
58 noMachineLabel = new Label(parent, SWT.NONE);
59 noMachineLabel.setText("No machine present...");// TODO internationalize?
60 addSimulationControlWidgets(parent);
61 recreateContextDependentControls();
64 private void recreateContextDependentControls()
67 // createPartControls has not been called yet
73 exec.stopLiveExecution();
75 Optional<Machine> machineOptional;
76 if (context != null && (machineOptional = context.getActiveMachine()).isPresent())
78 noMachineLabel.setVisible(false);
79 sbseButton.setEnabled(true);
80 pauseButton.setEnabled(true);
81 simSpeedSlider.setEnabled(true);
82 machine = machineOptional.get();
83 canvas = new LogicUICanvas(parent, SWT.NONE, machine.getModel());
84 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
85 userInput.buttonDrag = 3;
86 userInput.buttonZoom = 2;
87 userInput.enableUserInput();
89 GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
90 canvas.setLayoutData(uiData);
92 // initialize executer
93 exec = new LogicExecuter(machine.getTimeline());
94 exec.startLiveExecution();
97 noMachineLabel.setVisible(true);
98 sbseButton.setEnabled(false);
99 pauseButton.setEnabled(false);
100 simSpeedSlider.setEnabled(false);
104 private void addSimulationControlWidgets(Composite parent)
106 Composite c = new Composite(parent, SWT.NONE);
107 c.setLayout(new GridLayout(7, false));
109 sbseButton = new Button(c, SWT.CHECK);
110 pauseButton = new Button(c, SWT.TOGGLE);
111 LogicObserver clockObserver = o ->
113 if (((CoreClock) o).isOn())
115 exec.pauseLiveExecution();
116 Display.getDefault().asyncExec(() ->
118 pauseButton.setSelection(false);
119 setPauseText(pauseButton, false);
124 sbseButton.setText("Step by step execution");
125 sbseButton.addListener(SWT.Selection, e ->
127 CoreClock cl = machine.getClock();
128 if (sbseButton.getSelection())
129 cl.registerObserver(clockObserver);
131 cl.deregisterObserver(clockObserver);
133 sbseButton.setSelection(false);
135 pauseButton.setSelection(true);
136 setPauseText(pauseButton, false);
138 pauseButton.addListener(SWT.Selection, e ->
140 setPauseText(pauseButton, false);
141 if (pauseButton.getSelection())
143 exec.unpauseLiveExecution();
146 exec.pauseLiveExecution();
149 pauseButton.addMouseTrackListener(new MouseTrackListener()
152 public void mouseHover(MouseEvent e)
158 public void mouseExit(MouseEvent e)
160 setPauseText(pauseButton, false);
164 public void mouseEnter(MouseEvent e)
166 setPauseText(pauseButton, true);
170 Label speedLabel = new Label(c, SWT.NONE);
171 speedLabel.setText("Simulation Speed: ");
173 simSpeedSlider = new Slider(c, SWT.NONE);
174 simSpeedSlider.setMinimum(1);
175 simSpeedSlider.setMaximum(100 + simSpeedSlider.getThumb());
176 simSpeedSlider.setIncrement(1);
178 Label speedPercentageLabel = new Label(c, SWT.NONE);
179 speedPercentageLabel.setText("100%");
181 simSpeedSlider.addListener(SWT.Selection, e ->
183 int selection = simSpeedSlider.getSelection();
184 speedPercentageLabel.setText(selection + "%");
186 exec.setSpeedPercentage(simSpeedSlider.getSelection());
188 simSpeedSlider.setSelection(100);
190 c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
194 private static void setPauseText(Button pauseButton, boolean hovered)
197 if (pauseButton.getSelection())
198 pauseButton.setText("Pause?");
200 pauseButton.setText("Resume?");
201 else if (pauseButton.getSelection())
202 pauseButton.setText("Running");
204 pauseButton.setText("Paused");
208 public void init(IEditorSite site, IEditorInput input) throws PartInitException
210 if (input instanceof IFileEditorInput)
212 IFileEditorInput fileInput = (IFileEditorInput) input;
213 context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
214 recreateContextDependentControls();
216 setPartName(fileInput.getName());
217 open(fileInput.getFile());
219 throw new IllegalArgumentException("SimulationViewEditor can only be used with Files");
226 public void doSave(IProgressMonitor monitor)
228 IEditorInput input = getEditorInput();
229 if (input instanceof IFileEditorInput)
230 SafeRunnable.getRunner().run(() -> save(((IFileEditorInput) input).getFile(), monitor));
232 throw new IllegalArgumentException("SimulationViewEditor can only be used with Files");
235 private void save(IFile file, IProgressMonitor monitor) throws CoreException
237 file.setContents(new ByteArrayInputStream("actual contents will go here".getBytes()), 0, monitor);
240 private void open(IFile file)
246 public void doSaveAs()
248 throw new UnsupportedOperationException();
252 public boolean isDirty()
258 public boolean isSaveAsAllowed()
264 public void setFocus()
270 public void dispose()
272 exec.stopLiveExecution();