mirror of
https://github.com/pfalstad/circuitjs1.git
synced 2026-03-24 01:06:57 +01:00
store original subcircuit data in xml and view it when "Load Model Circuit" is pressed
This commit is contained in:
@@ -222,7 +222,12 @@ public class CustomCompositeElm extends CompositeElm {
|
||||
return;
|
||||
}
|
||||
if (n == 2) {
|
||||
app.readCircuit(model.modelCircuit);
|
||||
if (model.modelCircuit != null)
|
||||
app.readCircuit(model.modelCircuit);
|
||||
else {
|
||||
XMLDeserializer xml = new XMLDeserializer(app);
|
||||
xml.readCircuit(model.elmDoc);
|
||||
}
|
||||
CirSim.editDialog.closeDialog();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,8 +243,6 @@ public class CustomCompositeModel implements Comparable<CustomCompositeModel> {
|
||||
Element root = doc.createElement("ccm");
|
||||
doc.appendChild(root);
|
||||
buildXmlElement(doc, root);
|
||||
if (modelCircuit != null)
|
||||
XMLSerializer.dumpAttr(root, "mc", modelCircuit);
|
||||
stor.setItem("subcircuit:" + name, doc.toString());
|
||||
} else
|
||||
stor.removeItem("subcircuit:" + name);
|
||||
@@ -258,7 +256,6 @@ public class CustomCompositeModel implements Comparable<CustomCompositeModel> {
|
||||
String modelName = xml.parseStringAttr("nm", null);
|
||||
CustomCompositeModel model = new CustomCompositeModel();
|
||||
model.name = modelName;
|
||||
model.modelCircuit = xml.parseStringAttr("mc", null);
|
||||
model.parseXmlElement(xml);
|
||||
modelMap.put(modelName, model);
|
||||
sequenceNumber++;
|
||||
@@ -399,7 +396,19 @@ public class CustomCompositeModel implements Comparable<CustomCompositeModel> {
|
||||
}
|
||||
|
||||
boolean canLoadModelCircuit() {
|
||||
return modelCircuit != null && modelCircuit.length() > 0;
|
||||
if (modelCircuit != null && modelCircuit.length() > 0)
|
||||
return true;
|
||||
if (elmDoc != null) {
|
||||
NodeList children = elmDoc.getDocumentElement().getChildNodes();
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
Node node = children.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
if (((Element) node).getAttribute("x") != null)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void remove() {
|
||||
|
||||
@@ -1383,15 +1383,15 @@ public class SimulationManager {
|
||||
}
|
||||
}
|
||||
|
||||
boolean dumpAll = true;
|
||||
|
||||
// output all the elements as XML
|
||||
for (i = 0; i != elmList.size(); i++) {
|
||||
CircuitElm ce = getElm(i);
|
||||
if (sel && !ce.isSelected())
|
||||
continue;
|
||||
// don't need these elements dumped
|
||||
if (ce instanceof WireElm || ce instanceof LabeledNodeElm || ce instanceof ScopeElm)
|
||||
continue;
|
||||
if (ce instanceof GraphicElm || ce instanceof GroundElm)
|
||||
if (!dumpAll && (ce instanceof WireElm || ce instanceof LabeledNodeElm || ce instanceof ScopeElm ||
|
||||
ce instanceof GraphicElm || ce instanceof GroundElm))
|
||||
continue;
|
||||
int j;
|
||||
// build nn (node list) string
|
||||
@@ -1406,7 +1406,8 @@ public class SimulationManager {
|
||||
Element child = elmDoc.createElement(ce.getXmlDumpType());
|
||||
XMLSerializer.dumpAttr(child, "nn", nn);
|
||||
ce.dumpXml(elmDoc, child);
|
||||
child.removeAttribute("x");
|
||||
if (!dumpAll)
|
||||
child.removeAttribute("x");
|
||||
elmRoot.appendChild(child);
|
||||
}
|
||||
|
||||
|
||||
@@ -48,12 +48,9 @@ class XMLDeserializer {
|
||||
void readCircuit(String text, int readFlags) {
|
||||
// Parse the XML
|
||||
Document doc = XMLParser.parse(text);
|
||||
|
||||
|
||||
// Get the root element
|
||||
Element root = doc.getDocumentElement();
|
||||
|
||||
// Get all child elements of root
|
||||
NodeList children = root.getChildNodes();
|
||||
|
||||
if ((readFlags & CircuitLoader.RC_RETAIN) == 0)
|
||||
app.clearCircuit();
|
||||
@@ -71,7 +68,21 @@ class XMLDeserializer {
|
||||
app.powerBar.setValue(parseIntAttr("pb", app.powerBar.getValue()));
|
||||
sim.minTimeStep = parseDoubleAttr("mts", sim.minTimeStep);
|
||||
app.setGrid();
|
||||
|
||||
|
||||
readElements(root);
|
||||
app.loader.finishReadCircuit(readFlags);
|
||||
}
|
||||
|
||||
// read elements from an already-parsed XML document (e.g. from CustomCompositeModel.elmDoc)
|
||||
void readCircuit(Document doc) {
|
||||
app.clearCircuit();
|
||||
readElements(doc.getDocumentElement());
|
||||
app.loader.finishReadCircuit(0);
|
||||
}
|
||||
|
||||
void readElements(Element root) {
|
||||
NodeList children = root.getChildNodes();
|
||||
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
Node node = children.item(i);
|
||||
if (node.getNodeType() != Node.ELEMENT_NODE)
|
||||
@@ -79,7 +90,7 @@ class XMLDeserializer {
|
||||
|
||||
Element elem = (Element) node;
|
||||
String tagName = elem.getTagName();
|
||||
|
||||
|
||||
if (tagName.equals("o")) {
|
||||
Scope sc = new Scope(app, app.sim);
|
||||
currentXmlElement = elem;
|
||||
@@ -129,8 +140,10 @@ class XMLDeserializer {
|
||||
app.adjustables.add(adj);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
String x = elem.getAttribute("x");
|
||||
if (x == null)
|
||||
continue;
|
||||
String xs[] = x.split(" ");
|
||||
int x1 = Integer.parseInt(xs[0]);
|
||||
int y1 = Integer.parseInt(xs[1]);
|
||||
@@ -148,7 +161,6 @@ class XMLDeserializer {
|
||||
elm.setPosition(x1, y1, x2, y2);
|
||||
app.elmList.add(elm);
|
||||
}
|
||||
app.loader.finishReadCircuit(readFlags);
|
||||
}
|
||||
|
||||
public double parseDoubleAttr(String attr, double def) {
|
||||
|
||||
Reference in New Issue
Block a user