X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2Fmodel%2FViewModel.java;h=4eb81e876ab10ad53fde06fcfac58e53e05d4b8a;hb=f2886cbd57dd08b797921fc2421b41bd92915799;hp=5a41d0ea35e43bb9f12de9741ec46154fe580c0d;hpb=4a6a0a2c85c1a16112efaf7ca6d2b5fba3c9b466;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/model/ViewModel.java b/LogicUI/src/era/mi/gui/model/ViewModel.java index 5a41d0ea..4eb81e87 100644 --- a/LogicUI/src/era/mi/gui/model/ViewModel.java +++ b/LogicUI/src/era/mi/gui/model/ViewModel.java @@ -1,6 +1,7 @@ package era.mi.gui.model; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.function.Consumer; @@ -10,7 +11,9 @@ import era.mi.gui.model.wires.GUIWire; public class ViewModel { private final List components; + private final List componentsUnmodifiable; private final List wires; + private final List wiresUnmodifiable; private final List> componentAddedListeners; private final List> componentRemovedListeners; @@ -20,7 +23,9 @@ public class ViewModel public ViewModel() { components = new ArrayList<>(); + componentsUnmodifiable = Collections.unmodifiableList(components); wires = new ArrayList<>(); + wiresUnmodifiable = Collections.unmodifiableList(wires); componentAddedListeners = new ArrayList<>(); componentRemovedListeners = new ArrayList<>(); @@ -32,7 +37,7 @@ public class ViewModel * 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!"); @@ -44,7 +49,7 @@ public class ViewModel * 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!"); @@ -52,6 +57,40 @@ public class ViewModel 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 getComponents() + { + return componentsUnmodifiable; + } + + public List getWires() + { + return wiresUnmodifiable; + } + // @formatter:off public void addComponentAddedListener (Consumer listener){componentAddedListeners .add (listener);} public void addComponentRemovedListener (Consumer listener){componentRemovedListeners.add (listener);}