Started restructuring LogicUI
[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.List;
5 import java.util.function.Consumer;
6
7 import era.mi.gui.model.components.GUIComponent;
8 import era.mi.gui.model.wires.GUIWire;
9
10 public class ViewModel
11 {
12         private final List<GUIComponent> components;
13         private final List<GUIWire> wires;
14
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;
19
20         public ViewModel()
21         {
22                 components = new ArrayList<>();
23                 wires = new ArrayList<>();
24
25                 componentAddedListeners = new ArrayList<>();
26                 componentRemovedListeners = new ArrayList<>();
27                 wireAddedListeners = new ArrayList<>();
28                 wireRemovedListeners = new ArrayList<>();
29         }
30
31         /**
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.
34          */
35         public void addComponent(GUIComponent component)
36         {
37                 if (components.contains(component))
38                         throw new IllegalStateException("Don't add the same component twice!");
39                 components.add(component);
40                 componentAddedListeners.forEach(l -> l.accept(component));
41         }
42
43         /**
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.
46          */
47         public void removeComponent(GUIComponent component)
48         {
49                 if (!components.contains(component))
50                         throw new IllegalStateException("Don't remove the same component twice!");
51                 components.remove(component);
52                 componentRemovedListeners.forEach(l -> l.accept(component));
53         }
54
55         // @formatter:off
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);}
60
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);}
65         // @formatter:on
66 }