[2768] | 1 | using System;
|
---|
| 2 | using System.Drawing;
|
---|
| 3 | using System.Runtime.Serialization;
|
---|
| 4 | using System.Windows.Forms;
|
---|
[4068] | 5 | namespace Netron.Diagramming.Core {
|
---|
| 6 | /// <summary>
|
---|
| 7 | /// A bundle is a collection of diagram entities which does not
|
---|
| 8 | /// necessarily have a place in the scene graph but merely serves
|
---|
| 9 | /// to bundle items together. Various actions and functions are made
|
---|
| 10 | /// possible by bundles; cut/copy/paste, templates, user selections
|
---|
| 11 | /// and so on. While a group can have sub-groups, bundles cannot be
|
---|
| 12 | /// contained in another and are not necessarily connected (in the sense
|
---|
| 13 | /// of a connected branch in the scene graph).
|
---|
| 14 | /// </summary>
|
---|
| 15 | [Serializable()]
|
---|
| 16 | public class Bundle : DiagramEntityBase, IBundle, ISerializable {
|
---|
| 17 |
|
---|
| 18 | #region Fields
|
---|
[2768] | 19 | /// <summary>
|
---|
[4068] | 20 | /// the Shapes field
|
---|
[2768] | 21 | /// </summary>
|
---|
[4068] | 22 | private CollectionBase<IDiagramEntity> mEntities =
|
---|
| 23 | new CollectionBase<IDiagramEntity>();
|
---|
[2768] | 24 |
|
---|
[4068] | 25 | #endregion
|
---|
[2768] | 26 |
|
---|
[4068] | 27 | #region Properties
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// Gets or sets the entities.
|
---|
| 30 | /// </summary>
|
---|
| 31 | /// <value>The m entities.</value>
|
---|
| 32 | public CollectionBase<IDiagramEntity> Entities {
|
---|
| 33 | get {
|
---|
| 34 | return mEntities;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// Gets the name of the entity.
|
---|
| 39 | /// </summary>
|
---|
| 40 | /// <value>The name of the entity.</value>
|
---|
| 41 | public override string EntityName {
|
---|
| 42 | get { return "bundle"; }
|
---|
| 43 | }
|
---|
[2768] | 44 |
|
---|
[4068] | 45 | /// <summary>
|
---|
| 46 | /// Gets the area that all entities in the bundle encompass.
|
---|
| 47 | /// </summary>
|
---|
| 48 | /// <value>The rectangle.</value>
|
---|
| 49 | public override Rectangle Rectangle {
|
---|
| 50 | get {
|
---|
| 51 | if (mEntities.Count == 1) {
|
---|
| 52 | return mEntities[0].Rectangle;
|
---|
| 53 | } else if (mEntities.Count > 1) {
|
---|
| 54 | Rectangle union = mEntities[0].Rectangle;
|
---|
| 55 | for (int i = 1; i < mEntities.Count; i++) {
|
---|
| 56 | IDiagramEntity entity = mEntities[i];
|
---|
| 57 | if (entity.Rectangle != Rectangle.Empty) {
|
---|
| 58 | union = Rectangle.Union(union, entity.Rectangle);
|
---|
[2768] | 59 | }
|
---|
[4068] | 60 | }
|
---|
| 61 | //maybe this should be inflated a little here?
|
---|
| 62 | return union;
|
---|
| 63 | } else {
|
---|
| 64 | return Rectangle.Empty;
|
---|
[2768] | 65 | }
|
---|
[4068] | 66 | }
|
---|
| 67 | //set { throw new InconsistencyException("The rectangle of a bundle is the union of its constituents and cannot be set."); }
|
---|
| 68 | }
|
---|
[2768] | 69 |
|
---|
[4068] | 70 | #endregion
|
---|
[2768] | 71 |
|
---|
[4068] | 72 | #region Constructor
|
---|
[2768] | 73 |
|
---|
[4068] | 74 | /// <summary>
|
---|
| 75 | /// Initializes a new instance of the <see cref="T:Bundle"/> class.
|
---|
| 76 | /// </summary>
|
---|
| 77 | public Bundle(IModel model)
|
---|
| 78 | : base(model) {
|
---|
| 79 | mEntities = new CollectionBase<IDiagramEntity>();
|
---|
| 80 | }
|
---|
[2768] | 81 |
|
---|
[4068] | 82 | /// <summary>
|
---|
| 83 | /// Initializes a new instance of the <see cref="T:Bundle"/> class.
|
---|
| 84 | /// </summary>
|
---|
| 85 | /// <param name="info">The info.</param>
|
---|
| 86 | /// <param name="context">The context.</param>
|
---|
| 87 | protected Bundle(
|
---|
| 88 | SerializationInfo info,
|
---|
| 89 | StreamingContext context)
|
---|
| 90 | : base(info, context) {
|
---|
| 91 | throw new NotImplementedException();
|
---|
| 92 | }
|
---|
| 93 | /// <summary>
|
---|
| 94 | /// Initializes a new instance of the <see cref="T:Bundle"/> class.
|
---|
| 95 | /// </summary>
|
---|
| 96 | public Bundle()
|
---|
| 97 | : base() {
|
---|
| 98 | mEntities = new CollectionBase<IDiagramEntity>();
|
---|
| 99 | }
|
---|
[2768] | 100 |
|
---|
[4068] | 101 | /// <summary>
|
---|
| 102 | /// Creates a new bundle using a given collection of diagram entities.
|
---|
| 103 | /// </summary>
|
---|
| 104 | /// <param name="collection"></param>
|
---|
| 105 | public Bundle(CollectionBase<IDiagramEntity> collection)
|
---|
| 106 | : base() {
|
---|
[2768] | 107 |
|
---|
[4068] | 108 | mEntities = new CollectionBase<IDiagramEntity>();
|
---|
| 109 | //we could assign it directly but let's make sure the collection does not
|
---|
| 110 | //contain unwanted elements
|
---|
| 111 | foreach (IDiagramEntity entity in collection) {
|
---|
| 112 | if ((entity is IShape) || (entity is IConnection) || (entity is IGroup))
|
---|
| 113 | mEntities.Add(entity);
|
---|
| 114 | }
|
---|
[2768] | 115 |
|
---|
[4068] | 116 | //the following line would give problem. The event handler attached to the Selection would be triggered when
|
---|
| 117 | //the mEntities collection is changed!
|
---|
| 118 | //mEntities = collection;
|
---|
[2768] | 119 |
|
---|
[4068] | 120 | }
|
---|
[2768] | 121 |
|
---|
[4068] | 122 | #endregion
|
---|
[2768] | 123 |
|
---|
[4068] | 124 | #region Methods
|
---|
| 125 | /// <summary>
|
---|
| 126 | /// The custom menu to be added to the base menu of this entity
|
---|
| 127 | /// </summary>
|
---|
| 128 | /// <returns>ToolStripItem[]</returns>
|
---|
| 129 | public override ToolStripItem[] Menu() {
|
---|
| 130 | return null;
|
---|
| 131 | }
|
---|
[2768] | 132 |
|
---|
[4068] | 133 | // ------------------------------------------------------------------
|
---|
| 134 | /// <summary>
|
---|
| 135 | /// Sets the 'IsSelected' property to false for all entities in the
|
---|
| 136 | /// bundle.
|
---|
| 137 | /// </summary>
|
---|
| 138 | // ------------------------------------------------------------------
|
---|
| 139 | public void DeSelectAll() {
|
---|
| 140 | foreach (IDiagramEntity entity in mEntities) {
|
---|
| 141 | entity.IsSelected = false;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
[2768] | 144 |
|
---|
[4068] | 145 | // ------------------------------------------------------------------
|
---|
| 146 | /// <summary>
|
---|
| 147 | /// Sets the 'Hovered' property for all entities in the bundle to
|
---|
| 148 | /// the value specified.
|
---|
| 149 | /// </summary>
|
---|
| 150 | // ------------------------------------------------------------------
|
---|
| 151 | public void SetHovered(bool isHovered) {
|
---|
| 152 | foreach (IDiagramEntity entity in mEntities) {
|
---|
| 153 | entity.Hovered = isHovered;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
[2768] | 156 |
|
---|
[4068] | 157 | // ------------------------------------------------------------------
|
---|
| 158 | /// <summary>
|
---|
| 159 | /// Populates a
|
---|
| 160 | /// <see cref="T:System.Runtime.Serialization.SerializationInfo"></see>
|
---|
| 161 | /// with the data needed to serialize the target object.
|
---|
| 162 | /// </summary>
|
---|
| 163 | /// <param name="info">The
|
---|
| 164 | /// <see cref="T:System.Runtime.Serialization.SerializationInfo"></see>
|
---|
| 165 | /// to populate with data.</param>
|
---|
| 166 | /// <param name="context">The destination (see
|
---|
| 167 | /// <see cref="T:System.Runtime.Serialization.StreamingContext"></see>)
|
---|
| 168 | /// for this serialization.</param>
|
---|
| 169 | /// <exception cref="T:System.Security.SecurityException">The caller
|
---|
| 170 | /// does not have the required permission. </exception>
|
---|
| 171 | // ------------------------------------------------------------------
|
---|
| 172 | public override void GetObjectData(
|
---|
| 173 | SerializationInfo info,
|
---|
| 174 | StreamingContext context) {
|
---|
| 175 | throw new NotImplementedException();
|
---|
| 176 | }
|
---|
[2768] | 177 |
|
---|
[4068] | 178 | // ------------------------------------------------------------------
|
---|
| 179 | /// <summary>
|
---|
| 180 | /// Paints the bundle.
|
---|
| 181 | /// </summary>
|
---|
| 182 | /// <param name="g">Graphics</param>
|
---|
| 183 | // ------------------------------------------------------------------
|
---|
| 184 | public override void Paint(Graphics g) {
|
---|
| 185 | foreach (IDiagramEntity entity in mEntities) {
|
---|
| 186 | entity.Paint(g);
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[2768] | 189 |
|
---|
[4068] | 190 | /// <summary>
|
---|
| 191 | /// Tests whether the bundle is hit by the mouse
|
---|
| 192 | /// </summary>
|
---|
| 193 | /// <param name="p"></param>
|
---|
| 194 | /// <returns></returns>
|
---|
| 195 | public override bool Hit(Point p) {
|
---|
| 196 | foreach (IDiagramEntity entity in mEntities) {
|
---|
| 197 | if (entity.Hit(p)) return true;
|
---|
| 198 | }
|
---|
| 199 | return false;
|
---|
| 200 | }
|
---|
[2768] | 201 |
|
---|
[4068] | 202 | /// <summary>
|
---|
| 203 | /// Invalidates the entity
|
---|
| 204 | /// </summary>
|
---|
| 205 | public override void Invalidate() {
|
---|
| 206 | foreach (IDiagramEntity entity in mEntities) {
|
---|
| 207 | entity.Invalidate();
|
---|
| 208 | }
|
---|
[2768] | 209 | }
|
---|
[4068] | 210 |
|
---|
| 211 | /// <summary>
|
---|
| 212 | /// Moves the entity on the canvas
|
---|
| 213 | /// </summary>
|
---|
| 214 | /// <param name="p">a shift vector</param>
|
---|
| 215 | public override void MoveBy(Point p) {
|
---|
| 216 | foreach (IDiagramEntity entity in mEntities) {
|
---|
| 217 | entity.MoveBy(p);
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | #endregion
|
---|
| 222 | }
|
---|
[2768] | 223 | }
|
---|