Machine name is no in mpm files
[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().setTopIndex(index);
70                 });
71         }
72
73         private void addActivationButton(Composite parent)
74         {
75                 Button activationButton = new Button(parent, SWT.PUSH);
76                 activationButton.setText("Set Active");
77                 activationButton.addListener(SWT.Selection,
78                                 e -> MachineContext.getInstance().getMachine().getMicroInstructionMemory().bind(memory));
79         }
80
81         public void bindMicroInstructionMemory(MicroInstructionMemory memory)
82         {
83                 this.memory = memory;
84                 if (memory != null)
85                 {
86                         this.memory.registerCellModifiedListener(this);
87                         this.memory.registerActiveMicroInstructionChangedListener(this);
88                 }
89                 if (table != null)
90                         table.bindMicroInstructionMemory(memory);
91         }
92
93         private void open(IFile file)
94         {
95                 try
96                 {
97                         bindMicroInstructionMemory(MicroInstructionMemoryParser.parseMemory(
98                                         MachineContext.getInstance().getMachine().getDefinition().getMicroInstructionMemoryDefinition(), file.getContents()));
99                 }
100                 catch (IOException | MicroInstructionMemoryParseException | CoreException e)
101                 {
102                         e.printStackTrace();
103                 }
104         }
105
106         private void save(IFile file, IProgressMonitor progressMonitor)
107         {
108                 if (memory == null)
109                 {
110                         System.err.println("Failed to write MicroprogrammingMemory to File. No MicroprogrammingMemory assigned.");
111                         return;
112                 }
113                 try (InputStream toWrite = MicroInstructionMemoryParser.write(memory))
114                 {
115                         file.setContents(toWrite, 0, progressMonitor);
116                 }
117                 catch (IOException | CoreException e)
118                 {
119                         e.printStackTrace();
120                 }
121         }
122
123         @Override
124         public void setFocus()
125         {
126                 table.getTableViewer().getControl().setFocus();
127         }
128
129         @Override
130         public void doSave(IProgressMonitor progressMonitor)
131         {
132                 IEditorInput input = getEditorInput();
133                 if (input instanceof IFileEditorInput)
134                 {
135                         IFileEditorInput pathInput = (IFileEditorInput) input;
136                         save(pathInput.getFile(), progressMonitor);
137                         setDirty(false);
138                 }
139         }
140
141         @Override
142         public void doSaveAs()
143         {
144 //              openSaveAsDialog();
145         }
146
147 //      private void openSaveAsDialog()
148 //      {
149 //              FileDialog d = new FileDialog(table.getTableViewer().getTable().getShell(), SWT.SAVE);
150 //              d.open();
151 //              String filename = d.getFileName();
152 //              if (!filename.equals(""))
153 //              {
154 //                      save(d.getFilterPath() + File.separator + filename);
155 //                      setDirty(false);
156 //              }
157 //      }
158
159         @Override
160         public void init(IEditorSite site, IEditorInput input) throws PartInitException
161         {
162                 setSite(site);
163                 setInput(input);
164                 if (input instanceof IFileEditorInput)
165                 {
166                         IFileEditorInput fileInput = (IFileEditorInput) input;
167                         setPartName(fileInput.getName());
168                         open(fileInput.getFile());
169                 }
170
171         }
172
173         @Override
174         public boolean isDirty()
175         {
176                 return dirty;
177         }
178
179         @Override
180         public boolean isSaveAsAllowed()
181         {
182                 return false;
183         }
184
185         @Override
186         public void update(long address)
187         {
188                 setDirty(true);
189                 table.refresh();
190         }
191
192         private void setDirty(boolean value)
193         {
194                 dirty = value;
195                 firePropertyChange(PROP_DIRTY);
196         }
197
198         @Override
199         public void activeMicroInstructionChanged(long address)
200         {
201                 highlight((int) (address - memory.getDefinition().getMinimalAddress()));
202         }
203 }