Xcos: load and save all known xcos files 67/17567/2
Clément DAVID [Fri, 11 Dec 2015 10:34:48 +0000 (11:34 +0100)]
Change-Id: Ib879e6d3f7d5e126a46ddc44a25f4c6e57e7c5d5

scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/XcosDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/model/XcosCellFactory.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/scicos/LabelElement.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/writer/BlockWriter.java

index e221387..b9292b4 100644 (file)
@@ -1573,9 +1573,10 @@ public class XcosDiagram extends ScilabGraph {
             } else {
                 final String label = super.convertValueToString(cell);
                 if (label.isEmpty() && cell instanceof BasicBlock) {
-                    String[] interfaceFunction = new String[1];
+                    BasicBlock block = (BasicBlock) cell;
 
-                    controller.getObjectProperty(((BasicBlock) cell).getUID(), Kind.BLOCK, ObjectProperties.INTERFACE_FUNCTION, interfaceFunction);
+                    String[] interfaceFunction = new String[1];
+                    controller.getObjectProperty(block.getUID(), block.getKind(), ObjectProperties.INTERFACE_FUNCTION, interfaceFunction);
                     ret = interfaceFunction[0];
                 } else {
                     ret = label;
index d2806c8..0b166c7 100644 (file)
@@ -216,7 +216,11 @@ public final class XcosCellFactory {
         children.clear();
         controller.setObjectProperty(diagram.getUID(), diagram.getKind(), ObjectProperties.CHILDREN, children);
 
-        diagram.addCells(cells);
+        // add all the children without using the diagram modification tracking features
+        diagram.getView().getStates().clear();
+        for (XcosCell c : cells) {
+            ((XcosCell) diagram.getDefaultParent()).insert(c);
+        }
 
         // each cell has been referenced twice (CHILDREN insert and addCells), derefence them all by one
         Arrays.stream(cells).forEach(c -> controller.deleteObject(c.getUID()));
index ad7c367..1cdfdd8 100644 (file)
@@ -23,6 +23,8 @@ import org.scilab.modules.types.ScilabType;
 import org.scilab.modules.xcos.JavaController;
 import org.scilab.modules.xcos.Kind;
 import org.scilab.modules.xcos.block.TextBlock;
+import org.scilab.modules.xcos.graph.model.BlockInterFunction;
+import org.scilab.modules.xcos.graph.model.XcosCellFactory;
 import org.scilab.modules.xcos.io.scicos.ScicosFormatException.WrongElementException;
 import org.scilab.modules.xcos.io.scicos.ScicosFormatException.WrongStructureException;
 import org.scilab.modules.xcos.io.scicos.ScicosFormatException.WrongTypeException;
@@ -32,6 +34,7 @@ import org.scilab.modules.xcos.io.scicos.ScicosFormatException.WrongTypeExceptio
  */
 public final class LabelElement extends AbstractElement<TextBlock> {
     protected static final List<String> DATA_FIELD_NAMES = asList("Text", "graphics", "model", "void", "gui");
+    private static final int INTERFUNCTION_INDEX = DATA_FIELD_NAMES.indexOf("gui");
 
     /** Mutable field to easily get the data through methods */
     private ScilabMList data;
@@ -69,8 +72,10 @@ public final class LabelElement extends AbstractElement<TextBlock> {
 
         validate();
 
+        final String interfunction = ((ScilabString) data.get(INTERFUNCTION_INDEX)).getData()[0][0];
         if (into == null) {
-            block = new TextBlock(controller.createObject(Kind.ANNOTATION));
+            BlockInterFunction func = XcosCellFactory.lookForInterfunction(interfunction);
+            block = (TextBlock) XcosCellFactory.createBlock(controller, func, interfunction, controller.createObject(Kind.ANNOTATION), Kind.ANNOTATION);
         }
 
         block = beforeDecode(element, block);
index 78a2692..469ed9b 100644 (file)
@@ -48,7 +48,7 @@ public class BlockWriter extends ScilabWriter {
         String[] v = new String[1];
 
         shared.controller.getObjectProperty(uid, kind, ObjectProperties.UID, v);
-        while (v[0].isEmpty() || shared.uniqueUIDs.contains(v[0])) {
+        while (isInvalidUID(v[0])) {
             v[0] = new UID().toString();
         }
         shared.controller.setObjectProperty(uid, kind, ObjectProperties.UID, v[0]);
@@ -108,4 +108,35 @@ public class BlockWriter extends ScilabWriter {
         shared.layers.pop();
     }
 
+    private boolean isInvalidUID(String uid) {
+        if (uid.isEmpty()) {
+            return true;
+        }
+        if (shared.uniqueUIDs.contains(uid)) {
+            return true;
+        }
+
+        // more advanced check to remove non-generated UID without loosing performance
+        // a valid one is in the form : "-5151d0b0:12dcdfdd360:-7fff"
+        if (3 < uid.length() && uid.length() < 28) {
+            // more costly operations there but there is no allocation
+            int firstSep = uid.indexOf(':', 0);
+            if (firstSep <= 0) {
+                return true;
+            }
+
+            int secondSep = uid.indexOf(':', firstSep + 1);
+            if (secondSep <= firstSep + 1) {
+                return true;
+            }
+
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+
+
+
 }