* Bug #12796 fixed - Mismatch with superblock ports between implicit
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / utils / BlockPositioning.java
index 934823a..f4fdb17 100644 (file)
@@ -14,6 +14,7 @@ package org.scilab.modules.xcos.utils;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 
@@ -47,6 +48,25 @@ public final class BlockPositioning {
     }
 
     /**
+     *
+     */
+    public static class PortComparator implements Comparator<BasicPort> {
+        @Override
+        public int compare(BasicPort arg0, BasicPort arg1) {
+            int order_0 = arg0.getOrdering();
+            int order_1 = arg1.getOrdering();
+
+            if (order_0 < order_1) {
+                return -1;
+            } else if (order_0 > order_1) {
+                return 1;
+            } else {
+                return 0;
+            }
+        }
+    }
+
+    /**
      * Dispatch ports on Block's _WEST_ side.
      *
      * @param block
@@ -63,6 +83,11 @@ public final class BlockPositioning {
             gridSize = block.getParentDiagram().getGridSize();
         }
 
+        // BasicBlock.sortsort(List<?> children) takes into account different 
+        // parameters to order the ports list. We only need to order the ports
+        // given their ordering.
+        Collections.sort(ports, new PortComparator());
+
         final mxGeometry blockGeom = block.getGeometry();
         assert blockGeom != null;
         final int portsSize = ports.size();
@@ -75,12 +100,7 @@ public final class BlockPositioning {
             final mxGeometry portGeom = port.getGeometry();
 
             double nonVariantPosition = -portGeom.getWidth();
-            final int order;
-            if (port.getOrdering() <= portsSize) {
-                order = port.getOrdering() - 1;
-            } else {
-                order = i;
-            }
+            final int order = i;
             double alignedPosition = calculateAlignedPosition(gridSize, segLength, order);
 
             portGeom.setX(nonVariantPosition);
@@ -139,6 +159,11 @@ public final class BlockPositioning {
             gridSize = block.getParentDiagram().getGridSize();
         }
 
+        // BasicBlock.sortsort(List<?> children) takes into account different 
+        // parameters to order the ports list. We only need to order the ports
+        // given their ordering.
+        Collections.sort(ports, new PortComparator());
+
         final mxGeometry blockGeom = block.getGeometry();
         assert blockGeom != null;
         final int portsSize = ports.size();
@@ -151,12 +176,7 @@ public final class BlockPositioning {
             final mxGeometry portGeom = port.getGeometry();
 
             double nonVariantPosition = -portGeom.getHeight();
-            final int order;
-            if (port.getOrdering() <= portsSize) {
-                order = port.getOrdering() - 1;
-            } else {
-                order = i;
-            }
+            final int order = i;
             double alignedPosition = calculateAlignedPosition(gridSize, segLength, order);
 
             portGeom.setX(alignedPosition);
@@ -183,6 +203,11 @@ public final class BlockPositioning {
             gridSize = block.getParentDiagram().getGridSize();
         }
 
+        // BasicBlock.sortsort(List<?> children) takes into account different 
+        // parameters to order the ports list. We only need to order the ports
+        // given their ordering.
+        Collections.sort(ports, new PortComparator());
+
         final mxGeometry blockGeom = block.getGeometry();
         assert blockGeom != null;
         final int portsSize = ports.size();
@@ -195,12 +220,7 @@ public final class BlockPositioning {
             final mxGeometry portGeom = port.getGeometry();
 
             double nonVariantPosition = blockGeom.getWidth();
-            final int order;
-            if (port.getOrdering() <= portsSize) {
-                order = port.getOrdering() - 1;
-            } else {
-                order = i;
-            }
+            final int order = i;
             double alignedPosition = calculateAlignedPosition(gridSize, segLength, order);
 
             portGeom.setX(nonVariantPosition);
@@ -227,6 +247,11 @@ public final class BlockPositioning {
             gridSize = block.getParentDiagram().getGridSize();
         }
 
+        // BasicBlock.sortsort(List<?> children) takes into account different 
+        // parameters to order the ports list. We only need to order the ports
+        // given their ordering.
+        Collections.sort(ports, new PortComparator());
+
         final mxGeometry blockGeom = block.getGeometry();
         assert blockGeom != null;
         final int portsSize = ports.size();
@@ -239,12 +264,7 @@ public final class BlockPositioning {
             final mxGeometry portGeom = port.getGeometry();
 
             double nonVariantPosition = blockGeom.getHeight();
-            final int order;
-            if (port.getOrdering() <= portsSize) {
-                order = port.getOrdering() - 1;
-            } else {
-                order = i;
-            }
+            final int order = i;
             double alignedPosition = calculateAlignedPosition(gridSize, segLength, order);
 
             portGeom.setX(alignedPosition);