Xcos GUI: handle XCOS / ZCOS import
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / io / sax / CustomHandler.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
13 package org.scilab.modules.xcos.io.sax;
14
15 import java.util.HashMap;
16 import java.util.Optional;
17 import java.util.stream.Stream;
18
19 import org.scilab.modules.xcos.Kind;
20 import org.scilab.modules.xcos.ObjectProperties;
21 import org.scilab.modules.xcos.VectorOfDouble;
22 import org.scilab.modules.xcos.VectorOfInt;
23 import org.scilab.modules.xcos.graph.ScicosParameters;
24 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
25 import org.scilab.modules.xcos.graph.model.XcosCell;
26 import org.scilab.modules.xcos.graph.model.XcosCellFactory;
27 import org.scilab.modules.xcos.port.BasicPort;
28 import org.scilab.modules.xcos.port.Orientation;
29 import org.xml.sax.Attributes;
30
31 class CustomHandler implements ScilabHandler {
32
33     private final SAXHandler saxHandler;
34
35     /**
36      * Default constructor
37      * @param saxHandler the shared sax handler
38      */
39     CustomHandler(SAXHandler saxHandler) {
40         this.saxHandler = saxHandler;
41     }
42
43     public Object startElement(HandledElement found, final Attributes atts) {
44         long uid = 0l;
45         String v;
46
47         switch (found) {
48             case Orientation: {
49                 // defensive programming
50                 if (!(saxHandler.parents.peek() instanceof BasicPort)) {
51                     return null;
52                 }
53                 BasicPort parent = (BasicPort) saxHandler.parents.peek();
54
55                 v = atts.getValue("value");
56                 if (v != null) {
57                     final String orientationString = v;
58                     Optional<Orientation> orientation = Stream.of(Orientation.values()).filter(o -> o.name().equals(orientationString)).findFirst();
59                     if (orientation.isPresent()) {
60                         parent.setOrientation(orientation.get());
61                     }
62                 }
63
64                 return null;
65             }
66             case XcosDiagram:
67                 // do not allocate this is already allocated as #root
68                 uid = saxHandler.root.getUID();
69
70                 /*
71                  * Decode some graph properties
72                  */
73                 v = atts.getValue("savedFile");
74                 if (v != null) {
75                     saxHandler.controller.setObjectProperty(uid, Kind.DIAGRAM, ObjectProperties.PATH, v);
76                 }
77
78                 /*
79                  * Decode simulation properties
80                  */
81                 VectorOfDouble properties = new VectorOfDouble();
82                 saxHandler.controller.getObjectProperty(uid, Kind.DIAGRAM, ObjectProperties.PROPERTIES, properties);
83
84                 v = atts.getValue("finalIntegrationTime");
85                 if (v != null) {
86                     properties.set(ScicosParameters.FINAL_INTEGRATION_TIME, Double.valueOf(v));
87                 }
88                 v = atts.getValue("integratorAbsoluteTolerance");
89                 if (v != null) {
90                     properties.set(ScicosParameters.INTEGRATOR_ABSOLUTE_TOLERANCE, Double.valueOf(v));
91                 }
92                 v = atts.getValue("integratorRelativeTolerance");
93                 if (v != null) {
94                     properties.set(ScicosParameters.INTEGRATOR_RELATIVE_TOLERANCE, Double.valueOf(v));
95                 }
96                 v = atts.getValue("toleranceOnTime");
97                 if (v != null) {
98                     properties.set(ScicosParameters.TOLERANCE_ON_TIME, Double.valueOf(v));
99                 }
100                 v = atts.getValue("maxIntegrationTimeinterval");
101                 if (v != null) {
102                     properties.set(ScicosParameters.MAX_INTEGRATION_TIME_INTERVAL, Double.valueOf(v));
103                 }
104                 v = atts.getValue("maximumStepSize");
105                 if (v != null) {
106                     properties.set(ScicosParameters.MAXIMUM_STEP_SIZE, Double.valueOf(v));
107                 }
108                 v = atts.getValue("realTimeScaling");
109                 if (v != null) {
110                     properties.set(ScicosParameters.REAL_TIME_SCALING, Double.valueOf(v));
111                 }
112                 v = atts.getValue("solver");
113                 if (v != null) {
114                     properties.set(ScicosParameters.SOLVER, Double.valueOf(v));
115                 }
116
117                 saxHandler.controller.setObjectProperty(uid, Kind.DIAGRAM, ObjectProperties.PROPERTIES, properties);
118
119             // no break on purpose, we decode non-root specific properties later
120             case SuperBlockDiagram:
121                 final Kind kind;
122                 if (uid == 0l) {
123                     XcosCell parent = saxHandler.lookupForParentXcosCellElement();
124                     uid = parent.getUID();
125                     kind = parent.getKind();
126                 } else {
127                     kind = Kind.DIAGRAM;
128                 }
129
130                 /*
131                  * Decode the properties shared between a DIAGRAM and a BLOCK
132                  */
133                 VectorOfInt colors = new VectorOfInt();
134                 saxHandler.controller.getObjectProperty(uid, kind, ObjectProperties.COLOR, colors);
135                 v = atts.getValue("background");
136                 if (v != null) {
137                     colors.set(0, Integer.valueOf(v));
138                 }
139                 v = atts.getValue("gridEnabled");
140                 if (v != null) {
141                     colors.set(1, Integer.valueOf(v));
142                 }
143                 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.COLOR, colors);
144
145                 v = atts.getValue("title");
146                 if (v != null) {
147                     saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.TITLE, v);
148                 }
149
150                 /*
151                  * Update some states
152                  */
153                 saxHandler.allChildren.push(new HashMap<>());
154                 return new ScicosObjectOwner(uid, kind);
155             default:
156                 throw new IllegalArgumentException();
157         }
158     }
159
160     public void endElement(HandledElement found) {
161         switch (found) {
162             case Orientation:
163                 break;
164             case XcosDiagram:
165                 XcosCellFactory.insertChildren(saxHandler.controller, saxHandler.root);
166                 break;
167             case SuperBlockDiagram:
168                 saxHandler.allChildren.pop();
169                 break;
170             default:
171                 throw new IllegalArgumentException();
172         }
173
174         // FIXME manage unresolved link source
175         // FIXME manage unresolved link target
176
177     }
178 }