Search
Composite Commands

Sometimes you might prefer having a set of actions undo and redo as a single atomic operation. MindFusion.Diagramming provides the CompositeCmd class to help you with that. CompositeCmd objects act as containers of other Command-derived objects. When Undo or Redo are invoked on a composite command, all its subcommands are respectively undone or redone.

To add a command to the composite call the AddSubCmd method. Call Execute to execute all subcommands contained in the composite. Subcommands are executed or redone in the same order in which they were added to the container, and are undone in the opposite order.

Often MindFusion.Diagramming implicitly generates Command objects in response to users actions or method calls, and automatically records them in the history queue. You do not have direct access to those commands and cannot add them to a composite by AddSubCmd. To enable creating composites in such cases, the undo manager provides the notion of active composite. Active composite is created by calling the StartComposite function of UndoManager. Each action executed after that is made part of the composite, instead of adding it directly to the command history. Invoking the Execute method of the current composite completes it and saves it in the history queue.

C#  Copy Code

// Implicitly created undo records will be saved here
CompositeCmd composite = diagram.UndoManager.StartComposite("Create group");

// Call methods that create undo records
ShapeNode node1 = diagram.Factory.CreateShapeNode(0, 0, 30, 30);
ShapeNode node2 = diagram.Factory.CreateShapeNode (0, 0, 15, 15);
Group g = diagram.Factory.CreateGroup(node1);
g.AttachToCorner(node2, 0);

// This goes to the active composite too
ChangeItemCmd change = new ChangeItemCmd(node2, "");
node2.Locked = true;
change.Execute();

// Save the active composite in history
composite.Execute();

Visual Basic  Copy Code

' Implicitly created undo records will be saved here
Dim composite As CompositeCmd = Diagram.UndoManager.StartComposite("Create group")

' Call methods that create undo records
Dim node1 As ShapeNode = Diagram.Factory.CreateShapeNode(0, 0, 30, 30)
Dim node2 As ShapeNode = Diagram.Factory.CreateShapeNode(0, 0, 15, 15)
Dim g As Group = Diagram.Factory.CreateGroup(node1)
g.AttachToCorner(node2, 0)

' This goes to the active composite too
Dim change As New ChangeItemCmd(node2, "")
node2.Locked = True
change.Execute()

' Save the active composite in history
composite.Execute()