forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxEdgeLabelLayout.java
More file actions
executable file
·149 lines (125 loc) · 2.96 KB
/
Copy pathmxEdgeLabelLayout.java
File metadata and controls
executable file
·149 lines (125 loc) · 2.96 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
package com.mxgraph.layout;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.util.mxPoint;
import com.mxgraph.view.mxCellState;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;
public class mxEdgeLabelLayout extends mxGraphLayout
{
/**
* Constructs a new stack layout layout for the specified graph,
* spacing, orientation and offset.
*/
public mxEdgeLabelLayout(mxGraph graph)
{
super(graph);
}
/*
* (non-Javadoc)
* @see com.mxgraph.layout.mxIGraphLayout#execute(java.lang.Object)
*/
public void execute(Object parent)
{
mxGraphView view = graph.getView();
mxIGraphModel model = graph.getModel();
// Gets all vertices and edges inside the parent
List<Object> edges = new ArrayList<Object>();
List<Object> vertices = new ArrayList<Object>();
int childCount = model.getChildCount(parent);
for (int i = 0; i < childCount; i++)
{
Object cell = model.getChildAt(parent, i);
mxCellState state = view.getState(cell);
if (state != null)
{
if (!isVertexIgnored(cell))
{
vertices.add(state);
}
else if (!isEdgeIgnored(cell))
{
edges.add(state);
}
}
}
placeLabels(vertices.toArray(), edges.toArray());
}
/**
*
*/
protected void placeLabels(Object[] v, Object[] e)
{
mxIGraphModel model = graph.getModel();
// Moves the vertices to build a circle. Makes sure the
// radius is large enough for the vertices to not
// overlap
model.beginUpdate();
try
{
for (int i = 0; i < e.length; i++)
{
mxCellState edge = (mxCellState) e[i];
if (edge != null && edge.getLabelBounds() != null)
{
for (int j = 0; j < v.length; j++)
{
mxCellState vertex = (mxCellState) v[j];
if (vertex != null)
{
avoid(edge, vertex);
}
}
}
}
}
finally
{
model.endUpdate();
}
}
/**
*
*/
protected void avoid(mxCellState edge, mxCellState vertex)
{
mxIGraphModel model = graph.getModel();
Rectangle labRect = edge.getLabelBounds().getRectangle();
Rectangle vRect = vertex.getRectangle();
if (labRect.intersects(vRect))
{
int dy1 = -labRect.y - labRect.height + vRect.y;
int dy2 = -labRect.y + vRect.y + vRect.height;
int dy = (Math.abs(dy1) < Math.abs(dy2)) ? dy1 : dy2;
int dx1 = -labRect.x - labRect.width + vRect.x;
int dx2 = -labRect.x + vRect.x + vRect.width;
int dx = (Math.abs(dx1) < Math.abs(dx2)) ? dx1 : dx2;
if (Math.abs(dx) < Math.abs(dy))
{
dy = 0;
}
else
{
dx = 0;
}
mxGeometry g = model.getGeometry(edge.getCell());
if (g != null)
{
g = (mxGeometry) g.clone();
if (g.getOffset() != null)
{
g.getOffset().setX(g.getOffset().getX() + dx);
g.getOffset().setY(g.getOffset().getY() + dy);
}
else
{
g.setOffset(new mxPoint(dx, dy));
}
model.setGeometry(edge.getCell(), g);
}
}
}
}