1 package era.mi.gui.model;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.function.Consumer;
8 import era.mi.gui.model.components.GUIComponent;
9 import era.mi.gui.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;
25 components = new ArrayList<>();
26 componentsUnmodifiable = Collections.unmodifiableList(components);
27 wires = new ArrayList<>();
28 wiresUnmodifiable = Collections.unmodifiableList(wires);
30 componentAddedListeners = new ArrayList<>();
31 componentRemovedListeners = new ArrayList<>();
32 wireAddedListeners = new ArrayList<>();
33 wireRemovedListeners = new ArrayList<>();
37 * Adds the given component to the list of components and calls all componentAddedListeners. Don't call this method from application
38 * code as it is automatically called in GUIComponent::new.
40 public void componentCreated(GUIComponent component)
42 if (components.contains(component))
43 throw new IllegalStateException("Don't add the same component twice!");
44 components.add(component);
45 callComponentAddedListeners(component);
49 * Removes the given component from the list of components and calls all componentRemovedListeners. Don't call this method from
50 * application code as it is automatically called in GUIComponent::destroy.
52 public void componentDestroyed(GUIComponent component)
54 if (!components.contains(component))
55 throw new IllegalStateException("Don't remove the same component twice!");
56 components.remove(component);
57 callComponentRemovedListeners(component);
61 * Adds the given component to the list of components and calls all componentAddedListeners. Don't call this method from application
62 * code as it is automatically called in GUIComponent::new.
64 public void wireCreated(GUIWire wire)
66 if (wires.contains(wire))
67 throw new IllegalStateException("Don't add the same wire twice!");
69 callWireAddedListeners(wire);
73 * Removes the given component from the list of components and calls all componentRemovedListeners. Don't call this method from
74 * application code as it is automatically called in GUIComponent::destroy.
76 public void wireDestroyed(GUIWire wire)
78 if (!wires.contains(wire))
79 throw new IllegalStateException("Don't remove the same wire twice!");
81 callWireRemovedListeners(wire);
84 public List<GUIComponent> getComponents()
86 return componentsUnmodifiable;
89 public List<GUIWire> getWires()
91 return wiresUnmodifiable;
95 public void addComponentAddedListener (Consumer<? super GUIComponent> listener){componentAddedListeners .add (listener);}
96 public void addComponentRemovedListener (Consumer<? super GUIComponent> listener){componentRemovedListeners.add (listener);}
97 public void addWireAddedListener (Consumer<? super GUIWire > listener){wireAddedListeners .add (listener);}
98 public void addWireRemovedListener (Consumer<? super GUIWire > listener){wireRemovedListeners .add (listener);}
100 public void removeComponentAddedListener (Consumer<? super GUIComponent> listener){componentAddedListeners .remove(listener);}
101 public void removeComponentRemovedListener(Consumer<? super GUIComponent> listener){componentRemovedListeners.remove(listener);}
102 public void removeWireAddedListener (Consumer<? super GUIWire > listener){wireAddedListeners .remove(listener);}
103 public void removeWireRemovedListener (Consumer<? super GUIWire > listener){wireRemovedListeners .remove(listener);}
105 private void callComponentAddedListeners (GUIComponent c) {componentAddedListeners .forEach(l -> l.accept(c));}
106 private void callComponentRemovedListeners(GUIComponent c) {componentRemovedListeners.forEach(l -> l.accept(c));}
107 private void callWireAddedListeners (GUIWire w ) {wireAddedListeners .forEach(l -> l.accept(w));}
108 private void callWireRemovedListeners (GUIWire w ) {wireRemovedListeners .forEach(l -> l.accept(w));}