Xcos: fix "Region to Superblock" after cad91838 50/18550/4
Clément DAVID [Mon, 19 Sep 2016 14:28:07 +0000 (16:28 +0200)]
Change-Id: I735e7160c0d19c153afd3339979cbb1759bdbf31

scilab/modules/xcos/etc/Xcos-style.xml
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/actions/RegionToSuperblockAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/ContextUpdate.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/EventInBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/EventOutBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/ExplicitInBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/ExplicitOutBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/ImplicitInBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/io/ImplicitOutBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/XcosDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/model/XcosGraphModel.java

index 0cde7bd..d37a0da 100644 (file)
         <add as="rounded" value="1"/>
         <add as="fillColor" value="white"/>
         <add as="strokeColor" value="red"/>
+        <add as="displayedLabel" value="%s"/>
     </add>
     <add as="CURV_f" extend="blockWithLabel">
         <add as="displayedLabel" value="Curve"/>
         <add as="shape" value="hexagon"/>
         <add as="rounded" value="1"/>
         <add as="fillColor" value="white"/>
+        <add as="displayedLabel" value="%s"/>
     </add>
     <add as="READAU_f" extend="blockWithLabel">
         <add as="displayedLabel" value="Read from .au&lt;BR&gt;         sound file"/>
         <add as="shape" value="hexagon"/>
         <add as="rounded" value="1"/>
         <add as="fillColor" value="white"/>
+        <add as="displayedLabel" value="%s"/>
     </add>
     <add as="READC_f" extend="blockWithLabel">
         <add as="displayedLabel" value="Read from&lt;BR&gt;         C binary file"/>
     <add as="OUTIMPL_f" extend="blockWithLabel">
         <add as="shape" value="hexagon"/>
         <add as="fillColor" value="white"/>
+        <add as="displayedLabel" value="%s"/>
     </add>
     <!-- INTEGER -->
     <add as="BITCLEAR" extend="blockWithLabel">
         <add as="shape" value="hexagon"/>
         <add as="fillColor" value="white"/>
         <add as="strokeColor" value="red"/>
+        <add as="displayedLabel" value="%s"/>
     </add>
     <add as="CLKOUT_f" extend="CLKOUTV_f"/>
     <add as="CLKSOMV_f" extend="blockWithLabel">
         <add as="shape" value="hexagon"/>
         <add as="rounded" value="1"/>
         <add as="fillColor" value="white"/>
+        <add as="displayedLabel" value="%s"/>
     </add>
     <add as="RELATIONALOP" extend="blockWithLabel">
         <add as="displayedLabel" value="Relational&lt;BR&gt;         op : &amp;&lt;"/>
index 12d7700..d9a818a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) 2010 - DIGITEO - Clement DAVID
- * Copyright (C) 2011-2015 - Scilab Enterprises - Clement DAVID
+ * Copyright (C) 2011-2016 - Scilab Enterprises - Clement DAVID
  *
  * Copyright (C) 2012 - 2016 - Scilab Enterprises
  *
@@ -44,9 +44,9 @@ import org.scilab.modules.xcos.utils.XcosMessages;
 import com.mxgraph.model.mxGeometry;
 import com.mxgraph.model.mxGraphModel;
 import com.mxgraph.model.mxICell;
-import com.mxgraph.util.mxPoint;
 import com.mxgraph.util.mxRectangle;
 import java.nio.LongBuffer;
+import java.rmi.server.UID;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Level;
@@ -129,6 +129,8 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
         /**
          * Default Constructor
          *
+         * @param parentGraph
+         *            the parent graph
          * @param parentModel
          *            the parent model
          * @param source
@@ -140,7 +142,7 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
          * @param containsSource
          *            is the source in selection
          */
