X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2FViewModel.java;h=c60de3ed3f6facc69cb9477ad7977efb59479420;hb=026db85f46b6ea58e765ecff069545728eebdcac;hp=193813297d3584912290051a5875652a0a7a1647;hpb=b5d8c2d71e27350ea7c9314e40df5bb0584271cd;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 19381329..c60de3ed 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 @@ -14,31 +14,27 @@ public class ViewModel { private final Map components; private final Map componentsUnmodifiable; - private final List wires; - private final List wiresUnmodifiable; + private final Map wires; + private final Map wiresUnmodifiable; private final List> componentAddedListeners; private final List> componentRemovedListeners; private final List> wireAddedListeners; private final List> wireRemovedListeners; - private final List redrawListeners; - private final Runnable redrawListenerForSubcomponents; + private Runnable redrawHandler; protected ViewModel() { components = new HashMap<>(); componentsUnmodifiable = Collections.unmodifiableMap(components); - wires = new ArrayList<>(); - wiresUnmodifiable = Collections.unmodifiableList(wires); + wires = new HashMap<>(); + wiresUnmodifiable = Collections.unmodifiableMap(wires); componentAddedListeners = new ArrayList<>(); componentRemovedListeners = new ArrayList<>(); wireAddedListeners = new ArrayList<>(); wireRemovedListeners = new ArrayList<>(); - redrawListeners = new ArrayList<>(); - - redrawListenerForSubcomponents = this::callRedrawListeners; } /** @@ -51,8 +47,7 @@ public class ViewModel throw new IllegalStateException("Don't add the same component twice!"); components.put(component.name, component); callComponentAddedListeners(component); - component.addRedrawListener(redrawListenerForSubcomponents); - callRedrawListeners(); + requestRedraw(); } /** @@ -65,8 +60,7 @@ public class ViewModel throw new IllegalStateException("Don't remove the same component twice!"); components.remove(component.name); callComponentRemovedListeners(component); - component.removeRedrawListener(redrawListenerForSubcomponents); - callRedrawListeners(); + requestRedraw(); } /** @@ -75,12 +69,11 @@ public class ViewModel */ protected void wireCreated(GUIWire wire) { - if (wires.contains(wire)) + if (wires.containsKey(wire.name)) throw new IllegalStateException("Don't add the same wire twice!"); - wires.add(wire); + wires.put(wire.name, wire); callWireAddedListeners(wire); - wire.addRedrawListener(redrawListenerForSubcomponents); - callRedrawListeners(); + requestRedraw(); } /** @@ -89,12 +82,11 @@ public class ViewModel */ protected void wireDestroyed(GUIWire wire) { - if (!wires.contains(wire)) + if (!wires.containsKey(wire.name)) throw new IllegalStateException("Don't remove the same wire twice!"); - wires.remove(wire); + wires.remove(wire.name); callWireRemovedListeners(wire); - wire.removeRedrawListener(redrawListenerForSubcomponents); - callRedrawListeners(); + requestRedraw(); } public Map getComponentsByName() @@ -102,7 +94,7 @@ public class ViewModel return componentsUnmodifiable; } - public List getWires() + public Map getWiresByName() { return wiresUnmodifiable; } @@ -112,18 +104,31 @@ public class ViewModel 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 addRedrawListener (Runnable listener) {redrawListeners .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 removeRedrawListener (Runnable listener) {redrawListeners .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 callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));} // @formatter:on + + public void setRedrawHandler(Runnable handler) + { + this.redrawHandler = handler; + } + + public Runnable getRedrawHandler() + { + return redrawHandler; + } + + public void requestRedraw() + { + if (redrawHandler != null) + redrawHandler.run(); + } } \ No newline at end of file