Serializing now serializes everything; among many other things:
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / GUIComponent.java
1 package net.mograsim.logic.model.model.components;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.function.Consumer;
9
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
12
13 import net.haspamelodica.swt.helper.gcs.GeneralGC;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.JSONSerializable;
19 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
20
21 /**
22  * The base class for all GUI components.<br>
23  * A <code>GUIComponent</code> has a reference to the ViewModel it belongs to.<br>
24  * A <code>GUIComponent</code> has a name. This name is unique in the model the <code>GUIComponent</code> belongs to.<br>
25  * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.
26  * 
27  * @author Daniel Kirschten
28  */
29 public abstract class GUIComponent implements JSONSerializable
30 {
31         /**
32          * The model this component is a part of.
33          */
34         protected final ViewModelModifiable model;
35         /**
36          * The name of this component. Is unique for all components in its model.
37          */
38         public final String name;
39         private final Rectangle bounds;
40         /**
41          * The list of all pins of this component by name.
42          */
43         private final Map<String, Pin> pinsByName;
44         /**
45          * An unmodifiable view of {@link #pinsByName}.
46          */
47         protected final Map<String, Pin> pinsUnmodifiable;
48
49         private final List<Consumer<? super GUIComponent>> componentMovedListeners;
50         private final List<Consumer<? super GUIComponent>> componentResizedListeners;
51         private final List<Consumer<? super Pin>> pinAddedListeners;
52         private final List<Consumer<? super Pin>> pinRemovedListeners;
53         private final List<Runnable> redrawListeners;
54
55         private final Runnable redrawListenerForSubcomponents;
56
57         private HighLevelStateHandler highLevelStateHandler;
58
59         // creation and destruction
60
61         public GUIComponent(ViewModelModifiable model, String name)
62         {
63                 this.model = model;
64                 this.name = name == null ? model.getDefaultComponentName(this) : name;
65                 this.bounds = new Rectangle(0, 0, 0, 0);
66                 this.pinsByName = new HashMap<>();
67                 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
68
69                 this.componentMovedListeners = new ArrayList<>();
70                 this.componentResizedListeners = new ArrayList<>();
71                 this.pinAddedListeners = new ArrayList<>();
72                 this.pinRemovedListeners = new ArrayList<>();
73                 this.redrawListeners = new ArrayList<>();
74
75                 redrawListenerForSubcomponents = this::requestRedraw;
76
77                 model.componentCreated(this);
78         }
79
80         /**
81          * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
82          * componentDestroyed()} for the model this component is a part of.
83          * 
84          * @author Daniel Kirschten
85          */
86         public void destroy()
87         {
88                 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
89                 model.componentDestroyed(this);
90         }
91
92         // pins
93
94         /**
95          * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
96          * 
97          * @throws IllegalArgumentException if the pin doesn't belong to this component
98          * @throws IllegalArgumentException if there already is a pin with the given name
99          * 
100          * @author Daniel Kirschten
101          */
102         protected void addPin(Pin pin)
103         {
104                 if (pin.component != this)
105                         throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
106                 if (pinsByName.containsKey(pin.name))
107                         throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
108                 pinsByName.put(pin.name, pin);
109                 callPinAddedListeners(pin);
110                 pin.addRedrawListener(redrawListenerForSubcomponents);
111                 requestRedraw();
112         }
113
114         /**
115          * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
116          * 
117          * @throws NullPointerException if there was no pin with this name
118          * 
119          * @author Daniel Kirschten
120          */
121         protected void removePin(String name)
122         {
123                 Pin pin = pinsByName.remove(name);
124                 callPinRemovedListeners(pin);
125                 pin.removeRedrawListener(redrawListenerForSubcomponents);
126                 requestRedraw();
127         }
128
129         /**
130          * Returns a collection of pins of this component.
131          * 
132          * @author Daniel Kirschten
133          */
134         public Map<String, Pin> getPins()
135         {
136                 return pinsUnmodifiable;
137         }
138
139         /**
140          * Returns the pin with the given name of this component.
141          * 
142          * @throws IllegalArgumentException if there is no pin with the given name
143          * 
144          * @author Daniel Kirschten
145          */
146         public Pin getPin(String name)
147         {
148                 Pin pin = pinsByName.get(name);
149                 if (pin == null)
150                         throw new IllegalArgumentException("No pin with the name " + name);
151                 return pin;
152         }
153
154         // high-level access
155
156         /**
157          * @author Daniel Kirschten
158          */
159         protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
160         {
161                 this.highLevelStateHandler = highLevelStateHandler;
162         }
163
164         public HighLevelStateHandler getHighLevelStateHandler()
165         {
166                 return highLevelStateHandler;
167         }
168
169         /**
170          * Gets the current value of the given high-level state. <br>
171          * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
172          * 
173          * @see #setHighLevelState(String, Object)
174          * @see HighLevelStateHandler#getHighLevelState(String)
175          * 
176          * @author Daniel Kirschten
177          */
178         public Object getHighLevelState(String stateID)
179         {
180                 return highLevelStateHandler.getHighLevelState(stateID);
181         }
182
183         /**
184          * Sets the given high-level state to the given value. <br>
185          * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
186          * 
187          * @see #getHighLevelState(String)
188          * @see HighLevelStateHandler#setHighLevelState(String, Object)
189          * 
190          * @author Daniel Kirschten
191          */
192         public void setHighLevelState(String stateID, Object newState)
193         {
194                 highLevelStateHandler.setHighLevelState(stateID, newState);
195         }
196
197         // "graphical" operations
198
199         /**
200          * Sets the position of this component and calls componentMovedListeners and redrawListeners.
201          * 
202          * @author Daniel Kirschten
203          */
204         public void moveTo(double x, double y)
205         {
206                 bounds.x = x;
207                 bounds.y = y;
208                 callComponentMovedListeners();
209                 requestRedraw();
210         }
211
212         /**
213          * Sets the size of this component and calls redrawListeners.
214          * 
215          * @author Daniel Kirschten
216          */
217         protected void setSize(double width, double height)
218         {
219                 bounds.width = width;
220                 bounds.height = height;
221                 callComponentResizedListener();
222                 requestRedraw();
223         }
224
225         /**
226          * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
227          * {@link #getHeight}, because new objects are created.
228          * 
229          * @author Daniel Kirschten
230          */
231         public final Rectangle getBounds()
232         {
233                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
234         }
235
236         /**
237          * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
238          * 
239          * @author Daniel Kirschten
240          */
241         public double getPosX()
242         {
243                 return bounds.x;
244         }
245
246         /**
247          * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
248          * 
249          * @author Daniel Kirschten
250          */
251         public double getPosY()
252         {
253                 return bounds.y;
254         }
255
256         /**
257          * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
258          * 
259          * @author Daniel Kirschten
260          */
261         public double getWidth()
262         {
263                 return bounds.width;
264         }
265
266         /**
267          * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
268          * 
269          * @author Daniel Kirschten
270          */
271         public double getHeight()
272         {
273                 return bounds.height;
274         }
275
276         /**
277          * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
278          * click.
279          * 
280          * @author Daniel Kirschten
281          */
282         @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
283         public boolean clicked(double x, double y)
284         {
285                 return false;
286         }
287
288         /**
289          * Render this component to the given gc, in absoulute coordinates.
290          * 
291          * @author Daniel Kirschten
292          */
293         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
294
295         // serializing
296
297         @Override
298         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
299         {
300                 return JsonNull.INSTANCE;
301         }
302
303         // listeners
304
305         /**
306          * Calls redraw listeners.
307          * 
308          * @author Daniel Kirschten
309          */
310         protected void requestRedraw()
311         {
312                 callRedrawListeners();
313         }
314
315         // @formatter:off
316         public void addComponentMovedListener      (Consumer<? super GUIComponent> listener) {componentMovedListeners  .add   (listener);}
317         public void addComponentResizedListener    (Consumer<? super GUIComponent> listener) {componentResizedListeners.add   (listener);}
318         public void addPinAddedListener            (Consumer<? super Pin         > listener) {pinAddedListeners        .add   (listener);}
319         public void addPinRemovedListener          (Consumer<? super Pin         > listener) {pinRemovedListeners      .add   (listener);}
320         public void addRedrawListener              (Runnable                       listener) {redrawListeners          .add   (listener);}
321
322         public void removeComponentMovedListener   (Consumer<? super GUIComponent> listener) {componentMovedListeners  .remove(listener);}
323         public void removeComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.remove(listener);}
324         public void removePinAddedListener         (Consumer<? super Pin         > listener) {pinAddedListeners        .remove(listener);}
325         public void removePinRemovedListener       (Consumer<? super Pin         > listener) {pinRemovedListeners      .remove(listener);}
326         public void removeRedrawListener           (Runnable                       listener) {redrawListeners          .remove(listener);}
327
328         private void callComponentMovedListeners (     ) {componentMovedListeners  .forEach(l -> l.accept(this));}
329         private void callComponentResizedListener(     ) {componentResizedListeners.forEach(l -> l.accept(this));}
330         private void callPinAddedListeners       (Pin p) {pinAddedListeners        .forEach(l -> l.accept(p   ));}
331         private void callPinRemovedListeners     (Pin p) {pinRemovedListeners      .forEach(l -> l.accept(p   ));}
332         private void callRedrawListeners         (     ) {redrawListeners          .forEach(l -> l.run(       ));}
333         // @formatter:on
334 }