This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
/
mxHierarchicalLayout.java
751 lines (645 loc) · 17.8 KB
/
mxHierarchicalLayout.java
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
/*
* Copyright (c) 2005-2012, JGraph Ltd
*/
package com.mxgraph.layout.hierarchical;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxGraphLayout;
import com.mxgraph.layout.hierarchical.model.mxGraphHierarchyModel;
import com.mxgraph.layout.hierarchical.stage.mxCoordinateAssignment;
import com.mxgraph.layout.hierarchical.stage.mxHierarchicalLayoutStage;
import com.mxgraph.layout.hierarchical.stage.mxMedianHybridCrossingReduction;
import com.mxgraph.layout.hierarchical.stage.mxMinimumCycleRemover;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.view.mxCellState;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;
/**
* The top level compound layout of the hierarchical layout. The individual
* elements of the layout are called in sequence.
*/
public class mxHierarchicalLayout extends mxGraphLayout/*,
JGraphLayout.Stoppable*/
{
/** The root nodes of the layout */
protected List<Object> roots = null;
/**
* Specifies if the parent should be resized after the layout so that it
* contains all the child cells. Default is false. @See parentBorder.
*/
protected boolean resizeParent = true;
/**
* Specifies if the parnent should be moved if resizeParent is enabled.
* Default is false. @See resizeParent.
*/
protected boolean moveParent = false;
/**
* The border to be added around the children if the parent is to be
* resized using resizeParent. Default is 0. @See resizeParent.
*/
protected int parentBorder = 0;
/**
* The spacing buffer added between cells on the same layer
*/
protected double intraCellSpacing = 30.0;
/**
* The spacing buffer added between cell on adjacent layers
*/
protected double interRankCellSpacing = 50.0;
/**
* The spacing buffer between unconnected hierarchies
*/
protected double interHierarchySpacing = 60.0;
/**
* The distance between each parallel edge on each ranks for long edges
*/
protected double parallelEdgeSpacing = 10.0;
/**
* The position of the root node(s) relative to the laid out graph in.
* Default is <code>SwingConstants.NORTH</code>, i.e. top-down.
*/
protected int orientation = SwingConstants.NORTH;
/**
* Specifies if the STYLE_NOEDGESTYLE flag should be set on edges that are
* modified by the result. Default is true.
*/
protected boolean disableEdgeStyle = true;
/**
* Whether or not to perform local optimisations and iterate multiple times
* through the algorithm
*/
protected boolean fineTuning = true;
/**
* Whether or not to navigate edges whose terminal vertices
* have different parents but are in the same ancestry chain
*/
protected boolean traverseAncestors = true;
/**
* The internal model formed of the layout
*/
protected mxGraphHierarchyModel model = null;
/**
* The layout progress bar
*/
//protected JGraphLayoutProgress progress = new JGraphLayoutProgress();
/**
* Constructs a hierarchical layout
* @param graph the graph to lay out
*
*/
public mxHierarchicalLayout(mxGraph graph)
{
this(graph, SwingConstants.NORTH);
}
/**
* Constructs a hierarchical layout
* @param graph the graph to lay out
* @param orientation <code>SwingConstants.NORTH, SwingConstants.EAST, SwingConstants.SOUTH</code> or <code> SwingConstants.WEST</code>
*
*/
public mxHierarchicalLayout(mxGraph graph, int orientation)
{
super(graph);
this.orientation = orientation;
}
/**
* Returns the model for this layout algorithm.
*/
public mxGraphHierarchyModel getModel()
{
return model;
}
/**
* Executes the layout for the children of the specified parent.
*
* @param parent Parent cell that contains the children to be laid out.
*/
public void execute(Object parent)
{
execute(parent, null);
}
/**
* Executes the layout for the children of the specified parent.
*
* @param parent Parent cell that contains the children to be laid out.
* @param roots the starting roots of the layout
*/
public void execute(Object parent, List<Object> roots)
{
super.execute(parent);
mxIGraphModel model = graph.getModel();
// If the roots are set and the parent is set, only
// use the roots that are some dependent of the that
// parent.
// If just the root are set, use them as-is
// If just the parent is set use it's immediate
// children as the initial set
if (roots == null && parent == null)
{
// TODO indicate the problem
return;
}
if (roots != null && parent != null)
{
for (Object root : roots)
{
if (!model.isAncestor(parent, root))
{
roots.remove(root);
}
}
}
this.roots = roots;
model.beginUpdate();
try
{
run(parent);
if (isResizeParent() && !graph.isCellCollapsed(parent))
{
graph.updateGroupBounds(new Object[] { parent },
getParentBorder(), isMoveParent());
}
}
finally
{
model.endUpdate();
}
}
/**
* Returns all visible children in the given parent which do not have
* incoming edges. If the result is empty then the children with the
* maximum difference between incoming and outgoing edges are returned.
* This takes into account edges that are being promoted to the given
* root due to invisible children or collapsed cells.
*
* @param parent Cell whose children should be checked.
* @return List of tree roots in parent.
*/
public List<Object> findRoots(Object parent, Set<Object> vertices)
{
List<Object> roots = new ArrayList<Object>();
Object best = null;
int maxDiff = -100000;
mxIGraphModel model = graph.getModel();
for (Object vertex : vertices)
{
if (model.isVertex(vertex) && graph.isCellVisible(vertex))
{
Object[] conns = this.getEdges(vertex);
int fanOut = 0;
int fanIn = 0;
for (int k = 0; k < conns.length; k++)
{
Object src = graph.getView().getVisibleTerminal(conns[k],
true);
if (src == vertex)
{
fanOut++;
}
else
{
fanIn++;
}
}
if (fanIn == 0 && fanOut > 0)
{
roots.add(vertex);
}
int diff = fanOut - fanIn;
if (diff > maxDiff)
{
maxDiff = diff;
best = vertex;
}
}
}
if (roots.isEmpty() && best != null)
{
roots.add(best);
}
return roots;
}
/**
*
* @param cell
* @return
*/
public Object[] getEdges(Object cell)
{
mxIGraphModel model = graph.getModel();
boolean isCollapsed = graph.isCellCollapsed(cell);
List<Object> edges = new ArrayList<Object>();
int childCount = model.getChildCount(cell);
for (int i = 0; i < childCount; i++)
{
Object child = model.getChildAt(cell, i);
if (isCollapsed || !graph.isCellVisible(child))
{
edges.addAll(Arrays.asList(mxGraphModel.getEdges(model, child,
true, true, false)));
}
}
edges.addAll(Arrays.asList(mxGraphModel.getEdges(model, cell, true,
true, false)));
List<Object> result = new ArrayList<Object>(edges.size());
Iterator<Object> it = edges.iterator();
while (it.hasNext())
{
Object edge = it.next();
mxCellState state = graph.getView().getState(edge);
Object source = (state != null) ? state.getVisibleTerminal(true)
: graph.getView().getVisibleTerminal(edge, true);
Object target = (state != null) ? state.getVisibleTerminal(false)
: graph.getView().getVisibleTerminal(edge, false);
if (((source != target) && ((target == cell && (parent == null || graph
.isValidAncestor(source, parent, traverseAncestors))) || (source == cell && (parent == null || graph
.isValidAncestor(target, parent, traverseAncestors))))))
{
result.add(edge);
}
}
return result.toArray();
}
/**
* The API method used to exercise the layout upon the graph description
* and produce a separate description of the vertex position and edge
* routing changes made.
*/
public void run(Object parent)
{
// Separate out unconnected hierarchies
List<Set<Object>> hierarchyVertices = new ArrayList<Set<Object>>();
Set<Object> allVertexSet = new LinkedHashSet<Object>();
if (this.roots == null && parent != null)
{
Set<Object> filledVertexSet = filterDescendants(parent);
this.roots = new ArrayList<Object>();
while (!filledVertexSet.isEmpty())
{
List<Object> candidateRoots = findRoots(parent, filledVertexSet);
for (Object root : candidateRoots)
{
Set<Object> vertexSet = new LinkedHashSet<Object>();
hierarchyVertices.add(vertexSet);
traverse(root, true, null, allVertexSet, vertexSet,
hierarchyVertices, filledVertexSet);
}
this.roots.addAll(candidateRoots);
}
}
else
{
// Find vertex set as directed traversal from roots
for (int i = 0; i < roots.size(); i++)
{
Set<Object> vertexSet = new LinkedHashSet<Object>();
hierarchyVertices.add(vertexSet);
traverse(roots.get(i), true, null, allVertexSet, vertexSet,
hierarchyVertices, null);
}
}
// Iterate through the result removing parents who have children in this layout
// Perform a layout for each separate hierarchy
// Track initial coordinate x-positioning
double initialX = 0;
Iterator<Set<Object>> iter = hierarchyVertices.iterator();
while (iter.hasNext())
{
Set<Object> vertexSet = iter.next();
this.model = new mxGraphHierarchyModel(this, vertexSet.toArray(),
roots, parent);
cycleStage(parent);
layeringStage();
crossingStage(parent);
initialX = placementStage(initialX, parent);
}
}
/**
* Creates a set of descendant cells
* @param cell The cell whose descendants are to be calculated
* @return the descendants of the cell (not the cell)
*/
public Set<Object> filterDescendants(Object cell)
{
mxIGraphModel model = graph.getModel();
Set<Object> result = new LinkedHashSet<Object>();
if (model.isVertex(cell) && cell != this.parent && graph.isCellVisible(cell))
{
result.add(cell);
}
if (this.traverseAncestors || cell == this.parent
&& graph.isCellVisible(cell))
{
int childCount = model.getChildCount(cell);
for (int i = 0; i < childCount; i++)
{
Object child = model.getChildAt(cell, i);
// Ignore ports in the layout vertex list, they are dealt with
// in the traversal mechanisms
if (!isPort((mxCell) child)) {
result.addAll(filterDescendants(child));
}
}
}
return result;
}
private boolean isPort(mxCell cell) {
return cell.getGeometry().isRelative();
}
/**
* Traverses the (directed) graph invoking the given function for each
* visited vertex and edge. The function is invoked with the current vertex
* and the incoming edge as a parameter. This implementation makes sure
* each vertex is only visited once. The function may return false if the
* traversal should stop at the given vertex.
*
* @param vertex <mxCell> that represents the vertex where the traversal starts.
* @param directed Optional boolean indicating if edges should only be traversed
* from source to target. Default is true.
* @param edge Optional <mxCell> that represents the incoming edge. This is
* null for the first step of the traversal.
* @param allVertices Array of cell paths for the visited cells.
*/
protected void traverse(Object vertex, boolean directed, Object edge,
Set<Object> allVertices, Set<Object> currentComp,
List<Set<Object>> hierarchyVertices, Set<Object> filledVertexSet)
{
mxGraphView view = graph.getView();
mxIGraphModel model = graph.getModel();
if (vertex != null && allVertices != null)
{
// Has this vertex been seen before in any traversal
// And if the filled vertex set is populated, only
// process vertices in that it contains
if (!allVertices.contains(vertex)
&& (filledVertexSet == null ? true : filledVertexSet
.contains(vertex)))
{
currentComp.add(vertex);
allVertices.add(vertex);
if (filledVertexSet != null)
{
filledVertexSet.remove(vertex);
}
int edgeCount = model.getEdgeCount(vertex);
boolean[] edgeIsSource = new boolean[edgeCount];
if (edgeCount > 0) {
for (int i = 0; i < edgeCount; i++) {
Object e = model.getEdgeAt(vertex, i);
edgeIsSource[i] = view.getVisibleTerminal(e, true) == vertex;
}
}
if (edgeCount > 0)
{
for (int i = 0; i < edgeCount; i++)
{
Object e = model.getEdgeAt(vertex, i);
if (!directed || edgeIsSource[i])
{
Object next = view.getVisibleTerminal(e, !edgeIsSource[i]);
// Check whether there are more edges incoming from the target vertex than outgoing
// The hierarchical model treats bi-directional parallel edges as being sourced
// from the more "sourced" terminal. If the directions are equal in number, the direction
// is that of the natural direction from the roots of the layout.
// The checks below are slightly more verbose than need be for performance reasons
int netCount = 1;
for (int j = 0; j < edgeCount; j++) {
if (j != i) {
boolean isSorce2 = edgeIsSource[j];
Object e2 = model.getEdgeAt(vertex, j);
Object otherTerm = view.getVisibleTerminal(e2, !isSorce2);
if (otherTerm == next) {
if (isSorce2) {
netCount++;
} else {
netCount--;
}
}
}
}
if (netCount >= 0){
traverse(next, directed, e, allVertices,
currentComp, hierarchyVertices,
filledVertexSet);
}
}
}
}
}
else
{
if (!currentComp.contains(vertex))
{
// We've seen this vertex before, but not in the current component
// This component and the one it's in need to be merged
Set<Object> matchComp = null;
for (Set<Object> comp : hierarchyVertices)
{
if (comp.contains(vertex))
{
currentComp.addAll(comp);
matchComp = comp;
break;
}
}
if (matchComp != null)
{
hierarchyVertices.remove(matchComp);
}
}
}
}
}
/**
* Executes the cycle stage. This implementation uses the
* mxMinimumCycleRemover.
*/
public void cycleStage(Object parent)
{
mxHierarchicalLayoutStage cycleStage = new mxMinimumCycleRemover(this);
cycleStage.execute(parent);
}
/**
* Implements first stage of a Sugiyama layout.
*/
public void layeringStage()
{
model.initialRank();
model.fixRanks();
}
/**
* Executes the crossing stage using mxMedianHybridCrossingReduction.
*/
public void crossingStage(Object parent)
{
mxHierarchicalLayoutStage crossingStage = new mxMedianHybridCrossingReduction(
this);
crossingStage.execute(parent);
}
/**
* Executes the placement stage using mxCoordinateAssignment.
*/
public double placementStage(double initialX, Object parent)
{
mxCoordinateAssignment placementStage = new mxCoordinateAssignment(
this, intraCellSpacing, interRankCellSpacing, orientation,
initialX, parallelEdgeSpacing);
placementStage.setFineTuning(fineTuning);
placementStage.execute(parent);
return placementStage.getLimitX() + interHierarchySpacing;
}
/**
* Returns the resizeParent flag.
*/
public boolean isResizeParent()
{
return resizeParent;
}
/**
* Sets the resizeParent flag.
*/
public void setResizeParent(boolean value)
{
resizeParent = value;
}
/**
* Returns the moveParent flag.
*/
public boolean isMoveParent()
{
return moveParent;
}
/**
* Sets the moveParent flag.
*/
public void setMoveParent(boolean value)
{
moveParent = value;
}
/**
* Returns parentBorder.
*/
public int getParentBorder()
{
return parentBorder;
}
/**
* Sets parentBorder.
*/
public void setParentBorder(int value)
{
parentBorder = value;
}
/**
* @return Returns the intraCellSpacing.
*/
public double getIntraCellSpacing()
{
return intraCellSpacing;
}
/**
* @param intraCellSpacing
* The intraCellSpacing to set.
*/
public void setIntraCellSpacing(double intraCellSpacing)
{
this.intraCellSpacing = intraCellSpacing;
}
/**
* @return Returns the interRankCellSpacing.
*/
public double getInterRankCellSpacing()
{
return interRankCellSpacing;
}
/**
* @param interRankCellSpacing
* The interRankCellSpacing to set.
*/
public void setInterRankCellSpacing(double interRankCellSpacing)
{
this.interRankCellSpacing = interRankCellSpacing;
}
/**
* @return Returns the orientation.
*/
public int getOrientation()
{
return orientation;
}
/**
* @param orientation
* The orientation to set.
*/
public void setOrientation(int orientation)
{
this.orientation = orientation;
}
/**
* @return Returns the interHierarchySpacing.
*/
public double getInterHierarchySpacing()
{
return interHierarchySpacing;
}
/**
* @param interHierarchySpacing
* The interHierarchySpacing to set.
*/
public void setInterHierarchySpacing(double interHierarchySpacing)
{
this.interHierarchySpacing = interHierarchySpacing;
}
public double getParallelEdgeSpacing()
{
return parallelEdgeSpacing;
}
public void setParallelEdgeSpacing(double parallelEdgeSpacing)
{
this.parallelEdgeSpacing = parallelEdgeSpacing;
}
/**
* @return Returns the fineTuning.
*/
public boolean isFineTuning()
{
return fineTuning;
}
/**
* @param fineTuning
* The fineTuning to set.
*/
public void setFineTuning(boolean fineTuning)
{
this.fineTuning = fineTuning;
}
/**
*
*/
public boolean isDisableEdgeStyle()
{
return disableEdgeStyle;
}
/**
*
* @param disableEdgeStyle
*/
public void setDisableEdgeStyle(boolean disableEdgeStyle)
{
this.disableEdgeStyle = disableEdgeStyle;
}
/**
* Returns <code>Hierarchical</code>, the name of this algorithm.
*/
public String toString()
{
return "Hierarchical";
}
}