using System; using System.Drawing; using System.Runtime.Serialization; using System.Windows.Forms; namespace Netron.Diagramming.Core { /// /// A bundle is a collection of diagram entities which does not /// necessarily have a place in the scene graph but merely serves /// to bundle items together. Various actions and functions are made /// possible by bundles; cut/copy/paste, templates, user selections /// and so on. While a group can have sub-groups, bundles cannot be /// contained in another and are not necessarily connected (in the sense /// of a connected branch in the scene graph). /// [Serializable()] public class Bundle : DiagramEntityBase, IBundle, ISerializable { #region Fields /// /// the Shapes field /// private CollectionBase mEntities = new CollectionBase(); #endregion #region Properties /// /// Gets or sets the entities. /// /// The m entities. public CollectionBase Entities { get { return mEntities; } } /// /// Gets the name of the entity. /// /// The name of the entity. public override string EntityName { get { return "bundle"; } } /// /// Gets the area that all entities in the bundle encompass. /// /// The rectangle. public override Rectangle Rectangle { get { if (mEntities.Count == 1) { return mEntities[0].Rectangle; } else if (mEntities.Count > 1) { Rectangle union = mEntities[0].Rectangle; for (int i = 1; i < mEntities.Count; i++) { IDiagramEntity entity = mEntities[i]; if (entity.Rectangle != Rectangle.Empty) { union = Rectangle.Union(union, entity.Rectangle); } } //maybe this should be inflated a little here? return union; } else { return Rectangle.Empty; } } //set { throw new InconsistencyException("The rectangle of a bundle is the union of its constituents and cannot be set."); } } #endregion #region Constructor /// /// Initializes a new instance of the class. /// public Bundle(IModel model) : base(model) { mEntities = new CollectionBase(); } /// /// Initializes a new instance of the class. /// /// The info. /// The context. protected Bundle( SerializationInfo info, StreamingContext context) : base(info, context) { throw new NotImplementedException(); } /// /// Initializes a new instance of the class. /// public Bundle() : base() { mEntities = new CollectionBase(); } /// /// Creates a new bundle using a given collection of diagram entities. /// /// public Bundle(CollectionBase collection) : base() { mEntities = new CollectionBase(); //we could assign it directly but let's make sure the collection does not //contain unwanted elements foreach (IDiagramEntity entity in collection) { if ((entity is IShape) || (entity is IConnection) || (entity is IGroup)) mEntities.Add(entity); } //the following line would give problem. The event handler attached to the Selection would be triggered when //the mEntities collection is changed! //mEntities = collection; } #endregion #region Methods /// /// The custom menu to be added to the base menu of this entity /// /// ToolStripItem[] public override ToolStripItem[] Menu() { return null; } // ------------------------------------------------------------------ /// /// Sets the 'IsSelected' property to false for all entities in the /// bundle. /// // ------------------------------------------------------------------ public void DeSelectAll() { foreach (IDiagramEntity entity in mEntities) { entity.IsSelected = false; } } // ------------------------------------------------------------------ /// /// Sets the 'Hovered' property for all entities in the bundle to /// the value specified. /// // ------------------------------------------------------------------ public void SetHovered(bool isHovered) { foreach (IDiagramEntity entity in mEntities) { entity.Hovered = isHovered; } } // ------------------------------------------------------------------ /// /// Populates a /// /// with the data needed to serialize the target object. /// /// The /// /// to populate with data. /// The destination (see /// ) /// for this serialization. /// The caller /// does not have the required permission. // ------------------------------------------------------------------ public override void GetObjectData( SerializationInfo info, StreamingContext context) { throw new NotImplementedException(); } // ------------------------------------------------------------------ /// /// Paints the bundle. /// /// Graphics // ------------------------------------------------------------------ public override void Paint(Graphics g) { foreach (IDiagramEntity entity in mEntities) { entity.Paint(g); } } /// /// Tests whether the bundle is hit by the mouse /// /// /// public override bool Hit(Point p) { foreach (IDiagramEntity entity in mEntities) { if (entity.Hit(p)) return true; } return false; } /// /// Invalidates the entity /// public override void Invalidate() { foreach (IDiagramEntity entity in mEntities) { entity.Invalidate(); } } /// /// Moves the entity on the canvas /// /// a shift vector public override void MoveBy(Point p) { foreach (IDiagramEntity entity in mEntities) { entity.MoveBy(p); } } #endregion } }