GUIComponents now have names
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / ViewModel.java
index 717ee13..4d1f21b 100644 (file)
@@ -2,7 +2,9 @@ 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;
@@ -10,8 +12,8 @@ import net.mograsim.logic.ui.model.wires.GUIWire;
 
 public class ViewModel
 {
-       private final List<GUIComponent> components;
-       private final List<GUIComponent> componentsUnmodifiable;
+       private final Map<String, GUIComponent> components;
+       private final Map<String, GUIComponent> componentsUnmodifiable;
        private final List<GUIWire> wires;
        private final List<GUIWire> wiresUnmodifiable;
 
@@ -19,11 +21,14 @@ public class ViewModel
        private final List<Consumer<? super GUIComponent>> componentRemovedListeners;
        private final List<Consumer<? super GUIWire>> wireAddedListeners;
        private final List<Consumer<? super GUIWire>> wireRemovedListeners;
+       private final List<Runnable> redrawListeners;
 
-       public ViewModel()
+       private final Runnable redrawListenerForSubcomponents;
+
+       protected ViewModel()
        {
-               components = new ArrayList<>();
-               componentsUnmodifiable = Collections.unmodifiableList(components);
+               components = new HashMap<>();
+               componentsUnmodifiable = Collections.unmodifiableMap(components);
                wires = new ArrayList<>();
                wiresUnmodifiable = Collections.unmodifiableList(wires);
 
@@ -31,57 +36,68 @@ 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.
+        * code as it is automatically called in {@link GUIComponent}'s constructor.
         */
-       public void componentCreated(GUIComponent component)
+       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();
        }
 
        /**
         * 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()}.
         */
-       public void componentDestroyed(GUIComponent component)
+       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();
        }
 
        /**
-        * 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 {@link GUIWire}'s constructor(s).
         */
-       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 {@link 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<GUIComponent> getComponents()
+       public Map<String, GUIComponent> getComponentsByName()
        {
                return componentsUnmodifiable;
        }
@@ -92,19 +108,22 @@ public class ViewModel
        }
 
        // @formatter:off
-       public void addComponentAddedListener     (Consumer<? super GUIComponent> listener){componentAddedListeners  .add   (listener);}
-       public void addComponentRemovedListener   (Consumer<? super GUIComponent> listener){componentRemovedListeners.add   (listener);}
-       public void addWireAddedListener          (Consumer<? super GUIWire     > listener){wireAddedListeners       .add   (listener);}
-       public void addWireRemovedListener        (Consumer<? super GUIWire     > listener){wireRemovedListeners     .add   (listener);}
+       public void addComponentAddedListener     (Consumer<? super GUIComponent> listener) {componentAddedListeners  .add   (listener);}
+       public void addComponentRemovedListener   (Consumer<? super GUIComponent> listener) {componentRemovedListeners.add   (listener);}
+       public void addWireAddedListener          (Consumer<? super GUIWire     > listener) {wireAddedListeners       .add   (listener);}
+       public void addWireRemovedListener        (Consumer<? super GUIWire     > listener) {wireRemovedListeners     .add   (listener);}
+       public void addRedrawListener             (Runnable                       listener) {redrawListeners          .add   (listener);}
 
-       public void removeComponentAddedListener  (Consumer<? super GUIComponent> listener){componentAddedListeners  .remove(listener);}
-       public void removeComponentRemovedListener(Consumer<? super GUIComponent> listener){componentRemovedListeners.remove(listener);}
-       public void removeWireAddedListener       (Consumer<? super GUIWire     > listener){wireAddedListeners       .remove(listener);}
-       public void removeWireRemovedListener     (Consumer<? super GUIWire     > listener){wireRemovedListeners     .remove(listener);}
+       public void removeComponentAddedListener  (Consumer<? super GUIComponent> listener) {componentAddedListeners  .remove(listener);}
+       public void removeComponentRemovedListener(Consumer<? super GUIComponent> listener) {componentRemovedListeners.remove(listener);}
+       public void removeWireAddedListener       (Consumer<? super GUIWire     > listener) {wireAddedListeners       .remove(listener);}
+       public void removeWireRemovedListener     (Consumer<? super GUIWire     > 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