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