-        public Broken(mxGraphModel parentModel, BasicPort source, BasicPort target, BasicLink link, boolean containsSource) {
+        public Broken(XcosDiagram parentGraph, mxGraphModel parentModel, BasicPort source, BasicPort target, BasicLink link, boolean containsSource) {
             super();
             this.parentModel = parentModel;
 
@@ -162,8 +164,8 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
             final mxGeometry pos = parentModel.getGeometry(terminal);
             final mxGeometry parent = parentModel.getGeometry(parentModel.getParent(terminal));
             if (pos != null && parent != null) {
-                this.x = pos.getX() + parent.getX() + (pos.getWidth() / 2) - (parent.getWidth() / 2);
-                this.y = pos.getY() + parent.getY() + (pos.getHeight() / 2) - (parent.getHeight() / 2);
+                this.x = parentGraph.snap(pos.getX() + parent.getX() - (pos.getHeight() / 2));
+                this.y = parentGraph.snap(pos.getY() + parent.getY() - (pos.getWidth() / 2));
             } else {
                 this.x = 0.0;
                 this.y = 0.0;
@@ -258,9 +260,13 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
 
                 try {
                     if (containsSource) {
-                        parentPort = IOBlocks.getOpposite(target.getClass()).getConstructor(Long.TYPE).newInstance(uid);
+                        parentPort = IOBlocks.getOpposite(target.getClass())
+                                     .getConstructor(JavaController.class, Long.TYPE, Kind.class, Object.class, String.class, String.class)
+                                     .newInstance(controller, uid, Kind.PORT, null, null, new UID().toString());
                     } else {
-                        parentPort = IOBlocks.getOpposite(source.getClass()).getConstructor(Long.TYPE).newInstance(uid);
+                        parentPort = IOBlocks.getOpposite(source.getClass())
+                                     .getConstructor(JavaController.class, Long.TYPE, Kind.class, Object.class, String.class, String.class)
+                                     .newInstance(controller, uid, Kind.PORT, null, null, new UID().toString());;
                     }
                 } catch (ReflectiveOperationException e) {
                     Logger.getLogger(RegionToSuperblockAction.class.getName()).severe(e.toString());
@@ -283,8 +289,10 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
                 /*
                  * Set the child position
                  */
-                childBlock.getGeometry().setX(x);
-                childBlock.getGeometry().setY(y);
+                mxGeometry geom = childBlock.getGeometry();
+                geom.setX(x);
+                geom.setY(y);
+                childBlock.setGeometry(geom);
             }
 
             return childBlock;
@@ -521,7 +529,7 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
              * only one occurrence of a broken link.
              */
             brokenLinks = new TreeSet<>();
-            fillLinks(parentModel, inSelectionCells, brokenLinks);
+            fillLinks(parentGraph, parentModel, inSelectionCells, brokenLinks);
 
             /*
              * Disconnect the broken links
@@ -565,6 +573,8 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
      * and broken links with a broken object. Also disconnect all the broken
      * links.
      *
+     * @param parentGraph
+     *            the graph
      * @param parentModel
      *            the model
      * @param inSelectionCells
@@ -572,7 +582,7 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
      * @param brokenLinks
      *            the broken links to find.
      */
-    private void fillLinks(final mxGraphModel parentModel, final Collection<XcosCell> inSelectionCells, final Collection<Broken> brokenLinks) {
+    private void fillLinks(final XcosDiagram parentGraph, final mxGraphModel parentModel, final Collection<XcosCell> inSelectionCells, final Collection<Broken> brokenLinks) {
         final Queue<Object> loopQueue = new LinkedList<>(inSelectionCells);
 
         while (!loopQueue.isEmpty()) {
@@ -605,12 +615,12 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
                      */
 
                     if (containsSource) {
-                        brokenLinks.add(new Broken(parentModel, (BasicPort) source, (BasicPort) target, (BasicLink) edge, true));
+                        brokenLinks.add(new Broken(parentGraph, parentModel, (BasicPort) source, (BasicPort) target, (BasicLink) edge, true));
                         continue;
                     }
 
                     if (containsTarget) {
-                        brokenLinks.add(new Broken(parentModel, (BasicPort) source, (BasicPort) target, (BasicLink) edge, false));
+                        brokenLinks.add(new Broken(parentGraph, parentModel, (BasicPort) source, (BasicPort) target, (BasicLink) edge, false));
                         continue;
                     }
                 }
@@ -736,36 +746,6 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
         });
 
         controller.setObjectProperty(superBlock.getUID(), superBlock.getKind(), ObjectProperties.CHILDREN, children);
-
-
-        /*
-         * Translate the cells to the origin
-         *
-         * In this algorithm only block position are handled to avoid any
-         * placement issue and a static margin is added to avoid
-         * misplacement.
-         */
-        final double margin = 10.0;
-        final mxPoint orig = new mxPoint(Double.MAX_VALUE, Double.MAX_VALUE);
-        for (XcosCell c : cellsToCopy) {
-            if (c instanceof BasicBlock) {
-                final mxGeometry geom = c.getGeometry();
-
-                orig.setX(Math.min(geom.getX(), orig.getX()));
-                orig.setY(Math.min(geom.getY(), orig.getY()));
-            }
-        }
-
-        // move the cells
-        final VectorOfDouble geometry = new VectorOfDouble();
-        cellsToCopy.stream().forEach(c -> {
-            controller.getObjectProperty(c.getUID(), c.getKind(), ObjectProperties.GEOMETRY, geometry);
-
-            geometry.set(0, geometry.get(0) - orig.getX() + margin);
-            geometry.set(1, geometry.get(1) - orig.getY() + margin);
-
-            controller.setObjectProperty(c.getUID(), c.getKind(), ObjectProperties.GEOMETRY, geometry);
-        });
     }
 }
 // CSON: ClassFanOutComplexity
index 51bf410..51a6d17 100644 (file)
@@ -48,6 +48,8 @@ import org.scilab.modules.xcos.port.output.ImplicitOutputPort;
 import org.scilab.modules.xcos.port.output.OutputPort;
 
 import com.mxgraph.model.mxICell;
+import java.rmi.server.UID;
+import org.scilab.modules.xcos.ObjectProperties;
 
 /**
  * Common class for the SuperBlock I/O blocks (represent ports)
@@ -296,30 +298,25 @@ public abstract class ContextUpdate extends BasicBlock {
         public static ContextUpdate createBlock(BasicPort port) {
             for (IOBlocks io : IOBlocks.values()) {
                 if (io.getReferencedPortClass().isInstance(port)) {
-                    final XcosView disabledView = (XcosView) JavaController.lookup_view(Xcos.class.getName());
                     try {
                         JavaController controller = new JavaController();
 
-                        // TODO: dunno if I should disable the view there :
-                        // CHECK
-                        JavaController.unregister_view(disabledView);
+                        // create the Input/Output block
+                        Constructor<? extends ContextUpdate> blockCstr = io.getReferencedClass().getConstructor(JavaController.class);
+                        ContextUpdate block = blockCstr.newInstance(controller);
 
-                        Constructor<? extends ContextUpdate> blockCstr = io.getReferencedClass().getConstructor(Long.TYPE);
-                        ContextUpdate block = blockCstr.newInstance(controller.createObject(Kind.BLOCK));
+                        // create the single port of the block
+                        Constructor<? extends BasicPort> portCstr = io.getOppositeClass().getConstructor(
+                                    JavaController.class, Long.TYPE, Kind.class, Object.class, String.class, String.class);
+                        BasicPort blockPort = portCstr.newInstance(
+                                                  controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, new UID().toString());
 
-                        Constructor<? extends BasicPort> portCstr = io.getOppositeClass().getConstructor(Long.TYPE);
-                        BasicPort blockPort = portCstr.newInstance(controller.createObject(Kind.BLOCK));
-
-                        disabledView.getVisibleObjects().put(block.getUID(), block);
-                        disabledView.getVisibleObjects().put(blockPort.getUID(), blockPort);
-
-                        // controller.setObjectProperty(block.getUID(), k, p, v)
+                        // inser the port into the newly created block
+                        block.insert(blockPort);
 
                         return block;
                     } catch (ReflectiveOperationException e) {
                         Logger.getLogger(IOBlocks.class.getName()).severe(e.toString());
-                    } finally {
-                        JavaController.register_view(Xcos.class.getName(), disabledView);
                     }
                 }
             }
@@ -356,12 +353,26 @@ public abstract class ContextUpdate extends BasicBlock {
     private transient boolean isContextDependent;
 
     /**
-     * Constructor.
+     * Constructor
      */
     public ContextUpdate(JavaController controller, long uid, Kind kind, Object value, mxGeometry geometry, String style, String id) {
         super(controller, uid, kind, value, geometry, style, id);
     }
 
+    /**
+     * Constructor used to allocate a new block on the Java side
+     *
+     * The caller should add a port child and setup the simulation function accordingly to the style.
+     * @param controller the controller used to allocate the block
+     * @param blockName the interface function and style applied on the block
+     */
+    protected ContextUpdate(JavaController controller, String blockName) {
+        super(controller, controller.createObject(Kind.BLOCK), Kind.BLOCK, null, new mxGeometry(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT), blockName, new UID().toString());
+
+        controller.setObjectProperty(getUID(), Kind.BLOCK, ObjectProperties.INTERFACE_FUNCTION, blockName);
+    }
+
+
 
     /**
      * @param context
index e31af93..12af0b4 100644 (file)
@@ -38,4 +38,10 @@ public final class EventInBlock extends ContextUpdate {
 
         controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, "input");
     }
+
+    public EventInBlock(JavaController controller) {
+        super(controller, "CLKINV_f");
+
+        controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SIM_FUNCTION_NAME, "input");
+    }
 }
index 43178b2..f74bebe 100644 (file)
@@ -38,4 +38,10 @@ public final class EventOutBlock extends ContextUpdate {
 
         controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, "output");
     }
+
+    public EventOutBlock(JavaController controller) {
+        super(controller, "CLKOUTV_f");
+
+        controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SIM_FUNCTION_NAME, "output");
+    }
 }
index 3cb8740..2f635b8 100644 (file)
@@ -38,4 +38,10 @@ public final class ExplicitInBlock extends ContextUpdate {
 
         controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, "input");
     }
+
+    public ExplicitInBlock(JavaController controller) {
+        super(controller, "IN_f");
+
+        controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SIM_FUNCTION_NAME, "input");
+    }
 }
index ab63130..6fc0a91 100644 (file)
@@ -38,4 +38,10 @@ public final class ExplicitOutBlock extends ContextUpdate {
 
         controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, "output");
     }
+
+    public ExplicitOutBlock(JavaController controller) {
+        super(controller, "OUT_f");
+
+        controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SIM_FUNCTION_NAME, "output");
+    }
 }
index dc4e36c..f4ff8c4 100644 (file)
@@ -38,4 +38,10 @@ public final class ImplicitInBlock extends ContextUpdate {
 
         controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, "inimpl");
     }
+
+    public ImplicitInBlock(JavaController controller) {
+        super(controller, "INIMPL_f");
+
+        controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SIM_FUNCTION_NAME, "inimpl");
+    }
 }
index 10894a8..a1a4190 100644 (file)
@@ -38,4 +38,10 @@ public final class ImplicitOutBlock extends ContextUpdate {
 
         controller.setObjectProperty(uid, kind, ObjectProperties.SIM_FUNCTION_NAME, "outimpl");
     }
+
+    public ImplicitOutBlock(JavaController controller) {
+        super(controller, "OUTIMPL_f");
+
+        controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SIM_FUNCTION_NAME, "outimpl");
+    }
 }
index 16f4505..e23723a 100644 (file)
@@ -114,9 +114,6 @@ import com.mxgraph.view.mxMultiplicity;
 import java.lang.reflect.Constructor;
 import java.rmi.server.UID;
 import java.util.Hashtable;
-import org.scilab.modules.types.ScilabList;
-import org.scilab.modules.types.ScilabString;
-import org.scilab.modules.types.ScilabType;
 import org.scilab.modules.xcos.io.ScilabTypeCoder;
 
 /**
@@ -406,8 +403,8 @@ public class XcosDiagram extends ScilabGraph {
         // get the user index
         JavaController controller = new JavaController();
         VectorOfInt ipar = new VectorOfInt();
-        controller.getObjectProperty(realIndex, Kind.BLOCK, ObjectProperties.IPAR, ipar);
-        if (ipar.size() > 1) {
+        controller.getObjectProperty(block.getUID(), Kind.BLOCK, ObjectProperties.IPAR, ipar);
+        if (ipar.size() < 1) {
             return err;
         }
         final int userIndex = ipar.get(0);
index 917bc76..7d05596 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) 2011-2015 - Scilab Enterprises - Clement DAVID
+ * Copyright (C) 2011-2016 - Scilab Enterprises - Clement DAVID
  *
  * Copyright (C) 2012 - 2016 - Scilab Enterprises
  *