X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2FViewModel.java;h=4d1f21b25cc44da8bcd7c15f8d414914d3728ee0;hb=5ece0acf049bf9af2933f513fe0206565681f622;hp=445810a0670fe8398bfb4201a7e91104bf3b202c;hpb=b37ba7609a925cc945bbac0f6ead619d07912238;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 445810a0..4d1f21b2 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 @@ -2,16 +2,18 @@ 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 implements Visitable +public class ViewModel { - private final List components; - private final List componentsUnmodifiable; + private final Map components; + private final Map componentsUnmodifiable; private final List wires; private final List wiresUnmodifiable; @@ -25,8 +27,8 @@ public class ViewModel implements Visitable protected ViewModel() { - components = new ArrayList<>(); - componentsUnmodifiable = Collections.unmodifiableList(components); + components = new HashMap<>(); + componentsUnmodifiable = Collections.unmodifiableMap(components); wires = new ArrayList<>(); wiresUnmodifiable = Collections.unmodifiableList(wires); @@ -41,13 +43,13 @@ public class ViewModel implements Visitable /** * 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. + * code as it is automatically called in {@link GUIComponent}'s constructor. */ protected void componentCreated(GUIComponent component) { - if (components.contains(component)) + if (components.containsKey(component.name)) throw new IllegalStateException("Don't add the same component twice!"); - components.add(component); + components.put(component.name, component); callComponentAddedListeners(component); component.addRedrawListener(redrawListenerForSubcomponents); callRedrawListeners(); @@ -55,13 +57,13 @@ public class ViewModel implements Visitable /** * 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. + * application code as it is automatically called in {@link GUIComponent#destroy()}. */ protected void componentDestroyed(GUIComponent component) { - if (!components.contains(component)) + if (!components.containsKey(component.name)) throw new IllegalStateException("Don't remove the same component twice!"); - components.remove(component); + components.remove(component.name); callComponentRemovedListeners(component); component.removeRedrawListener(redrawListenerForSubcomponents); callRedrawListeners(); @@ -69,7 +71,7 @@ public class ViewModel implements Visitable /** * 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. + * automatically called in {@link GUIWire}'s constructor(s). */ protected void wireCreated(GUIWire wire) { @@ -83,7 +85,7 @@ public class ViewModel implements Visitable /** * 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. + * is automatically called in {@link GUIWire#destroy()}. */ protected void wireDestroyed(GUIWire wire) { @@ -95,7 +97,7 @@ public class ViewModel implements Visitable callRedrawListeners(); } - public List getComponents() + public Map getComponentsByName() { return componentsUnmodifiable; } @@ -105,12 +107,6 @@ public class ViewModel implements Visitable 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);}