using System; using System.Collections.Generic; using System.Text; namespace Netron.Diagramming.Core { /// /// Implements the ILayer interface/mechanism /// public partial class Layer : ILayer, IVersion { #region Events /// /// Occurs when an entity is added to the layer /// public event EventHandler OnEntityAdded; /// /// Occurs when an entity is removed from the layer /// public event EventHandler OnEntityRemoved; /// /// Occurs when the layer is cleared /// public event EventHandler OnClear; #endregion #region Fields // ------------------------------------------------------------------ /// /// Implementation of IVersion - the current version of /// Layer. /// // ------------------------------------------------------------------ protected const double layerVersion = 1.0; // ------------------------------------------------------------------ /// /// the Model field /// // ------------------------------------------------------------------ [NonSerialized] private IModel mModel; // ------------------------------------------------------------------ /// /// the Entities field /// // ------------------------------------------------------------------ private CollectionBase mEntities; // ------------------------------------------------------------------ /// /// the Name field /// // ------------------------------------------------------------------ private string mName; // ------------------------------------------------------------------ /// /// Specifies if this layer and all of its entities are drawn. /// // ------------------------------------------------------------------ protected bool mIsVisible = true; #endregion #region Properties // ------------------------------------------------------------------ /// /// Gets the current version. /// // ------------------------------------------------------------------ public virtual double Version { get { return layerVersion; } } // ------------------------------------------------------------------ /// /// Gets or sets if this layer and all of its entities are drawn. /// // ------------------------------------------------------------------ public bool IsVisible { get { return mIsVisible; } set { mIsVisible = value; } } // ------------------------------------------------------------------ /// /// Gets all shapes in this layer. /// // ------------------------------------------------------------------ public CollectionBase Shapes { get { CollectionBase shapes = new CollectionBase(); foreach (IDiagramEntity entity in mEntities) { if (entity is IShape) shapes.Add(entity as IShape); } return shapes; } } // ------------------------------------------------------------------ /// /// Gets all connections in this layer. /// // ------------------------------------------------------------------ public CollectionBase Connections { get { CollectionBase cons = new CollectionBase(); foreach (IDiagramEntity entity in mEntities) { if (entity is IConnection) cons.Add(entity as IConnection); } return cons; } } // ------------------------------------------------------------------ /// /// Gets or sets the Model /// // ------------------------------------------------------------------ public IModel Model { get { return mModel; } set { if (value == null) throw new InconsistencyException("The model you want to set has value 'null'."); mModel = value; foreach (IDiagramEntity entity in mEntities) entity.Model = value; } } // ------------------------------------------------------------------ /// /// Gets or sets the Name /// // ------------------------------------------------------------------ public string Name { get { return mName; } set { mName = value; } } // ------------------------------------------------------------------ /// /// Gets or sets the Entities /// // ------------------------------------------------------------------ public CollectionBase Entities { get { return mEntities; } //set { mEntities = value; } } #endregion #region Constructor // ------------------------------------------------------------------ /// /// Initializes a new instance of the class. /// /// The name. // ------------------------------------------------------------------ public Layer(string name) { mName = name; mEntities = new CollectionBase(); Init(); } private void AttachToEntityCollection(CollectionBase collection) { collection.OnItemAdded += new EventHandler>(mEntities_OnItemAdded); collection.OnItemRemoved += new EventHandler>(mEntities_OnItemRemoved); collection.OnClear += new EventHandler(mEntities_OnClear); } /// /// Initializes this object /// See also the event for post-deserialization actions to which this method is related. /// /// private void Init() { if (mEntities == null) throw new InconsistencyException("The entity collection is 'null'"); AttachToEntityCollection(mEntities); } /// /// Handles the OnClear event of the Entities. /// /// The source of the event. /// The instance containing the event data. void mEntities_OnClear(object sender, EventArgs e) { EventHandler handler = OnClear; if (handler != null) handler(sender, e); } /// /// Handles the OnItemRemoved event of the Entities /// /// The sender. /// The e. void mEntities_OnItemRemoved(object sender, CollectionEventArgs e) { EventHandler handler = OnEntityRemoved; if (handler != null) handler(this, new EntityEventArgs(e.Item)); } /// /// Bubbles the event up /// /// /// void mEntities_OnItemAdded(object sender, CollectionEventArgs e) { EventHandler handler = OnEntityAdded; if (handler != null) handler(this, new EntityEventArgs(e.Item)); } #endregion } }