8cdaae277491c2391a1a7dbd68a28d72cb04aac3
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / BooleanEditingSupport.java
1 package net.mograsim.plugin.tables.mi;
2
3 import org.eclipse.jface.viewers.CellEditor;
4 import org.eclipse.jface.viewers.ComboBoxCellEditor;
5 import org.eclipse.jface.viewers.EditingSupport;
6 import org.eclipse.jface.viewers.TableViewer;
7 import org.eclipse.swt.SWT;
8
9 import net.mograsim.logic.core.types.Bit;
10 import net.mograsim.machine.mi.MicroInstruction;
11 import net.mograsim.machine.mi.MicroInstructionDefinition;
12 import net.mograsim.machine.mi.parameters.BooleanClassification;
13 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
14 import net.mograsim.machine.mi.parameters.Mnemonic;
15
16 public class BooleanEditingSupport extends EditingSupport
17 {
18         private final ComboBoxCellEditor editor;
19         private final BooleanClassification boolClass;
20         private final TableViewer viewer;
21         private final int index;
22
23         public BooleanEditingSupport(TableViewer viewer, MicroInstructionDefinition definition, int index)
24         {
25                 super(viewer);
26                 this.viewer = viewer;
27                 this.boolClass = (BooleanClassification) definition.getParameterClassification(index);
28                 editor = new ComboBoxCellEditor(viewer.getTable(), boolClass.getStringValues(), SWT.READ_ONLY);
29                 editor.setActivationStyle(
30                                 ComboBoxCellEditor.DROP_DOWN_ON_TRAVERSE_ACTIVATION | ComboBoxCellEditor.DROP_DOWN_ON_PROGRAMMATIC_ACTIVATION
31                                                 | ComboBoxCellEditor.DROP_DOWN_ON_MOUSE_ACTIVATION | ComboBoxCellEditor.DROP_DOWN_ON_KEY_ACTIVATION);
32                 this.index = index;
33         }
34
35         @Override
36         protected boolean canEdit(Object element)
37         {
38                 return true;
39         }
40
41         @Override
42         protected CellEditor getCellEditor(Object element)
43         {
44                 return editor;
45         }
46
47         @Override
48         protected Object getValue(Object element)
49         {
50                 InstructionTableRow row = (InstructionTableRow) element;
51                 // true is 0 because the true value comes first in the combo box
52                 return row.data.getCell(row.address).getParameter(index).getValue().getMSBit(0).equals(Bit.ONE) ? 0 : 1;
53         }
54
55         @Override
56         protected void setValue(Object element, Object value)
57         {
58                 InstructionTableRow row = (InstructionTableRow) element;
59                 MicroInstructionParameter[] params = row.data.getCell(row.address).getParameters();
60                 // true is 0 because the true value comes first in the combo box
61                 Mnemonic newParam = boolClass.get(value.equals(0) ? true : false);
62                 if (newParam.equals(params[index]))
63                         return;
64                 params[index] = newParam;
65                 row.data.setCell(row.address, MicroInstruction.create(params));
66                 viewer.update(element, null);
67         }
68
69 }