Added active instruction preview to LogicUIPart
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / InstructionView.java
1 package net.mograsim.plugin.tables.mi;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.swt.widgets.FileDialog;
16 import org.eclipse.ui.IEditorInput;
17 import org.eclipse.ui.IEditorSite;
18 import org.eclipse.ui.IPathEditorInput;
19 import org.eclipse.ui.PartInitException;
20 import org.eclipse.ui.part.EditorPart;
21
22 import net.mograsim.machine.Memory.MemoryCellModifiedListener;
23 import net.mograsim.machine.mi.MicroInstructionMemory;
24 import net.mograsim.machine.mi.MicroInstructionMemory.ActiveMicroInstructionChangedListener;
25 import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
26 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
27 import net.mograsim.plugin.MachineContext;
28 import net.mograsim.plugin.tables.DisplaySettings;
29 import net.mograsim.plugin.tables.LazyTableViewer;
30 import net.mograsim.plugin.tables.RadixSelector;
31
32 public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener
33 {
34         private InstructionTableContentProvider provider;
35         private int highlighted = 0;
36         private boolean dirty = false;
37         private String machineName;
38         private MicroInstructionMemory memory;
39         private InstructionTable table;
40
41         @SuppressWarnings("unused")
42         @Override
43         public void createPartControl(Composite parent)
44         {
45                 provider = new InstructionTableLazyContentProvider();
46                 GridLayout layout = new GridLayout(3, false);
47                 parent.setLayout(layout);
48
49                 DisplaySettings displaySettings = new DisplaySettings();
50                 new RadixSelector(parent, displaySettings);
51
52                 addActivationButton(parent);
53
54                 table = new InstructionTable(parent, displaySettings);
55                 table.setContentProvider(provider);
56                 table.bindMicroInstructionMemory(memory);
57
58                 GridData viewerData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
59                 viewerData.horizontalSpan = 3;
60                 table.getTableViewer().getTable().setLayoutData(viewerData);
61         }
62
63         public void highlight(int index)
64         {
65                 Display.getDefault().asyncExec(() ->
66                 {
67                         LazyTableViewer viewer = table.getTableViewer();
68                         viewer.highlightRow(highlighted, false);
69                         highlighted = index;
70                         viewer.highlightRow(index, true);
71                         viewer.getTable().setTopIndex(index);
72                 });
73         }
74
75         private void addActivationButton(Composite parent)
76         {
77                 Button activationButton = new Button(parent, SWT.PUSH);
78                 activationButton.setText("Set Active");
79                 activationButton.addListener(SWT.Selection,
80                                 e -> MachineContext.getInstance().getMachine().getMicroInstructionMemory().bind(memory));
81         }
82
83         public void bindMicroInstructionMemory(MicroInstructionMemory memory)
84         {
85                 this.memory = memory;
86                 this.memory.registerCellModifiedListener(this);
87                 this.memory.registerActiveMicroInstructionChangedListener(this);
88                 if (table != null)
89                         table.bindMicroInstructionMemory(memory);
90         }
91
92         private void open(String file)
93         {
94                 try (BufferedReader bf = new BufferedReader(new FileReader(file)))
95                 {
96                         machineName = bf.readLine();
97                         bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(machineName, bf));
98                 }
99                 catch (IOException | MicroInstructionMemoryParseException e)
100                 {
101                         e.printStackTrace();
102                 }
103         }
104
105         private void save(String file)
106         {
107                 if (memory == null)
108                 {
109                         System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
110                         return;
111                 }
112                 try
113                 {
114                         MicroInstructionMemoryParser.write(memory, machineName, file);
115                 }
116                 catch (IOException e)
117                 {
118                         e.printStackTrace();
119                 }
120         }
121
122         @Override
123         public void setFocus()
124         {
125                 table.getTableViewer().getControl().setFocus();
126         }
127
128         @Override
129         public void doSave(IProgressMonitor progressMonitor)
130         {
131                 IEditorInput input = getEditorInput();
132                 if (input instanceof IPathEditorInput)
133                 {
134                         IPathEditorInput pathInput = (IPathEditorInput) input;
135                         save(pathInput.getPath().toOSString());
136                         setDirty(false);
137                 }
138         }
139
140         @Override
141         public void doSaveAs()
142         {
143                 openSaveAsDialog();
144         }
145
146         private void openSaveAsDialog()
147         {
148                 FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE);
149                 d.open();
150                 String filename = d.getFileName();
151                 if (!filename.equals(""))
152                 {
153                         save(d.getFilterPath() + File.separator + filename);
154                         setDirty(false);
155                 }
156         }
157
158         @Override
159         public void init(IEditorSite site, IEditorInput input) throws PartInitException
160         {
161                 setSite(site);
162                 setInput(input);
163                 if (input instanceof IPathEditorInput)
164                 {
165                         IPathEditorInput pathInput = (IPathEditorInput) input;
166                         setPartName(pathInput.getName());
167                         open(pathInput.getPath().toOSString());
168                 }
169         }
170
171         @Override
172         public boolean isDirty()
173         {
174                 return dirty;
175         }
176
177         @Override
178         public boolean isSaveAsAllowed()
179         {
180                 return true;
181         }
182
183         @Override
184         public void update(long address)
185         {
186                 setDirty(true);
187                 table.refresh();
188         }
189
190         private void setDirty(boolean value)
191         {
192                 dirty = value;
193                 firePropertyChange(PROP_DIRTY);
194         }
195
196         @Override
197         public void activeMicroInstructionChanged(long address)
198         {
199                 highlight((int) (address - memory.getDefinition().getMinimalAddress()));
200         }
201 }