1 package era.mi.gui.model;
3 import java.util.ArrayList;
5 import java.util.function.Consumer;
7 import era.mi.gui.model.components.GUIComponent;
8 import era.mi.gui.model.wires.GUIWire;
10 public class ViewModel
12 private final List<GUIComponent> components;
13 private final List<GUIWire> wires;
15 private final List<Consumer<GUIComponent>> componentAddedListeners;
16 private final List<Consumer<GUIComponent>> componentRemovedListeners;
17 private final List<Consumer<GUIWire>> wireAddedListeners;
18 private final List<Consumer<GUIWire>> wireRemovedListeners;
22 components = new ArrayList<>();
23 wires = new ArrayList<>();
25 componentAddedListeners = new ArrayList<>();
26 componentRemovedListeners = new ArrayList<>();
27 wireAddedListeners = new ArrayList<>();
28 wireRemovedListeners = new ArrayList<>();
32 * Adds the given component to the list of components and calls all componentAddedListeners. Don't call this method from application
33 * code as it is automatically called in GUIComponent::new.
35 public void addComponent(GUIComponent component)
37 if (components.contains(component))
38 throw new IllegalStateException("Don't add the same component twice!");
39 components.add(component);
40 callComponentAddedListeners(component);
44 * Removes the given component from the list of components and calls all componentRemovedListeners. Don't call this method from
45 * application code as it is automatically called in GUIComponent::destroy.
47 public void removeComponent(GUIComponent component)
49 if (!components.contains(component))
50 throw new IllegalStateException("Don't remove the same component twice!");
51 components.remove(component);
52 callComponentRemovedListeners(component);
56 public void addComponentAddedListener (Consumer<GUIComponent> listener){componentAddedListeners .add (listener);}
57 public void addComponentRemovedListener (Consumer<GUIComponent> listener){componentRemovedListeners.add (listener);}
58 public void addWireAddedListener (Consumer<GUIWire > listener){wireAddedListeners .add (listener);}
59 public void addWireRemovedListener (Consumer<GUIWire > listener){wireRemovedListeners .add (listener);}
61 public void removeComponentAddedListener (Consumer<GUIComponent> listener){componentAddedListeners .remove(listener);}
62 public void removeComponentRemovedListener(Consumer<GUIComponent> listener){componentRemovedListeners.remove(listener);}
63 public void removeWireAddedListener (Consumer<GUIWire > listener){wireAddedListeners .remove(listener);}
64 public void removeWireRemovedListener (Consumer<GUIWire > listener){wireRemovedListeners .remove(listener);}
66 private void callComponentAddedListeners (GUIComponent c) {componentAddedListeners .forEach(l -> l.accept(c));}
67 private void callComponentRemovedListeners(GUIComponent c) {componentRemovedListeners.forEach(l -> l.accept(c));}
68 private void callWireAddedListeners (GUIWire w ) {wireAddedListeners .forEach(l -> l.accept(w));}
69 private void callWireRemovedListeners (GUIWire w ) {wireRemovedListeners .forEach(l -> l.accept(w));}