X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fcomponents%2FGUIComponent.java;h=69b02bf91725a3782896e0bdd445e4de1d34678b;hb=19ee1a7fdb06a1df65cd7df78fed935124496707;hp=7254d8bd12c7bf172fabcfbcd9f1ea068886150d;hpb=29948edc1851d17311c50c7eff34070a0dc36ceb;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java index 7254d8bd..69b02bf9 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java @@ -1,8 +1,11 @@ package net.mograsim.logic.ui.model.components; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.function.Consumer; import java.util.function.Supplier; @@ -15,8 +18,8 @@ public abstract class GUIComponent { protected final ViewModelModifiable model; private final Rectangle bounds; - private final List pins; - protected final List pinsUnmodifiable; + private final Map pinsByName; + protected final Collection pinsUnmodifiable; private final List> componentMovedListeners; private final List> pinAddedListeners; @@ -31,8 +34,8 @@ public abstract class GUIComponent { this.model = model; this.bounds = new Rectangle(0, 0, 0, 0); - this.pins = new ArrayList<>(); - this.pinsUnmodifiable = Collections.unmodifiableList(pins); + this.pinsByName = new HashMap<>(); + this.pinsUnmodifiable = Collections.unmodifiableCollection(pinsByName.values()); this.componentMovedListeners = new ArrayList<>(); this.pinAddedListeners = new ArrayList<>(); @@ -46,7 +49,7 @@ public abstract class GUIComponent public void destroy() { - pins.forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p))); + pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p))); model.componentDestroyed(this); } @@ -76,13 +79,18 @@ public abstract class GUIComponent } /** - * Returns a list of pins of this component. + * Returns a collection of pins of this component. */ - public List getPins() + public Collection getPins() { return pinsUnmodifiable; } + public Pin getPin(String name) + { + return pinsByName.get(name); + } + // @formatter:off public void addComponentMovedListener (Consumer listener) {componentMovedListeners.add (listener);} public void addPinAddedListener (Consumer listener) {pinAddedListeners .add (listener);} @@ -119,15 +127,17 @@ public abstract class GUIComponent protected void addPin(Pin pin) { - pins.add(pin); + if (pinsByName.containsKey(pin.name)) + throw new IllegalArgumentException("Duplicate pin name: " + pin.name); + pinsByName.put(pin.name, pin); callPinAddedListeners(pin); pin.addRedrawListener(redrawListenerForSubcomponents); callRedrawListeners(); } - protected void removePin(Pin pin) + protected void removePin(String name) { - pins.remove(pin); + Pin pin = pinsByName.remove(name); callPinRemovedListeners(pin); pin.removeRedrawListener(redrawListenerForSubcomponents); callRedrawListeners();