1 | namespace Netron.Diagramming.Core {
|
---|
2 | /// <summary>
|
---|
3 | /// Group command
|
---|
4 | /// </summary>
|
---|
5 | class GroupCommand : Command {
|
---|
6 | #region Fields
|
---|
7 | IBundle bundle;
|
---|
8 | IController controller;
|
---|
9 | IGroup mGroup;
|
---|
10 | #endregion
|
---|
11 |
|
---|
12 | #region Properties
|
---|
13 | /// <summary>
|
---|
14 | /// Gets the newly created group after <see cref="Redo"/> was called
|
---|
15 | /// </summary>
|
---|
16 | public IGroup Group {
|
---|
17 | get { return mGroup; }
|
---|
18 | }
|
---|
19 |
|
---|
20 | /// <summary>
|
---|
21 | /// Gets the entities.
|
---|
22 | /// </summary>
|
---|
23 | /// <value>The shape.</value>
|
---|
24 | public CollectionBase<IDiagramEntity> Entities {
|
---|
25 | get { return bundle.Entities; }
|
---|
26 | }
|
---|
27 |
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 | #region Constructor
|
---|
31 | /// <summary>
|
---|
32 | /// Initializes a new instance of the <see cref="T:GroupCommand"/> class.
|
---|
33 | /// </summary>
|
---|
34 | /// <param name="controller">The controller.</param>
|
---|
35 | /// <param name="bundle">The bundle.</param>
|
---|
36 | public GroupCommand(IController controller, IBundle bundle)
|
---|
37 | : base(controller) {
|
---|
38 | this.Text = "Group";
|
---|
39 | this.controller = controller;
|
---|
40 | this.bundle = bundle;//the bundle should contain only IShape and IConnection entities!
|
---|
41 | }
|
---|
42 | #endregion
|
---|
43 |
|
---|
44 | #region Methods
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Perform redo of this command.
|
---|
48 | /// </summary>
|
---|
49 | public override void Redo() {
|
---|
50 | //create a new group; use the standard GroupShape or the CollapsibleGroupShape for a painted group with collapse/expand features.
|
---|
51 | //GroupShape group = new GroupShape(this.Controller.Model);
|
---|
52 | //CollapsibleGroupShape group = new CollapsibleGroupShape(this.controller.Model);
|
---|
53 | GroupShape group = new GroupShape(this.controller.Model);
|
---|
54 | //asign the entities to the group
|
---|
55 | group.Entities.Clear();
|
---|
56 |
|
---|
57 | foreach (IDiagramEntity entity in bundle.Entities) {
|
---|
58 | //this will be recursive if an entity is itself an IGroup
|
---|
59 | entity.Group = group;
|
---|
60 | group.Entities.Add(entity);
|
---|
61 | }
|
---|
62 | //add the new group to the layer
|
---|
63 | this.Controller.Model.AddEntity(group);
|
---|
64 |
|
---|
65 | mGroup = group;
|
---|
66 |
|
---|
67 | //select the newly created group
|
---|
68 | CollectionBase<IDiagramEntity> col = new CollectionBase<IDiagramEntity>();
|
---|
69 | col.Add(mGroup);
|
---|
70 | this.Controller.Model.Selection.SelectedItems = col;
|
---|
71 | mGroup.Invalidate();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Perform undo of this command.
|
---|
76 | /// </summary>
|
---|
77 | public override void Undo() {
|
---|
78 | if (mGroup.CanUnGroup == false) {
|
---|
79 | return;
|
---|
80 | }
|
---|
81 |
|
---|
82 | //remove the group from the layer
|
---|
83 | this.Controller.Model.DefaultPage.DefaultLayer.Entities.Remove(mGroup);
|
---|
84 |
|
---|
85 | // keep track of the entities removed.
|
---|
86 | CollectionBase<IDiagramEntity> removedItems =
|
---|
87 | new CollectionBase<IDiagramEntity>();
|
---|
88 |
|
---|
89 | int numberOfItems = mGroup.Entities.Count;
|
---|
90 | //detach the entities from the group
|
---|
91 | for (int i = 0; i < numberOfItems; numberOfItems--) {
|
---|
92 | IDiagramEntity entity = mGroup.Entities[0];
|
---|
93 | //this will be recursive if an entity is itself an IGroup
|
---|
94 | entity.Group = null;
|
---|
95 | mGroup.Entities.Remove(entity);
|
---|
96 | Controller.Model.AddEntity(entity);
|
---|
97 | entity.Invalidate();
|
---|
98 | removedItems.Add(entity);
|
---|
99 | }
|
---|
100 | //change the visuals such that the entities in the group are selected
|
---|
101 | this.Controller.Model.Selection.SelectedItems = removedItems;
|
---|
102 | //mGroup.Entities.Clear();
|
---|
103 |
|
---|
104 | //mGroup.Invalidate();
|
---|
105 |
|
---|
106 | mGroup = null;
|
---|
107 |
|
---|
108 | //note that the entities have never been disconnected from the layer
|
---|
109 | //so they don't have to be re-attached to the anything.
|
---|
110 | //The insertion of the Group simply got pushed in the scene-graph.
|
---|
111 |
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | #endregion
|
---|
117 | }
|
---|
118 |
|
---|
119 | } |
---|