* Bug #13030 fixed - Selection to Super block did not reset the origin 52/13052/3
Clément DAVID [Thu, 31 Oct 2013 13:13:40 +0000 (14:13 +0100)]
Re-implement bug #12167's fix to be compliant with undo/redo and graph
validation.

Change-Id: Ieb4b0cb142d11a4d81efd6386f673a9f57cc8e44

scilab/CHANGES_5.5.X
scilab/modules/graph/src/java/org/scilab/modules/graph/actions/RedoAction.java
scilab/modules/graph/src/java/org/scilab/modules/graph/actions/UndoAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/actions/RegionToSuperblockAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/SuperBlockDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/XcosDiagram.java

index ffaac28..832d219 100644 (file)
@@ -125,7 +125,9 @@ Xcos Bug Fixes
 
 * Bug #12998 fixed - 'ans' was not ignored in the context results.
 
-* Bug #13006 fixed - Selection to Super block (in_f and out_f) was failing.
+* Bug #13006 fixed - Selection to superblock (in_f and out_f) was failing.
+
+* Bug #13030 fixed - Selection to superblock did not reset the origin.
 
 
                  Changes between version 5.4.1 and 5.5.0-beta-1
index 198e79e..c5f7e15 100644 (file)
@@ -144,5 +144,9 @@ public class RedoAction extends DefaultAction {
         graph.removeUndoHandler();
         graph.getUndoManager().redo();
         graph.registerUndoHandler();
+
+        // revalidate the graph
+        graph.getAsComponent().clearCellOverlays();
+        graph.getAsComponent().validateGraph();
     }
 }
index 93b7b15..904cee5 100644 (file)
@@ -143,6 +143,10 @@ public class UndoAction extends DefaultAction {
         graph.removeUndoHandler();
         graph.getUndoManager().undo();
         graph.registerUndoHandler();
+
+        // revalidate the graph
+        graph.getAsComponent().clearCellOverlays();
+        graph.getAsComponent().validateGraph();
     }
 
 }
index 61b2947..6644a3a 100644 (file)
@@ -385,6 +385,12 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
             superBlock.invalidateRpar();
             superBlock.updateExportedPort();
 
+            /*
+             * Clear the transaction log in the child.
+             * From the user point of view, the operation cannot be undo'ed.
+             */
+            childGraph.getUndoManager().clear();
+
             Xcos.getInstance().addDiagram(parentGraph.getSavedFile(), childGraph);
         } finally {
             parentGraph.getModel().endUpdate();
@@ -675,10 +681,21 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
                 cellsToCopy.add(b.getChildLink());
             }
 
+            // create the local array to use the JGraphX API
+            final Object[] cells = cellsToCopy.toArray();
+
+            /*
+             * Translate the cells to the origin
+             *
+             * NOTE: this should be done on the parent diagram because
+             * getBoundsForCells only works for already added cells (not on the transaction)
+             */
+            mxRectangle rect = parentGraph.getBoundsForCells(cells, true, true, false);
+            parentGraph.moveCells(cells, -rect.getX(), -rect.getY());
+
             /*
              * Really copy the cells
              */
-            final Object[] cells = cellsToCopy.toArray();
             parentGraph.removeCells(cells, false);
             childGraph.addCells(cells);
 
index 1b25ee4..9d842cc 100644 (file)
@@ -34,11 +34,9 @@ import org.scilab.modules.xcos.block.io.ImplicitOutBlock;
 import org.scilab.modules.xcos.utils.XcosEvent;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
-import com.mxgraph.model.mxCell;
 import com.mxgraph.model.mxICell;
 import com.mxgraph.util.mxEvent;
 import com.mxgraph.util.mxEventObject;
