import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.function.Consumer;
import net.mograsim.logic.ui.model.components.GUIComponent;
public class ViewModel
{
- private final List<GUIComponent> components;
- private final List<GUIComponent> componentsUnmodifiable;
+ private final Map<String, GUIComponent> components;
+ private final Map<String, GUIComponent> componentsUnmodifiable;
private final List<GUIWire> wires;
private final List<GUIWire> wiresUnmodifiable;
protected ViewModel()
{
- components = new ArrayList<>();
- componentsUnmodifiable = Collections.unmodifiableList(components);
+ components = new HashMap<>();
+ componentsUnmodifiable = Collections.unmodifiableMap(components);
wires = new ArrayList<>();
wiresUnmodifiable = Collections.unmodifiableList(wires);
/**
* 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.
+ * code as it is automatically called in {@link GUIComponent}'s constructor.
*/
protected void componentCreated(GUIComponent component)
{
- if (components.contains(component))
+ if (components.containsKey(component.name))
throw new IllegalStateException("Don't add the same component twice!");
- components.add(component);
+ components.put(component.name, component);
callComponentAddedListeners(component);
component.addRedrawListener(redrawListenerForSubcomponents);
callRedrawListeners();
/**
* 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.
+ * application code as it is automatically called in {@link GUIComponent#destroy()}.
*/
protected void componentDestroyed(GUIComponent component)
{
- if (!components.contains(component))
+ if (!components.containsKey(component.name))
throw new IllegalStateException("Don't remove the same component twice!");
- components.remove(component);
+ components.remove(component.name);
callComponentRemovedListeners(component);
component.removeRedrawListener(redrawListenerForSubcomponents);
callRedrawListeners();
/**
* Adds the given wire to the list of wires and calls all wireAddedListeners. Don't call this method from application code as it is
- * automatically called in GUIWire::new.
+ * automatically called in {@link GUIWire}'s constructor(s).
*/
protected void wireCreated(GUIWire wire)
{
/**
* Removes the given wire from the list of wires and calls all wireRemovedListeners. Don't call this method from application code as it
- * is automatically called in GUIWire::destroy.
+ * is automatically called in {@link GUIWire#destroy()}.
*/
protected void wireDestroyed(GUIWire wire)
{
callRedrawListeners();
}
- public List<GUIComponent> getComponents()
+ public Map<String, GUIComponent> getComponentsByName()
{
return componentsUnmodifiable;
}