X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2FViewModel.java;h=445810a0670fe8398bfb4201a7e91104bf3b202c;hb=f14ea37d69488dd51518a36413af7176916b8bd7;hp=717ee13a59bcf4e972aa35fac4be57667b74be35;hpb=07faf07e3acb8b2afdc2bf65a46bc868faaed0f8;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/ViewModel.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/ViewModel.java index 717ee13a..445810a0 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/ViewModel.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/ViewModel.java @@ -8,7 +8,7 @@ import java.util.function.Consumer; import net.mograsim.logic.ui.model.components.GUIComponent; import net.mograsim.logic.ui.model.wires.GUIWire; -public class ViewModel +public class ViewModel implements Visitable { private final List components; private final List componentsUnmodifiable; @@ -19,8 +19,11 @@ public class ViewModel private final List> componentRemovedListeners; private final List> wireAddedListeners; private final List> wireRemovedListeners; + private final List redrawListeners; - public ViewModel() + private final Runnable redrawListenerForSubcomponents; + + protected ViewModel() { components = new ArrayList<>(); componentsUnmodifiable = Collections.unmodifiableList(components); @@ -31,54 +34,65 @@ public class ViewModel componentRemovedListeners = new ArrayList<>(); wireAddedListeners = new ArrayList<>(); wireRemovedListeners = new ArrayList<>(); + redrawListeners = new ArrayList<>(); + + redrawListenerForSubcomponents = this::callRedrawListeners; } /** * 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 GUIComponent::new. */ - public void componentCreated(GUIComponent component) + protected void componentCreated(GUIComponent component) { if (components.contains(component)) throw new IllegalStateException("Don't add the same component twice!"); components.add(component); callComponentAddedListeners(component); + component.addRedrawListener(redrawListenerForSubcomponents); + callRedrawListeners(); } /** * 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 GUIComponent::destroy. */ - public void componentDestroyed(GUIComponent component) + protected void componentDestroyed(GUIComponent component) { if (!components.contains(component)) throw new IllegalStateException("Don't remove the same component twice!"); components.remove(component); callComponentRemovedListeners(component); + component.removeRedrawListener(redrawListenerForSubcomponents); + callRedrawListeners(); } /** - * 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 GUIComponent::new. + * 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 GUIWire::new. */ - public void wireCreated(GUIWire wire) + protected void wireCreated(GUIWire wire) { if (wires.contains(wire)) throw new IllegalStateException("Don't add the same wire twice!"); wires.add(wire); callWireAddedListeners(wire); + wire.addRedrawListener(redrawListenerForSubcomponents); + callRedrawListeners(); } /** - * 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 GUIComponent::destroy. + * 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 GUIWire::destroy. */ - public void wireDestroyed(GUIWire wire) + protected void wireDestroyed(GUIWire wire) { if (!wires.contains(wire)) throw new IllegalStateException("Don't remove the same wire twice!"); wires.remove(wire); callWireRemovedListeners(wire); + wire.removeRedrawListener(redrawListenerForSubcomponents); + callRedrawListeners(); } public List getComponents() @@ -91,20 +105,29 @@ public class ViewModel return wiresUnmodifiable; } + @Override + public void accept(ModelVisitor mv) + { + mv.visit(this); + } + // @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 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 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 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 } \ No newline at end of file