1 package net.mograsim.logic.model.model.components;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import java.util.function.Consumer;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.wires.Pin;
14 import net.mograsim.logic.model.serializing.IdentifyParams;
15 import net.mograsim.logic.model.serializing.JSONSerializable;
16 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
17 import net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler;
20 * The base class for all model components.<br>
21 * A <code>ModelComponent</code> has a reference to the LogicModel it belongs to.<br>
22 * A <code>ModelComponent</code> has a name. This name is unique in the model the <code>ModelComponent</code> belongs to.<br>
23 * A <code>ModelComponent</code> has a position and size. The size can only be modified by subclasses.
25 * @author Daniel Kirschten
27 public abstract class ModelComponent implements JSONSerializable
30 * The model this component is a part of.
32 protected final LogicModelModifiable model;
34 * The name of this component. Is unique for all components in its model.<br>
35 * Does never change, but can't be final since it is set in {@link #init()}.
38 private final Rectangle bounds;
40 * The list of all pins of this component by name.
42 private final Map<String, Pin> pinsByName;
44 * An unmodifiable view of {@link #pinsByName}.
46 protected final Map<String, Pin> pinsUnmodifiable;
48 private final List<Consumer<? super ModelComponent>> componentMovedListeners;
49 private final List<Consumer<? super ModelComponent>> componentResizedListeners;
50 private final List<Consumer<? super Pin>> pinAddedListeners;
51 private final List<Consumer<? super Pin>> pinRemovedListeners;
53 private HighLevelStateHandler highLevelStateHandler;
55 // creation and destruction
57 public ModelComponent(LogicModelModifiable model, String name)
59 this(model, name, true);
63 * Creates a new {@link ModelComponent} and, if <code>callInit</code>, initializes the component (See {@link #init()}).<br>
64 * If <code>callInit==false</code>, make sure to call {@link #init()}!
66 * @author Daniel Kirschten
68 protected ModelComponent(LogicModelModifiable model, String name, boolean callInit)
72 this.bounds = new Rectangle(0, 0, 0, 0);
73 this.pinsByName = new HashMap<>();
74 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
76 this.componentMovedListeners = new ArrayList<>();
77 this.componentResizedListeners = new ArrayList<>();
78 this.pinAddedListeners = new ArrayList<>();
79 this.pinRemovedListeners = new ArrayList<>();
81 this.highLevelStateHandler = new DefaultHighLevelStateHandler();
88 * Initializes this component. This method should be called exactly once in this component's constructor.<br>
90 * <li>If <code>{@link #name}==null</code>, sets {@link #name} to {@link LogicModelModifiable#getDefaultComponentName(ModelComponent)}.
91 * <li>Registers this component in the model.
97 name = model.getDefaultComponentName(this);
98 model.componentCreated(this, this::destroyed);
103 public String getName()
109 * Destroys this component. This method is called from {@link LogicModelModifiable#componentDestroyed(ModelComponent)
110 * destroyComponent()} of the model this component is a part of.<br>
111 * When overriding, make sure to also call the original implementation.
113 * @author Daniel Kirschten
115 protected void destroyed()
117 pinsByName.values().forEach(this::removePinWithoutRedraw);
123 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
125 * @throws IllegalArgumentException if the pin doesn't belong to this component
126 * @throws IllegalArgumentException if there already is a pin with the given name
128 * @author Daniel Kirschten
130 protected void addPin(Pin pin)
132 if (pin.component != this)
133 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
134 if (pinsByName.containsKey(pin.name))
135 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
136 pinsByName.put(pin.name, pin);
137 callPinAddedListeners(pin);
138 model.requestRedraw();
142 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
144 * @throws NullPointerException if there was no pin with this name
146 * @author Daniel Kirschten
148 protected void removePin(String name)
150 removePinWithoutRedraw(pinsByName.remove(name));
151 model.requestRedraw();
154 private void removePinWithoutRedraw(Pin pin)
157 callPinRemovedListeners(pin);
161 * Returns a collection of pins of this component.
163 * @author Daniel Kirschten
165 public Map<String, Pin> getPins()
167 return pinsUnmodifiable;
171 * Returns the pin with the given name of this component.
173 * @throws IllegalArgumentException if there is no pin with the given name
174 * @see #getPinOrNull(String)
176 * @author Daniel Kirschten
178 public Pin getPin(String name)
180 Pin pin = getPinOrNull(name);
182 throw new IllegalArgumentException("No pin with the name " + name);
187 * Returns the pin with the given name of this component, or <code>null</code> if there is no such pin.
189 * @see #getPin(String)
191 * @author Daniel Kirschten
193 public Pin getPinOrNull(String name)
195 return pinsByName.get(name);
201 * @author Daniel Kirschten
203 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
205 this.highLevelStateHandler = highLevelStateHandler;
208 public HighLevelStateHandler getHighLevelStateHandler()
210 return highLevelStateHandler;
214 * Gets the current value of the given high-level state. <br>
215 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
217 * @see #setHighLevelState(String, Object)
218 * @see HighLevelStateHandler#get(String)
220 * @author Daniel Kirschten
222 public final Object getHighLevelState(String stateID)
224 return highLevelStateHandler.get(stateID);
228 * Sets the given high-level state to the given value. <br>
229 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
231 * @see #getHighLevelState(String)
232 * @see HighLevelStateHandler#set(String, Object)
234 * @author Daniel Kirschten
236 public final void setHighLevelState(String stateID, Object newState)
238 highLevelStateHandler.set(stateID, newState);
241 public final void addHighLevelStateListener(String stateID, Consumer<Object> stateChanged)
243 highLevelStateHandler.addListener(stateID, stateChanged);
246 public final void removeHighLevelStateListener(String stateID, Consumer<Object> stateChanged)
248 highLevelStateHandler.removeListener(stateID, stateChanged);
251 // "graphical" operations
254 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
256 * @author Daniel Kirschten
258 public void moveTo(double x, double y)
262 callComponentMovedListeners();
263 model.requestRedraw();
267 * Sets the size of this component and calls redrawListeners.
269 * @author Daniel Kirschten
271 protected void setSize(double width, double height)
273 bounds.width = width;
274 bounds.height = height;
275 callComponentResizedListener();
276 model.requestRedraw();
280 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
281 * {@link #getHeight}, because new objects are created.
283 * @author Daniel Kirschten
285 public final Rectangle getBounds()
287 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
291 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
293 * @author Daniel Kirschten
295 public double getPosX()
301 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
303 * @author Daniel Kirschten
305 public double getPosY()
311 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
313 * @author Daniel Kirschten
315 public double getWidth()
321 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
323 * @author Daniel Kirschten
325 public double getHeight()
327 return bounds.height;
331 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
334 * @author Daniel Kirschten
336 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
337 public boolean clicked(double x, double y)
343 * Render this component to the given gc, in absoulute coordinates.
345 * @author Daniel Kirschten
347 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
352 public Object getParamsForSerializing(IdentifyParams idParams)
360 public void addComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .add (listener);}
361 public void addComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.add (listener);}
362 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
363 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
365 public void removeComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .remove(listener);}
366 public void removeComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.remove(listener);}
367 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
368 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
370 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
371 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
372 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
373 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}