2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015-2017 - Scilab Enterprises - Clement DAVID
4 * Copyright (C) 2017-2018 - ESI Group - Clement DAVID
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 package org.scilab.modules.xcos.io.sax;
19 import org.scilab.modules.xcos.Kind;
20 import org.scilab.modules.xcos.ObjectProperties;
21 import org.scilab.modules.xcos.graph.model.XcosCell;
22 import org.scilab.modules.xcos.io.HandledElement;
23 import org.xml.sax.Attributes;
25 import com.mxgraph.model.mxGeometry;
26 import com.mxgraph.util.mxPoint;
27 import com.mxgraph.util.mxUtils;
29 import java.nio.DoubleBuffer;
30 import java.nio.LongBuffer;
31 import java.util.ArrayList;
32 import org.scilab.modules.xcos.VectorOfDouble;
33 import org.scilab.modules.xcos.VectorOfScicosID;
35 class JGraphXHandler implements ScilabHandler {
37 private final XcosSAXHandler saxHandler;
39 JGraphXHandler(XcosSAXHandler saxHandler) {
40 this.saxHandler = saxHandler;
46 public Object startElement(HandledElement found, Attributes atts) {
51 v = atts.getValue("parent");
53 long parentUID = saxHandler.allChildren.peek().getOrDefault(v, 0l);
55 return decodeCellAnnotation(parentUID, saxHandler.controller.getKind(parentUID), atts);
58 Object parent = saxHandler.parents.peek();
59 if (parent instanceof XcosCell) {
60 XcosCell cell = ((XcosCell) parent);
61 if (cell.getUID() != 0) {
62 return decodeCellAnnotation(cell.getUID(), cell.getKind(), atts);
69 mxGeometry g = new mxGeometry();
71 v = atts.getValue("x");
73 g.setX(Double.valueOf(v));
75 v = atts.getValue("y");
77 g.setY(Double.valueOf(v));
79 v = atts.getValue("width");
81 g.setWidth(Double.valueOf(v));
83 v = atts.getValue("height");
85 g.setHeight(Double.valueOf(v));
89 * the MVC only store absolute values, resolve the "relative" geometry flag for Scilab 5.5.2 annotation
91 v = atts.getValue("relative");
92 if (v != null && v.charAt(0) == '1') {
93 Object parent = saxHandler.parents.peek();
94 if (parent instanceof XcosCell) {
95 XcosCell cell = (XcosCell) parent;
96 long[] parentUID = {0};
97 saxHandler.controller.getObjectProperty(cell.getUID(), cell.getKind(), ObjectProperties.RELATED_TO, parentUID);
99 VectorOfDouble parentGeom = new VectorOfDouble(4);
100 saxHandler.controller.getObjectProperty(parentUID[0], saxHandler.controller.getKind(parentUID[0]), ObjectProperties.GEOMETRY, parentGeom);
101 g.setX(g.getX() * parentGeom.get(2));
102 g.setY(g.getY() * parentGeom.get(3));
109 mxPoint p = new mxPoint();
111 v = atts.getValue("x");
113 p.setX(Double.valueOf(v));
115 v = atts.getValue("y");
117 p.setY(Double.valueOf(v));
120 Object localParent = saxHandler.parents.peek();
121 if (localParent instanceof mxGeometry) {
122 mxGeometry parent = (mxGeometry) localParent;
123 v = atts.getValue("as");
124 if ("sourcePoint".equals(v)) {
125 parent.setSourcePoint(p);
126 } else if ("targetPoint".equals(v)) {
127 parent.setTargetPoint(p);
129 } else if (localParent instanceof RawDataHandler.RawDataDescriptor) {
130 RawDataHandler.RawDataDescriptor parent = (RawDataHandler.RawDataDescriptor) localParent;
131 ((ArrayList) parent.value).add(p);
132 } else if (localParent instanceof XcosCell) {
133 // Diagram origin, translate each children
134 XcosCell parent = (XcosCell) localParent;
136 VectorOfScicosID children = new VectorOfScicosID();
137 saxHandler.controller.getObjectProperty(parent.getUID(), parent.getKind(), ObjectProperties.CHILDREN, children);
139 VectorOfDouble geometry = new VectorOfDouble(4);
140 DoubleBuffer geom = geometry.asByteBuffer(0, 4).asDoubleBuffer();
142 LongBuffer childrenUIDs = children.asByteBuffer(0, children.size()).asLongBuffer();
143 while (childrenUIDs.hasRemaining()) {
144 long uid = childrenUIDs.get();
145 Kind kind = saxHandler.controller.getKind(uid);
147 saxHandler.controller.getObjectProperty(uid, kind, ObjectProperties.GEOMETRY, geometry);
148 geom.put(0, geom.get(0) + p.getX());
149 geom.put(1, geom.get(1) + p.getY());
150 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.GEOMETRY, geometry);
152 // translate the annotation
153 long[] annotation = { 0 };
154 saxHandler.controller.getObjectProperty(uid, kind, ObjectProperties.LABEL, annotation);
155 if (annotation[0] != 0) {
156 saxHandler.controller.getObjectProperty(annotation[0], Kind.ANNOTATION, ObjectProperties.GEOMETRY, geometry);
157 geom.put(0, geom.get(0) + p.getX());
158 geom.put(1, geom.get(1) + p.getY());
159 saxHandler.controller.setObjectProperty(annotation[0], Kind.ANNOTATION, ObjectProperties.GEOMETRY, geometry);
162 // translate control-points
163 if (kind == Kind.LINK) {
164 VectorOfDouble controlPoints = new VectorOfDouble();
165 saxHandler.controller.getObjectProperty(uid, kind, ObjectProperties.CONTROL_POINTS, controlPoints);
167 DoubleBuffer points = controlPoints.asByteBuffer(0, controlPoints.size()).asDoubleBuffer();
168 for (int i = 0; i < controlPoints.size(); i += 2) {
169 points.put(i, points.get(i) + p.getX());
170 points.put(i + 1, points.get(i + 1) + p.getY());
172 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.CONTROL_POINTS, controlPoints);
180 throw new IllegalArgumentException();
184 private XcosCell decodeCellAnnotation(long parentUID, Kind parentKind, Attributes atts) {
185 Kind kind = Kind.ANNOTATION;
186 final long uid = saxHandler.controller.createObject(kind);
187 String value = atts.getValue("value");
189 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.DESCRIPTION, mxUtils.getBodyMarkup(value, false));
191 String style = atts.getValue("style");
193 saxHandler.controller.setObjectProperty(uid, kind, ObjectProperties.STYLE, style);
195 String id = atts.getValue("id");
197 saxHandler.allChildren.peek().put(id, uid);
200 XcosCell label = new XcosCell(saxHandler.controller, uid, kind, value, null, style, id);
201 saxHandler.controller.setObjectProperty(parentUID, parentKind, ObjectProperties.LABEL, label.getUID());
202 saxHandler.controller.setObjectProperty(label.getUID(), label.getKind(), ObjectProperties.RELATED_TO, parentUID);
208 public void endElement(HandledElement found) {
213 // defensive programming
214 if (!(saxHandler.parents.peek() instanceof mxGeometry)) {
217 mxGeometry g = (mxGeometry) saxHandler.parents.peek();
218 if (!(saxHandler.parents.peek(1) instanceof XcosCell)) {
221 XcosCell cell = (XcosCell) saxHandler.parents.peek(1);
223 cell.setGeometry(saxHandler.controller, g);
229 throw new IllegalArgumentException();