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