forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxGraphModel.cs
More file actions
1286 lines (1124 loc) · 42.5 KB
/
mxGraphModel.cs
File metadata and controls
1286 lines (1124 loc) · 42.5 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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2007-2008, Gaudenz Alder
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Drawing;
namespace com.mxgraph
{
/// <summary>
/// Implements a graph model. The graph model acts as a wrapper around the
/// cells which are in charge of storing the actual graph datastructure.
/// The model acts as a transactional wrapper with event notification for
/// all changes, whereas the cells contain the atomic operations for
/// updating the actual datastructure.
/// </summary>
public class mxGraphModel: mxIGraphModel
{
/// <summary>
/// Fires when the graph model has changed.
/// </summary>
public event mxGraphModelChangeEventHandler GraphModelChange;
/// <summary>
/// Holds the root cell, which in turn contains the cells that represent the
/// layers of the diagram as child cells. That is, the actual element of the
/// diagram are supposed to live in the third generation of cells and below.
/// </summary>
protected mxICell root;
/// <summary>
/// Maps from Ids to cells.
/// </summary>
protected Dictionary<Object, Object> cells;
/// <summary>
/// Specifies if edges should automatically be moved into the nearest common
/// ancestor of their terminals. Default is true.
/// </summary>
protected bool createIds = true;
/// <summary>
/// Specifies if the parent of edges should be automatically change to point
/// to the nearest common ancestor of its terminals. Default is true.
/// </summary>
protected bool maintainEdgeParent = true;
/// <summary>
/// Specifies the next Id to be created. Initial value is 0.
/// </summary>
protected int nextId = 0;
/// <summary>
/// Counter for the depth of nested transactions. Each call to beginUpdate
/// increments this counter and each call to endUpdate decrements it. When
/// the counter reaches 0, the transaction is closed and the respective
/// events are fired. Initial value is 0.
/// </summary>
protected int updateLevel = 0;
/// <summary>
/// Constructs a new empty graph model.
/// </summary>
public mxGraphModel(): this(null) {}
/// <summary>
/// Constructs a new graph model. If no root is specified
/// then a new root mxCell with a default layer is created.
/// </summary>
/// <param name="root">Cell that represents the root cell.</param>
public mxGraphModel(Object root)
{
if (root != null)
{
Root = root;
}
else
{
Clear();
}
}
/// <summary>
/// Sets a new root using createRoot.
/// </summary>
public void Clear()
{
Root = CreateRoot();
}
/// <summary>
/// Creates a new root cell with a default layer (child 0).
/// </summary>
public Object CreateRoot()
{
mxCell root = new mxCell();
root.Insert(new mxCell());
return root;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Object GetCell(string id)
{
Object result = null;
if (cells != null)
{
cells.TryGetValue(id, out result);
}
return result;
}
/// <summary>
/// Sets of returns if edges should automatically be moved into the
/// nearest common ancestor of their terminals.
/// </summary>
public bool IsMaintainEdgeParent
{
get { return maintainEdgeParent; }
set { maintainEdgeParent = value; }
}
/// <summary>
/// Sets or returns if the model automatically creates Ids and resolves Id
/// collisions.
/// </summary>
public bool IsCreateIds
{
get { return createIds; }
set { createIds = value; }
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.Root
*/
public Object Root
{
get { return root; }
set {
BeginUpdate();
try
{
root = (mxICell) value;
this.nextId = 0;
this.cells = null;
CellAdded(root);
}
finally
{
EndUpdate();
}
}
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.IsAncestor(Object[], bool)
*/
public Object[] CloneCells(Object[] cells, bool includeChildren)
{
Hashtable mapping = new Hashtable();
Object[] clones = new Object[cells.Length];
for (int i = 0; i < cells.Length; i++)
{
clones[i] = CloneCell(cells[i], mapping, includeChildren);
}
for (int i = 0; i < cells.Length; i++)
{
RestoreClone(clones[i], cells[i], mapping);
}
return clones;
}
/// <summary>
/// Inner helper method for cloning cells recursively.
/// </summary>
protected Object CloneCell(Object cell, Hashtable mapping, bool includeChildren)
{
if (cell is mxICell)
{
mxICell mxc = (mxICell) ((mxICell) cell).Clone();
mapping[cell] = mxc;
if (includeChildren)
{
int childCount = GetChildCount(cell);
for (int i = 0; i < childCount; i++)
{
Object clone = CloneCell(GetChildAt(cell, i), mapping, true);
mxc.Insert((mxICell) clone);
}
}
return mxc;
}
return null;
}
/// <summary>
/// Inner helper method for restoring the connections in
/// a network of cloned cells.
/// </summary>
protected void RestoreClone(Object clone, Object cell, Hashtable mapping)
{
if (clone is mxICell)
{
mxICell mxc = (mxICell) clone;
Object source = GetTerminal(cell, true);
if (source is mxICell)
{
mxICell tmp = (mxICell) mapping[source];
if (tmp != null)
{
tmp.InsertEdge(mxc, true);
}
}
Object target = GetTerminal(cell, false);
if (target is mxICell)
{
mxICell tmp = (mxICell) mapping[target];
if (tmp != null)
{
tmp.InsertEdge(mxc, false);
}
}
}
int childCount = GetChildCount(clone);
for (int i = 0; i < childCount; i++)
{
RestoreClone(GetChildAt(clone, i), GetChildAt(cell, i), mapping);
}
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.IsAncestor(Object, Object)
*/
public bool IsAncestor(Object parent, Object child)
{
while (child != null && child != parent)
{
child = GetParent(child);
}
return child == parent;
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.Contains(Object)
*/
public bool Contains(Object cell)
{
return IsAncestor(Root, cell);
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.GetParent(Object)
*/
public Object GetParent(Object child)
{
return (child is mxICell) ? ((mxICell)child).Parent : null;
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.Add(Object, Object, int)
*/
public Object Add(Object parent, Object child, int index)
{
if (child != parent && parent is mxICell && child is mxICell)
{
bool parentChanged = parent != GetParent(child);
BeginUpdate();
try
{
((mxICell)parent).Insert((mxICell)child, index);
CellAdded(child);
}
finally
{
EndUpdate();
}
if (IsMaintainEdgeParent &&
parentChanged)
{
UpdateEdgeParents(child);
}
}
return child;
}
/// <summary>
/// Invoked after a cell has been added to a parent. This recursively
/// creates an Id for the new cell and/or resolves Id collisions.
/// </summary>
/// <param name="cell">Cell that has been added.</param>
protected void CellAdded(Object cell)
{
if (cell is mxICell)
{
mxICell mxc = (mxICell)cell;
if (mxc.Id == null && IsCreateIds)
{
mxc.Id = CreateId(cell);
}
if (mxc.Id != null)
{
Object collision = GetCell(mxc.Id);
if (collision != cell)
{
while (collision != null)
{
mxc.Id = CreateId(cell);
collision = GetCell(mxc.Id);
}
if (cells == null)
{
cells = new Dictionary<Object, Object>();
}
cells[mxc.Id] = cell;
}
}
// Makes sure IDs of deleted cells are not reused
try
{
int id = Convert.ToInt32(mxc.Id);
nextId = Math.Max(nextId, id + 1);
}
catch (FormatException e)
{
Trace.WriteLine(this + ".CellAdded(" + cell + "): " + e.Message);
}
int childCount = mxc.ChildCount();
for (int i = 0; i < childCount; i++)
{
CellAdded(mxc.GetChildAt(i));
}
}
}
/// <summary>
/// Creates a new Id for the given cell and increments the global counter
/// for creating new Ids.
/// </summary>
/// <param name="cell">Cell for which a new Id should be created.</param>
/// <returns>Returns a new Id for the given cell.</returns>
public string CreateId(Object cell)
{
string id = nextId.ToString();
nextId++;
return id;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.Remove(Object)
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
public Object Remove(Object cell)
{
if (cell is mxICell)
{
mxICell mx = (mxICell)cell;
BeginUpdate();
try
{
if (cell == root)
{
Root = null;
}
else
{
mx.RemoveFromParent();
}
CellRemoved(cell);
}
finally
{
EndUpdate();
}
}
return cell;
}
/// <summary>
/// Invoked after a cell has been removed from the model. This recursively
/// removes the cell from its terminals and removes the mapping from the Id
/// to the cell.
/// </summary>
/// <param name="cell">Cell that has been removed.</param>
protected void CellRemoved(Object cell)
{
if (cell is mxICell)
{
mxICell mxc = (mxICell)cell;
int childCount = mxc.ChildCount();
for (int i = 0; i < childCount; i++)
{
CellRemoved(mxc.GetChildAt(i));
}
mxc.RemoveFromTerminal(true);
mxc.RemoveFromTerminal(false);
if (cells != null && mxc.Id != null)
{
cells.Remove(mxc.Id);
}
}
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.GetChildCount(Object)
*/
public int GetChildCount(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).ChildCount() : 0;
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.GetChildAt(Object, int)
*/
public Object GetChildAt(Object parent, int index)
{
return (parent is mxICell) ? ((mxICell)parent).GetChildAt(index) : null;
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.GetTerminal(Object, bool)
*/
public Object GetTerminal(Object edge, bool isSource)
{
return (edge is mxICell) ? ((mxICell)edge).GetTerminal(isSource) : null;
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxIGraphModel.SetTerminal(Object, Object, bool)
*/
public Object SetTerminal(Object edge, Object terminal, bool isSource)
{
mxICell mxe = (mxICell)edge;
mxICell previous = mxe.GetTerminal(isSource);
BeginUpdate();
try
{
if (terminal != null)
{
((mxICell)terminal).InsertEdge(mxe, isSource);
}
else if (previous != null)
{
previous.RemoveEdge(mxe, isSource);
}
}
finally
{
EndUpdate();
}
if (IsMaintainEdgeParent)
{
UpdateEdgeParent(edge, Root);
}
return terminal;
}
/// <summary>
/// Updates the parents of the edges connected to the given cell and all its
/// descendants so that each edge is contained in the nearest common ancestor.
/// </summary>
/// <param name="cell">Cell whose edges should be checked and updated.</param>
public void UpdateEdgeParents(Object cell)
{
UpdateEdgeParents(cell, Root);
}
/// <summary>
/// Updates the parents of the edges connected to the given cell and all its
/// descendants so that the edge is contained in the nearest-common-ancestor.
/// </summary>
/// <param name="cell">Cell whose edges should be checked and updated.</param>
/// <param name="root">Root of the cell hierarchy that contains all cells.</param>
public void UpdateEdgeParents(Object cell, Object root)
{
// Updates edges on children first
int childCount = GetChildCount(cell);
for (int i = 0; i < childCount; i++)
{
Object child = GetChildAt(cell, i);
UpdateEdgeParents(child, root);
}
// Updates the parents of all connected edges
int edgeCount = GetEdgeCount(cell);
List<Object> edges = new List<Object>();
for (int i = 0; i < edgeCount; i++)
{
edges.Add(GetEdgeAt(cell, i));
}
foreach (Object edge in edges)
{
// Updates edge parent if edge and child have
// a common root node (does not need to be the
// model root node)
if (IsAncestor(root, edge))
{
UpdateEdgeParent(edge, root);
}
}
}
/// <summary>
/// Inner helper method to update the parent of the specified edge to the
/// nearest-common-ancestor of its two terminals.
/// </summary>
/// <param name="edge">Specifies the edge to be updated.</param>
/// <param name="root">Current root of the model.</param>
public void UpdateEdgeParent(Object edge, Object root)
{
Object source = GetTerminal(edge, true);
Object target = GetTerminal(edge, false);
Object cell = null;
// Uses the first non-relative descendants of the source terminal
while (source != null && !IsEdge(source) &&
GetGeometry(source) != null && GetGeometry(source).Relative)
{
source = GetParent(source);
}
// Uses the first non-relative descendants of the target terminal
while (target != null && !IsEdge(target) &&
GetGeometry(target) != null && GetGeometry(target).Relative)
{
target = GetParent(target);
}
if (IsAncestor(root, source) &&
IsAncestor(root, target))
{
if (source == target)
{
cell = GetParent(source);
}
else
{
cell = GetNearestCommonAncestor(source, target);
}
if (cell != null &&
GetParent(cell) != root &&
GetParent(edge) != cell)
{
mxGeometry geo = GetGeometry(edge);
if (geo != null)
{
mxPoint origin1 = GetOrigin(GetParent(edge));
mxPoint origin2 = GetOrigin(cell);
double dx = origin2.X - origin1.X;
double dy = origin2.Y - origin1.Y;
geo = (mxGeometry) geo.Clone();
geo.Translate(-dx, -dy);
SetGeometry(edge, geo);
}
Add(cell, edge, GetChildCount(cell));
}
}
}
/// <summary>
/// Returns the absolute, cummulated origin for the children inside the
/// given parent.
/// </summary>
public mxPoint GetOrigin(Object cell)
{
mxPoint result = null;
if (cell != null)
{
result = GetOrigin(GetParent(cell));
if (!IsEdge(cell))
{
mxGeometry geo = GetGeometry(cell);
if (geo != null)
{
result.X += geo.X;
result.Y += geo.Y;
}
}
}
else
{
result = new mxPoint();
}
return result;
}
/// <summary>
/// Returns the nearest common ancestor for the specified cells.
/// </summary>
/// <param name="cell1">Cell that specifies the first cell in the tree.</param>
/// <param name="cell2">Cell that specifies the second cell in the tree.</param>
/// <returns>Returns the nearest common ancestor of the given cells.</returns>
public Object GetNearestCommonAncestor(Object cell1, Object cell2)
{
if (cell1 != null && cell2 != null)
{
// Creates the cell path for the second cell
String path = mxCellPath.Create((mxICell)cell2);
if (path != null && path.Length > 0)
{
// Bubbles through the ancestors of the target
// cell to find the nearest common ancestor.
Object cell = cell1;
String current = mxCellPath.Create((mxICell)cell);
while (cell != null)
{
Object parent = GetParent(cell);
// Checks if the cell path is equal to the beginning
// of the given cell path
if (path.IndexOf(current + mxCellPath.PATH_SEPARATOR) == 0 &&
parent != null)
{
return cell;
}
current = mxCellPath.GetParentPath(current);
cell = parent;
}
}
}
return null;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.GetEdgeCount(Object)
/// </summary>
public int GetEdgeCount(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).EdgeCount() : 0;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.GetEdgeAt(Object, int)
/// </summary>
public Object GetEdgeAt(Object parent, int index)
{
return (parent is mxICell) ? ((mxICell)parent).GetEdgeAt(index) : null;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.IsVertex(Object)
/// </summary>
public bool IsVertex(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Vertex : false;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.IsEdge(Object)
/// </summary>
public bool IsEdge(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Edge : false;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.IsConnectable(Object)
/// </summary>
public bool IsConnectable(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Connectable : false;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.GetValue(Object)
/// </summary>
public Object GetValue(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Value : null;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.SetValue(Object, Object)
/// </summary>
public Object SetValue(Object cell, Object value)
{
BeginUpdate();
try
{
((mxICell) cell).Value = value;
}
finally
{
EndUpdate();
}
return value;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.GetGeometry(Object)
/// </summary>
public mxGeometry GetGeometry(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Geometry : null;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.SetGeometry(Object, mxGeometry)
/// </summary>
public mxGeometry SetGeometry(Object cell, mxGeometry geometry)
{
BeginUpdate();
try
{
((mxICell)cell).Geometry = geometry;
}
finally
{
EndUpdate();
}
return geometry;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.GetStyle(Object)
/// </summary>
public string GetStyle(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Style : null;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.SetStyle(Object, string)
/// </summary>
public string SetStyle(Object cell, string style)
{
BeginUpdate();
try
{
((mxICell)cell).Style = style;
}
finally
{
EndUpdate();
}
return style;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.IsVisible(Object)
/// </summary>
public bool IsVisible(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Visible : false;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.SetVisible(Object, bool)
/// </summary>
public bool SetVisible(Object cell, bool visible)
{
BeginUpdate();
try
{
((mxICell)cell).Visible = visible;
}
finally
{
EndUpdate();
}
return visible;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.IsCollapsed(Object)
/// </summary>
public bool IsCollapsed(Object cell)
{
return (cell is mxICell) ? ((mxICell)cell).Collapsed : false;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.SetCollapsed(Object, bool)
/// </summary>
public bool SetCollapsed(Object cell, bool collapsed)
{
BeginUpdate();
try
{
((mxICell)cell).Collapsed = collapsed;
}
finally
{
EndUpdate();
}
return collapsed;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.BeginUpdate()
/// </summary>
public void BeginUpdate()
{
updateLevel++;
}
/// <summary>
/// see com.mxgraph.mxIGraphModel.EndUpdate()
/// </summary>
public void EndUpdate()
{
updateLevel--;
if (updateLevel == 0 && GraphModelChange != null)
{
GraphModelChange();
}
}
/// <summary>
/// Merges the children of the given cell into the given target cell inside
/// this model. All cells are cloned unless there is a corresponding cell in
/// the model with the same id, in which case the source cell is ignored and
/// all edges are connected to the corresponding cell in this model. Edges
/// are considered to have no identity and are always cloned unless the
/// cloneAllEdges flag is set to false, in which case edges with the same
/// id in the target model are reconnected to reflect the terminals of the
/// source edges.
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="cloneAllEdges"></param>
public void MergeChildren(mxICell from, mxICell to, bool cloneAllEdges)
{
BeginUpdate();
try
{
Dictionary<Object, Object> mapping = new Dictionary<Object, Object>();
MergeChildrenImpl(from, to, cloneAllEdges, mapping);
// Post-processes all edges in the mapping and
// reconnects the terminals to the corresponding
// cells in the target model
foreach (KeyValuePair<Object, Object> kvp in mapping)
{
Object edge = kvp.Key;
Object cell = kvp.Value;
Object terminal = GetTerminal(edge, true);
if (terminal != null)
{
terminal = mapping[terminal];
SetTerminal(cell, terminal, true);
}
terminal = GetTerminal(edge, false);
if (terminal != null)
{
terminal = mapping[terminal];
SetTerminal(cell, terminal, false);
}
}
}
finally
{
EndUpdate();
}
}
/// <summary>
/// Clones the children of the source cell into the given target cell in
/// this model and adds an entry to the mapping that maps from the source
/// cell to the target cell with the same id or the clone of the source cell
/// that was inserted into this model.
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="cloneAllEdges"></param>
/// <param name="mapping"></param>
protected void MergeChildrenImpl(mxICell from, mxICell to, bool cloneAllEdges, Dictionary<Object, Object> mapping)
{
BeginUpdate();
try
{
int childCount = from.ChildCount();
for (int i = 0; i < childCount; i++)
{
mxICell cell = from.GetChildAt(i);
String id = cell.Id;
mxICell target = (mxICell) ((id != null && (!IsEdge(cell) || !cloneAllEdges)) ? GetCell(id)
: null);
// Clones and adds the child if no cell exists for the id
if (target == null)
{
mxCell clone = (mxCell) cell.Clone();
clone.Id = id;
// Do *NOT* use model.add as this will move the edge away
// from the parent in updateEdgeParent if maintainEdgeParent
// is enabled in the target model
target = to.Insert(clone);
CellAdded(target);
}
// Stores the mapping for later reconnecting edges
mapping[cell] = target;
// Recurses
MergeChildrenImpl(cell, target, cloneAllEdges, mapping);
}
}
finally
{
EndUpdate();
}
}
/// <summary>
/// Returns the number of incoming or outgoing edges.
/// </summary>
/// <param name="model">Graph model that contains the connection data.</param>
/// <param name="cell">Cell whose edges should be counted.</param>
/// <param name="outgoing">Boolean that specifies if the number of outgoing or
/// incoming edges should be returned.</param>
/// <returns>Returns the number of incoming or outgoing edges.</returns>
public static int GetDirectedEdgeCount(mxIGraphModel model, Object cell, bool outgoing)
{
return GetDirectedEdgeCount(model, cell, outgoing, null);
}
/// <summary>
/// Returns the number of incoming or outgoing edges, ignoring the given
/// edge.
/// </summary>
/// <param name="model">Graph model that contains the connection data.</param>
/// <param name="cell">Cell whose edges should be counted.</param>
/// <param name="outgoing">Boolean that specifies if the number of outgoing or
/// incoming edges should be returned.</param>
/// <param name="ignoredEdge">Object that represents an edge to be ignored.</param>
/// <returns>Returns the number of incoming or outgoing edges.</returns>
public static int GetDirectedEdgeCount(mxIGraphModel model, Object cell, bool outgoing, Object ignoredEdge)
{
int count = 0;