-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathEntity.cs
More file actions
684 lines (606 loc) · 15.9 KB
/
Entity.cs
File metadata and controls
684 lines (606 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Netron.GraphLib.UI;
using Netron.GraphLib.Interfaces;
using Netron.GraphLib.Attributes;
using Netron.GraphLib.Configuration;
namespace Netron.GraphLib
{
/// <summary>
/// Abstract base class for everything that participates in the diagram/graph (connection, connector...)
/// </summary>
[Serializable] public abstract class Entity : IEntity, IDisposable, ISerializable
{
#region Events
/// <summary>
/// Occurs when the mouse is pressed on this entity
/// </summary>
public event MouseEventHandler OnMouseDown;
/// <summary>
/// Occurs when the mouse is released while above this entity
/// </summary>
public event MouseEventHandler OnMouseUp;
/// <summary>
/// Occurs when the mouse is moved while above this entity
/// </summary>
public event MouseEventHandler OnMouseMove;
#endregion
#region Fields
/// <summary>
/// the layer to which the entity belongs,
/// the default layer is a static unique layer defined in the GraphAbstract.
/// </summary>
private GraphLayer mLayer;
/// <summary>
/// volatile all-purpose tag
/// </summary>
[NonSerialized] private object mTag;
/// <summary>
/// whether to recalculate the shape size, speed up the rendering
/// </summary>
private bool mRecalculateSize ;
/// <summary>
/// the property bag
/// </summary>
[NonSerialized] private PropertyBag mBag;
/// <summary>
/// default blue mPen, speeds up rendering
/// Note that the Pen is not serialzable!
/// </summary>
[NonSerialized] private Pen mBluePen;
/// <summary>
/// default black mPen, speeds up rendering
/// </summary>
[NonSerialized] private Pen mBlackPen;
/// <summary>
/// the mPen's width
/// </summary>
private float mPenWidth = 1F;
/// <summary>
/// default mPen
/// </summary>
[NonSerialized] private Pen mPen;
/// <summary>
/// whether the entity is reset
/// </summary>
private bool mIsGuiReset;
/// <summary>
/// the font family
/// </summary>
private string mFontFamily = "Verdana";
/// <summary>
/// whether the entity is selected
/// </summary>
private bool mIsSelected;
/// <summary>
/// whether this entity is being hovered
/// </summary>
private bool mIsHovered ;
/// <summary>
/// the unique identitfier
/// </summary>
private Guid mUID ;
/// <summary>
/// mText or label
/// </summary>
private string mText = "[Not set]";
/// <summary>
/// whether to show the mText label
/// </summary>
private bool mShowLabel = true;
/// <summary>
/// the default mText color
/// </summary>
private Color mTextColor = Color.Black;
/// <summary>
/// the default font size in points
/// </summary>
private float mFontSize = 7.8F;
/// <summary>
/// the default font for entities
/// </summary>
private Font mFont;
/// <summary>
/// the mSite of the entity
/// </summary>
[NonSerialized] private IGraphSite mSite;
#endregion
#region Properties
/// <summary>
/// Gets or sets the pen-object to paint and draw
/// </summary>
public Pen Pen
{
get{return mPen;}
set{mPen = value;}
}
/// <summary>
/// Gets the property-bag
/// </summary>
/// <remarks>The bag acts as a proxy-object for the properties and his part of the propertybag mechanism.</remarks>
protected PropertyBag Bag
{
get{return mBag;}
}
/// <summary>
/// Gets or sets whether the next painting roun will have to recalculate the size of the entity
/// </summary>
protected bool RecalculateSize
{
get{return mRecalculateSize;}
set{mRecalculateSize = value;}
}
/// <summary>
/// Gets the default black mPen for drawing text and lines
/// </summary>
protected Pen BlackPen
{
get{return mBlackPen;}
}
/// <summary>
/// Gets or sets the font-family used by derived class to draw and paint on the canvas
/// </summary>
protected virtual string FontFamily
{
get{return mFontFamily;}
set{mFontFamily = value;}
}
/// <summary>
/// Gets the layer ths shape is on. If null, the shape is in the default layer.
/// </summary>
/// <remarks>User the SetLayer() method to set or change the layer.
/// </remarks>
public GraphLayer Layer
{
get
{
return mLayer;
}
}
/// <summary>
/// Gets or sets a general purpose tag object
/// </summary>
public object Tag
{
get{return mTag;}
set{mTag = value;}
}
/// <summary>
/// Gets or sets the mPen width
/// </summary>
[GraphMLData]public float PenWidth
{
get{return mPenWidth;}
set{mPenWidth = value;}
}
/// <summary>
/// Gets or sets whether the shape label should be shown.
/// </summary>
[GraphMLData]public virtual bool ShowLabel
{
get
{
return mShowLabel;
}
set
{
mShowLabel=value; this.Invalidate();
}
}
/// <summary>
/// Allows to view/change the properties of the shape, most probably on double-clicking it.
/// </summary>
public virtual PropertyBag Properties
{
get{return mBag;}
}
/// <summary>
/// Gets or sets the entity label
/// </summary>
[GraphMLData]public virtual string Text
{
get
{
return mText;
}
set
{
if (value!=null) mText=value;
}
}
/// <summary>
/// Gets or sets the mSite of the entity
/// </summary>
public IGraphSite Site
{
get{return mSite;}
set{mSite = value;}
}
/// <summary>
/// Tells wether the entity (shape) is selected
/// </summary>
public virtual bool IsSelected
{
set { Invalidate(); mIsSelected = value; Invalidate(); }
get { return mIsSelected; }
}
/// <summary>
/// Gets or sets whether the entity's UID is reset
/// </summary>
/// <remarks>USed in the cotext of copy/paste</remarks>
protected internal virtual bool IsGuiReset
{
get{return mIsGuiReset;}
set{mIsGuiReset = value;}
}
/// <summary>
/// Gives true if the mouse is hovering over this entity
/// </summary>
protected internal virtual bool IsHovered
{
set { Invalidate(); mIsHovered = value; Invalidate(); }
get { return mIsHovered; }
}
/// <summary>
/// Gets or sets the mText color
/// </summary>
public virtual Color TextColor
{
get
{
return mTextColor;
}
set
{
mTextColor=value;
}
}
/// <summary>
/// Gets or sets the font size of the mText
/// </summary>
protected virtual float FontSize
{
get
{
return mFontSize;
}
set
{
mFontSize=value;
}
}
/// <summary>
/// Gets or sets the font to be used when drawing text-data
/// </summary>
protected Font Font
{
get{return this.mFont;}
set
{
this.mFont = value;
this.mFontFamily = value.FontFamily.ToString();
this.mFontSize = value.Size;
}
}
/// <summary>
/// Gets or sets the unique identifier for the shape.
/// </summary>
public Guid UID
{
get
{
return mUID;
}
set{mUID = value;}
}
/// <summary>
/// Gets the Summary for this entity
/// </summary>
public Summary Summary
{
get
{
if (Site == null)
{
return null;
}
return this.Site.GetSummary(this);
}
}
/// <summary>
/// Gets the tracker of the entity
/// </summary>
public virtual Tracker Tracker
{
get{return null;}
set{}
}
#endregion
#region Constructors
/// <summary>
/// Constructor for the entity class, initializes a new GUID for the entity
/// </summary>
protected Entity()
{
InitEntity();
}
/// <summary>
/// Creates a new entity, specifying the mSite
/// </summary>
/// <param name="site"></param>
protected Entity(IGraphSite site)
{
InitEntity();
this.mSite = site;
}
/// <summary>
/// Deserialization constructor
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
protected Entity(SerializationInfo info, StreamingContext context)
{
InitEntity();
//overwrite some members with the serialized data
this.mUID = new Guid(info.GetString("mUID"));
this.mText = info.GetString("mText");
this.mShowLabel = info.GetBoolean("mShowLabel");
this.mTextColor = (Color) info.GetValue("mTextColor",typeof(Color));
try
{
this.mFont = (Font) info.GetValue("mFont", typeof(Font));
}
catch
{
//font is set by default in the member definition
mFont = new Font("Tahoma", 10);
}
Tag = info.GetString("mLayer");
//this.mIsSelected = info.GetBoolean("mIsSelected");
}
#endregion
#region Methods
/// <summary>
/// IDispose implementation
/// </summary>
public void Dispose()
{
this.mFont.Dispose();
this.mPen.Dispose();
this.mBlackPen.Dispose();
this.mBluePen.Dispose();
}
/// <summary>
/// Sets the layer the entity belongs to
/// </summary>
/// <param name="layer"></param>
protected virtual void SetLayer(GraphLayer layer)
{
mLayer = layer;
return;
}
/// <summary>
/// Sets the shape in a layer.
/// Use "default" to set the shape in the default layer.
/// </summary>
/// <param name="name"></param>
public void SetLayer(string name)
{
GraphLayer layer = Site.Abstract.Layers[name];
if (layer == null)
{
SetLayer(Site.Abstract.DefaultLayer);
}
else
{
SetLayer(Site.Abstract.Layers[name]);
}
}
/// <summary>
/// Sets the shape in a layer.
/// Layer 0 is the default layer.
/// </summary>
/// <param name="index"></param>
public void SetLayer(int index)
{
SetLayer(Site.Abstract.Layers[index]);
}
/// <summary>
/// Determines which properties are accessible via the property grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void GetPropertyBagValue(object sender, PropertySpecEventArgs e)
{
switch(e.Property.Name)
{
case "Text": e.Value=this.mText;break;
case "ShowLabel": e.Value=this.ShowLabel; break;
}
}
/// <summary>
/// Sets the values passed by the property grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void SetPropertyBagValue(object sender, PropertySpecEventArgs e)
{
switch(e.Property.Name)
{
case "ShowLabel": this.ShowLabel=(bool) e.Value; break;
case "Text":
//use the logic and the constraint of the object that is being reflected
if(e.Value.ToString() != null)
{
this.mText=(string) e.Value;
}
else
MessageBox.Show("Not a valid label","Invalid label",MessageBoxButtons.OK,MessageBoxIcon.Warning);
break;
}
this.Invalidate();
}
/// <summary>
/// When overriden, allows user defined entities to get custom properties
/// </summary>
public virtual void AddProperties()
{
return;
}
/// <summary>
/// Initializes the class. This method is necessary when deserializing since various elements like
/// the Pen cannot be serialized to file and have to be, hence, set after deserialization.
/// </summary>
protected internal virtual void InitEntity()
{
if (Site != null)
{
mLayer = Site.Abstract.DefaultLayer;
}
mBluePen= new Pen(Brushes.DarkSlateBlue,1F);
mBlackPen = new Pen(Brushes.Black,1F);
mUID = Guid.NewGuid();
mRecalculateSize = false;
mFont = new Font(mFontFamily,mFontSize,FontStyle.Regular,GraphicsUnit.Point);
mPen=new Pen(Brushes.Black, mPenWidth);
// mLayer = Site.Abstract.DefaultLayer;//everything is initially in the default (static) layer
mBag=new PropertyBag(this);
try
{
mBag.GetValue+=new PropertySpecEventHandler(GetPropertyBagValue);
mBag.SetValue+=new PropertySpecEventHandler(SetPropertyBagValue);
mBag.Properties.Add(new PropertySpec("Text",typeof(string),"Appearance","The text attached to the entity","[Not set]"));
mBag.Properties.Add(new PropertySpec("ShowLabel",typeof(bool),"Appearance","Gets or sets whether the label will be shown."));
// PropertySpec spec=new PropertySpec("MDI children",typeof(string[]));
// spec.Attributes=new Attribute[]{new System.ComponentModel.ReadOnlyAttribute(true)};
// mBag.Properties.Add(spec);
//AddProperties(); //add the user defined shape properties
}
catch(Exception exc) //TODO: catch only those exceptions that you can handle gracefully
{
Trace.WriteLine(exc.Message, "Entity.InitEntity");
}
catch
{
Trace.WriteLine("Non-CLS compliant exception caught.", "Entity.InitEntity");
}
}
/// <summary>
/// creates the actual visual element on screen
/// </summary>
/// <param name="g"></param>
public abstract void Paint(Graphics g);
/// <summary>
/// Gets the cursor for the current position of the mouse
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public abstract Cursor GetCursor(PointF p);
/// <summary>
/// GraphAbstract delete method; deletes the entity from the plex
/// </summary>
internal protected abstract void Delete();
/// <summary>
/// Says wether, for the given rectangle, the underlying shape is contained in it.
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public abstract bool Hit(RectangleF r);
/// <summary>
/// Invalidating refreshes part or all of a control
/// </summary>
public abstract void Invalidate();
/// <summary>
/// Allows to paints additional things like the clickable elements on shapes
/// independently of the shape's design
/// </summary>
/// <param name="g"></param>
public abstract void PaintAdornments(Graphics g);
/// <summary>
/// Paints the tracker of the entity
/// </summary>
/// <param name="g"></param>
public void PaintTracker(Graphics g)
{
if(this.IsSelected) this.Tracker.Paint(g);
}
/// <summary>
/// Regenerates a GUID for this entity
/// </summary>
public void GenerateNewUID()
{
mUID=Guid.NewGuid();
mIsGuiReset=true;
}
/// <summary>
/// Raises the mouse down event
/// </summary>
/// <param name="e"></param>
internal virtual void RaiseMouseDown(MouseEventArgs e)
{
if(OnMouseDown != null) OnMouseDown(this, e);
}
/// <summary>
/// Raises the mouse up event
/// </summary>
/// <param name="e"></param>
internal void RaiseMouseUp(MouseEventArgs e)
{
if(OnMouseUp != null) OnMouseUp(this, e);
}
internal void RaiseMouseMove(MouseEventArgs e)
{
if(OnMouseMove!=null) OnMouseMove(this,e);
}
#endregion
#region ISerializable Members
/// <summary>
/// ISerializable implementation
/// </summary>
/// <param name="info">the serialization info</param>
/// <param name="context">the streaming context</param>
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("mUID",this.mUID.ToString());
info.AddValue("mTextColor",this.mTextColor,typeof(Color));
info.AddValue("mText",this.mText);
info.AddValue("mShowLabel",this.mShowLabel);
info.AddValue("mFont", this.mFont, typeof(Font));
//additional check in case we're making a deep copy of a bundle
if(mLayer==null)
info.AddValue("mLayer","Default");
else
info.AddValue("mLayer",this.mLayer.Name);
}
/// <summary>
/// Post-deserialization actions
/// </summary>
public virtual void PostDeserialization()
{
//add the properties
this.AddProperties();
if (Tag is string)
{
SetLayer((string)Tag);
Tag = null; //be nice to the host/user
}
Font = new Font("Tahoma", 10);
}
#endregion
}
}