X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2FViewModel.java;fp=LogicUI%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2FViewModel.java;h=717ee13a59bcf4e972aa35fac4be57667b74be35;hb=0009789a8df6b8d4562b6e1cbfa75102a7516ea8;hp=0000000000000000000000000000000000000000;hpb=a28f7aa0dab4248e99159c5a647676170cb17a4e;p=Mograsim.git diff --git a/LogicUI/src/net/mograsim/logic/ui/model/ViewModel.java b/LogicUI/src/net/mograsim/logic/ui/model/ViewModel.java new file mode 100644 index 00000000..717ee13a --- /dev/null +++ b/LogicUI/src/net/mograsim/logic/ui/model/ViewModel.java @@ -0,0 +1,110 @@ +package net.mograsim.logic.ui.model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; + +import net.mograsim.logic.ui.model.components.GUIComponent; +import net.mograsim.logic.ui.model.wires.GUIWire; + +public class ViewModel +{ + private final List components; + private final List componentsUnmodifiable; + private final List wires; + private final List wiresUnmodifiable; + + private final List> componentAddedListeners; + private final List> componentRemovedListeners; + private final List> wireAddedListeners; + private final List> wireRemovedListeners; + + public ViewModel() + { + components = new ArrayList<>(); + componentsUnmodifiable = Collections.unmodifiableList(components); + wires = new ArrayList<>(); + wiresUnmodifiable = Collections.unmodifiableList(wires); + + componentAddedListeners = new ArrayList<>(); + componentRemovedListeners = new ArrayList<>(); + wireAddedListeners = new ArrayList<>(); + wireRemovedListeners = 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 GUIComponent::new. + */ + public void componentCreated(GUIComponent component) + { + if (components.contains(component)) + throw new IllegalStateException("Don't add the same component twice!"); + components.add(component); + callComponentAddedListeners(component); + } + + /** + * 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) + { + if (!components.contains(component)) + throw new IllegalStateException("Don't remove the same component twice!"); + components.remove(component); + callComponentRemovedListeners(component); + } + + /** + * 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 wireCreated(GUIWire wire) + { + if (wires.contains(wire)) + throw new IllegalStateException("Don't add the same wire twice!"); + wires.add(wire); + callWireAddedListeners(wire); + } + + /** + * 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 wireDestroyed(GUIWire wire) + { + if (!wires.contains(wire)) + throw new IllegalStateException("Don't remove the same wire twice!"); + wires.remove(wire); + callWireRemovedListeners(wire); + } + + public List getComponents() + { + return componentsUnmodifiable; + } + + public List getWires() + { + return wiresUnmodifiable; + } + + // @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));} + // @formatter:on +} \ No newline at end of file