1 package net.mograsim.logic.ui.model;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.function.Consumer;
8 import net.mograsim.logic.ui.model.components.GUIComponent;
9 import net.mograsim.logic.ui.model.wires.GUIWire;
11 public class ViewModel
13 private final List<GUIComponent> components;
14 private final List<GUIComponent> componentsUnmodifiable;
15 private final List<GUIWire> wires;
16 private final List<GUIWire> wiresUnmodifiable;
18 private final List<Consumer<? super GUIComponent>> componentAddedListeners;
19 private final List<Consumer<? super GUIComponent>> componentRemovedListeners;
20 private final List<Consumer<? super GUIWire>> wireAddedListeners;
21 private final List<Consumer<? super GUIWire>> wireRemovedListeners;
22 private final List<Runnable> redrawListeners;
24 private final Runnable redrawListenerForSubcomponents;
28 components = new ArrayList<>();
29 componentsUnmodifiable = Collections.unmodifiableList(components);
30 wires = new ArrayList<>();
31 wiresUnmodifiable = Collections.unmodifiableList(wires);
33 componentAddedListeners = new ArrayList<>();
34 componentRemovedListeners = new ArrayList<>();
35 wireAddedListeners = new ArrayList<>();
36 wireRemovedListeners = new ArrayList<>();
37 redrawListeners = new ArrayList<>();
39 redrawListenerForSubcomponents = this::callRedrawListeners;
43 * Adds the given component to the list of components and calls all componentAddedListeners. Don't call this method from application
44 * code as it is automatically called in {@link GUIComponent}'s constructor.
46 protected void componentCreated(GUIComponent component)
48 if (components.contains(component))
49 throw new IllegalStateException("Don't add the same component twice!");
50 components.add(component);
51 callComponentAddedListeners(component);
52 component.addRedrawListener(redrawListenerForSubcomponents);
53 callRedrawListeners();
57 * Removes the given component from the list of components and calls all componentRemovedListeners. Don't call this method from
58 * application code as it is automatically called in {@link GUIComponent#destroy()}.
60 protected void componentDestroyed(GUIComponent component)
62 if (!components.contains(component))
63 throw new IllegalStateException("Don't remove the same component twice!");
64 components.remove(component);
65 callComponentRemovedListeners(component);
66 component.removeRedrawListener(redrawListenerForSubcomponents);
67 callRedrawListeners();
71 * Adds the given wire to the list of wires and calls all wireAddedListeners. Don't call this method from application code as it is
72 * automatically called in {@link GUIWire}'s constructor(s).
74 protected void wireCreated(GUIWire wire)
76 if (wires.contains(wire))
77 throw new IllegalStateException("Don't add the same wire twice!");
79 callWireAddedListeners(wire);
80 wire.addRedrawListener(redrawListenerForSubcomponents);
81 callRedrawListeners();
85 * Removes the given wire from the list of wires and calls all wireRemovedListeners. Don't call this method from application code as it
86 * is automatically called in {@link GUIWire#destroy()}.
88 protected void wireDestroyed(GUIWire wire)
90 if (!wires.contains(wire))
91 throw new IllegalStateException("Don't remove the same wire twice!");
93 callWireRemovedListeners(wire);
94 wire.removeRedrawListener(redrawListenerForSubcomponents);
95 callRedrawListeners();
98 public List<GUIComponent> getComponents()
100 return componentsUnmodifiable;
103 public List<GUIWire> getWires()
105 return wiresUnmodifiable;
109 public void addComponentAddedListener (Consumer<? super GUIComponent> listener) {componentAddedListeners .add (listener);}
110 public void addComponentRemovedListener (Consumer<? super GUIComponent> listener) {componentRemovedListeners.add (listener);}
111 public void addWireAddedListener (Consumer<? super GUIWire > listener) {wireAddedListeners .add (listener);}
112 public void addWireRemovedListener (Consumer<? super GUIWire > listener) {wireRemovedListeners .add (listener);}
113 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
115 public void removeComponentAddedListener (Consumer<? super GUIComponent> listener) {componentAddedListeners .remove(listener);}
116 public void removeComponentRemovedListener(Consumer<? super GUIComponent> listener) {componentRemovedListeners.remove(listener);}
117 public void removeWireAddedListener (Consumer<? super GUIWire > listener) {wireAddedListeners .remove(listener);}
118 public void removeWireRemovedListener (Consumer<? super GUIWire > listener) {wireRemovedListeners .remove(listener);}
119 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
121 private void callComponentAddedListeners (GUIComponent c) {componentAddedListeners .forEach(l -> l.accept(c));}
122 private void callComponentRemovedListeners(GUIComponent c) {componentRemovedListeners.forEach(l -> l.accept(c));}
123 private void callWireAddedListeners (GUIWire w ) {wireAddedListeners .forEach(l -> l.accept(w));}
124 private void callWireRemovedListeners (GUIWire w ) {wireRemovedListeners .forEach(l -> l.accept(w));}
125 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}