X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2FViewModel.java;h=cf679cd1042cab9989438c8358ff8d6866443223;hb=0a04a4ed66ecebd4254541c4977599f6052c115a;hp=c60de3ed3f6facc69cb9477ad7977efb59479420;hpb=e8e95e2e345f4d9c5927aa78f5c0fb607d352a97;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/ViewModel.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/ViewModel.java index c60de3ed..cf679cd1 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/ViewModel.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/ViewModel.java @@ -13,49 +13,60 @@ import net.mograsim.logic.model.model.wires.GUIWire; public class ViewModel { private final Map components; + private final Map componentDestroyFunctions; private final Map componentsUnmodifiable; private final Map wires; + private final Map wireDestroyFunctions; private final Map wiresUnmodifiable; private final List> componentAddedListeners; private final List> componentRemovedListeners; private final List> wireAddedListeners; private final List> wireRemovedListeners; + private final List> redrawHandlerChangedListeners; private Runnable redrawHandler; protected ViewModel() { components = new HashMap<>(); + componentDestroyFunctions = new HashMap<>(); componentsUnmodifiable = Collections.unmodifiableMap(components); wires = new HashMap<>(); + wireDestroyFunctions = new HashMap<>(); wiresUnmodifiable = Collections.unmodifiableMap(wires); componentAddedListeners = new ArrayList<>(); componentRemovedListeners = new ArrayList<>(); wireAddedListeners = new ArrayList<>(); wireRemovedListeners = new ArrayList<>(); + redrawHandlerChangedListeners = new ArrayList<>(); } /** * Adds the given component to the list of components and calls all componentAddedListeners. Don't call this method from application * code as it is automatically called in {@link GUIComponent}'s constructor. + * + * @author Daniel Kirschten */ - protected void componentCreated(GUIComponent component) + protected void componentCreated(GUIComponent component, Runnable destroyed) { if (components.containsKey(component.name)) throw new IllegalStateException("Don't add the same component twice!"); components.put(component.name, component); + componentDestroyFunctions.put(component.name, destroyed); callComponentAddedListeners(component); requestRedraw(); } /** - * Removes the given component from the list of components and calls all componentRemovedListeners. Don't call this method from - * application code as it is automatically called in {@link GUIComponent#destroy()}. + * Destroyes the given component, removes it from the list of components and calls all componentRemovedListeners. + * + * @author Daniel Kirschten */ - protected void componentDestroyed(GUIComponent component) + protected void destroyComponent(GUIComponent component) { + componentDestroyFunctions.get(component.name).run(); if (!components.containsKey(component.name)) throw new IllegalStateException("Don't remove the same component twice!"); components.remove(component.name); @@ -64,24 +75,28 @@ public class ViewModel } /** - * Adds the given wire to the list of wires and calls all wireAddedListeners. Don't call this method from application code as it is - * automatically called in {@link GUIWire}'s constructor(s). + * Adds the given wire to the list of wires and calls all wireAddedListeners. + * + * @author Daniel Kirschten */ - protected void wireCreated(GUIWire wire) + protected void wireCreated(GUIWire wire, Runnable destroyed) { if (wires.containsKey(wire.name)) throw new IllegalStateException("Don't add the same wire twice!"); wires.put(wire.name, wire); + wireDestroyFunctions.put(wire.name, destroyed); callWireAddedListeners(wire); requestRedraw(); } /** - * Removes the given wire from the list of wires and calls all wireRemovedListeners. Don't call this method from application code as it - * is automatically called in {@link GUIWire#destroy()}. + * Destroys the given wire, removes it from the list of wires and calls all wireRemovedListeners. + * + * @author Daniel Kirschten */ - protected void wireDestroyed(GUIWire wire) + protected void destroyWire(GUIWire wire) { + wireDestroyFunctions.get(wire.name).run(); if (!wires.containsKey(wire.name)) throw new IllegalStateException("Don't remove the same wire twice!"); wires.remove(wire.name); @@ -100,25 +115,29 @@ public class ViewModel } // @formatter:off - public void addComponentAddedListener (Consumer listener) {componentAddedListeners .add (listener);} - public void addComponentRemovedListener (Consumer listener) {componentRemovedListeners.add (listener);} - public void addWireAddedListener (Consumer listener) {wireAddedListeners .add (listener);} - public void addWireRemovedListener (Consumer listener) {wireRemovedListeners .add (listener);} - - public void removeComponentAddedListener (Consumer listener) {componentAddedListeners .remove(listener);} - public void removeComponentRemovedListener(Consumer listener) {componentRemovedListeners.remove(listener);} - public void removeWireAddedListener (Consumer listener) {wireAddedListeners .remove(listener);} - public void removeWireRemovedListener (Consumer listener) {wireRemovedListeners .remove(listener);} - - private void callComponentAddedListeners (GUIComponent c) {componentAddedListeners .forEach(l -> l.accept(c));} - private void callComponentRemovedListeners(GUIComponent c) {componentRemovedListeners.forEach(l -> l.accept(c));} - private void callWireAddedListeners (GUIWire w ) {wireAddedListeners .forEach(l -> l.accept(w));} - private void callWireRemovedListeners (GUIWire w ) {wireRemovedListeners .forEach(l -> l.accept(w));} + public void addComponentAddedListener (Consumer listener) {componentAddedListeners .add (listener);} + public void addComponentRemovedListener (Consumer listener) {componentRemovedListeners .add (listener);} + public void addWireAddedListener (Consumer listener) {wireAddedListeners .add (listener);} + public void addWireRemovedListener (Consumer listener) {wireRemovedListeners .add (listener);} + public void addRedrawHandlerChangedListener (Consumer listener) {redrawHandlerChangedListeners.add (listener);} + + public void removeComponentAddedListener (Consumer listener) {componentAddedListeners .remove(listener);} + public void removeComponentRemovedListener (Consumer listener) {componentRemovedListeners .remove(listener);} + public void removeWireAddedListener (Consumer listener) {wireAddedListeners .remove(listener);} + public void removeWireRemovedListener (Consumer listener) {wireRemovedListeners .remove(listener);} + public void removeRedrawHandlerChangedListener(Consumer listener) {redrawHandlerChangedListeners.remove(listener);} + + private void callComponentAddedListeners (GUIComponent c) {componentAddedListeners .forEach(l -> l.accept(c));} + private void callComponentRemovedListeners (GUIComponent c) {componentRemovedListeners .forEach(l -> l.accept(c));} + private void callWireAddedListeners (GUIWire w) {wireAddedListeners .forEach(l -> l.accept(w));} + private void callWireRemovedListeners (GUIWire w) {wireRemovedListeners .forEach(l -> l.accept(w));} + private void callRedrawHandlerChangedListener(Runnable r) {redrawHandlerChangedListeners.forEach(l -> l.accept(r));} // @formatter:on public void setRedrawHandler(Runnable handler) { this.redrawHandler = handler; + callRedrawHandlerChangedListener(handler); } public Runnable getRedrawHandler()