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