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