4eb81e876ab10ad53fde06fcfac58e53e05d4b8a
[Mograsim.git] / LogicUI / src / era / mi / gui / model / ViewModel.java
1 package era.mi.gui.model;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.function.Consumer;
7
8 import era.mi.gui.model.components.GUIComponent;
9 import era.mi.gui.model.wires.GUIWire;
10
11 public class ViewModel
12 {
13         private final List<GUIComponent> components;
14         private final List<GUIComponent> componentsUnmodifiable;
15         private final List<GUIWire> wires;
16         private final List<GUIWire> wiresUnmodifiable;
17
18         private final List<Consumer<GUIComponent>> componentAddedListeners;
19         private final List<Consumer<GUIComponent>> componentRemovedListeners;
20         private final List<Consumer<GUIWire>> wireAddedListeners;
21         private final List<Consumer<GUIWire>> wireRemovedListeners;
22
23         public ViewModel()
24         {
25                 components = new ArrayList<>();
26                 componentsUnmodifiable = Collections.unmodifiableList(components);
27                 wires = new ArrayList<>();
28                 wiresUnmodifiable = Collections.unmodifiableList(wires);
29
30                 componentAddedListeners = new ArrayList<>();
31                 componentRemovedListeners = new ArrayList<>();
32                 wireAddedListeners = new ArrayList<>();
33                 wireRemovedListeners = new ArrayList<>();
34         }
35
36         /**
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.
39          */
40         public void componentCreated(GUIComponent component)
41         {
42                 if (components.contains(component))
43                         throw new IllegalStateException("Don't add the same component twice!");
44                 components.add(component);
45                 callComponentAddedListeners(component);
46         }
47
48         /**
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.
51          */
52         public void componentDestroyed(GUIComponent component)
53         {
54                 if (!components.contains(component))
55                         throw new IllegalStateException("Don't remove the same component twice!");
56                 components.remove(component);
57                 callComponentRemovedListeners(component);
58         }
59
60         /**
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.
63          */
64         public void wireCreated(GUIWire wire)
65         {
66                 if (wires.contains(wire))
67                         throw new IllegalStateException("Don't add the same wire twice!");
68                 wires.add(wire);
69                 callWireAddedListeners(wire);
70         }
71
72         /**
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.
75          */
76         public void wireDestroyed(GUIWire wire)
77         {
78                 if (!wires.contains(wire))
79                         throw new IllegalStateException("Don't remove the same wire twice!");
80                 wires.remove(wire);
81                 callWireRemovedListeners(wire);
82         }
83
84         public List<GUIComponent> getComponents()
85         {
86                 return componentsUnmodifiable;
87         }
88
89         public List<GUIWire> getWires()
90         {
91                 return wiresUnmodifiable;
92         }
93
94         // @formatter:off
95         public void addComponentAddedListener     (Consumer<GUIComponent> listener){componentAddedListeners  .add   (listener);}
96         public void addComponentRemovedListener   (Consumer<GUIComponent> listener){componentRemovedListeners.add   (listener);}
97         public void addWireAddedListener          (Consumer<GUIWire     > listener){wireAddedListeners       .add   (listener);}
98         public void addWireRemovedListener        (Consumer<GUIWire     > listener){wireRemovedListeners     .add   (listener);}
99
100         public void removeComponentAddedListener  (Consumer<GUIComponent> listener){componentAddedListeners  .remove(listener);}
101         public void removeComponentRemovedListener(Consumer<GUIComponent> listener){componentRemovedListeners.remove(listener);}
102         public void removeWireAddedListener       (Consumer<GUIWire     > listener){wireAddedListeners       .remove(listener);}
103         public void removeWireRemovedListener     (Consumer<GUIWire     > listener){wireRemovedListeners     .remove(listener);}
104
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));}
109         // @formatter:on
110 }