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