X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2FGUIComponent.java;h=63582600833759c73be6384a34e81786331c3dca;hb=0a04a4ed66ecebd4254541c4977599f6052c115a;hp=1cf7460f81af679671497161b7962a38bf653909;hpb=020445ceb5c54ee5ff384a7e6b529ee350575b12;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/GUIComponent.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/GUIComponent.java index 1cf7460f..63582600 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/GUIComponent.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/GUIComponent.java @@ -7,13 +7,12 @@ import java.util.List; import java.util.Map; import java.util.function.Consumer; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; - import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; import net.mograsim.logic.model.model.ViewModelModifiable; import net.mograsim.logic.model.model.wires.Pin; +import net.mograsim.logic.model.serializing.IdentifyParams; +import net.mograsim.logic.model.serializing.JSONSerializable; import net.mograsim.logic.model.snippets.HighLevelStateHandler; /** @@ -24,7 +23,7 @@ import net.mograsim.logic.model.snippets.HighLevelStateHandler; * * @author Daniel Kirschten */ -public abstract class GUIComponent +public abstract class GUIComponent implements JSONSerializable { /** * The model this component is a part of. @@ -48,9 +47,6 @@ public abstract class GUIComponent private final List> componentResizedListeners; private final List> pinAddedListeners; private final List> pinRemovedListeners; - private final List redrawListeners; - - private final Runnable redrawListenerForSubcomponents; private HighLevelStateHandler highLevelStateHandler; @@ -68,23 +64,22 @@ public abstract class GUIComponent this.componentResizedListeners = new ArrayList<>(); this.pinAddedListeners = new ArrayList<>(); this.pinRemovedListeners = new ArrayList<>(); - this.redrawListeners = new ArrayList<>(); - redrawListenerForSubcomponents = this::requestRedraw; - - model.componentCreated(this); + // TODO this will crash the high level state debug shell because submodel is not yet set. + // The same problem exists in ViewModelModifiable.getDefaultComponentName; see there + model.componentCreated(this, this::destroyed); } /** - * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent) - * componentDestroyed()} for the model this component is a part of. + * Destroys this component. This method is called from {@link ViewModelModifiable#componentDestroyed(GUIComponent) destroyComponent()} + * of the model this component is a part of.
+ * When overriding, make sure to also call the original implementation. * * @author Daniel Kirschten */ - public void destroy() + protected void destroyed() { pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p))); - model.componentDestroyed(this); } // pins @@ -105,8 +100,7 @@ public abstract class GUIComponent throw new IllegalArgumentException("Duplicate pin name: " + pin.name); pinsByName.put(pin.name, pin); callPinAddedListeners(pin); - pin.addRedrawListener(redrawListenerForSubcomponents); - requestRedraw(); + model.requestRedraw(); } /** @@ -120,8 +114,7 @@ public abstract class GUIComponent { Pin pin = pinsByName.remove(name); callPinRemovedListeners(pin); - pin.removeRedrawListener(redrawListenerForSubcomponents); - requestRedraw(); + model.requestRedraw(); } /** @@ -159,6 +152,11 @@ public abstract class GUIComponent this.highLevelStateHandler = highLevelStateHandler; } + public HighLevelStateHandler getHighLevelStateHandler() + { + return highLevelStateHandler; + } + /** * Gets the current value of the given high-level state.
* See {@link HighLevelStateHandler} for an explanation of high-level state IDs. @@ -199,7 +197,7 @@ public abstract class GUIComponent bounds.x = x; bounds.y = y; callComponentMovedListeners(); - requestRedraw(); + model.requestRedraw(); } /** @@ -212,7 +210,7 @@ public abstract class GUIComponent bounds.width = width; bounds.height = height; callComponentResizedListener(); - requestRedraw(); + model.requestRedraw(); } /** @@ -287,41 +285,28 @@ public abstract class GUIComponent // serializing - @SuppressWarnings("static-method") // this method is intended to be overridden - public JsonElement getParamsForSerializing() + @Override + public Object getParamsForSerializing(IdentifyParams idParams) { - return JsonNull.INSTANCE; + return null; } // listeners - /** - * Calls redraw listeners. - * - * @author Daniel Kirschten - */ - protected void requestRedraw() - { - callRedrawListeners(); - } - // @formatter:off public void addComponentMovedListener (Consumer listener) {componentMovedListeners .add (listener);} public void addComponentResizedListener (Consumer listener) {componentResizedListeners.add (listener);} public void addPinAddedListener (Consumer listener) {pinAddedListeners .add (listener);} public void addPinRemovedListener (Consumer listener) {pinRemovedListeners .add (listener);} - public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);} public void removeComponentMovedListener (Consumer listener) {componentMovedListeners .remove(listener);} public void removeComponentResizedListener (Consumer listener) {componentResizedListeners.remove(listener);} public void removePinAddedListener (Consumer listener) {pinAddedListeners .remove(listener);} public void removePinRemovedListener (Consumer listener) {pinRemovedListeners .remove(listener);} - public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);} private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));} private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));} private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));} private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));} - private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));} // @formatter:on } \ No newline at end of file