*/
public final void setActiveMachine(Machine machine)
{
+ Optional<Machine> oldMachine = activeMachine;
activeMachine = Optional.ofNullable(machine);
updateStatus();
- notifyActiveMachineListeners();
+ notifyActiveMachineListeners(oldMachine, activeMachine);
}
public final Optional<String> getMachineId()
{
if ((status == DEAD || status == CLOSED) && activeMachine.isPresent())
{
+ Optional<Machine> oldMachine = activeMachine;
activeMachine = Optional.empty();
- notifyActiveMachineListeners();
+ notifyActiveMachineListeners(oldMachine, activeMachine);
}
}
}
}
- private void notifyActiveMachineListeners()
+ private void notifyActiveMachineListeners(Optional<Machine> oldMachine, Optional<Machine> newMachine)
{
- machineListeners.forEach(ob -> ob.setMachine(activeMachine));
+ machineListeners.forEach(ob -> ob.setMachine(oldMachine, newMachine));
}
public void addActiveMachineListener(ActiveMachineListener ob)
{
machineListeners.add(ob);
- ob.setMachine(activeMachine);
+ ob.setMachine(Optional.empty(), activeMachine);
}
public void removeActiveMachineListener(ActiveMachineListener ob)
@FunctionalInterface
public static interface ActiveMachineListener
{
- void setMachine(Optional<Machine> machine);
+ void setMachine(Optional<Machine> oldMachine, Optional<Machine> newMachine);
}
@FunctionalInterface
import net.mograsim.machine.mi.MicroInstructionMemoryParseException;
import net.mograsim.machine.mi.MicroInstructionMemoryParser;
import net.mograsim.plugin.nature.MachineContext;
+import net.mograsim.plugin.nature.MachineContext.ActiveMachineListener;
import net.mograsim.plugin.nature.ProjectMachineContext;
import net.mograsim.plugin.tables.DisplaySettings;
import net.mograsim.plugin.tables.RadixSelector;
-public class InstructionView extends EditorPart implements MemoryCellModifiedListener, ActiveMicroInstructionChangedListener
+public class InstructionView extends EditorPart
{
private InstructionTableContentProvider provider;
private boolean dirty = false;
private InstructionTable table;
private MachineContext context;
+ // Listeners
+ private MemoryCellModifiedListener cellModifiedListener = address ->
+ {
+ setDirty(true);
+ table.refresh();
+ };
+
+ private ActiveMicroInstructionChangedListener activeInstructionChangedListener = address -> highlight(
+ (int) (address - memory.getDefinition().getMinimalAddress()));
+
+ private MIMemoryReassignedListener reassignedListener = newAssignee ->
+ {
+ // clear highlighting if the memory is reassigned
+ if (newAssignee != memory)
+ highlight(-1);
+ };
+
+ private ActiveMachineListener activeMachineListener = (oldMachine, newMachine) ->
+ {
+ // clear highlighting if the active machine changes
+ if (newMachine.isEmpty() || !newMachine.equals(oldMachine))
+ {
+ highlight(-1);
+ oldMachine.ifPresent(m -> m.getMicroInstructionMemory().deregisterMemoryReassignedListener(reassignedListener));
+ }
+ };
+
@SuppressWarnings("unused")
@Override
public void createPartControl(Composite parent)
activationButton.setText("Set Active");
activationButton.addListener(SWT.Selection, e -> context.getActiveMachine().ifPresent(m ->
{
- // clear highlighting if the memory is reassigned
- MIMemoryReassignedListener memReassignedListener = n ->
- {
- if (n != memory)
- highlight(-1);
- };
- m.getMicroInstructionMemory().registerMemoryReassignedListener(memReassignedListener);
- // clear highlighting if the active machine changes
- context.addActiveMachineListener(n ->
- {
- if (n.isEmpty() || n.get() != m)
- {
- highlight(-1);
- m.getMicroInstructionMemory().deregisterMemoryReassignedListener(memReassignedListener);
- }
- });
+ m.getMicroInstructionMemory().registerMemoryReassignedListener(reassignedListener);
+ context.addActiveMachineListener(activeMachineListener);
m.getMicroInstructionMemory().bind(memory);
}));
}
{
if (this.memory != null)
{
- this.memory.deregisterCellModifiedListener(this);
- this.memory.deregisterActiveMicroInstructionChangedListener(this);
+ this.memory.deregisterCellModifiedListener(cellModifiedListener);
+ this.memory.deregisterActiveMicroInstructionChangedListener(activeInstructionChangedListener);
}
this.memory = memory;
if (memory != null)
{
- this.memory.registerCellModifiedListener(this);
- this.memory.registerActiveMicroInstructionChangedListener(this);
+ this.memory.registerCellModifiedListener(cellModifiedListener);
+ this.memory.registerActiveMicroInstructionChangedListener(activeInstructionChangedListener);
}
if (table != null)
table.bindMicroInstructionMemory(memory);
return false;
}
- @Override
- public void update(long address)
- {
- setDirty(true);
- table.refresh();
- }
-
private void setDirty(boolean value)
{
dirty = value;
firePropertyChange(PROP_DIRTY);
}
- @Override
- public void activeMicroInstructionChanged(long address)
- {
- highlight((int) (address - memory.getDefinition().getMinimalAddress()));
- }
-
@Override
public void dispose()
{
- table.dispose();
+ memory.deregisterActiveMicroInstructionChangedListener(activeInstructionChangedListener);
+ memory.deregisterCellModifiedListener(cellModifiedListener);
+ context.getActiveMachine().ifPresent(m -> m.getMicroInstructionMemory().deregisterMemoryReassignedListener(reassignedListener));
+ context.removeActiveMachineListener(activeMachineListener);
super.dispose();
}
}