Xcos: load/save should preserve UID values 64/17564/2
Clément DAVID [Thu, 10 Dec 2015 05:45:07 +0000 (06:45 +0100)]
Change-Id: Ib0deb52dc244cb915f7813aa8d28ea12b96e660d

scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/model/XcosCellFactory.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/scicos/BlockElement.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/spec/ContentEntry.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/spec/DictionaryEntry.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/spec/XcosPackage.java

index a8ce59a..d2806c8 100644 (file)
@@ -285,7 +285,7 @@ public final class XcosCellFactory {
             }
 
             if (EnumSet.of(Kind.BLOCK, Kind.ANNOTATION).contains(last.getKind())) {
-                block = createBlock(controller, func, interfaceFunction, last.getUID());
+                block = createBlock(controller, func, interfaceFunction, last.getUID(), last.getKind());
             } else {
                 block = null;
             }
@@ -306,7 +306,7 @@ public final class XcosCellFactory {
 
         final BlockInterFunction func = lookForInterfunction(interfaceFunction[0]);
 
-        return createBlock(controller, func, interfaceFunction[0], uid);
+        return createBlock(controller, func, interfaceFunction[0], uid, kind);
     }
 
     public static BlockInterFunction lookForInterfunction(String interfaceFunction) {
@@ -334,7 +334,33 @@ public final class XcosCellFactory {
      *            the allocated uid
      * @return A new instance of a block.
      */
-    public static BasicBlock createBlock(final JavaController controller, BlockInterFunction func, String interfaceFunction, long uid) {
+    public static BasicBlock createBlock(final JavaController controller, BlockInterFunction func, String interfaceFunction, long uid, Kind kind) {
+        /*
+         * Retrieve the JGraphX data before cell creation
+         */
+        String[] strUID = new String[1];
+        controller.getObjectProperty(uid, kind, ObjectProperties.UID, strUID);
+
+        String[] style = new String[1];
+        controller.getObjectProperty(uid, kind, ObjectProperties.STYLE, style);
+
+        String value;
+        if (kind == Kind.ANNOTATION) {
+            String[] description = new String[1];
+            controller.getObjectProperty(uid, kind, ObjectProperties.DESCRIPTION, description);
+            value = description[0];
+        } else { // BLOCK
+            String[] label = new String[1];
+            controller.getObjectProperty(uid, kind, ObjectProperties.LABEL, label);
+            value = label[0];
+        }
+
+        VectorOfDouble geom = new VectorOfDouble(4);
+        controller.getObjectProperty(uid, kind, ObjectProperties.GEOMETRY, geom);
+
+        /*
+         * Instanciate the block
+         */
         BasicBlock block = null;
         try {
             block = func.getKlass().getConstructor(Long.TYPE).newInstance(uid);
@@ -359,32 +385,16 @@ public final class XcosCellFactory {
             insertPortChildren(controller, properties, block);
         }
 
-        String[] strUID = new String[1];
-        controller.getObjectProperty(block.getUID(), block.getKind(), ObjectProperties.UID, strUID);
+        block.setId(strUID[0]);
 
-        String[] style = new String[1];
-        controller.getObjectProperty(block.getUID(), block.getKind(), ObjectProperties.STYLE, style);
         if (style[0].isEmpty()) {
             block.setStyle(interfaceFunction);
         } else {
             block.setStyle(style[0]);
         }
 
-        String value;
-        if (block.getKind() == Kind.ANNOTATION) {
-            String[] description = new String[1];
-            controller.getObjectProperty(block.getUID(), block.getKind(), ObjectProperties.DESCRIPTION, description);
-            value = description[0];
-        } else { // BLOCK
-            String[] label = new String[1];
-            controller.getObjectProperty(block.getUID(), block.getKind(), ObjectProperties.LABEL, label);
-            value = label[0];
-        }
         block.setValue(value);
 
-        VectorOfDouble geom = new VectorOfDouble(4);
-        controller.getObjectProperty(block.getUID(), block.getKind(), ObjectProperties.GEOMETRY, geom);
-
         double x = geom.get(0);
         double y = geom.get(1);
         double w = geom.get(2);
@@ -443,7 +453,7 @@ public final class XcosCellFactory {
         String[] interfaceFunction = new String[1];
         BlockInterFunction func = lookForInterfunction(interfaceFunction[0]);
 
-        return createBlock(controller, func, interfaceFunction[0], lastCreated.getUID());
+        return createBlock(controller, func, interfaceFunction[0], lastCreated.getUID(), lastCreated.getKind());
     }
 
     /*
index 6dd95e0..05ae3b2 100644 (file)
@@ -103,7 +103,7 @@ public final class BlockElement extends AbstractElement<BasicBlock> {
         final String interfunction = ((ScilabString) data.get(INTERFUNCTION_INDEX)).getData()[0][0];
         if (block == null) {
             BlockInterFunction func = XcosCellFactory.lookForInterfunction(interfunction);
-            block = XcosCellFactory.createBlock(controller, func, interfunction, controller.createObject(Kind.BLOCK));
+            block = XcosCellFactory.createBlock(controller, func, interfunction, controller.createObject(Kind.BLOCK), Kind.BLOCK);
         }
 
         block = beforeDecode(element, block);
index 60a04fa..0a436c8 100644 (file)
@@ -80,7 +80,7 @@ public class ContentEntry implements Entry {
          * Append a ZipEntry
          */
         final ZipEntry entry = new ZipEntry(getFullPath());
-        entry.setTime(0l);
+        entry.setTime(pack.getTime());
         stream.putNextEntry(entry);
 
         /*
index e014fa8..abbca34 100644 (file)
@@ -70,7 +70,7 @@ public class DictionaryEntry implements Entry {
          * Append a ZipEntry
          */
         final ZipEntry entry = new ZipEntry(getFullPath());
-        entry.setTime(0l);
+        entry.setTime(pack.getTime());
         stream.putNextEntry(entry);
 
         /*
index 68e140a..ffc6645 100644 (file)
@@ -38,6 +38,8 @@ import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
+import org.scilab.modules.commons.ScilabCommons;
+import org.scilab.modules.commons.ScilabCommonsUtils;
 
 import org.scilab.modules.commons.xml.ScilabDocumentBuilderFactory;
 import org.scilab.modules.commons.xml.ScilabTransformerFactory;
@@ -110,6 +112,7 @@ public class XcosPackage {
      */
     private final File file;
     private Document manifest;
+    private long time;
 
     /**
      * Entries encoder/decoder stored in the encoding order
@@ -147,6 +150,8 @@ public class XcosPackage {
         manifest = ScilabDocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
         final Element root = manifest.createElementNS("urn:scilab:xcos:xmlns:manifest:0.1", "manifest:manifest");
         manifest.appendChild(root);
+
+        time = 0l;
     }
 
     private boolean hasInvalidManifest() {
@@ -287,7 +292,7 @@ public class XcosPackage {
         crc.update(MIME_BYTES);
         entry.setCrc(crc.getValue());
         entry.setMethod(ZipEntry.STORED);
-        entry.setTime(0l);
+        entry.setTime(getTime());
         zout.putNextEntry(entry);
         zout.write(MIME_BYTES);
 
@@ -306,7 +311,7 @@ public class XcosPackage {
          * Append the entry
          */
         final ZipEntry entry = new ZipEntry(META_INF_MANIFEST_XML);
-        entry.setTime(0l);
+        entry.setTime(getTime());
         zout.putNextEntry(entry);
 
         /*
@@ -347,6 +352,10 @@ public class XcosPackage {
         return dictionary;
     }
 
+    public long getTime() {
+        return time;
+    }
+
     /*
      * Utilities
      */