namespace Netron.Diagramming.Core {
///
/// Ungroup command
///
class UngroupCommand : 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 group.
public UngroupCommand(IController controller, IGroup group)
: base(controller) {
this.Text = "Ungroup";
this.controller = controller;
this.mGroup = group;
bundle = new Bundle();
}
#endregion
#region Methods
///
/// Perform redo of this command.
///
public override void Redo() {
//remove the group from the layer
this.Controller.Model.DefaultPage.DefaultLayer.Entities.Remove(mGroup);
//detach the entities from the group
foreach (IDiagramEntity entity in mGroup.Entities) {
//this will be recursive if an entity is itself an IGroup
entity.Group = null;
bundle.Entities.Add(entity);
//mGroup.Entities.Remove(entity);
Controller.Model.AddEntity(entity);
}
//change the visuals such that the entities in the group are selected
CollectionBase col = new CollectionBase();
col.AddRange(mGroup.Entities);
this.Controller.Model.Selection.SelectedItems = col;
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.
}
///
/// Perform undo of this command.
///
public override void Undo() {
//create a new group
GroupShape group = new GroupShape(this.Controller.Model);
//asign the entities to the group
group.Entities = bundle.Entities;
foreach (IDiagramEntity entity in group.Entities) {
//this will be recursive if an entity is itself an IGroup
entity.Group = group;
}
//add the new group to the layer
this.Controller.Model.DefaultPage.DefaultLayer.Entities.Add(group);
mGroup = group;
//select the newly created group
CollectionBase col = new CollectionBase();
col.Add(mGroup);
this.Controller.Model.Selection.SelectedItems = col;
mGroup.Invalidate();
}
#endregion
}
}