-import com.mxgraph.util.mxUtils;
 
 public final class SuperBlockDiagram extends XcosDiagram implements Serializable, Cloneable {
 
@@ -124,6 +122,8 @@ public final class SuperBlockDiagram extends XcosDiagram implements Serializable
     /**
      * Validate I/O ports.
      *
+     * /!\ No model modification should be made in this method, this is only a validation method.
+     *
      * @param cell
      *            Cell that represents the cell to validate.
      * @param context
@@ -193,19 +193,6 @@ public final class SuperBlockDiagram extends XcosDiagram implements Serializable
             err = str.toString();
         }
 
-        // Update the port labels on the superblock
-        if (err == null) {
-            mxCell identifier = this.getOrCreateCellIdentifier(block);
-            final Object current = this.getModel().getValue(identifier);
-            String text = "";
-            if (current == null) {
-                text = "";
-            } else {
-                text = mxUtils.getBodyMarkup(current.toString(), false);
-            }
-            this.fireEvent(new mxEventObject(mxEvent.LABEL_CHANGED, "cell", identifier, "value", text, "parent", block));
-        }
-
         return err;
     }
 
@@ -252,6 +239,10 @@ public final class SuperBlockDiagram extends XcosDiagram implements Serializable
             if (block != null) {
                 block.updateExportedPort();
             }
+
+            // validate display errors
+            graph.getAsComponent().clearCellOverlays();
+            graph.getAsComponent().validateGraph();
         }
     }
 
index b0519e1..d25780f 100644 (file)
@@ -105,6 +105,7 @@ import com.mxgraph.model.mxGraphModel.Filter;
 import com.mxgraph.model.mxGraphModel.mxChildChange;
 import com.mxgraph.model.mxGraphModel.mxStyleChange;
 import com.mxgraph.model.mxICell;
+import com.mxgraph.model.mxIGraphModel;
 import com.mxgraph.model.mxIGraphModel.mxAtomicGraphModelChange;
 import com.mxgraph.util.mxEvent;
 import com.mxgraph.util.mxEventObject;
@@ -112,6 +113,7 @@ import com.mxgraph.util.mxPoint;
 import com.mxgraph.util.mxRectangle;
 import com.mxgraph.util.mxUndoableEdit;
 import com.mxgraph.util.mxUndoableEdit.mxUndoableChange;
+import com.mxgraph.util.mxUtils;
 import com.mxgraph.view.mxGraphSelectionModel;
 import com.mxgraph.view.mxMultiplicity;
 import com.mxgraph.view.mxStylesheet;
@@ -457,11 +459,25 @@ public class XcosDiagram extends ScilabGraph {
                     @Override
                     public boolean filter(Object cell) {
                         if (cell instanceof BasicBlock) {
+                            final BasicBlock blk = (BasicBlock) cell;
+
                             // Update parent on cell addition
-                            ((BasicBlock) cell).setParentDiagram(diagram);
+                            blk.setParentDiagram(diagram);
 
                             // update port numbering
-                            diagram.updateIOBlocks((BasicBlock) cell);
+                            diagram.updateIOBlocks(blk);
+
+                            // Fire an identifier update to let the I/O ports update their labels
+                            mxCell identifier = diagram.getCellIdentifier(blk);
+                            if (identifier != null) {
+                                final Object current = diagram.getModel().getValue(identifier);
+                                if (current != null) {
+                                    final String text = mxUtils.getBodyMarkup(current.toString(), false);
+                                    diagram.fireEvent(new mxEventObject(mxEvent.LABEL_CHANGED, "cell", identifier, "value", text, "parent", blk));
+                                }
+                            }
+
+                            diagram.getView().invalidate();
                         }
                         return false;
                     }
@@ -715,6 +731,7 @@ public class XcosDiagram extends ScilabGraph {
                 for (final Object object : changedCells) {
                     if (object instanceof BasicBlock) {
                         final BasicBlock current = (BasicBlock) object;
+                        final XcosDiagram graph = current.getParentDiagram();
 
                         // When we change the style property we have to update
                         // some BasiBlock fields
@@ -724,7 +741,7 @@ public class XcosDiagram extends ScilabGraph {
 
                         // update the superblock container ports if the block is
                         // inside a superblock diagram
-                        if (current.getParentDiagram() instanceof SuperBlockDiagram) {
+                        if (graph instanceof SuperBlockDiagram) {
                             SuperBlockDiagram superdiagram = (SuperBlockDiagram) current.getParentDiagram();
                             SuperBlock superblock = superdiagram.getContainer();
                             superblock.updateExportedPort();
@@ -735,21 +752,21 @@ public class XcosDiagram extends ScilabGraph {
 
                         // force a refresh of the block ports and links
                         // connected to these ports
-                        final int childCount = current.getParentDiagram().getModel().getChildCount(current);
+                        final int childCount = model.getChildCount(current);
                         for (int i = 0; i < childCount; i++) {
-                            final Object port = current.getParentDiagram().getModel().getChildAt(current, i);
-                            current.getParentDiagram().getView().clear(port, true, true);
-                            final int edgeCount = current.getParentDiagram().getModel().getEdgeCount(port);
+                            final Object port = model.getChildAt(current, i);
+                            graph.getView().clear(port, true, true);
+                            final int edgeCount = model.getEdgeCount(port);
                             for (int j = 0; j < edgeCount; j++) {
-                                final Object edge = current.getParentDiagram().getModel().getEdgeAt(port, j);
-                                current.getParentDiagram().getView().clear(edge, true, true);
+                                final Object edge = model.getEdgeAt(port, j);
+                                graph.getView().clear(edge, true, true);
                             }
                         }
                         // force a refresh of the block
-                        current.getParentDiagram().getView().clear(current, true, true);
+                        graph.getView().clear(current, true, true);
 
-                        current.getParentDiagram().getView().validate();
-                        current.getParentDiagram().repaint();
+                        graph.getView().validate();
+                        graph.repaint();
                     }
                 }
             } finally {
@@ -801,7 +818,10 @@ public class XcosDiagram extends ScilabGraph {
 
                 diagram.getView().clear(updatedBlock, true, true);
 
+                // validate display errors
+                diagram.getAsComponent().clearCellOverlays();
                 diagram.getAsComponent().validateGraph();
+
                 diagram.getView().validate();
             } finally {
                 diagram.getModel().endUpdate();