X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fserializing%2FViewModelSerializer.java;h=443fe050e3e9b3c116048c6ca7623a79dc33edf5;hb=93b398d6271a538a2a4c9f4de07a3b4a8a2a7fd4;hp=ffd18c94c0208635d2650541488fdf39dc84ce1f;hpb=853d979edee5d49d3b3c5fe08609f6cfd82d863f;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ViewModelSerializer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ViewModelSerializer.java index ffd18c94..443fe050 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ViewModelSerializer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ViewModelSerializer.java @@ -1,7 +1,9 @@ package net.mograsim.logic.model.serializing; import java.io.IOException; +import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -12,9 +14,9 @@ import com.google.gson.JsonElement; import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.mograsim.logic.model.model.ViewModel; import net.mograsim.logic.model.model.ViewModelModifiable; -import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.model.components.ModelComponent; import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; -import net.mograsim.logic.model.model.wires.GUIWire; +import net.mograsim.logic.model.model.wires.ModelWire; import net.mograsim.logic.model.serializing.ViewModelParams.ComponentParams; import net.mograsim.logic.model.serializing.ViewModelParams.WireParams; import net.mograsim.logic.model.serializing.ViewModelParams.WireParams.PinParams; @@ -76,9 +78,9 @@ public class ViewModelSerializer * * @author Daniel Kirschten */ - public static void serialize(ViewModel model, IdentifierGetter idGetter, String targetPath) throws IOException + public static void serialize(ViewModel model, IdentifyParams idParams, String targetPath) throws IOException { - JsonHandler.writeJson(serialize(model, idGetter), targetPath); + JsonHandler.writeJson(serialize(model, idParams), targetPath); } /** @@ -89,7 +91,7 @@ public class ViewModelSerializer */ public static ViewModelParams serialize(ViewModel model) { - return serialize(model, new IdentifierGetter()); + return serialize(model, new IdentifyParams()); } // "core" methods @@ -99,22 +101,22 @@ public class ViewModelSerializer * @author Fabian Stemmler * @author Daniel Kirschten */ - @SuppressWarnings("unused") // for GUIWire being created + @SuppressWarnings("unused") // for ModelWire being created public static void deserialize(ViewModelModifiable model, ViewModelParams params) { - Map componentsByName = model.getComponentsByName(); - GUIComponent[] components = new GUIComponent[params.components.length]; + Map componentsByName = model.getComponentsByName(); + ModelComponent[] components = new ModelComponent[params.components.length]; for (int i = 0; i < components.length; i++) { ComponentParams compParams = params.components[i]; - components[i] = IndirectGUIComponentCreator.createComponent(model, compParams.id, compParams.params, compParams.name); + components[i] = IndirectModelComponentCreator.createComponent(model, compParams.id, compParams.params, compParams.name); components[i].moveTo(compParams.pos.x, compParams.pos.y); } for (int i = 0; i < params.wires.length; i++) { WireParams wire = params.wires[i]; - new GUIWire(model, wire.name, componentsByName.get(wire.pin1.compName).getPin(wire.pin1.pinName), + new ModelWire(model, wire.name, componentsByName.get(wire.pin1.compName).getPin(wire.pin1.pinName), componentsByName.get(wire.pin2.compName).getPin(wire.pin2.pinName), wire.path); } } @@ -129,20 +131,20 @@ public class ViewModelSerializer * {@link DeserializedSubmodelComponent#paramsForSerializingOverride paramsForSerializingOverride} are written.
* If this case doesn't apply (e.g. if the component is not a SubmodelComponent; or it is a SubmodelComponent, * but hasn't been deserialized; or it has no {@link DeserializedSubmodelComponent#idForSerializingOverride idForSerializingOverride} - * set), the ID defined by idGetter and the params obtained by {@link GUIComponent#getParamsForSerializing() getParams()} + * set), the ID defined by idGetter and the params obtained by {@link ModelComponent#getParamsForSerializing() getParams()} * are written. * * @author Fabian Stemmler * @author Daniel Kirschten */ - public static ViewModelParams serialize(ViewModel model, IdentifierGetter idGetter) + public static ViewModelParams serialize(ViewModel model, IdentifyParams idParams) { ViewModelParams modelParams = new ViewModelParams(CURRENT_JSON_VERSION); - Map components = new HashMap<>(model.getComponentsByName()); + Map components = new HashMap<>(model.getComponentsByName()); components.remove(SubmodelComponent.SUBMODEL_INTERFACE_NAME); Set componentsParams = new HashSet<>(); - for (GUIComponent component : components.values()) + for (ModelComponent component : components.values()) { ComponentParams compParams = new ComponentParams(); componentsParams.add(compParams); @@ -155,16 +157,17 @@ public class ViewModelSerializer compParams.params = innerCompCasted.paramsForSerializingOverride; } else { - compParams.id = idGetter.componentIDs.apply(component); - compParams.params = component.getParamsForSerializing(idGetter); + compParams.id = component.getIDForSerializing(idParams); + compParams.params = component.getParamsForSerializingJSON(idParams); } compParams.name = component.name; } modelParams.components = componentsParams.toArray(ComponentParams[]::new); + Arrays.sort(modelParams.components, Comparator.comparing(c -> c.name)); - Collection wires = model.getWiresByName().values(); + Collection wires = model.getWiresByName().values(); Set wiresParams = new HashSet<>(); - for (GUIWire innerWire : wires) + for (ModelWire innerWire : wires) { WireParams innerWireParams = new WireParams(); wiresParams.add(innerWireParams); @@ -180,6 +183,7 @@ public class ViewModelSerializer innerWireParams.path = innerWire.getPath(); } modelParams.wires = wiresParams.toArray(WireParams[]::new); + Arrays.sort(modelParams.wires, Comparator.comparing(c -> c.name)); return modelParams; }