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=edc3d7b715355fd6196302c96394124da40ccf74;hb=c223a9de7b0ef783bcb4f7612da350583ca29abd;hp=5f74dadd9a411636a3f50039502b5de20379224f;hpb=b2f3d0b16783289fab229c667c18d61f84119bbd;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 5f74dadd..edc3d7b7 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,9 +1,14 @@ 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.TreeMap; import java.util.function.Consumer; +import java.util.function.Supplier; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; @@ -14,8 +19,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; @@ -23,13 +28,15 @@ public abstract class GUIComponent private final List redrawListeners; private final Runnable redrawListenerForSubcomponents; + // Defines how the GUIComponent is referenced in SubmodelComponentParams + protected Supplier identifierDelegate = () -> "class:".concat(getClass().getCanonicalName()); public GUIComponent(ViewModelModifiable model) { 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<>(); @@ -43,7 +50,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); } @@ -62,6 +69,26 @@ public abstract class GUIComponent return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height); } + public double getPosX() + { + return bounds.x; + } + + public double getPosY() + { + return bounds.y; + } + + public double getWidth() + { + return bounds.width; + } + + public double getHeight() + { + return bounds.height; + } + /** * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this * click. @@ -73,13 +100,21 @@ 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) + { + Pin pin = pinsByName.get(name); + if (pin == null) + throw new IllegalArgumentException("No pin with the name " + name); + return pin; + } + // @formatter:off public void addComponentMovedListener (Consumer listener) {componentMovedListeners.add (listener);} public void addPinAddedListener (Consumer listener) {pinAddedListeners .add (listener);} @@ -116,22 +151,33 @@ 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(); } + /** + * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams} + */ public String getIdentifier() { - return "class:".concat(getClass().getCanonicalName()); + return identifierDelegate.get(); + } + + @SuppressWarnings("static-method") + public Map getInstantiationParameters() + { + return new TreeMap<>(); } } \ No newline at end of file