X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2FViewModel.java;fp=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2FViewModel.java;h=0000000000000000000000000000000000000000;hb=b5d8c2d71e27350ea7c9314e40df5bb0584271cd;hp=4d1f21b25cc44da8bcd7c15f8d414914d3728ee0;hpb=69cb6725ef670328959d55649257ded6ac924b33;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 deleted file mode 100644 index 4d1f21b2..00000000 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/ViewModel.java +++ /dev/null @@ -1,129 +0,0 @@ -package net.mograsim.logic.ui.model; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -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 Map components; - private final Map 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; - private final List redrawListeners; - - private final Runnable redrawListenerForSubcomponents; - - protected ViewModel() - { - components = new HashMap<>(); - componentsUnmodifiable = Collections.unmodifiableMap(components); - wires = new ArrayList<>(); - wiresUnmodifiable = Collections.unmodifiableList(wires); - - componentAddedListeners = new ArrayList<>(); - 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 {@link GUIComponent}'s constructor. - */ - protected void componentCreated(GUIComponent component) - { - if (components.containsKey(component.name)) - throw new IllegalStateException("Don't add the same component twice!"); - components.put(component.name, 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 {@link GUIComponent#destroy()}. - */ - protected void componentDestroyed(GUIComponent component) - { - if (!components.containsKey(component.name)) - throw new IllegalStateException("Don't remove the same component twice!"); - components.remove(component.name); - callComponentRemovedListeners(component); - component.removeRedrawListener(redrawListenerForSubcomponents); - callRedrawListeners(); - } - - /** - * 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). - */ - 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 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()}. - */ - 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 Map getComponentsByName() - { - 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 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 -} \ No newline at end of file