Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/BaseClasses/DiagramEntityBase.cs @ 2768

Last change on this file since 2768 was 2768, checked in by mkommend, 14 years ago

added solution folders and sources for the netron library (ticket #867)

File size: 49.6 KB
Line 
1using System;
2using System.Drawing;
3using System.ComponentModel;
4
5using System.Windows.Forms;
6using System.Collections.Generic;
7using System.Drawing.Drawing2D;
8namespace Netron.Diagramming.Core
9{
10    // ----------------------------------------------------------------------
11    /// <summary>
12    /// Abstract base class for every diagram entity.
13    /// </summary>
14    // ----------------------------------------------------------------------
15    public abstract partial class DiagramEntityBase :
16        IDiagramEntity,
17        IMouseListener,
18        IHoverListener,
19        IKeyboardListener,
20        IDisposable
21    {
22        #region Events
23
24        // ------------------------------------------------------------------
25        /// <summary>
26        /// Occurs when the user click on the entity.
27        /// </summary>
28        // ------------------------------------------------------------------
29        public event EventHandler<EntityEventArgs> OnClick;
30
31        // ------------------------------------------------------------------
32        /// <summary>
33        /// Occurs when a mouse button is pressed while over the entity.
34        /// </summary>
35        // ------------------------------------------------------------------
36        public event EventHandler<EntityMouseEventArgs> OnMouseDown;
37
38        // ------------------------------------------------------------------
39        /// <summary>
40        /// Occurs when a mouse button is released while over the entity.
41        /// </summary>
42        // ------------------------------------------------------------------
43        public event EventHandler<EntityMouseEventArgs> OnMouseUp;
44
45        // ------------------------------------------------------------------
46        /// <summary>
47        /// Occurs when the mouse is moved while over the entity.
48        /// </summary>
49        // ------------------------------------------------------------------
50        public event EventHandler<EntityMouseEventArgs> OnMouseMove;
51
52        // ------------------------------------------------------------------
53        /// <summary>
54        /// Occurs when the mouse enters the entity.
55        /// </summary>
56        // ------------------------------------------------------------------
57        public event EventHandler<EntityMouseEventArgs> OnMouseEnter;
58
59        // ------------------------------------------------------------------
60        /// <summary>
61        /// Occurs when the mouse hovers over the entity.
62        /// </summary>
63        // ------------------------------------------------------------------
64        public event EventHandler<EntityMouseEventArgs> OnMouseHover;
65
66        // ------------------------------------------------------------------
67        /// <summary>
68        /// Occurs when the mouse leaves the entity.
69        /// </summary>
70        // ------------------------------------------------------------------
71        public event EventHandler<EntityMouseEventArgs> OnMouseLeave;
72
73        // ------------------------------------------------------------------
74        /// <summary>
75        /// Occurs when the entity's properties have changed
76        /// </summary>
77        // ------------------------------------------------------------------
78        public event EventHandler<EntityEventArgs> OnEntityChange;
79
80        // ------------------------------------------------------------------
81        /// <summary>
82        /// Occurs when the entity is selected. This can be different than the
83        /// OnClick because the selector can select and entity without
84        /// clicking on it.
85        /// </summary>
86        // ------------------------------------------------------------------
87        public event EventHandler<EntityEventArgs> OnEntitySelect;
88
89        #endregion
90
91        #region Fields
92
93        // ------------------------------------------------------------------
94        /// <summary>
95        /// Implementation of IVersion - the current version of
96        /// DiagramEntityBase.
97        /// </summary>
98        // ------------------------------------------------------------------
99        protected const double diagramEntityBaseVersion = 1.0;
100
101        // ------------------------------------------------------------------
102        /// <summary>
103        /// The services of this entity.
104        /// </summary>
105        // ------------------------------------------------------------------
106        protected Dictionary<Type, IInteraction> mServices;
107
108        // ------------------------------------------------------------------
109        /// <summary>
110        /// The Rectangle on which any bundle lives.
111        /// </summary>
112        // ------------------------------------------------------------------
113        protected Rectangle mRectangle = Rectangle.Empty;
114
115        // ------------------------------------------------------------------
116        /// <summary>
117        /// General prupose tag
118        /// </summary>
119        // ------------------------------------------------------------------
120        protected object mTag;
121
122        // ------------------------------------------------------------------
123        /// <summary>
124        /// tells whether the current entity is hovered by the mouse
125        /// </summary>
126        // ------------------------------------------------------------------
127        protected bool mHovered;
128
129        // ------------------------------------------------------------------
130        /// <summary>
131        /// The current magnification of the view.
132        /// </summary>
133        // ------------------------------------------------------------------
134        protected SizeF mMagnification = new SizeF(100F, 100F);
135
136        // ------------------------------------------------------------------
137        /// <summary>
138        /// The Model to which the eneity belongs.
139        /// </summary>
140        // ------------------------------------------------------------------
141        protected IModel mModel;
142
143        // ------------------------------------------------------------------
144        /// <summary>
145        /// The layer to which this entity is attached in the Model.
146        /// </summary>
147        // ------------------------------------------------------------------
148        protected ILayer mLayer;
149
150        // ------------------------------------------------------------------
151        /// <summary>
152        /// tells whether the entity is selected
153        /// </summary>
154        // ------------------------------------------------------------------
155        protected bool mIsSelected;
156
157        // ------------------------------------------------------------------
158        /// <summary>
159        /// the current draw style
160        /// </summary>
161        // ------------------------------------------------------------------
162        protected IPenStyle mPenStyle;
163
164        // ------------------------------------------------------------------
165        /// <summary>
166        /// the current paint style
167        /// </summary>
168        // ------------------------------------------------------------------
169        protected IPaintStyle mPaintStyle;
170
171        // ------------------------------------------------------------------
172        /// <summary>
173        /// the default pen to be used by the Paint method
174        /// </summary>
175        // ------------------------------------------------------------------
176        protected Pen mPen;
177
178        // ------------------------------------------------------------------
179        /// <summary>
180        /// the default brush to be used by the Paint method
181        /// </summary>
182        // ------------------------------------------------------------------
183        protected Brush mBrush;
184
185        // ------------------------------------------------------------------
186        /// <summary>
187        /// the name of the entity
188        /// </summary>
189        // ------------------------------------------------------------------
190        protected string mName;
191
192        // ------------------------------------------------------------------
193        /// <summary>
194        /// a weak reference to the parent
195        /// </summary>
196        // ------------------------------------------------------------------
197        protected WeakReference mParent;
198
199        // ------------------------------------------------------------------
200        /// <summary>
201        /// the scene index, i.e. the index of this entity in the scene-graph.
202        /// </summary>
203        // ------------------------------------------------------------------
204        protected int mSceneIndex;
205
206        // ------------------------------------------------------------------
207        /// <summary>
208        /// The top-group to underneath which this entity resides.
209        /// </summary>
210        // ------------------------------------------------------------------
211        protected IGroup mGroup;
212
213        // ------------------------------------------------------------------
214        /// <summary>
215        /// Specifies if the entity can be moved.
216        /// </summary>
217        // ------------------------------------------------------------------
218        protected bool mAllowMove = true;
219
220        // ------------------------------------------------------------------
221        /// <summary>
222        /// Specifies if the entity can be deleted.
223        /// </summary>
224        // ------------------------------------------------------------------
225        protected bool mAllowDelete = true;
226
227        // ------------------------------------------------------------------
228        /// <summary>
229        /// Specifies if the entity can be resized.
230        /// </summary>
231        // ------------------------------------------------------------------
232        protected bool mResizable = true;
233
234        // ------------------------------------------------------------------
235        /// <summary>
236        /// The minimum size this entity can be.  The default value is:
237        /// width = 10, height = 10.  This seems to be the min size that
238        /// keeps the connectors relative position correct during a Transform.
239        /// </summary>
240        // ------------------------------------------------------------------
241        protected Size myMinSize = new Size(10, 10);
242
243        // ------------------------------------------------------------------
244        /// <summary>
245        /// The maximum size this entity can be.  The default value is:
246        /// width = 10000, height = 10000.
247        /// </summary>
248        // ------------------------------------------------------------------
249        protected Size myMaxSize = new Size(10000, 10000);
250
251        // ------------------------------------------------------------------
252        /// <summary>
253        /// The unique identifier of this entity
254        /// </summary>
255        // ------------------------------------------------------------------
256        protected Guid mUid = Guid.NewGuid();
257
258        // ------------------------------------------------------------------
259        /// <summary>
260        /// whether the entity is visible
261        /// </summary>
262        // ------------------------------------------------------------------
263        protected bool mVisible = true;
264
265        // ------------------------------------------------------------------
266        /// <summary>
267        /// The Enabled field.
268        /// </summary>
269        // ------------------------------------------------------------------
270        protected bool mEnabled = true;
271       
272        #endregion
273
274        #region Properties
275
276        // ------------------------------------------------------------------
277        /// <summary>
278        /// Gets the current version.
279        /// </summary>
280        // ------------------------------------------------------------------
281        public virtual double Version
282        {
283            get
284            {
285                return diagramEntityBaseVersion;
286            }
287        }
288
289        // ------------------------------------------------------------------
290        /// <summary>
291        /// Gets the services provided by this entity.
292        /// </summary>
293        /// <value>The services.</value>
294        // ------------------------------------------------------------------
295        public Dictionary<Type, IInteraction> Services
296        {
297            get { return mServices; }
298        }
299
300        // ------------------------------------------------------------------
301        /// <summary>
302        /// Gets or sets whether this entity is Enabled.
303        /// </summary>
304        // ------------------------------------------------------------------
305        public virtual bool Enabled
306        {
307            get
308            {
309                return mEnabled;
310            }
311            set
312            {
313                mEnabled = value;
314                RaiseOnChange(this, new EntityEventArgs(this));
315            }
316        }
317
318        // ------------------------------------------------------------------
319        /// <summary>
320        /// Gets or sets a value indicating whether this entity is visible.
321        /// </summary>
322        /// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
323        // ------------------------------------------------------------------
324        public virtual bool Visible
325        {
326            get
327            {
328                return mVisible;
329            }
330            set
331            {
332                mVisible = value;
333                RaiseOnChange(this, new EntityEventArgs(this));
334            }
335        }
336
337        // ------------------------------------------------------------------
338        /// <summary>
339        /// Gets or sets the drawing style.
340        /// </summary>
341        /// <value>The draw style.</value>
342        // ------------------------------------------------------------------
343        public virtual IPenStyle PenStyle
344        {
345            get
346            {
347                return mPenStyle;
348            }
349            set
350            {
351                mPenStyle = value;
352                RaiseOnChange(this, new EntityEventArgs(this));
353                UpdatePaintingMaterial();
354            }
355        }
356
357        // ------------------------------------------------------------------
358        /// <summary>
359        /// Gets or sets the paint style.
360        /// </summary>
361        /// <value>The paint style.</value>
362        // ------------------------------------------------------------------
363        public virtual IPaintStyle PaintStyle
364        {
365            get
366            {
367                return mPaintStyle;
368            }
369            set
370            {
371                mPaintStyle = value;
372                RaiseOnChange(this, new EntityEventArgs(this));
373                UpdatePaintingMaterial();
374            }
375        }
376
377        // ------------------------------------------------------------------
378        /// <summary>
379        /// Gets the globally unique identifier of this entity
380        /// </summary>
381        /// <value></value>
382        // ------------------------------------------------------------------
383        public virtual Guid Uid
384        {
385            get
386            {
387                return mUid;
388            }
389
390        }
391
392        // ------------------------------------------------------------------
393        /// <summary>
394        /// Gets or sets a value indicating whether this
395        /// <see cref="IDiagramEntity"/> can be moved.
396        /// </summary>
397        /// <value><c>true</c> if movable; otherwise, <c>false</c>.</value>
398        // ------------------------------------------------------------------
399        public virtual bool AllowMove
400        {
401            get
402            {
403                return mAllowMove;
404            }
405            set
406            {
407                mAllowMove = value;
408            }
409        }
410
411        // ------------------------------------------------------------------
412        /// <summary>
413        /// Gets or sets a value indicating whether this
414        /// <see cref="IDiagramEntity"/> can be deleted.
415        /// </summary>
416        /// <value><c>true</c> if deletable; otherwise, <c>false</c>.</value>
417        // ------------------------------------------------------------------
418        public virtual bool AllowDelete
419        {
420            get
421            {
422                return mAllowDelete;
423            }
424            set
425            {
426                mAllowDelete = value;
427            }
428        }
429
430        // ------------------------------------------------------------------
431        /// <summary>
432        /// Gets or sets the Resizable
433        /// </summary>
434        // ------------------------------------------------------------------
435        public virtual bool Resizable
436        {
437            get
438            {
439                return mResizable;
440            }
441            set
442            {
443                mResizable = value;
444            }
445        }
446
447        // ------------------------------------------------------------------
448        /// <summary>
449        /// Gets the minimum size of the entity.
450        /// </summary>
451        // ------------------------------------------------------------------
452        public virtual Size MinSize
453        {
454            get
455            {
456                return myMinSize;
457            }
458        }
459
460        // ------------------------------------------------------------------
461        /// <summary>
462        /// Gets the maximum size of the entity.
463        /// </summary>
464        // ------------------------------------------------------------------
465        public virtual Size MaxSize
466        {
467            get
468            {
469                return myMaxSize;
470            }
471        }
472
473        // ------------------------------------------------------------------
474        /// <summary>
475        /// Gets the <see cref="Brush"/> to paint this entity.
476        /// </summary>
477        // ------------------------------------------------------------------
478        public virtual Brush Brush
479        {
480            get
481            {
482                return mBrush;
483            }
484        }
485
486        // ------------------------------------------------------------------
487        /// <summary>
488        /// Gets the pen to draw this entity.
489        /// </summary>
490        /// <value>The pen.</value>
491        // ------------------------------------------------------------------
492        public virtual Pen Pen
493        {
494            get
495            {
496                return mPen;
497            }
498        }
499
500        // ------------------------------------------------------------------
501        /// <summary>
502        /// Gets the friendly name of the entity to be displayed in the UI.
503        /// </summary>
504        /// <value></value>
505        // ------------------------------------------------------------------
506        public abstract string EntityName
507        {
508            get;
509        }
510
511        // ------------------------------------------------------------------
512        /// <summary>
513        /// Gets or sets the general purpose tag
514        /// </summary>
515        // ------------------------------------------------------------------
516        public virtual object Tag
517        {
518            get
519            {
520                return mTag;
521            }
522            set
523            {
524                mTag = value;
525            }
526        }
527
528        // ------------------------------------------------------------------
529        /// <summary>
530        /// Gets or sets the index of this entity in the scene-graph.
531        /// </summary>
532        /// <value>The index of the scene.</value>
533        // ------------------------------------------------------------------
534        public virtual int SceneIndex
535        {
536            get
537            {
538                return mSceneIndex;
539            }
540            set
541            {
542                mSceneIndex = value;
543            }
544        }
545
546        // ------------------------------------------------------------------
547        /// <summary>
548        /// Gets or sets the unique top-group to which this entity belongs.
549        /// </summary>
550        /// <value></value>
551        // ------------------------------------------------------------------
552        public virtual IGroup Group
553        {
554            get
555            {
556                return mGroup;
557            }
558            set
559            {
560                mGroup = value;
561                // Propagate downwards if this is a group shape, but not if
562                // the value is 'null' since the group becomes the value of
563                // the Group property.  Note that we could have used a formal
564                // depth-traversal algorithm.
565                if (this is IGroup)
566                {
567                    if (value == null)//occurs on an ungroup action
568                    {
569                        foreach (IDiagramEntity entity in
570                            (this as IGroup).Entities)
571                        {
572                            entity.Group = this as IGroup;
573                        }
574                    }
575                    else //occurs when grouping
576                    {
577                        foreach (IDiagramEntity entity in
578                            (this as IGroup).Entities)
579                        {
580                            entity.Group = value;
581                        }
582                    }
583                }
584
585            }
586        }
587
588        // ------------------------------------------------------------------
589        /// <summary>
590        /// Gets or sets whether the entity is hovered by the mouse
591        /// </summary>
592        // ------------------------------------------------------------------
593        public virtual bool Hovered
594        {
595            get
596            {
597                return mHovered;
598            }
599            set
600            {
601                mHovered = value;
602                Invalidate();
603            }
604        }
605
606        // ------------------------------------------------------------------
607        /// <summary>
608        /// Gets or sets the parent of the entity
609        /// </summary>
610        // ------------------------------------------------------------------
611        public virtual object Parent
612        {
613            get
614            {
615                if (mParent != null && mParent.IsAlive)
616                {
617                    return mParent.Target;
618                }
619                else
620                {
621                    return null;
622                }
623            }
624            set
625            {
626                mParent = new WeakReference(value);
627                RaiseOnChange(this, new EntityEventArgs(this));
628            }
629        }
630
631        // ------------------------------------------------------------------
632        /// <summary>
633        /// Gets or sets the name of the entity
634        /// </summary>
635        // ------------------------------------------------------------------
636        public virtual string Name
637        {
638            get
639            {
640                return mName;
641            }
642            set
643            {
644                mName = value;
645                RaiseOnChange(this, new EntityEventArgs(this));
646            }
647        }
648
649        #region Bounds and point calculations
650
651        // ------------------------------------------------------------------
652        /// <summary>
653        /// Gets the bounds of the paintable entity.
654        /// </summary>
655        /// <value></value>
656        // ------------------------------------------------------------------
657        public abstract Rectangle Rectangle
658        {
659            get;
660        }
661
662        // ------------------------------------------------------------------
663        /// <summary>
664        /// Gets the top left corner of this entity, which is the same as
665        /// 'Rectangle.Location'.
666        /// </summary>
667        // ------------------------------------------------------------------
668        public virtual Point TopLeftCorner
669        {
670            get
671            {               
672                return this.Rectangle.Location;
673            }
674        }
675
676        // ------------------------------------------------------------------
677        /// <summary>
678        /// Gets the top right corner of this entity.
679        /// </summary>
680        // ------------------------------------------------------------------
681        public virtual Point TopRightCorner
682        {
683            get
684            {
685                return new Point(
686                    Rectangle.Right,
687                    Rectangle.Top);
688            }
689        }
690
691        // ------------------------------------------------------------------
692        /// <summary>
693        /// Gets the bottom left corner of this entity.
694        /// </summary>
695        // ------------------------------------------------------------------
696        public virtual Point BottomLeftCorner
697        {
698            get
699            {
700                return new Point(
701                    Rectangle.Left,
702                    Rectangle.Bottom);
703            }
704        }
705
706        // ------------------------------------------------------------------
707        /// <summary>
708        /// Gets the bottom right corner of this entity.
709        /// </summary>
710        // ------------------------------------------------------------------
711        public virtual Point BottomRightCorner
712        {
713            get
714            {
715                return new Point(
716                    Rectangle.Right,
717                    Rectangle.Bottom);
718            }
719        }
720
721        // ------------------------------------------------------------------
722        /// <summary>
723        /// Gets the center point of the paintable entity (the center of the
724        /// Rectangle).
725        /// </summary>
726        /// <value>Point</value>
727        // ------------------------------------------------------------------
728        public virtual Point Center
729        {
730            get
731            {
732                // Make sure the bounds are legal first.
733                if ((this.mRectangle == null) ||
734                    (this.mRectangle == Rectangle.Empty))
735                {
736                    return Point.Empty;
737                }
738
739                int x =
740                    (this.mRectangle.Left) +
741                    (this.mRectangle.Width / 2);
742
743                int y =
744                    (this.mRectangle.Top) +
745                    (this.mRectangle.Height / 2);
746                return new Point(x, y);
747            }
748        }
749
750        // ------------------------------------------------------------------
751        /// <summary>
752        /// Gets the center of the bottom edge of the bounding rectangle.
753        /// </summary>
754        // ------------------------------------------------------------------
755        public virtual Point BottomCenter
756        {
757            get
758            {
759                return new Point(
760                    BottomLeftCorner.X + (mRectangle.Width / 2),
761                    BottomLeftCorner.Y);
762            }
763        }
764
765        // ------------------------------------------------------------------
766        /// <summary>
767        /// Gets the center of the top edge of the bounding rectangle.
768        /// </summary>
769        // ------------------------------------------------------------------
770        public virtual Point TopCenter
771        {
772            get
773            {
774                return new Point(
775                    TopLeftCorner.X + (mRectangle.Width / 2),
776                    TopLeftCorner.Y);
777            }
778        }
779
780        #endregion
781
782        // ------------------------------------------------------------------
783        /// <summary>
784        /// Gets or sets whether the entity is selected
785        /// </summary>
786        // ------------------------------------------------------------------
787        [Browsable(false)]
788        public virtual bool IsSelected
789        {
790            get
791            {
792                return mIsSelected;
793            }
794            set
795            {
796                mIsSelected = value;
797                if (value == true)
798                {
799                    this.RaiseOnSelect(this, new EntityEventArgs(this));
800                }
801            }
802        }
803
804        // ------------------------------------------------------------------
805        /// <summary>
806        /// Gets or sets the current magnification used by the view.
807        /// </summary>
808        // ------------------------------------------------------------------
809        public virtual SizeF Magnification
810        {
811            get
812            {
813                return mMagnification;
814            }
815            set
816            {
817                mMagnification = value;
818            }
819        }
820
821        // ------------------------------------------------------------------
822        /// <summary>
823        /// Gets or sets the canvas to which the entity belongs.
824        /// </summary>
825        // ------------------------------------------------------------------
826        [Browsable(false)]
827        public virtual IModel Model
828        {
829            get
830            {
831                return mModel;
832            }
833            set
834            {
835                mModel = value;
836            }
837        }
838
839        // ------------------------------------------------------------------
840        /// <summary>
841        /// Gets or sets the ILayer this entity is attached to in the IModel.
842        /// </summary>
843        // ------------------------------------------------------------------
844        public virtual ILayer Layer
845        {
846            get
847            {
848                return mLayer;
849            }
850            set
851            {
852                mLayer = value;
853            }
854        }
855
856        #endregion
857
858        #region Constructor
859
860        // ------------------------------------------------------------------
861        /// <summary>
862        /// Constructor with the model of the entity.
863        /// </summary>
864        /// <param mName="model">IModel</param>
865        // ------------------------------------------------------------------
866        protected DiagramEntityBase(IModel model)
867        {
868            this.mModel = model;
869            Initialize();
870        }
871
872        // ------------------------------------------------------------------
873        /// <summary>
874        /// The empty constructor is required to make deserialization work.
875        /// </summary>
876        // ------------------------------------------------------------------
877        protected DiagramEntityBase()
878        {
879            Initialize();
880        } 
881     
882        #endregion
883
884        #region Methods
885
886        // ------------------------------------------------------------------
887        /// <summary>
888        /// Called after an entity is deleted.
889        /// </summary>
890        /// <param name="deleteCommand">DeleteCommand: The un/redoable command
891        /// that's part of the undo/redo mechanism.</param>
892        // ------------------------------------------------------------------
893        public virtual void OnAfterDelete(DeleteCommand deleteCommand)
894        {
895        }
896
897        // ------------------------------------------------------------------
898        /// <summary>
899        /// Called before an entity is deleted.
900        /// </summary>
901        /// <param name="deleteCommand">DeleteCommand: The un/redoable command
902        /// that's part of the undo/redo mechanism.</param>
903        // ------------------------------------------------------------------
904        public virtual void OnBeforeDelete(DeleteCommand deleteCommand)
905        {
906        }
907
908        // ------------------------------------------------------------------
909        /// <summary>
910        /// Called when a new DiagramEntityBase is instantiated.
911        /// </summary>
912        // ------------------------------------------------------------------
913        protected virtual void Initialize()
914        {
915            PaintStyle = ArtPalette.GetDefaultPaintStyle();
916            PenStyle = ArtPalette.GetDefaultPenStyle();
917
918            mServices = new Dictionary<Type, IInteraction>();
919            mServices[typeof(IMouseListener)] = this;
920            mServices[typeof(IHoverListener)] = this;
921        }
922
923        // ------------------------------------------------------------------
924        /// <summary>
925        /// Generates a new Uid for this entity.
926        /// </summary>
927        /// <param name="recursive">if the Uid has to be changed recursively
928        /// down to the sub-entities, set to true, otherwise false.</param>
929        // ------------------------------------------------------------------
930        public virtual void NewUid(bool recursive)
931        {
932            this.mUid = Guid.NewGuid();
933            RaiseOnChange(this, new EntityEventArgs(this));
934        }
935
936        // ------------------------------------------------------------------
937        /// <summary>
938        /// Defines a mechanism for retrieving a service object; that is, an
939        /// object that provides custom support to other objects.
940        /// </summary>
941        /// <param name="serviceType">An object that specifies the type of
942        /// service object to get.</param>
943        /// <returns>
944        /// A service object of type serviceType.-or- null if there is no
945        /// service object of type serviceType.
946        /// </returns>
947        // ------------------------------------------------------------------
948        public virtual object GetService(Type serviceType)
949        {
950            if (Services.ContainsKey(serviceType))
951            {
952                return Services[serviceType];
953            }
954            else
955            {
956                return null;
957            }
958        }
959
960        // ------------------------------------------------------------------
961        /// <summary>
962        /// Paints the entity on the control
963        /// </summary>
964        /// <param mName="g">the graphics object to paint on</param>
965        // ------------------------------------------------------------------
966        public abstract void Paint(Graphics g);
967
968        // ------------------------------------------------------------------
969        /// <summary>
970        /// Tests whether the entity is hit by the mouse
971        /// </summary>
972        /// <param>a Point location</param>
973        /// <param name="p"></param>
974        // ------------------------------------------------------------------
975        public abstract bool Hit(Point p);
976
977        // ------------------------------------------------------------------
978        /// <summary>
979        /// Invalidates the entity
980        /// </summary>
981        // ------------------------------------------------------------------
982        public abstract void Invalidate();
983
984        // ------------------------------------------------------------------
985        /// <summary>
986        /// Called when the entity is detached from the canvas (temporarily
987        /// removed but not disposed, like in a cut operation).
988        /// </summary>
989        // ------------------------------------------------------------------
990        public virtual void Detached(ILayer layer)
991        {
992            mLayer = null;
993        }
994
995        // ------------------------------------------------------------------
996        /// <summary>
997        /// Called when the entity is attached to a Layer.
998        /// </summary>
999        // ------------------------------------------------------------------
1000        public virtual void Attached(ILayer layer)
1001        {
1002            mLayer = layer;
1003        }
1004
1005        // ------------------------------------------------------------------
1006        /// <summary>
1007        /// Invalidates a rectangle of the canvas
1008        /// </summary>
1009        /// <param name="rectangle"></param>
1010        // ------------------------------------------------------------------
1011        public virtual void Invalidate(Rectangle rectangle)
1012        {
1013            if (Model != null)
1014                Model.RaiseOnInvalidateRectangle(rectangle);
1015        }
1016
1017        // ------------------------------------------------------------------
1018        /// <summary>
1019        /// Updates pens and brushes
1020        /// </summary>
1021        // ------------------------------------------------------------------
1022        protected virtual void UpdatePaintingMaterial()       
1023        {
1024            // First make sure we have a valid rectangle.
1025            if (mRectangle.Width == 0)
1026            {
1027                mRectangle.Width = 1;
1028            }
1029
1030            if (mRectangle.Height == 0)
1031            {
1032                mRectangle.Height = 1;
1033            }
1034
1035            if (mPenStyle != null)
1036            {
1037                mPen = mPenStyle.DrawingPen();
1038            }
1039
1040            if (mPaintStyle != null)
1041            {
1042                mBrush = mPaintStyle.GetBrush(this.Rectangle);
1043            }
1044
1045            Invalidate();
1046        }
1047
1048        // ------------------------------------------------------------------
1049        /// <summary>
1050        /// Moves the entity on the canvas
1051        /// </summary>
1052        /// <param mName="p">the shifting vector, not an absolute
1053        /// position!</param>
1054        // ------------------------------------------------------------------
1055        public abstract void MoveBy(Point p);
1056
1057        // ------------------------------------------------------------------
1058        /// <summary>
1059        /// The custom elements to be added to the menu on a per-entity basis.
1060        /// </summary>
1061        /// <returns>ToolStripItem[]</returns>
1062        // ------------------------------------------------------------------
1063        public abstract ToolStripItem[] Menu();
1064
1065        #region Raisers
1066
1067        // ------------------------------------------------------------------
1068        /// <summary>
1069        /// Raises the onclick event.
1070        /// </summary>
1071        /// <param name="e"></param>
1072        // ------------------------------------------------------------------
1073        public virtual void RaiseOnClick(EntityEventArgs e)
1074        {
1075            if (OnClick != null)
1076                OnClick(this, e);
1077        }
1078
1079        // ------------------------------------------------------------------
1080        /// <summary>
1081        /// Raises the OnMouseDown event.
1082        /// </summary>
1083        /// <param name="e">EntityMouseEventArgs</param>
1084        // ------------------------------------------------------------------
1085        public virtual void RaiseOnMouseDown(EntityMouseEventArgs e)
1086        {
1087            if (OnMouseDown != null)
1088            {
1089                OnMouseDown(this, e);
1090            }
1091        }
1092
1093        // ------------------------------------------------------------------
1094        /// <summary>
1095        /// Raises the OnMouseUp event.
1096        /// </summary>
1097        /// <param name="e">EntityMouseEventArgs</param>
1098        // ------------------------------------------------------------------
1099        public virtual void RaiseOnMouseUp(EntityMouseEventArgs e)
1100        {
1101            if (OnMouseUp != null)
1102            {
1103                OnMouseUp(this, e);
1104            }
1105        }
1106
1107        // ------------------------------------------------------------------
1108        /// <summary>
1109        /// Raises the OnMouseMove event.
1110        /// </summary>
1111        /// <param name="e">EntityMouseEventArgs</param>
1112        // ------------------------------------------------------------------
1113        public virtual void RaiseOnMouseMove(EntityMouseEventArgs e)
1114        {
1115            if (OnMouseMove != null)
1116            {
1117                OnMouseMove(this, e);
1118            }
1119        }
1120
1121        // ------------------------------------------------------------------
1122        /// <summary>
1123        /// Raises the OnMouseEnter event.
1124        /// </summary>
1125        /// <param name="e">EntityMouseEventArgs</param>
1126        // ------------------------------------------------------------------
1127        public virtual void RaiseOnMouseEnter(EntityMouseEventArgs e)
1128        {
1129            if (OnMouseEnter != null)
1130            {
1131                OnMouseEnter(this, e);
1132            }
1133        }
1134
1135        // ------------------------------------------------------------------
1136        /// <summary>
1137        /// Raises the OnMouseEnter event.
1138        /// </summary>
1139        /// <param name="e">EntityMouseEventArgs</param>
1140        // ------------------------------------------------------------------
1141        public virtual void RaiseOnMouseHover(EntityMouseEventArgs e)
1142        {
1143            if (OnMouseHover != null)
1144            {
1145                OnMouseHover(this, e);
1146            }
1147        }
1148
1149        // ------------------------------------------------------------------
1150        /// <summary>
1151        /// Raises the OnMouseLeave event.
1152        /// </summary>
1153        /// <param name="e">EntityMouseEventArgs</param>
1154        // ------------------------------------------------------------------
1155        public virtual void RaiseOnMouseLeave(EntityMouseEventArgs e)
1156        {
1157            if (OnMouseLeave != null)
1158            {
1159                OnMouseLeave(this, e);
1160            }
1161        }
1162
1163        // ------------------------------------------------------------------
1164        /// <summary>
1165        /// Raises the OnSelect event.
1166        /// </summary>
1167        /// <param name="sender">The sender.</param>
1168        /// <param name="e">The
1169        /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
1170        /// containing the event data.</param>
1171        // ------------------------------------------------------------------
1172        protected virtual void RaiseOnSelect(
1173            object sender,
1174            EntityEventArgs e)
1175        {
1176            if (OnEntitySelect != null)
1177            {
1178                OnEntitySelect(sender, e);
1179            }
1180        }
1181
1182        // ------------------------------------------------------------------
1183        /// <summary>
1184        /// Raises the OnChange event.
1185        /// </summary>
1186        /// <param name="sender">The sender.</param>
1187        /// <param name="e">The
1188        /// <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance
1189        /// containing the event data.</param>
1190        // ------------------------------------------------------------------
1191        protected virtual void RaiseOnChange(object sender, EntityEventArgs e)
1192        {
1193            if (OnEntityChange != null)
1194                OnEntityChange(sender, e);
1195        }
1196
1197        #endregion
1198
1199        #endregion
1200
1201        #region Standard IDispose implementation
1202        /// <summary>
1203        /// Disposes the entity.
1204        /// </summary>
1205        public void Dispose()
1206        {
1207            Dispose(true);
1208            GC.SuppressFinalize(this);
1209
1210
1211        }
1212
1213        /// <summary>
1214        /// Disposes the entity.
1215        /// </summary>
1216        /// <param name="disposing">if set to <c>true</c> [disposing].</param>
1217        protected virtual void Dispose(bool disposing)
1218        {
1219            if (disposing)
1220            {
1221                #region free managed resources
1222
1223
1224                if (mPen != null)
1225                {
1226                    mPen.Dispose();
1227                    mPen = null;
1228                }
1229                if (mBrush != null)
1230                {
1231                    mBrush.Dispose();
1232                    mBrush = null;
1233                }
1234                #endregion
1235            }
1236
1237        }
1238
1239        #endregion
1240       
1241        #region IMouseListener Members
1242
1243        // ------------------------------------------------------------------
1244        /// <summary>
1245        /// Implementation of the <see cref="IMouseListener"/>.  This is
1246        /// the method called when a mouse button is pressed while over this
1247        /// entity.
1248        /// </summary>
1249        /// <param name="e">The
1250        /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1251        /// containing the event data.</param>
1252        /// <returns>bool: Whether or not the mouse down was handled by this
1253        /// entity.  The default here is false.  Sub-entities should override
1254        /// this to provide their own functionality if needed.</returns>
1255        // ------------------------------------------------------------------
1256        public virtual bool MouseDown(MouseEventArgs e)
1257        {
1258            this.RaiseOnMouseDown(new EntityMouseEventArgs(this, e));
1259
1260            // By default we're not handling the mouse down event here,
1261            // we're just passing it on.  Let the sub-entities handle it
1262            // by overriding this method.
1263            return false;
1264        }
1265
1266        // ------------------------------------------------------------------
1267        /// <summary>
1268        /// Implementation of the <see cref="IMouseListener"/>.  This is
1269        /// the method called when the mouse is moved while over this
1270        /// entity.
1271        /// </summary>
1272        /// <param name="e">The
1273        /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1274        /// containing the event data.</param>
1275        // ------------------------------------------------------------------
1276        public virtual void MouseMove(MouseEventArgs e)
1277        {
1278            this.RaiseOnMouseMove(new EntityMouseEventArgs(this, e));
1279        }
1280
1281        // ------------------------------------------------------------------
1282        /// <summary>
1283        /// Implementation of the <see cref="IMouseListener"/>.  This is
1284        /// the method called when a mouse button is released while over this
1285        /// entity.
1286        /// </summary>
1287        /// <param name="e">The
1288        /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1289        /// containing the event data.</param>
1290        // ------------------------------------------------------------------
1291        public virtual void MouseUp(MouseEventArgs e)
1292        {
1293            this.RaiseOnMouseUp(new EntityMouseEventArgs(this, e));
1294        }
1295
1296        #endregion
1297
1298        #region IHoverListener Members
1299
1300        // ------------------------------------------------------------------
1301        /// <summary>
1302        /// Implementation of the <see cref="IHoverListener"/>.  This is
1303        /// the method called when the mouse hovers over this entity.
1304        /// </summary>
1305        /// <param name="e">The
1306        /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1307        /// containing the event data.</param>
1308        // ------------------------------------------------------------------
1309        public virtual void MouseHover(MouseEventArgs e)
1310        {
1311            this.RaiseOnMouseHover(new EntityMouseEventArgs(this, e));
1312        }
1313
1314        // ------------------------------------------------------------------
1315        /// <summary>
1316        /// Implementation of the <see cref="IHoverListener"/>.  This is
1317        /// the method called when the mouse enters this entity.
1318        /// </summary>
1319        /// <param name="e">The
1320        /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1321        /// containing the event data.</param>
1322        // ------------------------------------------------------------------
1323        public virtual void MouseEnter(MouseEventArgs e)
1324        {
1325            this.RaiseOnMouseEnter(new EntityMouseEventArgs(this, e));
1326        }
1327
1328        // ------------------------------------------------------------------
1329        /// <summary>
1330        /// Implementation of the <see cref="IHoverListener"/>.  This is
1331        /// the method called when the mouse leaves this entity.
1332        /// </summary>
1333        /// <param name="e">The
1334        /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
1335        /// containing the event data.</param>
1336        // ------------------------------------------------------------------
1337        public virtual void MouseLeave(MouseEventArgs e)
1338        {
1339            this.RaiseOnMouseLeave(new EntityMouseEventArgs(this, e));
1340        }
1341
1342        #endregion
1343
1344        #region IKeyboardListener Members
1345
1346        // ------------------------------------------------------------------
1347        /// <summary>
1348        /// Implementation of the <see cref="IKeyboardListener"/>.  This is
1349        /// the method called when a key is pressed down.
1350        /// </summary>
1351        /// <param name="e">The
1352        /// <see cref="T:System.Windows.Forms.KeyEventArgs"/> instance
1353        /// containing the event data.</param>
1354        // ------------------------------------------------------------------
1355        public virtual void KeyDown(KeyEventArgs e)
1356        {
1357        }
1358
1359        // ------------------------------------------------------------------
1360        /// <summary>
1361        /// Implementation of the <see cref="IKeyboardListener"/>.  This is
1362        /// the method called when a key is released.
1363        /// </summary>
1364        /// <param name="e">The
1365        /// <see cref="T:System.Windows.Forms.KeyEventArgs"/> instance
1366        /// containing the event data.</param>
1367        // ------------------------------------------------------------------
1368        public virtual void KeyUp(KeyEventArgs e)
1369        {
1370        }
1371
1372        // ------------------------------------------------------------------
1373        /// <summary>
1374        /// Implementation of the <see cref="IKeyboardListener"/>.  This is
1375        /// the method called when a key is pressed.
1376        /// </summary>
1377        /// <param name="e">The
1378        /// <see cref="T:System.Windows.Forms.KeyPressEventArgs"/> instance
1379        /// containing the event data.</param>
1380        // ------------------------------------------------------------------
1381        public virtual void KeyPress(KeyPressEventArgs e)
1382        {
1383        }
1384
1385        #endregion
1386    }
1387}
Note: See TracBrowser for help on using the repository browser.