namespace Netron.Diagramming.Core {
///
/// Group command
///
class GroupCommand : Command {
#region Fields
IBundle bundle;
IController controller;
IGroup mGroup;
#endregion
#region Properties
///
/// Gets the newly created group after was called
///
public IGroup Group {
get { return mGroup; }
}
///
/// Gets the entities.
///
/// The shape.
public CollectionBase Entities {
get { return bundle.Entities; }
}
#endregion
#region Constructor
///
/// Initializes a new instance of the class.
///
/// The controller.
/// The bundle.
public GroupCommand(IController controller, IBundle bundle)
: base(controller) {
this.Text = "Group";
this.controller = controller;
this.bundle = bundle;//the bundle should contain only IShape and IConnection entities!
}
#endregion
#region Methods
///
/// Perform redo of this command.
///
public override void Redo() {
//create a new group; use the standard GroupShape or the CollapsibleGroupShape for a painted group with collapse/expand features.
//GroupShape group = new GroupShape(this.Controller.Model);
//CollapsibleGroupShape group = new CollapsibleGroupShape(this.controller.Model);
GroupShape group = new GroupShape(this.controller.Model);
//asign the entities to the group
group.Entities.Clear();
foreach (IDiagramEntity entity in bundle.Entities) {
//this will be recursive if an entity is itself an IGroup
entity.Group = group;
group.Entities.Add(entity);
}
//add the new group to the layer
this.Controller.Model.AddEntity(group);
mGroup = group;
//select the newly created group
CollectionBase col = new CollectionBase();
col.Add(mGroup);
this.Controller.Model.Selection.SelectedItems = col;
mGroup.Invalidate();
}
///
/// Perform undo of this command.
///
public override void Undo() {
if (mGroup.CanUnGroup == false) {
return;
}
//remove the group from the layer
this.Controller.Model.DefaultPage.DefaultLayer.Entities.Remove(mGroup);
// keep track of the entities removed.
CollectionBase removedItems =
new CollectionBase();
int numberOfItems = mGroup.Entities.Count;
//detach the entities from the group
for (int i = 0; i < numberOfItems; numberOfItems--) {
IDiagramEntity entity = mGroup.Entities[0];
//this will be recursive if an entity is itself an IGroup
entity.Group = null;
mGroup.Entities.Remove(entity);
Controller.Model.AddEntity(entity);
entity.Invalidate();
removedItems.Add(entity);
}
//change the visuals such that the entities in the group are selected
this.Controller.Model.Selection.SelectedItems = removedItems;
//mGroup.Entities.Clear();
//mGroup.Invalidate();
mGroup = null;
//note that the entities have never been disconnected from the layer
//so they don't have to be re-attached to the anything.
//The insertion of the Group simply got pushed in the scene-graph.
}
#endregion
}
}