1 | using System;
|
---|
2 |
|
---|
3 | namespace Netron.Diagramming.Core {
|
---|
4 | /// <summary>
|
---|
5 | /// Implements the ILayer interface/mechanism
|
---|
6 | /// </summary>
|
---|
7 | public partial class Layer : ILayer, IVersion {
|
---|
8 | #region Events
|
---|
9 | /// <summary>
|
---|
10 | /// Occurs when an entity is added to the layer
|
---|
11 | /// </summary>
|
---|
12 | public event EventHandler<EntityEventArgs> OnEntityAdded;
|
---|
13 | /// <summary>
|
---|
14 | /// Occurs when an entity is removed from the layer
|
---|
15 | /// </summary>
|
---|
16 | public event EventHandler<EntityEventArgs> OnEntityRemoved;
|
---|
17 | /// <summary>
|
---|
18 | /// Occurs when the layer is cleared
|
---|
19 | /// </summary>
|
---|
20 | public event EventHandler OnClear;
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | #region Fields
|
---|
24 |
|
---|
25 | // ------------------------------------------------------------------
|
---|
26 | /// <summary>
|
---|
27 | /// Implementation of IVersion - the current version of
|
---|
28 | /// Layer.
|
---|
29 | /// </summary>
|
---|
30 | // ------------------------------------------------------------------
|
---|
31 | protected const double layerVersion = 1.0;
|
---|
32 |
|
---|
33 | // ------------------------------------------------------------------
|
---|
34 | /// <summary>
|
---|
35 | /// the Model field
|
---|
36 | /// </summary>
|
---|
37 | // ------------------------------------------------------------------
|
---|
38 | [NonSerialized]
|
---|
39 | private IModel mModel;
|
---|
40 |
|
---|
41 | // ------------------------------------------------------------------
|
---|
42 | /// <summary>
|
---|
43 | /// the Entities field
|
---|
44 | /// </summary>
|
---|
45 | // ------------------------------------------------------------------
|
---|
46 | private CollectionBase<IDiagramEntity> mEntities;
|
---|
47 |
|
---|
48 | // ------------------------------------------------------------------
|
---|
49 | /// <summary>
|
---|
50 | /// the Name field
|
---|
51 | /// </summary>
|
---|
52 | // ------------------------------------------------------------------
|
---|
53 | private string mName;
|
---|
54 |
|
---|
55 | // ------------------------------------------------------------------
|
---|
56 | /// <summary>
|
---|
57 | /// Specifies if this layer and all of its entities are drawn.
|
---|
58 | /// </summary>
|
---|
59 | // ------------------------------------------------------------------
|
---|
60 | protected bool mIsVisible = true;
|
---|
61 |
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Properties
|
---|
65 |
|
---|
66 | // ------------------------------------------------------------------
|
---|
67 | /// <summary>
|
---|
68 | /// Gets the current version.
|
---|
69 | /// </summary>
|
---|
70 | // ------------------------------------------------------------------
|
---|
71 | public virtual double Version {
|
---|
72 | get {
|
---|
73 | return layerVersion;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | // ------------------------------------------------------------------
|
---|
78 | /// <summary>
|
---|
79 | /// Gets or sets if this layer and all of its entities are drawn.
|
---|
80 | /// </summary>
|
---|
81 | // ------------------------------------------------------------------
|
---|
82 | public bool IsVisible {
|
---|
83 | get {
|
---|
84 | return mIsVisible;
|
---|
85 | }
|
---|
86 | set {
|
---|
87 | mIsVisible = value;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | // ------------------------------------------------------------------
|
---|
92 | /// <summary>
|
---|
93 | /// Gets all shapes in this layer.
|
---|
94 | /// </summary>
|
---|
95 | // ------------------------------------------------------------------
|
---|
96 | public CollectionBase<IShape> Shapes {
|
---|
97 | get {
|
---|
98 | CollectionBase<IShape> shapes = new CollectionBase<IShape>();
|
---|
99 | foreach (IDiagramEntity entity in mEntities) {
|
---|
100 | if (entity is IShape)
|
---|
101 | shapes.Add(entity as IShape);
|
---|
102 | }
|
---|
103 | return shapes;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | // ------------------------------------------------------------------
|
---|
108 | /// <summary>
|
---|
109 | /// Gets all connections in this layer.
|
---|
110 | /// </summary>
|
---|
111 | // ------------------------------------------------------------------
|
---|
112 | public CollectionBase<IConnection> Connections {
|
---|
113 | get {
|
---|
114 | CollectionBase<IConnection> cons = new CollectionBase<IConnection>();
|
---|
115 | foreach (IDiagramEntity entity in mEntities) {
|
---|
116 | if (entity is IConnection)
|
---|
117 | cons.Add(entity as IConnection);
|
---|
118 | }
|
---|
119 | return cons;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | // ------------------------------------------------------------------
|
---|
124 | /// <summary>
|
---|
125 | /// Gets or sets the Model
|
---|
126 | /// </summary>
|
---|
127 | // ------------------------------------------------------------------
|
---|
128 | public IModel Model {
|
---|
129 | get { return mModel; }
|
---|
130 | set {
|
---|
131 | if (value == null)
|
---|
132 | throw new InconsistencyException("The model you want to set has value 'null'.");
|
---|
133 |
|
---|
134 | mModel = value;
|
---|
135 | foreach (IDiagramEntity entity in mEntities)
|
---|
136 | entity.Model = value;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | // ------------------------------------------------------------------
|
---|
141 | /// <summary>
|
---|
142 | /// Gets or sets the Name
|
---|
143 | /// </summary>
|
---|
144 | // ------------------------------------------------------------------
|
---|
145 | public string Name {
|
---|
146 | get { return mName; }
|
---|
147 | set { mName = value; }
|
---|
148 | }
|
---|
149 |
|
---|
150 | // ------------------------------------------------------------------
|
---|
151 | /// <summary>
|
---|
152 | /// Gets or sets the Entities
|
---|
153 | /// </summary>
|
---|
154 | // ------------------------------------------------------------------
|
---|
155 | public CollectionBase<IDiagramEntity> Entities {
|
---|
156 | get { return mEntities; }
|
---|
157 | //set { mEntities = value; }
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | #endregion
|
---|
162 |
|
---|
163 | #region Constructor
|
---|
164 |
|
---|
165 | // ------------------------------------------------------------------
|
---|
166 | /// <summary>
|
---|
167 | /// Initializes a new instance of the <see cref="T:Layer"/> class.
|
---|
168 | /// </summary>
|
---|
169 | /// <param name="name">The name.</param>
|
---|
170 | // ------------------------------------------------------------------
|
---|
171 | public Layer(string name) {
|
---|
172 | mName = name;
|
---|
173 | mEntities = new CollectionBase<IDiagramEntity>();
|
---|
174 | Init();
|
---|
175 | }
|
---|
176 | private void AttachToEntityCollection(CollectionBase<IDiagramEntity> collection) {
|
---|
177 | collection.OnItemAdded += new EventHandler<CollectionEventArgs<IDiagramEntity>>(mEntities_OnItemAdded);
|
---|
178 | collection.OnItemRemoved += new EventHandler<CollectionEventArgs<IDiagramEntity>>(mEntities_OnItemRemoved);
|
---|
179 | collection.OnClear += new EventHandler(mEntities_OnClear);
|
---|
180 | }
|
---|
181 | /// <summary>
|
---|
182 | /// Initializes this object
|
---|
183 | /// <remarks>See also the <see cref="OnDeserialized"/> event for post-deserialization actions to which this method is related.
|
---|
184 | /// </remarks>
|
---|
185 | /// </summary>
|
---|
186 | private void Init() {
|
---|
187 | if (mEntities == null)
|
---|
188 | throw new InconsistencyException("The entity collection is 'null'");
|
---|
189 | AttachToEntityCollection(mEntities);
|
---|
190 | }
|
---|
191 | /// <summary>
|
---|
192 | /// Handles the OnClear event of the Entities.
|
---|
193 | /// </summary>
|
---|
194 | /// <param name="sender">The source of the event.</param>
|
---|
195 | /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
|
---|
196 | void mEntities_OnClear(object sender, EventArgs e) {
|
---|
197 | EventHandler handler = OnClear;
|
---|
198 | if (handler != null)
|
---|
199 | handler(sender, e);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /// <summary>
|
---|
203 | /// Handles the OnItemRemoved event of the Entities
|
---|
204 | /// </summary>
|
---|
205 | /// <param name="sender">The sender.</param>
|
---|
206 | /// <param name="e">The e.</param>
|
---|
207 | void mEntities_OnItemRemoved(object sender, CollectionEventArgs<IDiagramEntity> e) {
|
---|
208 | EventHandler<EntityEventArgs> handler = OnEntityRemoved;
|
---|
209 | if (handler != null)
|
---|
210 | handler(this, new EntityEventArgs(e.Item));
|
---|
211 | }
|
---|
212 |
|
---|
213 | /// <summary>
|
---|
214 | /// Bubbles the event up
|
---|
215 | /// </summary>
|
---|
216 | /// <param name="sender"></param>
|
---|
217 | /// <param name="e"></param>
|
---|
218 | void mEntities_OnItemAdded(object sender, CollectionEventArgs<IDiagramEntity> e) {
|
---|
219 | EventHandler<EntityEventArgs> handler = OnEntityAdded;
|
---|
220 | if (handler != null)
|
---|
221 | handler(this, new EntityEventArgs(e.Item));
|
---|
222 | }
|
---|
223 | #endregion
|
---|
224 | }
|
---|
225 | }
|
---|