package era.mi.gui.model;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class ViewModel
{
private final List<GUIComponent> components;
+ private final List<GUIComponent> componentsUnmodifiable;
private final List<GUIWire> wires;
+ private final List<GUIWire> wiresUnmodifiable;
- private final List<Consumer<GUIComponent>> componentAddedListeners;
- private final List<Consumer<GUIComponent>> componentRemovedListeners;
- private final List<Consumer<GUIWire>> wireAddedListeners;
- private final List<Consumer<GUIWire>> wireRemovedListeners;
+ private final List<Consumer<? super GUIComponent>> componentAddedListeners;
+ private final List<Consumer<? super GUIComponent>> componentRemovedListeners;
+ private final List<Consumer<? super GUIWire>> wireAddedListeners;
+ private final List<Consumer<? super GUIWire>> wireRemovedListeners;
public ViewModel()
{
components = new ArrayList<>();
+ componentsUnmodifiable = Collections.unmodifiableList(components);
wires = new ArrayList<>();
+ wiresUnmodifiable = Collections.unmodifiableList(wires);
componentAddedListeners = new ArrayList<>();
componentRemovedListeners = new ArrayList<>();
* Adds the given component to the list of components and calls all componentAddedListeners. Don't call this method from application
* code as it is automatically called in GUIComponent::new.
*/
- public void addComponent(GUIComponent component)
+ public void componentCreated(GUIComponent component)
{
if (components.contains(component))
throw new IllegalStateException("Don't add the same component twice!");
* Removes the given component from the list of components and calls all componentRemovedListeners. Don't call this method from
* application code as it is automatically called in GUIComponent::destroy.
*/
- public void removeComponent(GUIComponent component)
+ public void componentDestroyed(GUIComponent component)
{
if (!components.contains(component))
throw new IllegalStateException("Don't remove the same component twice!");
callComponentRemovedListeners(component);
}
+ /**
+ * Adds the given component to the list of components and calls all componentAddedListeners. Don't call this method from application
+ * code as it is automatically called in GUIComponent::new.
+ */
+ public void wireCreated(GUIWire wire)
+ {
+ if (wires.contains(wire))
+ throw new IllegalStateException("Don't add the same wire twice!");
+ wires.add(wire);
+ callWireAddedListeners(wire);
+ }
+
+ /**
+ * Removes the given component from the list of components and calls all componentRemovedListeners. Don't call this method from
+ * application code as it is automatically called in GUIComponent::destroy.
+ */
+ public void wireDestroyed(GUIWire wire)
+ {
+ if (!wires.contains(wire))
+ throw new IllegalStateException("Don't remove the same wire twice!");
+ wires.remove(wire);
+ callWireRemovedListeners(wire);
+ }
+
+ public List<GUIComponent> getComponents()
+ {
+ return componentsUnmodifiable;
+ }
+
+ public List<GUIWire> getWires()
+ {
+ return wiresUnmodifiable;
+ }
+
// @formatter:off
- public void addComponentAddedListener (Consumer<GUIComponent> listener){componentAddedListeners .add (listener);}
- public void addComponentRemovedListener (Consumer<GUIComponent> listener){componentRemovedListeners.add (listener);}
- public void addWireAddedListener (Consumer<GUIWire > listener){wireAddedListeners .add (listener);}
- public void addWireRemovedListener (Consumer<GUIWire > listener){wireRemovedListeners .add (listener);}
+ public void addComponentAddedListener (Consumer<? super GUIComponent> listener){componentAddedListeners .add (listener);}
+ public void addComponentRemovedListener (Consumer<? super GUIComponent> listener){componentRemovedListeners.add (listener);}
+ public void addWireAddedListener (Consumer<? super GUIWire > listener){wireAddedListeners .add (listener);}
+ public void addWireRemovedListener (Consumer<? super GUIWire > listener){wireRemovedListeners .add (listener);}
- public void removeComponentAddedListener (Consumer<GUIComponent> listener){componentAddedListeners .remove(listener);}
- public void removeComponentRemovedListener(Consumer<GUIComponent> listener){componentRemovedListeners.remove(listener);}
- public void removeWireAddedListener (Consumer<GUIWire > listener){wireAddedListeners .remove(listener);}
- public void removeWireRemovedListener (Consumer<GUIWire > listener){wireRemovedListeners .remove(listener);}
+ public void removeComponentAddedListener (Consumer<? super GUIComponent> listener){componentAddedListeners .remove(listener);}
+ public void removeComponentRemovedListener(Consumer<? super GUIComponent> listener){componentRemovedListeners.remove(listener);}
+ public void removeWireAddedListener (Consumer<? super GUIWire > listener){wireAddedListeners .remove(listener);}
+ public void removeWireRemovedListener (Consumer<? super GUIWire > listener){wireRemovedListeners .remove(listener);}
private void callComponentAddedListeners (GUIComponent c) {componentAddedListeners .forEach(l -> l.accept(c));}
private void callComponentRemovedListeners(GUIComponent c) {componentRemovedListeners.forEach(l -> l.accept(c));}