Xcos I/O: decode more properties and store them
[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
45                 /*
46                  * Add a version comment at startup
47                  */
48
49                 final Package p = Package.getPackage("org.scilab.modules.xcos");
50                 String comment = new StringBuilder().append(Xcos.TRADENAME).append(SEP).append(Xcos.VERSION).append(SEP)
51                 .append(p.getSpecificationVersion()).append(SEP).append(p.getImplementationVersion()).toString();
52                 shared.stream.writeComment(comment);
53                 shared.stream.writeCharacters("\n");
54
55                 shared.stream.writeStartElement(HandledElement.XcosDiagram.name());
56
57                 /*
58                  * Write diagram content
59                  */
60
61                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.PATH, str);
62                 shared.stream.writeAttribute("savedFile", str[0]);
63                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.DEBUG_LEVEL, integer);
64                 shared.stream.writeAttribute("debugLevel", Integer.toString(integer[0]));
65
66                 // write simulation properties
67                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.PROPERTIES, vDouble);
68                 String prop;
69
70                 prop = Double.toString(vDouble.get(ScicosParameters.FINAL_INTEGRATION_TIME));
71                 shared.stream.writeAttribute("finalIntegrationTime", prop);
72                 prop = Double.toString(vDouble.get(ScicosParameters.INTEGRATOR_ABSOLUTE_TOLERANCE));
73                 shared.stream.writeAttribute("integratorAbsoluteTolerance", prop);
74                 prop = Double.toString(vDouble.get(ScicosParameters.INTEGRATOR_RELATIVE_TOLERANCE));
75                 shared.stream.writeAttribute("integratorRelativeTolerance", prop);
76                 prop = Double.toString(vDouble.get(ScicosParameters.TOLERANCE_ON_TIME));
77                 shared.stream.writeAttribute("toleranceOnTime", prop);
78                 prop = Double.toString(vDouble.get(ScicosParameters.MAX_INTEGRATION_TIME_INTERVAL));
79                 shared.stream.writeAttribute("maxIntegrationTimeInterval", prop);
80                 prop = Double.toString(vDouble.get(ScicosParameters.MAXIMUM_STEP_SIZE));
81                 shared.stream.writeAttribute("maximumStepSize", prop);
82                 prop = Double.toString(vDouble.get(ScicosParameters.REAL_TIME_SCALING));
83                 shared.stream.writeAttribute("realTimeScaling", prop);
84                 prop = Double.toString(vDouble.get(ScicosParameters.SOLVER));
85                 shared.stream.writeAttribute("solver", prop);
86
87                 // write the graphical part and children
88                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.CHILDREN, children);
89                 writeDiagramAndSuperDiagramContent(uid, kind, children);
90
91                 shared.stream.writeEndElement();
92                 shared.stream.writeEndDocument();
93
94                 // post processing for binary data handling
95                 shared.rawDataWriter.fillSharedDictionary();
96                 break;
97             case BLOCK:
98                 shared.controller.getObjectProperty(uid, kind, ObjectProperties.CHILDREN, children);
99                 if (children.size() > 0) {
100                     shared.stream.writeStartElement(HandledElement.SuperBlockDiagram.name());
101                     shared.stream.writeAttribute("as", "child");
102                     writeDiagramAndSuperDiagramContent(uid, kind, children);
103                     shared.stream.writeEndElement(); // SuperBlockDiagram
104                 }
105                 break;
106             case PORT:
107                 // FIXME encode orientation from the model or not?
108                 break;
109             default:
110                 break;
111         }
112     }
113
114     private void writeDiagramAndSuperDiagramContent(long uid, Kind kind, VectorOfScicosID children) throws XMLStreamException {
115         VectorOfInt colors = new VectorOfInt();
116         shared.controller.getObjectProperty(uid, kind, ObjectProperties.COLOR, colors);
117         shared.stream.writeAttribute("background", Integer.toString(colors.get(0)));
118         shared.stream.writeAttribute("gridEnabled", Integer.toString(colors.get(1)));
119
120         String[] str = new String[1];
121         shared.controller.getObjectProperty(uid, kind, ObjectProperties.TITLE, str);
122         shared.stream.writeAttribute("title", str[0]);
123
124         /*
125          * encode some content then the children
126          */
127         shared.rawDataWriter.write(uid, kind);
128
129         UUID root = UUID.randomUUID();
130         UUID layer = UUID.randomUUID();
131         shared.layers.push(layer.toString());
132
133         // children header
134         shared.stream.writeStartElement("mxGraphModel");
135         shared.stream.writeAttribute("as", "model");
136         shared.stream.writeStartElement("root");
137         shared.stream.writeEmptyElement("mxCell");
138         shared.stream.writeAttribute("id", root.toString());
139         shared.stream.writeEmptyElement("mxCell");
140         shared.stream.writeAttribute("id", shared.layers.peek());
141         shared.stream.writeAttribute("parent", root.toString());
142
143         // loop on all children, encode the blocks and ports then the links
144         VectorOfInt kinds = new VectorOfInt();
145         shared.controller.sortAndFillKind(children, kinds);
146
147         final int len = children.size();
148         for (int i = 0; i < len; i++) {
149             long child = children.get(i);
150             Kind childKind = Kind.values()[kinds.get(i)];
151             shared.write(child, childKind);
152         }
153
154         shared.layers.pop();
155         shared.stream.writeEndElement(); // root
156         shared.stream.writeEndElement(); // mxGraphModel
157         shared.stream.writeEmptyElement("mxCell");
158         shared.stream.writeAttribute("as", "defaultParent");
159         shared.stream.writeAttribute("id", layer.toString());
160         shared.stream.writeAttribute("parent", root.toString());
161     }
162 }