Xcos: save and load the DEBUG_LEVEL
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / writer / CustomWriter.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2015-2015 - Scilab Enterprises - Clement DAVID
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12 package org.scilab.modules.xcos.io.writer;
13
14 import java.util.UUID;
15
16 import javax.xml.stream.XMLStreamException;
17
18 import org.scilab.modules.xcos.Kind;
19 import org.scilab.modules.xcos.ObjectProperties;
20 import org.scilab.modules.xcos.VectorOfDouble;
21 import org.scilab.modules.xcos.VectorOfInt;
22 import org.scilab.modules.xcos.VectorOfScicosID;
23 import org.scilab.modules.xcos.Xcos;
24 import org.scilab.modules.xcos.graph.ScicosParameters;
25 import org.scilab.modules.xcos.io.HandledElement;
26
27 public class CustomWriter extends ScilabWriter {
28     private static final String SEP = " - ";
29
30     public CustomWriter(XcosWriter writer) {
31         super(writer);
32     }
33
34     @Override
35     public void write(long uid, Kind kind) throws XMLStreamException {
36         String[] str = new String[1];
37         int[] integer = new int[1];
38         VectorOfDouble vDouble = new VectorOfDouble();
39         VectorOfScicosID children = new VectorOfScicosID();
40
41         switch (kind) {
42             case DIAGRAM:
43                 shared.stream.writeStartDocument();
44                 shared.stream.writeStartElement(HandledElement.XcosDiagram.name());
45
46                 /*
47                  * Write diagram content
48                  */
49
50                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.PATH, str);
51                 shared.stream.writeAttribute("savedFile", str[0]);
52                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.DEBUG_LEVEL, integer);
53                 shared.stream.writeAttribute("debugLevel", Integer.toString(integer[0]));
54
55                 // write simulation properties
56                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.PROPERTIES, vDouble);
57                 String prop;
58
59                 prop = Double.toString(vDouble.get(ScicosParameters.FINAL_INTEGRATION_TIME));
60                 shared.stream.writeAttribute("finalIntegrationTime", prop);
61                 prop = Double.toString(vDouble.get(ScicosParameters.INTEGRATOR_ABSOLUTE_TOLERANCE));
62                 shared.stream.writeAttribute("integratorAbsoluteTolerance", prop);
63                 prop = Double.toString(vDouble.get(ScicosParameters.INTEGRATOR_RELATIVE_TOLERANCE));
64                 shared.stream.writeAttribute("integratorRelativeTolerance", prop);
65                 prop = Double.toString(vDouble.get(ScicosParameters.TOLERANCE_ON_TIME));
66                 shared.stream.writeAttribute("toleranceOnTime", prop);
67                 prop = Double.toString(vDouble.get(ScicosParameters.MAX_INTEGRATION_TIME_INTERVAL));
68                 shared.stream.writeAttribute("maxIntegrationTimeInterval", prop);
69                 prop = Double.toString(vDouble.get(ScicosParameters.MAXIMUM_STEP_SIZE));
70                 shared.stream.writeAttribute("maximumStepSize", prop);
71                 prop = Double.toString(vDouble.get(ScicosParameters.REAL_TIME_SCALING));
72                 shared.stream.writeAttribute("realTimeScaling", prop);
73                 prop = Double.toString(vDouble.get(ScicosParameters.SOLVER));
74                 shared.stream.writeAttribute("solver", prop);
75
76                 // write the graphical part and children
77                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.CHILDREN, children);
78                 writeDiagramAndSuperDiagramContent(uid, kind, children);
79
80                 shared.stream.writeEndElement();
81                 shared.stream.writeEndDocument();
82
83                 // post processing for binary data handling
84                 shared.rawDataWriter.fillSharedDictionary();
85                 break;
86             case BLOCK:
87                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.CHILDREN, children);
88                 if (children.size() > 0) {
89                     shared.stream.writeStartElement(HandledElement.SuperBlockDiagram.name());
90                     writeDiagramAndSuperDiagramContent(uid, kind, children);
91                     shared.stream.writeEndElement(); // SuperBlockDiagram
92                 }
93                 break;
94             case PORT:
95                 // FIXME encode orientation from the model or not?
96                 break;
97             default:
98                 break;
99         }
100     }
101
102     private void writeDiagramAndSuperDiagramContent(long uid, Kind kind, VectorOfScicosID children) throws XMLStreamException {
103
104         VectorOfInt colors = new VectorOfInt();
105         shared.controller.getObjectProperty(uid, kind, ObjectProperties.COLOR, colors);
106         shared.stream.writeAttribute("background", Integer.toString(colors.get(0)));
107         shared.stream.writeAttribute("gridEnabled", Integer.toString(colors.get(1)));
108
109         String[] str = new String[1];
110         shared.controller.getObjectProperty(uid, kind, ObjectProperties.TITLE, str);
111         shared.stream.writeAttribute("title", str[0]);
112
113         /*
114          * Add the legacy version comment
115          */
116         final Package p = Package.getPackage("org.scilab.modules.xcos");
117         StringBuilder comment = new StringBuilder().append(Xcos.TRADENAME).append(SEP).append(Xcos.VERSION).append(SEP)
118         .append(p.getSpecificationVersion()).append(SEP).append(p.getImplementationVersion());
119         shared.stream.writeComment(comment.toString());
120
121         /*
122          * encode some content then the children
123          */
124         shared.rawDataWriter.write(uid, kind);
125
126         UUID root = UUID.randomUUID();
127         UUID layer = UUID.randomUUID();
128         shared.layers.push(layer.toString());
129
130         // children header
131         shared.stream.writeStartElement("mxGraphModel");
132         shared.stream.writeAttribute("as", "model");
133         shared.stream.writeStartElement("root");
134         shared.stream.writeEmptyElement("mxCell");
135         shared.stream.writeAttribute("id", root.toString());
136         shared.stream.writeEmptyElement("mxCell");
137         shared.stream.writeAttribute("id", shared.layers.peek());
138         shared.stream.writeAttribute("parent", root.toString());
139
140         // loop on all children, encode the blocks and ports then the links
141         VectorOfInt kinds = new VectorOfInt();
142         shared.controller.sortAndFillKind(children, kinds);
143
144         final int len = children.size();
145         for (int i = 0; i < len; i++) {
146             long child = children.get(i);
147             Kind childKind = Kind.values()[kinds.get(i)];
148             shared.write(child, childKind);
149         }
150
151         shared.layers.pop();
152         shared.stream.writeEndElement(); // root
153         shared.stream.writeEndElement(); // mxGraphModel
154     }
155 }