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;
private final Runnable redrawListenerForSubcomponents;
- public ViewModel()
+ 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.
*/
- public void componentCreated(GUIComponent component)
+ 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()}.
*/
- public void componentDestroyed(GUIComponent component)
+ 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 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.
+ * 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 {@link GUIWire}'s constructor(s).
*/
- public void wireCreated(GUIWire wire)
+ protected void wireCreated(GUIWire wire)
{
if (wires.contains(wire))
throw new IllegalStateException("Don't add the same wire 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.
+ * 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 {@link GUIWire#destroy()}.
*/
- public void wireDestroyed(GUIWire wire)
+ protected void wireDestroyed(GUIWire wire)
{
if (!wires.contains(wire))
throw new IllegalStateException("Don't remove the same wire twice!");
callRedrawListeners();
}
- public List<GUIComponent> getComponents()
+ public Map<String, GUIComponent> getComponentsByName()
{
return componentsUnmodifiable;
}
return wiresUnmodifiable;
}
-// public void requestRedraw()
-// {
-// callRedrawListeners();
-// }
-
// @formatter:off
public void addComponentAddedListener (Consumer<? super GUIComponent> listener) {componentAddedListeners .add (listener);}
public void addComponentRemovedListener (Consumer<? super GUIComponent> listener) {componentRemovedListeners.add (listener);}